Remove problematic numbers regexp

This commit is contained in:
Nathan Baulch 2020-08-16 23:02:43 +10:00
parent 820267b5d9
commit 6fab653004
4 changed files with 14 additions and 43 deletions

View File

@ -38,7 +38,6 @@ func toCamelInitCase(s string, initCase bool) string {
s = a
}
s = addWordBoundariesToNumbers(s)
n := strings.Builder{}
n.Grow(len(s))
capNext := initCase
@ -56,9 +55,12 @@ func toCamelInitCase(s string, initCase bool) string {
v -= 'A'
}
}
if vIsCap || vIsLow || (v >= '0' && v <= '9') {
if vIsCap || vIsLow {
n.WriteByte(v)
capNext = false
} else if vIsNum := v >= '0' && v <= '9'; vIsNum {
n.WriteByte(v)
capNext = true
} else {
capNext = v == '_' || v == ' ' || v == '-' || v == '.'
}

View File

@ -1,38 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ian Coleman
*
* 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 (
"regexp"
)
var numberSequence = regexp.MustCompile(`([a-zA-Z])(\d+)([a-zA-Z]?)`)
var numberReplacement = []byte(`$1 $2 $3`)
func addWordBoundariesToNumbers(s string) string {
b := []byte(s)
b = numberSequence.ReplaceAll(b, numberReplacement)
return string(b)
}

View File

@ -64,7 +64,6 @@ func ToDelimited(s string, delimiter uint8) string {
// or delimited.snake.case
// (in this case `delimiter = '.'; screaming = false`)
func ToScreamingDelimited(s string, delimiter uint8, ignore uint8, screaming bool) string {
s = addWordBoundariesToNumbers(s)
n := strings.Builder{}
n.Grow(len(s) + 2) // nominal 2 bytes of extra space for inserted delimiters
start := true
@ -99,10 +98,12 @@ func ToScreamingDelimited(s string, delimiter uint8, ignore uint8, screaming boo
// 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'
// add underscore if next letter case type is changed
if (vIsCap && nextIsLow) || (vIsLow && nextIsCap) {
if (vIsCap && (nextIsLow || nextIsNum)) || (vIsLow && (nextIsCap || nextIsNum)) || (vIsNum && (nextIsCap || nextIsLow)) {
if prevIgnore := ignore > 0 && i > 0 && s[i-1] == ignore; !prevIgnore {
if vIsCap && nextIsLow {
if prevIsCap := i > 0 && s[i-1] >= 'A' && s[i-1] <= 'Z'; prevIsCap {
@ -110,7 +111,7 @@ func ToScreamingDelimited(s string, delimiter uint8, ignore uint8, screaming boo
}
}
n.WriteByte(v)
if vIsLow {
if vIsLow || vIsNum || nextIsNum {
n.WriteByte(delimiter)
}
continue

View File

@ -48,6 +48,12 @@ func toSnake(tb testing.TB) {
{"JSONData", "json_data"},
{"userID", "user_id"},
{"AAAbbb", "aa_abbb"},
{"1A2", "1_a_2"},
{"A1B", "a_1_b"},
{"A1A2A3", "a_1_a_2_a_3"},
{"A1 A2 A3", "a_1_a_2_a_3"},
{"AB1AB2AB3", "ab_1_ab_2_ab_3"},
{"AB1 AB2 AB3", "ab_1_ab_2_ab_3"},
}
for _, i := range cases {
in := i[0]