Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comments update for package gstr #3233

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions container/garray/garray_normal_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ func (a *IntArray) InsertBefore(index int, values ...int) error {
a.mu.Lock()
defer a.mu.Unlock()
if index < 0 || index >= len(a.array) {
return gerror.NewCodef(gcode.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
return gerror.NewCodef(
gcode.CodeInvalidParameter,
"index %d out of array range %d",
index, len(a.array),
)
}
rear := append([]int{}, a.array[index:]...)
a.array = append(a.array[0:index], values...)
Expand All @@ -186,7 +190,11 @@ func (a *IntArray) InsertAfter(index int, values ...int) error {
a.mu.Lock()
defer a.mu.Unlock()
if index < 0 || index >= len(a.array) {
return gerror.NewCodef(gcode.CodeInvalidParameter, "index %d out of array range %d", index, len(a.array))
return gerror.NewCodef(
gcode.CodeInvalidParameter,
"index %d out of array range %d",
index, len(a.array),
)
}
rear := append([]int{}, a.array[index+1:]...)
a.array = append(a.array[0:index+1], values...)
Expand Down Expand Up @@ -583,7 +591,11 @@ func (a *IntArray) Fill(startIndex int, num int, value int) error {
a.mu.Lock()
defer a.mu.Unlock()
if startIndex < 0 || startIndex > len(a.array) {
return gerror.NewCodef(gcode.CodeInvalidParameter, "index %d out of array range %d", startIndex, len(a.array))
return gerror.NewCodef(
gcode.CodeInvalidParameter,
"index %d out of array range %d",
startIndex, len(a.array),
)
}
for i := startIndex; i < startIndex+num; i++ {
if i > len(a.array)-1 {
Expand Down
6 changes: 4 additions & 2 deletions container/gqueue/gqueue_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ func TestQueue_Close(t *testing.T) {
q1 := gqueue.New()
q1.Push(1)
q1.Push(2)
time.Sleep(time.Millisecond)
// wait sync to channel
time.Sleep(10 * time.Millisecond)
t.Assert(q1.Len(), 2)
q1.Close()
})
gtest.C(t, func(t *gtest.T) {
q1 := gqueue.New(2)
q1.Push(1)
q1.Push(2)
time.Sleep(time.Millisecond)
// wait sync to channel
time.Sleep(10 * time.Millisecond)
t.Assert(q1.Len(), 2)
q1.Close()
})
Expand Down
28 changes: 18 additions & 10 deletions os/gfile/gfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,19 @@ func SelfDir() string {
// Trailing path separators are removed before extracting the last element.
// If the path is empty, Base returns ".".
// If the path consists entirely of separators, Basename returns a single separator.
//
// Example:
// /var/www/file.js -> file.js
// file.js -> file.js
// Basename("/var/www/file.js") -> file.js
// Basename("file.js") -> file.js
func Basename(path string) string {
return filepath.Base(path)
}

// Name returns the last element of path without file extension.
//
// Example:
// /var/www/file.js -> file
// file.js -> file
// Name("/var/www/file.js") -> file
// Name("file.js") -> file
func Name(path string) string {
base := filepath.Base(path)
if i := strings.LastIndexByte(base, '.'); i != -1 {
Expand All @@ -378,6 +380,10 @@ func Name(path string) string {
// If the `path` is ".", Dir treats the path as current working directory.
// If the `path` consists entirely of separators, Dir returns a single separator.
// The returned path does not end in a separator unless it is the root directory.
//
// Example:
// Dir("/var/www/file.js") -> "/var/www"
// Dir("file.js") -> "."
func Dir(path string) string {
if path == "." {
return filepath.Dir(RealPath(path))
Expand Down Expand Up @@ -416,9 +422,10 @@ func IsEmpty(path string) bool {
// in the final element of path; it is empty if there is
// no dot.
// Note: the result contains symbol '.'.
// Eg:
// main.go => .go
// api.json => .json
//
// Example:
// Ext("main.go") => .go
// Ext("api.json") => .json
func Ext(path string) string {
ext := filepath.Ext(path)
if p := strings.IndexByte(ext, '?'); p != -1 {
Expand All @@ -429,9 +436,10 @@ func Ext(path string) string {

// ExtName is like function Ext, which returns the file name extension used by path,
// but the result does not contain symbol '.'.
// Eg:
// main.go => go
// api.json => json
//
// Example:
// ExtName("main.go") => go
// ExtName("api.json") => json
func ExtName(path string) string {
return strings.TrimLeft(Ext(path), ".")
}
Expand Down
3 changes: 3 additions & 0 deletions text/gstr/gstr_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func InArray(a []string, s string) bool {
}

// PrefixArray adds `prefix` string for each item of `array`.
//
// Example:
// PrefixArray(["a","b"], "gf_") -> ["gf_a", "gf_b"]
func PrefixArray(array []string, prefix string) {
for k, v := range array {
array[k] = prefix + v
Expand Down
29 changes: 28 additions & 1 deletion text/gstr/gstr_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,17 @@ func CaseConvert(s string, caseType CaseType) string {
}

// CaseCamel converts a string to CamelCase.
//
// Example:
// CaseCamel("any_kind_of_string") -> AnyKindOfString
func CaseCamel(s string) string {
return toCamelInitCase(s, true)
}

// CaseCamelLower converts a string to lowerCamelCase.
//
// Example:
// CaseCamelLower("any_kind_of_string") -> anyKindOfString
func CaseCamelLower(s string) string {
if s == "" {
return s
Expand All @@ -120,17 +126,26 @@ func CaseCamelLower(s string) string {
}

// CaseSnake converts a string to snake_case.
//
// Example:
// CaseSnake("AnyKindOfString") -> any_kind_of_string
func CaseSnake(s string) string {
return CaseDelimited(s, '_')
}

// CaseSnakeScreaming converts a string to SNAKE_CASE_SCREAMING.
//
// Example:
// CaseSnakeScreaming("AnyKindOfString") -> ANY_KIND_OF_STRING
func CaseSnakeScreaming(s string) string {
return CaseDelimitedScreaming(s, '_', true)
}

// CaseSnakeFirstUpper converts a string like "RGBCodeMd5" to "rgb_code_md5".
// TODO for efficiency should change regexp to traversing string in future.
//
// Example:
// CaseSnakeFirstUpper("RGBCodeMd5") -> rgb_code_md5
func CaseSnakeFirstUpper(word string, underscore ...string) string {
replace := "_"
if len(underscore) > 0 {
Expand All @@ -157,22 +172,34 @@ func CaseSnakeFirstUpper(word string, underscore ...string) string {
return TrimLeft(word, replace)
}

// CaseKebab converts a string to kebab-case
// CaseKebab converts a string to kebab-case.
//
// Example:
// CaseKebab("AnyKindOfString") -> any-kind-of-string
func CaseKebab(s string) string {
return CaseDelimited(s, '-')
}

// CaseKebabScreaming converts a string to KEBAB-CASE-SCREAMING.
//
// Example:
// CaseKebab("AnyKindOfString") -> ANY-KIND-OF-STRING
func CaseKebabScreaming(s string) string {
return CaseDelimitedScreaming(s, '-', true)
}

// CaseDelimited converts a string to snake.case.delimited.
//
// Example:
// CaseDelimited("AnyKindOfString", '.') -> any.kind.of.string
func CaseDelimited(s string, del byte) string {
return CaseDelimitedScreaming(s, del, false)
}

// CaseDelimitedScreaming converts a string to DELIMITED.SCREAMING.CASE or delimited.screaming.case.
//
// Example:
// CaseDelimitedScreaming("AnyKindOfString", '.') -> ANY.KIND.OF.STRING
func CaseDelimitedScreaming(s string, del uint8, screaming bool) string {
s = addWordBoundariesToNumbers(s)
s = strings.Trim(s, " ")
Expand Down
28 changes: 24 additions & 4 deletions text/gstr/gstr_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,26 @@ var (
)

// Chr return the ascii string of a number(0-255).
//
// Example:
// Chr(65) -> "A"
func Chr(ascii int) string {
return string([]byte{byte(ascii % 256)})
}

// Ord converts the first byte of a string to a value between 0 and 255.
//
// Example:
// Chr("A") -> 65
func Ord(char string) int {
return int(char[0])
}

// OctStr converts string container octal string to its original string,
// for example, to Chinese string.
// Eg: `\346\200\241` -> 怡
//
// Example:
// OctStr("\346\200\241") -> 怡
func OctStr(str string) string {
return octReg.ReplaceAllStringFunc(
str,
Expand All @@ -47,6 +55,9 @@ func OctStr(str string) string {
}

// Reverse returns a string which is the reverse of `str`.
//
// Example:
// Reverse("123456") -> "654321"
func Reverse(str string) string {
runes := []rune(str)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
Expand All @@ -56,10 +67,14 @@ func Reverse(str string) string {
}

// NumberFormat formats a number with grouped thousands.
// `decimals`: Sets the number of decimal points.
// `decPoint`: Sets the separator for the decimal point.
// `thousandsSep`: Sets the thousands' separator.
// Parameter `decimals`: Sets the number of decimal points.
// Parameter `decPoint`: Sets the separator for the decimal point.
// Parameter `thousandsSep`: Sets the thousands' separator.
// See http://php.net/manual/en/function.number-format.php.
//
// Example:
// NumberFormat(1234.56, 2, ".", "") -> 1234,56
// NumberFormat(1234.56, 2, ",", " ") -> 1 234,56
func NumberFormat(number float64, decimals int, decPoint, thousandsSep string) string {
neg := false
if number < 0 {
Expand Down Expand Up @@ -103,6 +118,11 @@ func NumberFormat(number float64, decimals int, decPoint, thousandsSep string) s

// Shuffle randomly shuffles a string.
// It considers parameter `str` as unicode string.
//
// Example:
// Shuffle("123456") -> "325164"
// Shuffle("123456") -> "231546"
// ...
func Shuffle(str string) string {
runes := []rune(str)
s := make([]rune, len(runes))
Expand Down
3 changes: 3 additions & 0 deletions text/gstr/gstr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ package gstr
import "strings"

// Repeat returns a new string consisting of multiplier copies of the string input.
//
// Example:
// Repeat("a", 3) -> "aaa"
func Repeat(input string, multiplier int) string {
return strings.Repeat(input, multiplier)
}
Loading
Loading