Remove problematic numbers regexp
This commit is contained in:
parent
820267b5d9
commit
6fab653004
6
camel.go
6
camel.go
@ -38,7 +38,6 @@ func toCamelInitCase(s string, initCase bool) string {
|
|||||||
s = a
|
s = a
|
||||||
}
|
}
|
||||||
|
|
||||||
s = addWordBoundariesToNumbers(s)
|
|
||||||
n := strings.Builder{}
|
n := strings.Builder{}
|
||||||
n.Grow(len(s))
|
n.Grow(len(s))
|
||||||
capNext := initCase
|
capNext := initCase
|
||||||
@ -56,9 +55,12 @@ func toCamelInitCase(s string, initCase bool) string {
|
|||||||
v -= 'A'
|
v -= 'A'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if vIsCap || vIsLow || (v >= '0' && v <= '9') {
|
if vIsCap || vIsLow {
|
||||||
n.WriteByte(v)
|
n.WriteByte(v)
|
||||||
capNext = false
|
capNext = false
|
||||||
|
} else if vIsNum := v >= '0' && v <= '9'; vIsNum {
|
||||||
|
n.WriteByte(v)
|
||||||
|
capNext = true
|
||||||
} else {
|
} else {
|
||||||
capNext = v == '_' || v == ' ' || v == '-' || v == '.'
|
capNext = v == '_' || v == ' ' || v == '-' || v == '.'
|
||||||
}
|
}
|
||||||
|
38
numbers.go
38
numbers.go
@ -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)
|
|
||||||
}
|
|
7
snake.go
7
snake.go
@ -64,7 +64,6 @@ func ToDelimited(s string, delimiter uint8) string {
|
|||||||
// or delimited.snake.case
|
// or delimited.snake.case
|
||||||
// (in this case `delimiter = '.'; screaming = false`)
|
// (in this case `delimiter = '.'; screaming = false`)
|
||||||
func ToScreamingDelimited(s string, delimiter uint8, ignore uint8, screaming bool) string {
|
func ToScreamingDelimited(s string, delimiter uint8, ignore uint8, screaming bool) string {
|
||||||
s = addWordBoundariesToNumbers(s)
|
|
||||||
n := strings.Builder{}
|
n := strings.Builder{}
|
||||||
n.Grow(len(s) + 2) // nominal 2 bytes of extra space for inserted delimiters
|
n.Grow(len(s) + 2) // nominal 2 bytes of extra space for inserted delimiters
|
||||||
start := true
|
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
|
// treat acronyms as words, eg for JSONData -> JSON is a whole word
|
||||||
if i+1 < len(s) {
|
if i+1 < len(s) {
|
||||||
next := s[i+1]
|
next := s[i+1]
|
||||||
|
vIsNum := v >= '0' && v <= '9'
|
||||||
nextIsCap := next >= 'A' && next <= 'Z'
|
nextIsCap := next >= 'A' && next <= 'Z'
|
||||||
nextIsLow := next >= 'a' && next <= 'z'
|
nextIsLow := next >= 'a' && next <= 'z'
|
||||||
|
nextIsNum := next >= '0' && next <= '9'
|
||||||
// add underscore if next letter case type is changed
|
// 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 prevIgnore := ignore > 0 && i > 0 && s[i-1] == ignore; !prevIgnore {
|
||||||
if vIsCap && nextIsLow {
|
if vIsCap && nextIsLow {
|
||||||
if prevIsCap := i > 0 && s[i-1] >= 'A' && s[i-1] <= 'Z'; prevIsCap {
|
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)
|
n.WriteByte(v)
|
||||||
if vIsLow {
|
if vIsLow || vIsNum || nextIsNum {
|
||||||
n.WriteByte(delimiter)
|
n.WriteByte(delimiter)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
|
@ -48,6 +48,12 @@ func toSnake(tb testing.TB) {
|
|||||||
{"JSONData", "json_data"},
|
{"JSONData", "json_data"},
|
||||||
{"userID", "user_id"},
|
{"userID", "user_id"},
|
||||||
{"AAAbbb", "aa_abbb"},
|
{"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 {
|
for _, i := range cases {
|
||||||
in := i[0]
|
in := i[0]
|
||||||
|
Loading…
Reference in New Issue
Block a user