Skip to content

Commit

Permalink
internal: increase perfomance
Browse files Browse the repository at this point in the history
  • Loading branch information
i582 committed Nov 17, 2020
1 parent 131bf09 commit fc16950
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
40 changes: 26 additions & 14 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -92,7 +96,7 @@ func Parse(format string) string {

lastToken = string(tempToken)

tempToken = nil
tempToken = tempToken[:0]
continue
}

Expand All @@ -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
Expand All @@ -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)
}
Expand Down
9 changes: 4 additions & 5 deletions internal/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions tests/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
22 changes: 11 additions & 11 deletions tests/style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type StyleBuilderSuite struct {
Value string
Value []string
Error string
}

Expand All @@ -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",
},
}
Expand Down

0 comments on commit fc16950

Please sign in to comment.