From 7f0ae55aff828e1899978b6e3c6a917f140fc115 Mon Sep 17 00:00:00 2001
From: dmit
Date: Sat, 5 Oct 2024 13:40:38 +0300
Subject: [PATCH] regex optimization
---
internal/cmd/tlgen/gen/tl_gen_interfaces.go | 11 +---
internal/cmd/tlgen/gen/tl_gen_methods.go | 10 +---
internal/cmd/tlgen/gen/utils.go | 36 ++++++++++--
telegram/auth.go | 7 ++-
telegram/utils.go | 65 +++++++++++++--------
5 files changed, 82 insertions(+), 47 deletions(-)
diff --git a/internal/cmd/tlgen/gen/tl_gen_interfaces.go b/internal/cmd/tlgen/gen/tl_gen_interfaces.go
index cb7c5299..2e78788a 100644
--- a/internal/cmd/tlgen/gen/tl_gen_interfaces.go
+++ b/internal/cmd/tlgen/gen/tl_gen_interfaces.go
@@ -1,7 +1,6 @@
package gen
import (
- "regexp"
"sort"
"sync"
@@ -59,15 +58,7 @@ func (g *Generator) generateInterfaces(f *jen.File, d bool) {
if pComments != nil && len(pComments) == len(_type.Parameters) {
for j := range _type.Parameters {
- pComments[j] = regexp.MustCompile(`(?i)([^<]+)`).ReplaceAllString(pComments[j], "$2")
- pComments[j] = regexp.MustCompile(`(?i)([^<]+)`).ReplaceAllString(pComments[j], "$1")
- pComments[j] = regexp.MustCompile(`\[(.+)\]\(.+\)`).ReplaceAllString(pComments[j], "$1")
- pComments[j] = regexp.MustCompile(`(?i)see here[^.]*`).ReplaceAllString(pComments[j], "")
- pComments[j] = regexp.MustCompile(`(?i)([^<]+)
`).ReplaceAllString(pComments[j], "'$1'")
- pComments[j] = regexp.MustCompile(`(?i)
`).ReplaceAllString(pComments[j], "\n")
- pComments[j] = regexp.MustCompile(`(?i)»`).ReplaceAllString(pComments[j], "")
- pComments[j] = regexp.MustCompile(`(?i)\s+\.`).ReplaceAllString(pComments[j], ".")
- pComments[j] = regexp.MustCompile(`(?i),\s*$`).ReplaceAllString(pComments[j], "")
+ pComments[j] = cleanComment(pComments[j])
structs[i].Parameters[j].Comment = pComments[j]
}
}
diff --git a/internal/cmd/tlgen/gen/tl_gen_methods.go b/internal/cmd/tlgen/gen/tl_gen_methods.go
index 544cf6b6..befd8d49 100644
--- a/internal/cmd/tlgen/gen/tl_gen_methods.go
+++ b/internal/cmd/tlgen/gen/tl_gen_methods.go
@@ -4,7 +4,6 @@ import (
"fmt"
"io"
"net/http"
- "regexp"
"sort"
"strings"
"sync"
@@ -86,9 +85,8 @@ func (g *Generator) generateComment(name, _type string) (string, []string) {
}
ack := string(body)
- re := regexp.MustCompile(`.*? | \s*(.*?) | `)
- matches := re.FindAllStringSubmatch(ack, -1)
+ matches := regexTableTag.FindAllStringSubmatch(ack, -1)
var descs []string
@@ -100,11 +98,9 @@ func (g *Generator) generateComment(name, _type string) (string, []string) {
ack = strings.Split(ack, "
")[0]
ack = strings.ReplaceAll(ack, "", "")
//ack = strings.ReplaceAll(ack, "see .", "")
- a_tag_regex := regexp.MustCompile(`([^<]*)`)
- ack = a_tag_regex.ReplaceAllString(ack, "[$2](https://core.telegram.org$1)")
+ ack = regexLinkTag.ReplaceAllString(ack, "[$2](https://core.telegram.org$1)")
- code_tag_regex := regexp.MustCompile(`([^<]*)
`)
- ack = code_tag_regex.ReplaceAllString(ack, "`$1`")
+ ack = regexCodeTag.ReplaceAllString(ack, "`$1`")
ack = strings.TrimSpace(ack)
diff --git a/internal/cmd/tlgen/gen/utils.go b/internal/cmd/tlgen/gen/utils.go
index daf0b3b8..fc63bad3 100644
--- a/internal/cmd/tlgen/gen/utils.go
+++ b/internal/cmd/tlgen/gen/utils.go
@@ -2,6 +2,7 @@ package gen
import (
"fmt"
+ "regexp"
"strings"
"unicode"
@@ -30,14 +31,14 @@ func haveOptionalParams(params []tlparser.Parameter) bool {
}
func maxBitflag(params []tlparser.Parameter) int {
- max := 0
+ maximum := 0
for _, param := range params {
- if param.BitToTrigger > max {
- max = param.BitToTrigger
+ if param.BitToTrigger > maximum {
+ maximum = param.BitToTrigger
}
}
- return max
+ return maximum
}
func goify(name string, public bool) string {
@@ -120,3 +121,30 @@ func SliceContains(slice []string, item string) bool {
return false
}
+
+var (
+ regexLinkTag = regexp.MustCompile(`(?i)([^<]+)`)
+ regexStrongTag = regexp.MustCompile(`(?i)([^<]+)`)
+ regexMarkdownLink = regexp.MustCompile(`\[(.+)\]\(.+\)`)
+ regexSeeHereTag = regexp.MustCompile(`(?i)see here[^.]*`)
+ regexCodeTag = regexp.MustCompile(`(?i)([^<]+)
`)
+ regexBreakTag = regexp.MustCompile(`(?i)
`)
+ regexArrowRightTag = regexp.MustCompile(`(?i)»`)
+ regexExtraSpaces = regexp.MustCompile(`(?i)\s+\.`)
+ regexTrailingComma = regexp.MustCompile(`(?i),\s*$`)
+ regexTableTag = regexp.MustCompile(`
.*? | \s*(.*?) | `)
+)
+
+func cleanComment(comment string) string {
+ comment = regexLinkTag.ReplaceAllString(comment, "$2")
+ comment = regexStrongTag.ReplaceAllString(comment, "$1")
+ comment = regexMarkdownLink.ReplaceAllString(comment, "$1")
+ comment = regexSeeHereTag.ReplaceAllString(comment, "")
+ comment = regexCodeTag.ReplaceAllString(comment, "$1")
+ comment = regexBreakTag.ReplaceAllString(comment, "\n")
+ comment = regexArrowRightTag.ReplaceAllString(comment, "")
+ comment = regexExtraSpaces.ReplaceAllString(comment, ".")
+ comment = regexTrailingComma.ReplaceAllString(comment, "")
+
+ return comment
+}
diff --git a/telegram/auth.go b/telegram/auth.go
index 6dc5a6d0..1ed47a70 100755
--- a/telegram/auth.go
+++ b/telegram/auth.go
@@ -28,6 +28,11 @@ func (c *Client) ConnectBot(botToken string) error {
return c.LoginBot(botToken)
}
+var (
+ botTokenRegex = regexp.MustCompile(`^\d+:[\w\d_-]+$`)
+ phoneRegex = regexp.MustCompile(`^\+?\d+$`)
+)
+
// AuthPromt will prompt user to enter phone number or bot token to authorize client
func (c *Client) AuthPrompt() error {
if au, _ := c.IsAuthorized(); au {
@@ -39,14 +44,12 @@ func (c *Client) AuthPrompt() error {
fmt.Printf("Enter phone number (with country code [+42xxx]) or bot token: ")
fmt.Scanln(&input)
if input != "" {
- botTokenRegex := regexp.MustCompile(`^\d+:[\w\d_-]+$`)
if botTokenRegex.MatchString(input) {
err := c.LoginBot(input)
if err != nil {
return err
}
} else {
- phoneRegex := regexp.MustCompile(`^\+?\d+$`)
if phoneRegex.MatchString(strings.TrimSpace(input)) {
_, err := c.Login(input)
if err != nil {
diff --git a/telegram/utils.go b/telegram/utils.go
index 9b54a91d..947f1164 100755
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -132,32 +132,48 @@ func init() {
mimeTypes.addMime(".tgs", "application/x-tgsticker")
}
+var (
+ regexDataCenter = regexp.MustCompile(`DC (\d+)`)
+ regexCode = regexp.MustCompile(`code (\d+)`)
+)
+
func getErrorCode(err error) (int, int) {
datacenter := 0
code := 0
- if err != nil {
- if re := regexp.MustCompile(`DC (\d+)`); re.MatchString(err.Error()) {
- datacenter, _ = strconv.Atoi(re.FindStringSubmatch(err.Error())[1])
- }
- if re := regexp.MustCompile(`code (\d+)`); re.MatchString(err.Error()) {
- code, _ = strconv.Atoi(re.FindStringSubmatch(err.Error())[1])
- }
+ if err == nil {
+ return datacenter, code
}
+
+ if regexDataCenter.MatchString(err.Error()) {
+ datacenter, _ = strconv.Atoi(regexDataCenter.FindStringSubmatch(err.Error())[1])
+ }
+ if regexCode.MatchString(err.Error()) {
+ code, _ = strconv.Atoi(regexCode.FindStringSubmatch(err.Error())[1])
+ }
+
return datacenter, code
}
+var (
+ regexFloodWait = regexp.MustCompile(`A wait of (\d+) seconds is required`)
+ regexFloodWaitBasic = regexp.MustCompile(`FLOOD_WAIT_(\d+)`)
+ regexFloodWaitPremium = regexp.MustCompile(`FLOOD_PREMIUM_WAIT_(\d+)`)
+)
+
func getFloodWait(err error) int {
- if err != nil {
- if re := regexp.MustCompile(`A wait of (\d+) seconds is required`); re.MatchString(err.Error()) {
- wait, _ := strconv.Atoi(re.FindStringSubmatch(err.Error())[1])
- return wait
- } else if re := regexp.MustCompile(`FLOOD_WAIT_(\d+)`); re.MatchString(err.Error()) {
- wait, _ := strconv.Atoi(re.FindStringSubmatch(err.Error())[1])
- return wait
- } else if re := regexp.MustCompile(`FLOOD_PREMIUM_WAIT_(\d+)`); re.MatchString(err.Error()) {
- wait, _ := strconv.Atoi(re.FindStringSubmatch(err.Error())[1])
- return wait
- }
+ if err == nil {
+ return 0
+ }
+
+ if regexFloodWait.MatchString(err.Error()) {
+ wait, _ := strconv.Atoi(regexFloodWait.FindStringSubmatch(err.Error())[1])
+ return wait
+ } else if regexFloodWaitBasic.MatchString(err.Error()) {
+ wait, _ := strconv.Atoi(regexFloodWaitBasic.FindStringSubmatch(err.Error())[1])
+ return wait
+ } else if regexFloodWaitPremium.MatchString(err.Error()) {
+ wait, _ := strconv.Atoi(regexFloodWaitPremium.FindStringSubmatch(err.Error())[1])
+ return wait
}
return 0
@@ -265,13 +281,13 @@ func getMax(a []int32) int32 {
if len(a) == 0 {
return 0
}
- var max = a[0]
+ var maximum = a[0]
for _, v := range a {
- if v > max {
- max = v
+ if v > maximum {
+ maximum = v
}
}
- return max
+ return maximum
}
func PathIsDir(path string) bool {
@@ -417,9 +433,10 @@ func IsURL(str string) bool {
return err == nil && u.Scheme != "" && u.Host != ""
}
+var regexPhone = regexp.MustCompile(`^\+?[0-9]{10,13}$`)
+
func IsPhone(phone string) bool {
- phoneRe := regexp.MustCompile(`^\+?[0-9]{10,13}$`)
- return phoneRe.MatchString(phone)
+ return regexPhone.MatchString(phone)
}
func getValue[T comparable](val, def T) T {