diff --git a/_xtool/llcppsymg/_cmptest/names_test/llgo.expect b/_xtool/llcppsymg/_cmptest/names_test/llgo.expect index 717438a9..20e685d2 100644 --- a/_xtool/llcppsymg/_cmptest/names_test/llgo.expect +++ b/_xtool/llcppsymg/_cmptest/names_test/llgo.expect @@ -25,13 +25,14 @@ Input: sqlite_file, Output: SqliteFile Input: _gmp_err, Output: X_gmpErr Input: 123illegal, Output: X123illegal Input: alreadyCamel, Output: AlreadyCamel -Input: _x__y, Output: X_xY -Input: _x_y, Output: X_xY -Input: _x___y, Output: X_xY -Input: x_y, Output: XY -Input: x__y, Output: XY -Input: x_y_, Output: XY -Input: x__y_, Output: XY +Input: _ab__cd, Output: X_abCd +Input: _ab_cd, Output: X_abCd +Input: _ab___cd, Output: X_abCd +Input: ab_cd, Output: AbCd +Input: ab__cd, Output: AbCd +Input: ab_cd_, Output: AbCd_ +Input: ab__cd_, Output: AbCd_ +Input: ab__cd__, Output: AbCd__ Input: _, Output: X_ Input: __, Output: X__ Input: ___, Output: X___ @@ -42,13 +43,13 @@ Input: _sqlite_file, Output: X_sqlite_file Input: 123illegal, Output: X123illegal Input: CODE_MASK, Output: CODE_MASK Input: _CODE_MASK, Output: X_CODE_MASK -Input: _x__y, Output: X_x__y -Input: _x_y, Output: X_x_y -Input: _x___y, Output: X_x___y -Input: x_y, Output: X_y -Input: x__y, Output: X__y -Input: x_y_, Output: X_y_ -Input: x__y_, Output: X__y_ +Input: _ab__cd, Output: X_ab__cd +Input: _ab_cd, Output: X_ab_cd +Input: _ab___cd, Output: X_ab___cd +Input: ab_cd, Output: Ab_cd +Input: ab__cd, Output: Ab__cd +Input: ab_cd_, Output: Ab_cd_ +Input: ab__cd_, Output: Ab__cd_ Input: _, Output: X_ Input: __, Output: X__ Input: ___, Output: X___ diff --git a/_xtool/llcppsymg/_cmptest/names_test/names.go b/_xtool/llcppsymg/_cmptest/names_test/names.go index a37e2e6e..2a6f0ced 100644 --- a/_xtool/llcppsymg/_cmptest/names_test/names.go +++ b/_xtool/llcppsymg/_cmptest/names_test/names.go @@ -83,13 +83,14 @@ func TestPubName() { {"_gmp_err", "X_gmpErr"}, {"123illegal", "X123illegal"}, {"alreadyCamel", "AlreadyCamel"}, - {"_x__y", "X_xY"}, - {"_x_y", "X_xY"}, - {"_x___y", "X_xY"}, - {"x_y", "XY"}, - {"x__y", "XY"}, - {"x_y_", "XY"}, - {"x__y_", "XY"}, + {"_ab__cd", "X_abCd"}, + {"_ab_cd", "X_abCd"}, + {"_ab___cd", "X_abCd"}, + {"ab_cd", "AbCd"}, + {"ab__cd", "AbCd"}, + {"ab_cd_", "AbCd_"}, + {"ab__cd_", "AbCd_"}, + {"ab__cd__", "AbCd__"}, {"_", "X_"}, {"__", "X__"}, {"___", "X___"}, @@ -116,13 +117,13 @@ func TestExportName() { {"123illegal", "X123illegal"}, {"CODE_MASK", "CODE_MASK"}, {"_CODE_MASK", "X_CODE_MASK"}, - {"_x__y", "X_x__y"}, - {"_x_y", "X_x_y"}, - {"_x___y", "X_x___y"}, - {"x_y", "X_y"}, - {"x__y", "X__y"}, - {"x_y_", "X_y_"}, - {"x__y_", "X__y_"}, + {"_ab__cd", "X_ab__cd"}, + {"_ab_cd", "X_ab_cd"}, + {"_ab___cd", "X_ab___cd"}, + {"ab_cd", "Ab_cd"}, + {"ab__cd", "Ab__cd"}, + {"ab_cd_", "Ab_cd_"}, + {"ab__cd_", "Ab__cd_"}, {"_", "X_"}, {"__", "X__"}, {"___", "X___"}, diff --git a/_xtool/llcppsymg/names/names.go b/_xtool/llcppsymg/names/names.go index 163a78b0..43ece329 100644 --- a/_xtool/llcppsymg/names/names.go +++ b/_xtool/llcppsymg/names/names.go @@ -81,22 +81,25 @@ func removePrefixedName(name string, trimPrefixes []string) string { } func PubName(name string) string { - if len(name) == 0 { - return name - } - fChar := name[0] - if fChar == '_' { - i := 0 - for i < len(name) && name[i] == '_' { - i++ - } - prefix := name[:i] - return "X" + prefix + ToCamelCase(name[i:], false) + baseName := strings.Trim(name, "_") + if len(baseName) == 0 { + return "X" + name } - if unicode.IsDigit(rune(fChar)) { - return "X" + ToCamelCase(name, false) + prefix := preUScore(name) + suffix := sufUScore(name) + + if len(prefix) != 0 || unicode.IsDigit(rune(baseName[0])) { + return "X" + prefix + ToCamelCase(baseName, false) + suffix } - return ToCamelCase(name, true) + return ToCamelCase(baseName, true) + suffix +} + +func sufUScore(name string) string { + return strings.Repeat("_", len(name)-len(strings.TrimRight(name, "_"))) +} + +func preUScore(name string) string { + return strings.Repeat("_", len(name)-len(strings.TrimLeft(name, "_"))) } func ToCamelCase(s string, firstPartUpper bool) string {