Skip to content

Commit 614f62f

Browse files
committed
Fix snake_case parser for an input ending in an idle state (remove delimiter)
1 parent 871319b commit 614f62f

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

camel.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
func CamelCase(input string) string {
1212
var b strings.Builder
1313

14-
stateMachine := idle
14+
state := idle
1515
for i := 0; i < len(input); {
1616
r, size := utf8.DecodeRuneInString(input[i:])
1717
i += size
18-
stateMachine = stateMachine.next(r)
19-
switch stateMachine {
18+
state = state.next(r)
19+
switch state {
2020
case firstAlphaNum:
2121
if b.Len() > 0 {
2222
b.WriteRune(unicode.ToUpper(r))

parser.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import (
44
"unicode"
55
)
66

7-
type stateMachine int
7+
type parserStateMachine int
88

99
const (
10-
idle stateMachine = iota // 0 _$$_This is some text, OK?!
11-
firstAlphaNum // 1 ↑ ↑ ↑ ↑ ↑
12-
alphaNum // 2 ↑↑↑ ↑ ↑↑↑ ↑↑↑ ↑
13-
delimiter // 3 ↑ ↑ ↑ ↑ ↑
10+
_ parserStateMachine = iota // _$$_This is some text, OK?!
11+
idle // 1 ↑↑↑↑ ↑ ↑
12+
firstAlphaNum // 2 ↑ ↑ ↑ ↑ ↑
13+
alphaNum // 3 ↑↑↑ ↑ ↑↑↑ ↑↑↑ ↑
14+
delimiter // 4 ↑ ↑ ↑ ↑ ↑
1415
)
1516

16-
func (s stateMachine) next(r rune) stateMachine {
17+
func (s parserStateMachine) next(r rune) parserStateMachine {
1718
switch s {
1819
case idle:
1920
if isAlphaNum(r) {

pascal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
func PascalCase(input string) string {
1212
var b strings.Builder
1313

14-
stateMachine := idle
14+
state := idle
1515
for i := 0; i < len(input); {
1616
r, size := utf8.DecodeRuneInString(input[i:])
1717
i += size
18-
stateMachine = stateMachine.next(r)
19-
switch stateMachine {
18+
state = state.next(r)
19+
switch state {
2020
case firstAlphaNum:
2121
b.WriteRune(unicode.ToUpper(r))
2222
case alphaNum:

snake.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ import (
1111
func SnakeCase(str string) string {
1212
var b bytes.Buffer
1313

14-
stateMachine := idle
14+
state := idle
1515
for i := 0; i < len(str); {
1616
r, size := utf8.DecodeRuneInString(str[i:])
1717
i += size
18-
stateMachine = stateMachine.next(r)
19-
switch stateMachine {
18+
state = state.next(r)
19+
switch state {
2020
case firstAlphaNum, alphaNum:
2121
b.WriteRune(unicode.ToLower(r))
2222
case delimiter:
2323
b.WriteByte('_')
2424
}
2525
}
26-
if stateMachine == idle {
27-
return string(bytes.TrimSuffix(b.Bytes(), []byte{'_'}))
26+
if (state == idle || state == delimiter) && b.Len() > 0 {
27+
b.Truncate(b.Len() - 1)
2828
}
29+
2930
return b.String()
3031
}

textcase_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestTextCases(t *testing.T) {
2424
{in: "a___", camel: "a", snake: "a"},
2525
{in: "a___b", camel: "aB", snake: "a_b"},
2626
{in: "ax___by", camel: "axBy", snake: "ax_by"},
27+
{in: "Háčky, čárky. Příliš žluťoučký kůň úpěl ďábelské ódy.", camel: "háčkyČárkyPřílišŽluťoučkýKůňÚpělĎábelskéÓdy", snake: "háčky_čárky_příliš_žluťoučký_kůň_úpěl_ďábelské_ódy"},
2728
}
2829

2930
for _, test := range tt {

0 commit comments

Comments
 (0)