From fc1695011d6b8b9d3f66df7d1b14a0e318422950 Mon Sep 17 00:00:00 2001 From: i582 Date: Wed, 18 Nov 2020 01:19:49 +0300 Subject: [PATCH] internal: increase perfomance --- internal/parser.go | 40 ++++++++++++++++++++++++++-------------- internal/style.go | 9 ++++----- tests/parser_test.go | 6 +++--- tests/style_test.go | 22 +++++++++++----------- 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/internal/parser.go b/internal/parser.go index 5c1d80a..3683a2e 100644 --- a/internal/parser.go +++ b/internal/parser.go @@ -7,8 +7,10 @@ import ( // Parse parses the passed format string and applies styles, returning a styled string. func Parse(format string) string { - var tempToken = make([]rune, 0, 20) - var resParts = make([]string, 0, strings.Count(format, "::")) + var tempToken = make([]byte, 0, 50) + var resParts = make([]string, 0, 50) + var formatSlice = make([]string, 0, 5) + var lastToken string var inText bool @@ -25,13 +27,15 @@ func Parse(format string) string { inText = true - for index, s := range format { + for index := 0; index < len(format); index++ { + s := format[index] + if inText { // find {{ if s == '{' && index+1 < len(format) && format[index+1] == '{' { if len(tempToken) != 0 { resParts = append(resParts, string(tempToken)) - tempToken = nil + tempToken = tempToken[:0] } inFormatGroup = true @@ -92,7 +96,7 @@ func Parse(format string) string { lastToken = string(tempToken) - tempToken = nil + tempToken = tempToken[:0] continue } @@ -107,14 +111,23 @@ func Parse(format string) string { continue } + if s == '|' { + formatSlice = append(formatSlice, string(tempToken)) + tempToken = tempToken[:0] + continue + } + if !(s >= 'a' && s <= 'z' || s >= 'A' && s <= 'Z') && s != '|' && !(s >= '0' && s <= '9') && s != '#' { - lastToken = groupStyle(format, lastToken, string(tempToken)) + formatSlice = append(formatSlice, string(tempToken)) + lastToken = groupStyle(format, lastToken, formatSlice) resParts = append(resParts, lastToken) - tempToken = make([]rune, 0, 20) + tempToken = tempToken[:0] tempToken = append(tempToken, s) + formatSlice = formatSlice[:0] + inFormatGroup = false inFormat = false inText = true @@ -129,20 +142,19 @@ func Parse(format string) string { } } - if lastIsFormatGroup && len(lastToken) != 0 { - lastToken = groupStyle(format, lastToken, string(tempToken)) + if lastIsFormatGroup { + formatSlice = append(formatSlice, string(tempToken)) + lastToken = groupStyle(format, lastToken, formatSlice) resParts = append(resParts, lastToken) - } - - if !lastIsFormatGroup && len(tempToken) != 0 { + } else { resParts = append(resParts, string(tempToken)) } return strings.Join(resParts, "") } -func groupStyle(format string, token string, style string) string { - styleText, err := StyleBuilder(style, token) +func groupStyle(format string, token string, styles []string) string { + styleText, err := StyleBuilder(styles, token) if err != nil { log.Fatalf("Error parse style string in '%s' format string: %v", format, err) } diff --git a/internal/style.go b/internal/style.go index cdd4e2e..cbb39f9 100644 --- a/internal/style.go +++ b/internal/style.go @@ -8,16 +8,15 @@ import ( ) // StyleBuilder is a function that returns a styled string based on the supplied style. -func StyleBuilder(styleString string, text string) (string, error) { - if styleString == "" { +func StyleBuilder(styles []string, text string) (string, error) { + if len(styles) == 0 { return "", fmt.Errorf("style string is empty") } - var styleParts = strings.Split(styleString, "|") - var colors = make([]color.Color, 0, len(styleParts)) + var colors = make([]color.Color, 0, len(styles)) color.New() - for _, style := range styleParts { + for _, style := range styles { if isHex(style) { err := checkHex(style) if err != nil { diff --git a/tests/parser_test.go b/tests/parser_test.go index e701a3a..09e59d5 100644 --- a/tests/parser_test.go +++ b/tests/parser_test.go @@ -11,11 +11,11 @@ func TestParse(t *testing.T) { return cfmt.Sprintf("{{%s}}::red|underline", s) }) - cfmt.Println("{{correct group}}::code") - cfmt.Println("{{correct group}}::red|underline and {{other}}::red") + cfmt.Println("{{こんにちは, correct group}}::code") + cfmt.Println("{{привет, correct group}}::red|underline and {{other}}::red") cfmt.Print("{{error group}} \n") cfmt.Print("{{overline group}}::overline\n") - cfmt.Print("{{reverse group}}::reverse\n") + cfmt.Print("{{reverse group, こんにちは}}::reverse\n") cfmt.Print(cfmt.Sprintln("{{faint group}}::faint")) cfmt.Println(cfmt.Sprint("{{blink group}}::blink")) cfmt.Printf("{{hex %s}}::#ff00ff\n", "color group") diff --git a/tests/style_test.go b/tests/style_test.go index bee4a06..35d4e47 100644 --- a/tests/style_test.go +++ b/tests/style_test.go @@ -10,7 +10,7 @@ import ( ) type StyleBuilderSuite struct { - Value string + Value []string Error string } @@ -21,43 +21,43 @@ func TestStyleBuilder(t *testing.T) { suites := []StyleBuilderSuite{ { - Value: "red", + Value: []string{"red"}, Error: "", }, { - Value: "red|bold", + Value: []string{"red", "bold"}, Error: "", }, { - Value: "#ff00ff|bold", + Value: []string{"#ff00ff", "bold"}, Error: "", }, { - Value: "bg#ff00ff|bold", + Value: []string{"bg#ff00ff", "bold"}, Error: "", }, { - Value: "code|underline|blink", + Value: []string{"code", "underline", "blink"}, Error: "", }, { - Value: "overline|reverse|faint", + Value: []string{"overline", "reverse", "faint"}, Error: "", }, { - Value: "bg#ff0|bold", + Value: []string{"bg#ff0", "bold"}, Error: "invalid hex: length of hex color must be 6", }, { - Value: "#ff0|bold", + Value: []string{"#ff0", "bold"}, Error: "invalid hex: length of hex color must be 6", }, { - Value: "some|bold", + Value: []string{"some", "bold"}, Error: "unknown style some", }, { - Value: "", + Value: []string{}, Error: "style string is empty", }, }