-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
247 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright (C) 2020-2021, Xiongfa Li. | ||
// @author xiongfa.li | ||
// @version V1.0 | ||
// Description: | ||
|
||
package stringutils | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSnake2Camel(t *testing.T) { | ||
t.Run("compute_node", func(t *testing.T) { | ||
s := Snake2camel("compute_node") | ||
if s != "ComputeNode" { | ||
t.Fatal("expect ComputeNode got: ", s) | ||
} | ||
}) | ||
t.Run("COMPUTE_NODE", func(t *testing.T) { | ||
s := Snake2camel("COMPUTE_NODE") | ||
if s != "ComputeNode" { | ||
t.Fatal("expect ComputeNode got: ", s) | ||
} | ||
}) | ||
|
||
t.Run("COmpute_NODE", func(t *testing.T) { | ||
s := Snake2camel("COmpute_NODE") | ||
if s != "ComputeNode" { | ||
t.Fatal("expect ComputeNode got: ", s) | ||
} | ||
}) | ||
|
||
t.Run("COmuteNODE", func(t *testing.T) { | ||
s := Snake2camel("COmuteNODE") | ||
if s != "COmuteNODE" { | ||
t.Fatal("expect COmuteNODE got: ", s) | ||
} | ||
}) | ||
|
||
t.Run("__compute_nOde", func(t *testing.T) { | ||
s := Snake2camel("__compute_nOde") | ||
if s != "ComputeNode" { | ||
t.Fatal("expect ComputeNode got: ", s) | ||
} | ||
}) | ||
|
||
t.Run("__compute__nOde", func(t *testing.T) { | ||
s := Snake2camel("__compute__nOde") | ||
if s != "ComputeNode" { | ||
t.Fatal("expect ComputeNode got: ", s) | ||
} | ||
}) | ||
|
||
t.Run("computeNode", func(t *testing.T) { | ||
s := Snake2camel("computeNode") | ||
if s != "ComputeNode" { | ||
t.Fatal("expect ComputeNode got: ", s) | ||
} | ||
}) | ||
|
||
t.Run("_computeNode_", func(t *testing.T) { | ||
s := Snake2camel("_computeNode_") | ||
if s != "ComputeNode" { | ||
t.Fatal("expect ComputeNode got: ", s) | ||
} | ||
}) | ||
|
||
t.Run("ComputeNode", func(t *testing.T) { | ||
s := Snake2camel("ComputeNode") | ||
if s != "ComputeNode" { | ||
t.Fatal("expect ComputeNode got: ", s) | ||
} | ||
}) | ||
|
||
t.Run("ComputeNode_detail", func(t *testing.T) { | ||
s := Snake2camel("ComputeNode_detail") | ||
if s != "ComputeNodeDetail" { | ||
t.Fatal("expect ComputeNode got: ", s) | ||
} | ||
}) | ||
|
||
t.Run("_computeNode_detail_", func(t *testing.T) { | ||
s := Snake2camel("_computeNode_detail_") | ||
if s != "ComputeNodeDetail" { | ||
t.Fatal("expect ComputeNode got: ", s) | ||
} | ||
}) | ||
|
||
t.Run("_compute_Node_detail_", func(t *testing.T) { | ||
s := Snake2camel("_compute_Node_detail_") | ||
if s != "ComputeNodeDetail" { | ||
t.Fatal("expect ComputeNode got: ", s) | ||
} | ||
}) | ||
|
||
t.Run("_compute_node_detail_", func(t *testing.T) { | ||
s := Snake2camel("_compute_node_detail_") | ||
if s != "ComputeNodeDetail" { | ||
t.Fatal("expect ComputeNode got: ", s) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* Copyright (C) 2019, Xiongfa Li. | ||
* All right reserved. | ||
* @author xiongfa.li | ||
* @version V1.0 | ||
* Description: | ||
*/ | ||
|
||
package stringutils | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
) | ||
|
||
// snake string, XxYy to xx_yy , XxYY to xx_yy | ||
func Camel2snake(s string) string { | ||
data := make([]byte, 0, len(s)*2) | ||
j := false | ||
num := len(s) | ||
for i := 0; i < num; i++ { | ||
d := s[i] | ||
if i > 0 && d >= 'A' && d <= 'Z' && j { | ||
data = append(data, '_') | ||
} | ||
if d != '_' { | ||
j = true | ||
} | ||
data = append(data, d) | ||
} | ||
return strings.ToLower(string(data)) | ||
} | ||
|
||
// camel string, xx_yy to XxYy | ||
func Snake2camel(s string) string { | ||
s = strings.Trim(s, "_") | ||
if strings.Index(s, "_") != -1 { | ||
return Snake2camel3(s) | ||
} else { | ||
d := []byte(s) | ||
if len(d) > 0 { | ||
if d[0] >= 'a' && d[0] <= 'z' { | ||
d[0] -= 32 | ||
return string(d) | ||
} | ||
} | ||
} | ||
return s | ||
} | ||
|
||
func Snake2camel2(s string) string { | ||
data := make([]byte, 0, len(s)) | ||
j := false | ||
k := false | ||
num := len(s) - 1 | ||
for i := 0; i <= num; i++ { | ||
d := s[i] | ||
if k == false && d >= 'A' && d <= 'Z' { | ||
k = true | ||
} | ||
if d >= 'a' && d <= 'z' && (j || k == false) { | ||
d = d - 32 | ||
j = false | ||
k = true | ||
} | ||
if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' { | ||
j = true | ||
continue | ||
} | ||
data = append(data, d) | ||
} | ||
return string(data) | ||
} | ||
|
||
func Snake2camel3(s string) string { | ||
strs := strings.Split(s, "_") | ||
buf := bytes.Buffer{} | ||
buf.Grow(len(s)) | ||
for _, v := range strs { | ||
if v == "" { | ||
continue | ||
} | ||
// 第一个字符必须大写 | ||
d := v[0] | ||
if d >= 'a' && d <= 'z' { | ||
d -= 32 | ||
} | ||
buf.WriteByte(d) | ||
low := false | ||
for i := 1; i < len(v); i++ { | ||
d := v[i] | ||
// 中间夹杂小写 | ||
if d >= 'a' && d <= 'z' { | ||
low = true | ||
} else if d >= 'A' && d <= 'Z' && !low { | ||
// 大写且中间没有经过小写,则表明是连续大写,则改写为小写 | ||
buf.WriteByte(d + 32) | ||
continue | ||
} | ||
buf.WriteByte(d) | ||
} | ||
} | ||
return buf.String() | ||
} |
Oops, something went wrong.