Skip to content

Commit

Permalink
Simplify strcase #3
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd committed May 8, 2024
1 parent b1bc41e commit c34ca2c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
20 changes: 3 additions & 17 deletions pkg/strcase/strcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package strcase
import (
"strings"
"unicode"
"unicode/utf8"
)

// Delimited converts a string to delimited.lower.case, here using `.` as delimiter.
Expand Down Expand Up @@ -41,23 +40,10 @@ func convert(s string, _case int, d rune) string {
n := strings.Builder{}
n.Grow(len(s) + 2) // Allow adding at least 2 delimiters without another allocation.

var pos int
var prevRune int32
var prevRune rune

for _, r := range s[pos:] {
n.WriteRune(unicode.To(_case, r))

pos += utf8.RuneLen(r)
prevRune = r

// This loop just extracts the first rune of the string,
// without needing to allocate a new slice of runes as
// range iterates over the runes of the string.
break
}

for _, r := range s[pos:] {
if unicode.IsUpper(r) && (unicode.IsNumber(prevRune) || unicode.IsLower(prevRune)) {
for i, r := range s {
if i > 0 && unicode.IsUpper(r) && (unicode.IsNumber(prevRune) || unicode.IsLower(prevRune)) {
n.WriteRune(d)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/strcase/strcase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var tests = [][]string{
{"icinga💯points", "icinga💯points"},
{"😃🙃😀", "😃🙃😀"},
{"こんにちは", "こんにちは"},
{"\xff\xfe\xfd", "�"},
{"\xff\xfe\xfd", "���"},
}

func TestSnake(t *testing.T) {
Expand Down

0 comments on commit c34ca2c

Please sign in to comment.