-
Notifications
You must be signed in to change notification settings - Fork 232
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
zhongkaixiang.kaka
committed
May 13, 2021
1 parent
6a6cce9
commit f9d166a
Showing
8 changed files
with
710 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,7 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# IDE or Editor's config | ||
.idea/ | ||
|
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,9 @@ | ||
# stringx | ||
|
||
## Introduction | ||
Extension/Helper of String Operation. | ||
|
||
## Features | ||
- Transform(Reverse, Rotate, Shuffle ...) | ||
- Construction(Pad, Repeat...) | ||
- Matching(IsAlpha, IsAlphanumeric, IsNumeric ...) |
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,16 @@ | ||
// Copyright 2021 ByteDance Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package stringx provides extensions of string Operation. | ||
package stringx |
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,76 @@ | ||
// Copyright 2021 ByteDance Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package stringx | ||
|
||
import ( | ||
"fmt" | ||
"unicode/utf8" | ||
) | ||
|
||
func Example_sub() { | ||
fmt.Printf("Sub-[0:100]=%s\n", Sub("", 0, 100)) | ||
fmt.Printf("Sub-facgbheidjk[3:9]=%s\n", Sub("facgbheidjk", 3, 9)) | ||
fmt.Printf("Sub-facgbheidjk[-50:100]=%s\n", Sub("facgbheidjk", -50, 100)) | ||
fmt.Printf("Sub-facgbheidjk[-3:length]=%s\n", Sub("facgbheidjk", -3, utf8.RuneCountInString("facgbheidjk"))) | ||
fmt.Printf("Sub-facgbheidjk[-3:-1]=%s\n", Sub("facgbheidjk", -3, -1)) | ||
fmt.Printf("Sub-zh英文hun排[2:5]=%s\n", Sub("zh英文hun排", 2, 5)) | ||
fmt.Printf("Sub-zh英文hun排[2:-1]=%s\n", Sub("zh英文hun排", 2, -1)) | ||
fmt.Printf("Sub-zh英文hun排[-100:-1]=%s\n", Sub("zh英文hun排", -100, -1)) | ||
fmt.Printf("Sub-zh英文hun排[-100:-90]=%s\n", Sub("zh英文hun排", -100, -90)) | ||
fmt.Printf("Sub-zh英文hun排[-10:-90]=%s\n", Sub("zh英文hun排", -10, -90)) | ||
|
||
// Output: | ||
// Sub-[0:100]= | ||
// Sub-facgbheidjk[3:9]=gbheid | ||
// Sub-facgbheidjk[-50:100]=facgbheidjk | ||
// Sub-facgbheidjk[-3:length]=djk | ||
// Sub-facgbheidjk[-3:-1]=dj | ||
// Sub-zh英文hun排[2:5]=英文h | ||
// Sub-zh英文hun排[2:-1]=英文hun | ||
// Sub-zh英文hun排[-100:-1]=zh英文hun | ||
// Sub-zh英文hun排[-100:-90]= | ||
// Sub-zh英文hun排[-10:-90]= | ||
} | ||
|
||
func Example_substart() { | ||
fmt.Printf("SubStart-[0:]=%s\n", SubStart("", 0)) | ||
fmt.Printf("SubStart-[2:]=%s\n", SubStart("", 2)) | ||
fmt.Printf("SubStart-facgbheidjk[3:]=%s\n", SubStart("facgbheidjk", 3)) | ||
fmt.Printf("SubStart-facgbheidjk[-50:]=%s\n", SubStart("facgbheidjk", -50)) | ||
fmt.Printf("SubStart-facgbheidjk[-3:]=%s\n", SubStart("facgbheidjk", -3)) | ||
fmt.Printf("SubStart-zh英文hun排[3:]=%s\n", SubStart("zh英文hun排", 3)) | ||
|
||
// Output: | ||
// SubStart-[0:]= | ||
// SubStart-[2:]= | ||
// SubStart-facgbheidjk[3:]=gbheidjk | ||
// SubStart-facgbheidjk[-50:]=facgbheidjk | ||
// SubStart-facgbheidjk[-3:]=djk | ||
// SubStart-zh英文hun排[3:]=文hun排 | ||
} | ||
|
||
func Example_pad() { | ||
|
||
fmt.Printf("PadLeft=[%s]\n", PadLeftSpace("abc", 7)) | ||
fmt.Printf("PadLeft=[%s]\n", PadLeftChar("abc", 7, '-')) | ||
fmt.Printf("PadCenter=[%s]\n", PadCenterChar("abc", 7, '-')) | ||
fmt.Printf("PadCenter=[%s]\n", PadCenterChar("abcd", 7, '-')) | ||
|
||
// Output: | ||
// PadLeft=[ abc] | ||
// PadLeft=[----abc] | ||
// PadCenter=[--abc--] | ||
// PadCenter=[-abcd--] | ||
} |
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,53 @@ | ||
// Copyright 2021 ByteDance Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package stringx | ||
|
||
import ( | ||
"unicode" | ||
) | ||
|
||
// IsAlpha checks if the string contains only unicode letters. | ||
func IsAlpha(s string) bool { | ||
for _, v := range s { | ||
if !unicode.IsLetter(v) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// IsAlphanumeric checks if the string contains only Unicode letters or digits. | ||
func IsAlphanumeric(s string) bool { | ||
for _, v := range s { | ||
if !isAlphanumeric(v) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// IsNumeric Checks if the string contains only digits. A decimal point is not a digit and returns false. | ||
func IsNumeric(s string) bool { | ||
for _, v := range s { | ||
if !unicode.IsDigit(v) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func isAlphanumeric(v rune) bool { | ||
return unicode.IsDigit(v) || unicode.IsLetter(v) | ||
} |
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,37 @@ | ||
// Copyright 2021 ByteDance Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package stringx | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIs(t *testing.T) { | ||
is := assert.New(t) | ||
|
||
is.False(IsNumeric(" bob ")) | ||
is.True(IsNumeric("123")) | ||
|
||
is.False(IsAlpha("123")) | ||
is.True(IsAlpha("Voa")) | ||
is.True(IsAlpha("bròwn")) | ||
|
||
is.True(IsAlphanumeric("Voa")) | ||
is.True(IsAlphanumeric("123")) | ||
is.True(IsAlphanumeric("v123oa")) | ||
is.False(IsAlphanumeric("v123oa,")) | ||
} |
Oops, something went wrong.