From 17eaa869ec770b9c5852272c433fc78608e41d4e Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Sun, 28 May 2023 04:19:31 +0800 Subject: [PATCH 1/3] Use ASCIISet --- camel.go | 8 ++++---- chars.go | 33 +++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ snake.go | 18 +++++++++--------- 5 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 chars.go create mode 100644 go.sum diff --git a/camel.go b/camel.go index cd5a260..e433f87 100644 --- a/camel.go +++ b/camel.go @@ -43,8 +43,8 @@ func toCamelInitCase(s string, initCase bool) string { n.Grow(len(s)) capNext := initCase for i, v := range []byte(s) { - vIsCap := v >= 'A' && v <= 'Z' - vIsLow := v >= 'a' && v <= 'z' + vIsCap := capitalLetters.Contains(v) + vIsLow := smallLetters.Contains(v) if capNext { if vIsLow { v += 'A' @@ -59,11 +59,11 @@ func toCamelInitCase(s string, initCase bool) string { if vIsCap || vIsLow { n.WriteByte(v) capNext = false - } else if vIsNum := v >= '0' && v <= '9'; vIsNum { + } else if vIsNum := numbers.Contains(v); vIsNum { n.WriteByte(v) capNext = true } else { - capNext = v == '_' || v == ' ' || v == '-' || v == '.' + capNext = separators.Contains(v) } } return n.String() diff --git a/chars.go b/chars.go new file mode 100644 index 0000000..a4b5214 --- /dev/null +++ b/chars.go @@ -0,0 +1,33 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Ian Coleman + * Copyright (c) 2018 Ma_124, + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, Subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or Substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package strcase + +import asciiset "github.com/elliotwutingfeng/asciiset" + +var numbers, _ = asciiset.MakeASCIISet("0123456789") +var smallLetters, _ = asciiset.MakeASCIISet("abcdefghijklmnopqrstuvwxyz") +var capitalLetters, _ = asciiset.MakeASCIISet("ABCDEFGHIJKLMNOPQRSTUVWXYZ") +var separators, _ = asciiset.MakeASCIISet(" _-.") diff --git a/go.mod b/go.mod index a6b5cb6..6254bee 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/iancoleman/strcase go 1.16 + +require github.com/elliotwutingfeng/asciiset v0.0.0-20230318033524-4982e1c19ba9 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..de56bab --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/elliotwutingfeng/asciiset v0.0.0-20230318033524-4982e1c19ba9 h1:FFVdM8Ai0s6G5W5ltfXGV/3nINtnUe9590OjdwKamuA= +github.com/elliotwutingfeng/asciiset v0.0.0-20230318033524-4982e1c19ba9/go.mod h1:GLo/8fDswSAniFG+BFIaiSPcK610jyzgEhWYPQwuQdw= diff --git a/snake.go b/snake.go index df018bc..1b2582b 100644 --- a/snake.go +++ b/snake.go @@ -68,8 +68,8 @@ func ToScreamingDelimited(s string, delimiter uint8, ignore string, screaming bo n := strings.Builder{} n.Grow(len(s) + 2) // nominal 2 bytes of extra space for inserted delimiters for i, v := range []byte(s) { - vIsCap := v >= 'A' && v <= 'Z' - vIsLow := v >= 'a' && v <= 'z' + vIsCap := capitalLetters.Contains(v) + vIsLow := smallLetters.Contains(v) if vIsLow && screaming { v += 'A' v -= 'a' @@ -81,16 +81,16 @@ func ToScreamingDelimited(s string, delimiter uint8, ignore string, screaming bo // treat acronyms as words, eg for JSONData -> JSON is a whole word if i+1 < len(s) { next := s[i+1] - vIsNum := v >= '0' && v <= '9' - nextIsCap := next >= 'A' && next <= 'Z' - nextIsLow := next >= 'a' && next <= 'z' - nextIsNum := next >= '0' && next <= '9' + vIsNum := numbers.Contains(v) + nextIsCap := capitalLetters.Contains(next) + nextIsLow := smallLetters.Contains(next) + nextIsNum := numbers.Contains(next) // add underscore if next letter case type is changed if (vIsCap && (nextIsLow || nextIsNum)) || (vIsLow && (nextIsCap || nextIsNum)) || (vIsNum && (nextIsCap || nextIsLow)) { - prevIgnore := ignore != "" && i > 0 && strings.ContainsAny(string(s[i-1]), ignore) + prevIgnore := ignore != "" && i > 0 && strings.IndexByte(ignore, s[i-1]) != -1 if !prevIgnore { if vIsCap && nextIsLow { - if prevIsCap := i > 0 && s[i-1] >= 'A' && s[i-1] <= 'Z'; prevIsCap { + if prevIsCap := i > 0 && capitalLetters.Contains(s[i-1]); prevIsCap { n.WriteByte(delimiter) } } @@ -103,7 +103,7 @@ func ToScreamingDelimited(s string, delimiter uint8, ignore string, screaming bo } } - if (v == ' ' || v == '_' || v == '-' || v == '.') && !strings.ContainsAny(string(v), ignore) { + if separators.Contains(v) && strings.IndexByte(ignore, v) == -1 { // replace space/underscore/hyphen/dot with delimiter n.WriteByte(delimiter) } else { From ddf193a132994a0d2e974883caa8235c97b51d6b Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Sun, 28 May 2023 06:13:48 +0800 Subject: [PATCH 2/3] Upgrade ASCIISet --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 6254bee..8f4b981 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/iancoleman/strcase go 1.16 -require github.com/elliotwutingfeng/asciiset v0.0.0-20230318033524-4982e1c19ba9 +require github.com/elliotwutingfeng/asciiset v0.0.0-20230527213918-af247325a7ab diff --git a/go.sum b/go.sum index de56bab..0563b5d 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -github.com/elliotwutingfeng/asciiset v0.0.0-20230318033524-4982e1c19ba9 h1:FFVdM8Ai0s6G5W5ltfXGV/3nINtnUe9590OjdwKamuA= -github.com/elliotwutingfeng/asciiset v0.0.0-20230318033524-4982e1c19ba9/go.mod h1:GLo/8fDswSAniFG+BFIaiSPcK610jyzgEhWYPQwuQdw= +github.com/elliotwutingfeng/asciiset v0.0.0-20230527213918-af247325a7ab h1:L06ECyWA04vwnHZbHMa1llt3SU0q02fx5JOsV0c/B6U= +github.com/elliotwutingfeng/asciiset v0.0.0-20230527213918-af247325a7ab/go.mod h1:GLo/8fDswSAniFG+BFIaiSPcK610jyzgEhWYPQwuQdw= From afeba1d279a0526492a1a46ba7817c7c768fcd06 Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Fri, 2 Jun 2023 08:55:32 +0800 Subject: [PATCH 3/3] Upgrade ASCIISet --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 8f4b981..ae26dd3 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/iancoleman/strcase go 1.16 -require github.com/elliotwutingfeng/asciiset v0.0.0-20230527213918-af247325a7ab +require github.com/elliotwutingfeng/asciiset v0.0.0-20230602005253-7f88bc28b13f diff --git a/go.sum b/go.sum index 0563b5d..d1d39ee 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -github.com/elliotwutingfeng/asciiset v0.0.0-20230527213918-af247325a7ab h1:L06ECyWA04vwnHZbHMa1llt3SU0q02fx5JOsV0c/B6U= -github.com/elliotwutingfeng/asciiset v0.0.0-20230527213918-af247325a7ab/go.mod h1:GLo/8fDswSAniFG+BFIaiSPcK610jyzgEhWYPQwuQdw= +github.com/elliotwutingfeng/asciiset v0.0.0-20230602005253-7f88bc28b13f h1:2zrq6AHwG5HdTmNunOU3tRNPQ0F+R9W8mIxgSJzGwkE= +github.com/elliotwutingfeng/asciiset v0.0.0-20230602005253-7f88bc28b13f/go.mod h1:GLo/8fDswSAniFG+BFIaiSPcK610jyzgEhWYPQwuQdw=