From 6fab6530048ce8268098c5ef39c193028c5947ec Mon Sep 17 00:00:00 2001 From: Nathan Baulch Date: Sun, 16 Aug 2020 23:02:43 +1000 Subject: [PATCH] Remove problematic numbers regexp --- camel.go | 6 ++++-- numbers.go | 38 -------------------------------------- snake.go | 7 ++++--- snake_test.go | 6 ++++++ 4 files changed, 14 insertions(+), 43 deletions(-) delete mode 100644 numbers.go diff --git a/camel.go b/camel.go index ab2d261..f31646e 100644 --- a/camel.go +++ b/camel.go @@ -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 == '.' } diff --git a/numbers.go b/numbers.go deleted file mode 100644 index fdf07cb..0000000 --- a/numbers.go +++ /dev/null @@ -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) -} diff --git a/snake.go b/snake.go index fa1f8f4..b849a58 100644 --- a/snake.go +++ b/snake.go @@ -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 diff --git a/snake_test.go b/snake_test.go index 5482999..2e34372 100644 --- a/snake_test.go +++ b/snake_test.go @@ -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]