Skip to content

Commit

Permalink
Merge pull request #152 from sarff/regex_optimization
Browse files Browse the repository at this point in the history
regex optimization
  • Loading branch information
AmarnathCJD authored Oct 5, 2024
2 parents b957f8d + 7f0ae55 commit 5311272
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 47 deletions.
11 changes: 1 addition & 10 deletions internal/cmd/tlgen/gen/tl_gen_interfaces.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gen

import (
"regexp"
"sort"
"sync"

Expand Down Expand Up @@ -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)<a\s+href="([^"]+)"\s*>([^<]+)</a>`).ReplaceAllString(pComments[j], "$2")
pComments[j] = regexp.MustCompile(`(?i)<strong>([^<]+)</strong>`).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)<code>([^<]+)</code>`).ReplaceAllString(pComments[j], "'$1'")
pComments[j] = regexp.MustCompile(`(?i)<br>`).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]
}
}
Expand Down
10 changes: 3 additions & 7 deletions internal/cmd/tlgen/gen/tl_gen_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"net/http"
"regexp"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -86,9 +85,8 @@ func (g *Generator) generateComment(name, _type string) (string, []string) {
}

ack := string(body)
re := regexp.MustCompile(`<td style="text-align: center;">.*?</td>\s*<td>(.*?)</td>`)

matches := re.FindAllStringSubmatch(ack, -1)
matches := regexTableTag.FindAllStringSubmatch(ack, -1)

var descs []string

Expand All @@ -100,11 +98,9 @@ func (g *Generator) generateComment(name, _type string) (string, []string) {
ack = strings.Split(ack, "</p>")[0]
ack = strings.ReplaceAll(ack, "<p>", "")
//ack = strings.ReplaceAll(ack, "see .", "")
a_tag_regex := regexp.MustCompile(`<a href="([^"]*)">([^<]*)</a>`)
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(`<code>([^<]*)</code>`)
ack = code_tag_regex.ReplaceAllString(ack, "`$1`")
ack = regexCodeTag.ReplaceAllString(ack, "`$1`")

ack = strings.TrimSpace(ack)

Expand Down
36 changes: 32 additions & 4 deletions internal/cmd/tlgen/gen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gen

import (
"fmt"
"regexp"
"strings"
"unicode"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -120,3 +121,30 @@ func SliceContains(slice []string, item string) bool {

return false
}

var (
regexLinkTag = regexp.MustCompile(`(?i)<a\s+href="([^"]+)"\s*>([^<]+)</a>`)
regexStrongTag = regexp.MustCompile(`(?i)<strong>([^<]+)</strong>`)
regexMarkdownLink = regexp.MustCompile(`\[(.+)\]\(.+\)`)
regexSeeHereTag = regexp.MustCompile(`(?i)see here[^.]*`)
regexCodeTag = regexp.MustCompile(`(?i)<code>([^<]+)</code>`)
regexBreakTag = regexp.MustCompile(`(?i)<br>`)
regexArrowRightTag = regexp.MustCompile(`(?i)»`)
regexExtraSpaces = regexp.MustCompile(`(?i)\s+\.`)
regexTrailingComma = regexp.MustCompile(`(?i),\s*$`)
regexTableTag = regexp.MustCompile(`<td style="text-align: center;">.*?</td>\s*<td>(.*?)</td>`)
)

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
}
7 changes: 5 additions & 2 deletions telegram/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
65 changes: 41 additions & 24 deletions telegram/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5311272

Please sign in to comment.