diff --git a/.example/os/console.go b/.example/os/console.go deleted file mode 100644 index c3d7f80a7..000000000 --- a/.example/os/console.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/gogf/gf/os/gcmd" -) - -func doEcho() { - fmt.Println("do echo") -} - -func main() { - fmt.Println(gcmd.Value.GetAll()) - - fmt.Println(gcmd.Value.Get(1)) - - gcmd.BindHandle("echo", doEcho) - gcmd.RunHandle("echo") - - gcmd.AutoRun() -} diff --git a/.example/os/gcmd/gcmd1.go b/.example/os/gcmd/gcmd1.go deleted file mode 100644 index 562024415..000000000 --- a/.example/os/gcmd/gcmd1.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/gogf/gf/os/gcmd" -) - -func help() { - fmt.Println("This is help.") -} - -func test() { - fmt.Println("This is test.") -} - -func main() { - gcmd.BindHandle("help", help) - gcmd.BindHandle("test", test) - gcmd.AutoRun() -} diff --git a/.example/other/test2.go b/.example/other/test2.go index 3843cee04..8a7d1b5fa 100644 --- a/.example/other/test2.go +++ b/.example/other/test2.go @@ -1,11 +1,13 @@ package main import ( - "fmt" - - "github.com/gogf/gf/os/gfile" + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/text/gregex" ) func main() { - fmt.Println(gfile.TempDir()) + s := `-abc` + m, err := gregex.MatchString(`^\-{1,2}a={0,1}(.*)`, s) + g.Dump(err) + g.Dump(m) } diff --git a/container/garray/garray_normal_interface.go b/container/garray/garray_normal_any.go similarity index 99% rename from container/garray/garray_normal_interface.go rename to container/garray/garray_normal_any.go index b5131f62e..cd5d889b3 100644 --- a/container/garray/garray_normal_interface.go +++ b/container/garray/garray_normal_any.go @@ -453,13 +453,13 @@ func (a *Array) Merge(array interface{}) *Array { a.Append(gconv.Interfaces(v.Slice())...) case *IntArray: a.Append(gconv.Interfaces(v.Slice())...) - case *StringArray: + case *StrArray: a.Append(gconv.Interfaces(v.Slice())...) case *SortedArray: a.Append(gconv.Interfaces(v.Slice())...) case *SortedIntArray: a.Append(gconv.Interfaces(v.Slice())...) - case *SortedStringArray: + case *SortedStrArray: a.Append(gconv.Interfaces(v.Slice())...) default: a.Append(gconv.Interfaces(array)...) diff --git a/container/garray/garray_normal_int.go b/container/garray/garray_normal_int.go index 1b1056dde..872d1f55b 100644 --- a/container/garray/garray_normal_int.go +++ b/container/garray/garray_normal_int.go @@ -459,13 +459,13 @@ func (a *IntArray) Merge(array interface{}) *IntArray { a.Append(gconv.Ints(v.Slice())...) case *IntArray: a.Append(gconv.Ints(v.Slice())...) - case *StringArray: + case *StrArray: a.Append(gconv.Ints(v.Slice())...) case *SortedArray: a.Append(gconv.Ints(v.Slice())...) case *SortedIntArray: a.Append(gconv.Ints(v.Slice())...) - case *SortedStringArray: + case *SortedStrArray: a.Append(gconv.Ints(v.Slice())...) default: a.Append(gconv.Ints(array)...) diff --git a/container/garray/garray_normal_string.go b/container/garray/garray_normal_str.go similarity index 80% rename from container/garray/garray_normal_string.go rename to container/garray/garray_normal_str.go index 72dfcb3fd..8d8972601 100644 --- a/container/garray/garray_normal_string.go +++ b/container/garray/garray_normal_str.go @@ -18,45 +18,45 @@ import ( "github.com/gogf/gf/util/grand" ) -type StringArray struct { +type StrArray struct { mu *rwmutex.RWMutex array []string } -// NewStringArray creates and returns an empty array. +// NewStrArray creates and returns an empty array. // The parameter used to specify whether using array in concurrent-safety, // which is false in default. -func NewStringArray(safe ...bool) *StringArray { - return NewStringArraySize(0, 0, safe...) +func NewStrArray(safe ...bool) *StrArray { + return NewStrArraySize(0, 0, safe...) } -// NewStringArraySize create and returns an array with given size and cap. +// NewStrArraySize create and returns an array with given size and cap. // The parameter used to specify whether using array in concurrent-safety, // which is false in default. -func NewStringArraySize(size int, cap int, safe ...bool) *StringArray { - return &StringArray{ +func NewStrArraySize(size int, cap int, safe ...bool) *StrArray { + return &StrArray{ mu: rwmutex.New(safe...), array: make([]string, size, cap), } } -// NewStringArrayFrom creates and returns an array with given slice . +// NewStrArrayFrom creates and returns an array with given slice . // The parameter used to specify whether using array in concurrent-safety, // which is false in default. -func NewStringArrayFrom(array []string, safe ...bool) *StringArray { - return &StringArray{ +func NewStrArrayFrom(array []string, safe ...bool) *StrArray { + return &StrArray{ mu: rwmutex.New(safe...), array: array, } } -// NewStringArrayFromCopy creates and returns an array from a copy of given slice . +// NewStrArrayFromCopy creates and returns an array from a copy of given slice . // The parameter used to specify whether using array in concurrent-safety, // which is false in default. -func NewStringArrayFromCopy(array []string, safe ...bool) *StringArray { +func NewStrArrayFromCopy(array []string, safe ...bool) *StrArray { newArray := make([]string, len(array)) copy(newArray, array) - return &StringArray{ + return &StrArray{ mu: rwmutex.New(safe...), array: newArray, } @@ -64,7 +64,7 @@ func NewStringArrayFromCopy(array []string, safe ...bool) *StringArray { // Get returns the value of the specified index, // the caller should notice the boundary of the array. -func (a *StringArray) Get(index int) string { +func (a *StrArray) Get(index int) string { a.mu.RLock() defer a.mu.RUnlock() value := a.array[index] @@ -72,7 +72,7 @@ func (a *StringArray) Get(index int) string { } // Set sets value to specified index. -func (a *StringArray) Set(index int, value string) *StringArray { +func (a *StrArray) Set(index int, value string) *StrArray { a.mu.Lock() defer a.mu.Unlock() a.array[index] = value @@ -80,7 +80,7 @@ func (a *StringArray) Set(index int, value string) *StringArray { } // SetArray sets the underlying slice array with the given . -func (a *StringArray) SetArray(array []string) *StringArray { +func (a *StrArray) SetArray(array []string) *StrArray { a.mu.Lock() defer a.mu.Unlock() a.array = array @@ -88,7 +88,7 @@ func (a *StringArray) SetArray(array []string) *StringArray { } // Replace replaces the array items by given from the beginning of array. -func (a *StringArray) Replace(array []string) *StringArray { +func (a *StrArray) Replace(array []string) *StrArray { a.mu.Lock() defer a.mu.Unlock() max := len(array) @@ -102,7 +102,7 @@ func (a *StringArray) Replace(array []string) *StringArray { } // Sum returns the sum of values in an array. -func (a *StringArray) Sum() (sum int) { +func (a *StrArray) Sum() (sum int) { a.mu.RLock() defer a.mu.RUnlock() for _, v := range a.array { @@ -114,7 +114,7 @@ func (a *StringArray) Sum() (sum int) { // Sort sorts the array in increasing order. // The parameter controls whether sort // in increasing order(default) or decreasing order -func (a *StringArray) Sort(reverse ...bool) *StringArray { +func (a *StrArray) Sort(reverse ...bool) *StrArray { a.mu.Lock() defer a.mu.Unlock() if len(reverse) > 0 && reverse[0] { @@ -131,7 +131,7 @@ func (a *StringArray) Sort(reverse ...bool) *StringArray { } // SortFunc sorts the array by custom function . -func (a *StringArray) SortFunc(less func(v1, v2 string) bool) *StringArray { +func (a *StrArray) SortFunc(less func(v1, v2 string) bool) *StrArray { a.mu.Lock() defer a.mu.Unlock() sort.Slice(a.array, func(i, j int) bool { @@ -141,7 +141,7 @@ func (a *StringArray) SortFunc(less func(v1, v2 string) bool) *StringArray { } // InsertBefore inserts the to the front of . -func (a *StringArray) InsertBefore(index int, value string) *StringArray { +func (a *StrArray) InsertBefore(index int, value string) *StrArray { a.mu.Lock() defer a.mu.Unlock() rear := append([]string{}, a.array[index:]...) @@ -151,7 +151,7 @@ func (a *StringArray) InsertBefore(index int, value string) *StringArray { } // InsertAfter inserts the to the back of . -func (a *StringArray) InsertAfter(index int, value string) *StringArray { +func (a *StrArray) InsertAfter(index int, value string) *StrArray { a.mu.Lock() defer a.mu.Unlock() rear := append([]string{}, a.array[index+1:]...) @@ -161,7 +161,7 @@ func (a *StringArray) InsertAfter(index int, value string) *StringArray { } // Remove removes an item by index. -func (a *StringArray) Remove(index int) string { +func (a *StrArray) Remove(index int) string { a.mu.Lock() defer a.mu.Unlock() // Determine array boundaries when deleting to improve deletion efficiency。 @@ -183,7 +183,7 @@ func (a *StringArray) Remove(index int) string { } // PushLeft pushes one or multiple items to the beginning of array. -func (a *StringArray) PushLeft(value ...string) *StringArray { +func (a *StrArray) PushLeft(value ...string) *StrArray { a.mu.Lock() a.array = append(value, a.array...) a.mu.Unlock() @@ -192,7 +192,7 @@ func (a *StringArray) PushLeft(value ...string) *StringArray { // PushRight pushes one or multiple items to the end of array. // It equals to Append. -func (a *StringArray) PushRight(value ...string) *StringArray { +func (a *StrArray) PushRight(value ...string) *StrArray { a.mu.Lock() a.array = append(a.array, value...) a.mu.Unlock() @@ -200,7 +200,7 @@ func (a *StringArray) PushRight(value ...string) *StringArray { } // PopLeft pops and returns an item from the beginning of array. -func (a *StringArray) PopLeft() string { +func (a *StrArray) PopLeft() string { a.mu.Lock() defer a.mu.Unlock() value := a.array[0] @@ -209,7 +209,7 @@ func (a *StringArray) PopLeft() string { } // PopRight pops and returns an item from the end of array. -func (a *StringArray) PopRight() string { +func (a *StrArray) PopRight() string { a.mu.Lock() defer a.mu.Unlock() index := len(a.array) - 1 @@ -219,12 +219,12 @@ func (a *StringArray) PopRight() string { } // PopRand randomly pops and return an item out of array. -func (a *StringArray) PopRand() string { +func (a *StrArray) PopRand() string { return a.Remove(grand.Intn(len(a.array))) } // PopRands randomly pops and returns items out of array. -func (a *StringArray) PopRands(size int) []string { +func (a *StrArray) PopRands(size int) []string { a.mu.Lock() defer a.mu.Unlock() if size > len(a.array) { @@ -240,7 +240,7 @@ func (a *StringArray) PopRands(size int) []string { } // PopLefts pops and returns items from the beginning of array. -func (a *StringArray) PopLefts(size int) []string { +func (a *StrArray) PopLefts(size int) []string { a.mu.Lock() defer a.mu.Unlock() length := len(a.array) @@ -253,7 +253,7 @@ func (a *StringArray) PopLefts(size int) []string { } // PopRights pops and returns items from the end of array. -func (a *StringArray) PopRights(size int) []string { +func (a *StrArray) PopRights(size int) []string { a.mu.Lock() defer a.mu.Unlock() index := len(a.array) - size @@ -272,7 +272,7 @@ func (a *StringArray) PopRights(size int) []string { // If is negative, then the offset will start from the end of array. // If is omitted, then the sequence will have everything from start up // until the end of the array. -func (a *StringArray) Range(start int, end ...int) []string { +func (a *StrArray) Range(start int, end ...int) []string { a.mu.RLock() defer a.mu.RUnlock() offsetEnd := len(a.array) @@ -308,7 +308,7 @@ func (a *StringArray) Range(start int, end ...int) []string { // If it is omitted, then the sequence will have everything from offset up until the end of the array. // // Any possibility crossing the left border of array, it will fail. -func (a *StringArray) SubSlice(offset int, length ...int) []string { +func (a *StrArray) SubSlice(offset int, length ...int) []string { a.mu.RLock() defer a.mu.RUnlock() size := len(a.array) @@ -346,7 +346,7 @@ func (a *StringArray) SubSlice(offset int, length ...int) []string { } // See PushRight. -func (a *StringArray) Append(value ...string) *StringArray { +func (a *StrArray) Append(value ...string) *StrArray { a.mu.Lock() a.array = append(a.array, value...) a.mu.Unlock() @@ -354,7 +354,7 @@ func (a *StringArray) Append(value ...string) *StringArray { } // Len returns the length of array. -func (a *StringArray) Len() int { +func (a *StrArray) Len() int { a.mu.RLock() length := len(a.array) a.mu.RUnlock() @@ -364,7 +364,7 @@ func (a *StringArray) Len() int { // Slice returns the underlying data of array. // Notice, if in concurrent-safe usage, it returns a copy of slice; // else a pointer to the underlying data. -func (a *StringArray) Slice() []string { +func (a *StrArray) Slice() []string { array := ([]string)(nil) if a.mu.IsSafe() { a.mu.RLock() @@ -378,16 +378,16 @@ func (a *StringArray) Slice() []string { } // Clone returns a new array, which is a copy of current array. -func (a *StringArray) Clone() (newArray *StringArray) { +func (a *StrArray) Clone() (newArray *StrArray) { a.mu.RLock() array := make([]string, len(a.array)) copy(array, a.array) a.mu.RUnlock() - return NewStringArrayFrom(array, !a.mu.IsSafe()) + return NewStrArrayFrom(array, !a.mu.IsSafe()) } // Clear deletes all items of current array. -func (a *StringArray) Clear() *StringArray { +func (a *StrArray) Clear() *StrArray { a.mu.Lock() if len(a.array) > 0 { a.array = make([]string, 0) @@ -397,13 +397,13 @@ func (a *StringArray) Clear() *StringArray { } // Contains checks whether a value exists in the array. -func (a *StringArray) Contains(value string) bool { +func (a *StrArray) Contains(value string) bool { return a.Search(value) != -1 } // Search searches array by , returns the index of , // or returns -1 if not exists. -func (a *StringArray) Search(value string) int { +func (a *StrArray) Search(value string) int { if len(a.array) == 0 { return -1 } @@ -420,7 +420,7 @@ func (a *StringArray) Search(value string) int { } // Unique uniques the array, clear repeated items. -func (a *StringArray) Unique() *StringArray { +func (a *StrArray) Unique() *StrArray { a.mu.Lock() for i := 0; i < len(a.array)-1; i++ { for j := i + 1; j < len(a.array); j++ { @@ -434,7 +434,7 @@ func (a *StringArray) Unique() *StringArray { } // LockFunc locks writing by callback function . -func (a *StringArray) LockFunc(f func(array []string)) *StringArray { +func (a *StrArray) LockFunc(f func(array []string)) *StrArray { a.mu.Lock() defer a.mu.Unlock() f(a.array) @@ -442,7 +442,7 @@ func (a *StringArray) LockFunc(f func(array []string)) *StringArray { } // RLockFunc locks reading by callback function . -func (a *StringArray) RLockFunc(f func(array []string)) *StringArray { +func (a *StrArray) RLockFunc(f func(array []string)) *StrArray { a.mu.RLock() defer a.mu.RUnlock() f(a.array) @@ -453,19 +453,19 @@ func (a *StringArray) RLockFunc(f func(array []string)) *StringArray { // The parameter can be any garray or slice type. // The difference between Merge and Append is Append supports only specified slice type, // but Merge supports more parameter types. -func (a *StringArray) Merge(array interface{}) *StringArray { +func (a *StrArray) Merge(array interface{}) *StrArray { switch v := array.(type) { case *Array: a.Append(gconv.Strings(v.Slice())...) case *IntArray: a.Append(gconv.Strings(v.Slice())...) - case *StringArray: + case *StrArray: a.Append(gconv.Strings(v.Slice())...) case *SortedArray: a.Append(gconv.Strings(v.Slice())...) case *SortedIntArray: a.Append(gconv.Strings(v.Slice())...) - case *SortedStringArray: + case *SortedStrArray: a.Append(gconv.Strings(v.Slice())...) default: a.Append(gconv.Strings(array)...) @@ -475,7 +475,7 @@ func (a *StringArray) Merge(array interface{}) *StringArray { // Fill fills an array with num entries of the value , // keys starting at the parameter. -func (a *StringArray) Fill(startIndex int, num int, value string) *StringArray { +func (a *StrArray) Fill(startIndex int, num int, value string) *StrArray { a.mu.Lock() defer a.mu.Unlock() if startIndex < 0 { @@ -494,7 +494,7 @@ func (a *StringArray) Fill(startIndex int, num int, value string) *StringArray { // Chunk splits an array into multiple arrays, // the size of each array is determined by . // The last chunk may contain less than size elements. -func (a *StringArray) Chunk(size int) [][]string { +func (a *StrArray) Chunk(size int) [][]string { if size < 1 { return nil } @@ -518,7 +518,7 @@ func (a *StringArray) Chunk(size int) [][]string { // If size is positive then the array is padded on the right, or negative on the left. // If the absolute value of is less than or equal to the length of the array // then no padding takes place. -func (a *StringArray) Pad(size int, value string) *StringArray { +func (a *StrArray) Pad(size int, value string) *StrArray { a.mu.Lock() defer a.mu.Unlock() if size == 0 || (size > 0 && size < len(a.array)) || (size < 0 && size > -len(a.array)) { @@ -542,14 +542,14 @@ func (a *StringArray) Pad(size int, value string) *StringArray { } // Rand randomly returns one item from array(no deleting). -func (a *StringArray) Rand() string { +func (a *StrArray) Rand() string { a.mu.RLock() defer a.mu.RUnlock() return a.array[grand.Intn(len(a.array))] } // Rands randomly returns items from array(no deleting). -func (a *StringArray) Rands(size int) []string { +func (a *StrArray) Rands(size int) []string { a.mu.RLock() defer a.mu.RUnlock() if size > len(a.array) { @@ -566,7 +566,7 @@ func (a *StringArray) Rands(size int) []string { } // Shuffle randomly shuffles the array. -func (a *StringArray) Shuffle() *StringArray { +func (a *StrArray) Shuffle() *StrArray { a.mu.Lock() defer a.mu.Unlock() for i, v := range grand.Perm(len(a.array)) { @@ -576,7 +576,7 @@ func (a *StringArray) Shuffle() *StringArray { } // Reverse makes array with elements in reverse order. -func (a *StringArray) Reverse() *StringArray { +func (a *StrArray) Reverse() *StrArray { a.mu.Lock() defer a.mu.Unlock() for i, j := 0, len(a.array)-1; i < j; i, j = i+1, j-1 { @@ -586,7 +586,7 @@ func (a *StringArray) Reverse() *StringArray { } // Join joins array elements with a string . -func (a *StringArray) Join(glue string) string { +func (a *StrArray) Join(glue string) string { a.mu.RLock() defer a.mu.RUnlock() buffer := bytes.NewBuffer(nil) @@ -600,7 +600,7 @@ func (a *StringArray) Join(glue string) string { } // CountValues counts the number of occurrences of all values in the array. -func (a *StringArray) CountValues() map[string]int { +func (a *StrArray) CountValues() map[string]int { m := make(map[string]int) a.mu.RLock() defer a.mu.RUnlock() @@ -611,7 +611,7 @@ func (a *StringArray) CountValues() map[string]int { } // String returns current array as a string. -func (a *StringArray) String() string { +func (a *StrArray) String() string { a.mu.RLock() defer a.mu.RUnlock() jsonContent, _ := json.Marshal(a.array) @@ -619,7 +619,7 @@ func (a *StringArray) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (a *StringArray) MarshalJSON() ([]byte, error) { +func (a *StrArray) MarshalJSON() ([]byte, error) { a.mu.RLock() defer a.mu.RUnlock() return json.Marshal(a.array) diff --git a/container/garray/garray_sorted_interface.go b/container/garray/garray_sorted_any.go similarity index 99% rename from container/garray/garray_sorted_interface.go rename to container/garray/garray_sorted_any.go index 14cbeabdc..a482fd93d 100644 --- a/container/garray/garray_sorted_interface.go +++ b/container/garray/garray_sorted_any.go @@ -450,13 +450,13 @@ func (a *SortedArray) Merge(array interface{}) *SortedArray { a.Add(gconv.Interfaces(v.Slice())...) case *IntArray: a.Add(gconv.Interfaces(v.Slice())...) - case *StringArray: + case *StrArray: a.Add(gconv.Interfaces(v.Slice())...) case *SortedArray: a.Add(gconv.Interfaces(v.Slice())...) case *SortedIntArray: a.Add(gconv.Interfaces(v.Slice())...) - case *SortedStringArray: + case *SortedStrArray: a.Add(gconv.Interfaces(v.Slice())...) default: a.Add(gconv.Interfaces(array)...) diff --git a/container/garray/garray_sorted_int.go b/container/garray/garray_sorted_int.go index ce8fb5917..b1a3231e2 100644 --- a/container/garray/garray_sorted_int.go +++ b/container/garray/garray_sorted_int.go @@ -449,13 +449,13 @@ func (a *SortedIntArray) Merge(array interface{}) *SortedIntArray { a.Add(gconv.Ints(v.Slice())...) case *IntArray: a.Add(gconv.Ints(v.Slice())...) - case *StringArray: + case *StrArray: a.Add(gconv.Ints(v.Slice())...) case *SortedArray: a.Add(gconv.Ints(v.Slice())...) case *SortedIntArray: a.Add(gconv.Ints(v.Slice())...) - case *SortedStringArray: + case *SortedStrArray: a.Add(gconv.Ints(v.Slice())...) default: a.Add(gconv.Ints(array)...) diff --git a/container/garray/garray_sorted_string.go b/container/garray/garray_sorted_str.go similarity index 78% rename from container/garray/garray_sorted_string.go rename to container/garray/garray_sorted_str.go index 3f7e24ef4..8670d5d5d 100644 --- a/container/garray/garray_sorted_string.go +++ b/container/garray/garray_sorted_str.go @@ -18,33 +18,33 @@ import ( ) // It's using increasing order in default. -type SortedStringArray struct { +type SortedStrArray struct { mu *rwmutex.RWMutex array []string unique *gtype.Bool // Whether enable unique feature(false) comparator func(a, b string) int // Comparison function(it returns -1: a < b; 0: a == b; 1: a > b) } -// NewSortedStringArray creates and returns an empty sorted array. +// NewSortedStrArray creates and returns an empty sorted array. // The parameter used to specify whether using array in concurrent-safety, // which is false in default. -func NewSortedStringArray(safe ...bool) *SortedStringArray { - return NewSortedStringArraySize(0, safe...) +func NewSortedStrArray(safe ...bool) *SortedStrArray { + return NewSortedStrArraySize(0, safe...) } -// NewSortedStringArrayComparator creates and returns an empty sorted array with specified comparator. +// NewSortedStrArrayComparator creates and returns an empty sorted array with specified comparator. // The parameter used to specify whether using array in concurrent-safety which is false in default. -func NewSortedStringArrayComparator(comparator func(a, b string) int, safe ...bool) *SortedStringArray { - array := NewSortedStringArray(safe...) +func NewSortedStrArrayComparator(comparator func(a, b string) int, safe ...bool) *SortedStrArray { + array := NewSortedStrArray(safe...) array.comparator = comparator return array } -// NewSortedStringArraySize create and returns an sorted array with given size and cap. +// NewSortedStrArraySize create and returns an sorted array with given size and cap. // The parameter used to specify whether using array in concurrent-safety, // which is false in default. -func NewSortedStringArraySize(cap int, safe ...bool) *SortedStringArray { - return &SortedStringArray{ +func NewSortedStrArraySize(cap int, safe ...bool) *SortedStrArray { + return &SortedStrArray{ mu: rwmutex.New(safe...), array: make([]string, 0, cap), unique: gtype.NewBool(), @@ -52,27 +52,27 @@ func NewSortedStringArraySize(cap int, safe ...bool) *SortedStringArray { } } -// NewSortedStringArrayFrom creates and returns an sorted array with given slice . +// NewSortedStrArrayFrom creates and returns an sorted array with given slice . // The parameter used to specify whether using array in concurrent-safety, // which is false in default. -func NewSortedStringArrayFrom(array []string, safe ...bool) *SortedStringArray { - a := NewSortedStringArraySize(0, safe...) +func NewSortedStrArrayFrom(array []string, safe ...bool) *SortedStrArray { + a := NewSortedStrArraySize(0, safe...) a.array = array quickSortStr(a.array, a.comparator) return a } -// NewSortedStringArrayFromCopy creates and returns an sorted array from a copy of given slice . +// NewSortedStrArrayFromCopy creates and returns an sorted array from a copy of given slice . // The parameter used to specify whether using array in concurrent-safety, // which is false in default. -func NewSortedStringArrayFromCopy(array []string, safe ...bool) *SortedStringArray { +func NewSortedStrArrayFromCopy(array []string, safe ...bool) *SortedStrArray { newArray := make([]string, len(array)) copy(newArray, array) - return NewSortedStringArrayFrom(newArray, safe...) + return NewSortedStrArrayFrom(newArray, safe...) } // SetArray sets the underlying slice array with the given . -func (a *SortedStringArray) SetArray(array []string) *SortedStringArray { +func (a *SortedStrArray) SetArray(array []string) *SortedStrArray { a.mu.Lock() defer a.mu.Unlock() a.array = array @@ -83,7 +83,7 @@ func (a *SortedStringArray) SetArray(array []string) *SortedStringArray { // Sort sorts the array in increasing order. // The parameter controls whether sort // in increasing order(default) or decreasing order. -func (a *SortedStringArray) Sort() *SortedStringArray { +func (a *SortedStrArray) Sort() *SortedStrArray { a.mu.Lock() defer a.mu.Unlock() quickSortStr(a.array, a.comparator) @@ -91,7 +91,7 @@ func (a *SortedStringArray) Sort() *SortedStringArray { } // Add adds one or multiple values to sorted array, the array always keeps sorted. -func (a *SortedStringArray) Add(values ...string) *SortedStringArray { +func (a *SortedStrArray) Add(values ...string) *SortedStrArray { if len(values) == 0 { return a } @@ -118,7 +118,7 @@ func (a *SortedStringArray) Add(values ...string) *SortedStringArray { // Get returns the value of the specified index, // the caller should notice the boundary of the array. -func (a *SortedStringArray) Get(index int) string { +func (a *SortedStrArray) Get(index int) string { a.mu.RLock() defer a.mu.RUnlock() value := a.array[index] @@ -126,7 +126,7 @@ func (a *SortedStringArray) Get(index int) string { } // Remove removes an item by index. -func (a *SortedStringArray) Remove(index int) string { +func (a *SortedStrArray) Remove(index int) string { a.mu.Lock() defer a.mu.Unlock() // Determine array boundaries when deleting to improve deletion efficiency. @@ -148,7 +148,7 @@ func (a *SortedStringArray) Remove(index int) string { } // PopLeft pops and returns an item from the beginning of array. -func (a *SortedStringArray) PopLeft() string { +func (a *SortedStrArray) PopLeft() string { a.mu.Lock() defer a.mu.Unlock() value := a.array[0] @@ -157,7 +157,7 @@ func (a *SortedStringArray) PopLeft() string { } // PopRight pops and returns an item from the end of array. -func (a *SortedStringArray) PopRight() string { +func (a *SortedStrArray) PopRight() string { a.mu.Lock() defer a.mu.Unlock() index := len(a.array) - 1 @@ -167,12 +167,12 @@ func (a *SortedStringArray) PopRight() string { } // PopRand randomly pops and return an item out of array. -func (a *SortedStringArray) PopRand() string { +func (a *SortedStrArray) PopRand() string { return a.Remove(grand.Intn(len(a.array))) } // PopRands randomly pops and returns items out of array. -func (a *SortedStringArray) PopRands(size int) []string { +func (a *SortedStrArray) PopRands(size int) []string { a.mu.Lock() defer a.mu.Unlock() if size > len(a.array) { @@ -188,7 +188,7 @@ func (a *SortedStringArray) PopRands(size int) []string { } // PopLefts pops and returns items from the beginning of array. -func (a *SortedStringArray) PopLefts(size int) []string { +func (a *SortedStrArray) PopLefts(size int) []string { a.mu.Lock() defer a.mu.Unlock() length := len(a.array) @@ -201,7 +201,7 @@ func (a *SortedStringArray) PopLefts(size int) []string { } // PopRights pops and returns items from the end of array. -func (a *SortedStringArray) PopRights(size int) []string { +func (a *SortedStrArray) PopRights(size int) []string { a.mu.Lock() defer a.mu.Unlock() index := len(a.array) - size @@ -220,7 +220,7 @@ func (a *SortedStringArray) PopRights(size int) []string { // If is negative, then the offset will start from the end of array. // If is omitted, then the sequence will have everything from start up // until the end of the array. -func (a *SortedStringArray) Range(start int, end ...int) []string { +func (a *SortedStrArray) Range(start int, end ...int) []string { a.mu.RLock() defer a.mu.RUnlock() offsetEnd := len(a.array) @@ -256,7 +256,7 @@ func (a *SortedStringArray) Range(start int, end ...int) []string { // If it is omitted, then the sequence will have everything from offset up until the end of the array. // // Any possibility crossing the left border of array, it will fail. -func (a *SortedStringArray) SubSlice(offset int, length ...int) []string { +func (a *SortedStrArray) SubSlice(offset int, length ...int) []string { a.mu.RLock() defer a.mu.RUnlock() size := len(a.array) @@ -294,7 +294,7 @@ func (a *SortedStringArray) SubSlice(offset int, length ...int) []string { } // Sum returns the sum of values in an array. -func (a *SortedStringArray) Sum() (sum int) { +func (a *SortedStrArray) Sum() (sum int) { a.mu.RLock() defer a.mu.RUnlock() for _, v := range a.array { @@ -304,7 +304,7 @@ func (a *SortedStringArray) Sum() (sum int) { } // Len returns the length of array. -func (a *SortedStringArray) Len() int { +func (a *SortedStrArray) Len() int { a.mu.RLock() length := len(a.array) a.mu.RUnlock() @@ -314,7 +314,7 @@ func (a *SortedStringArray) Len() int { // Slice returns the underlying data of array. // Notice, if in concurrent-safe usage, it returns a copy of slice; // else a pointer to the underlying data. -func (a *SortedStringArray) Slice() []string { +func (a *SortedStrArray) Slice() []string { array := ([]string)(nil) if a.mu.IsSafe() { a.mu.RLock() @@ -328,13 +328,13 @@ func (a *SortedStringArray) Slice() []string { } // Contains checks whether a value exists in the array. -func (a *SortedStringArray) Contains(value string) bool { +func (a *SortedStrArray) Contains(value string) bool { return a.Search(value) != -1 } // Search searches array by , returns the index of , // or returns -1 if not exists. -func (a *SortedStringArray) Search(value string) (index int) { +func (a *SortedStrArray) Search(value string) (index int) { if i, r := a.binSearch(value, true); r == 0 { return i } @@ -346,7 +346,7 @@ func (a *SortedStringArray) Search(value string) (index int) { // If equals to 0, it means the value at is equals to . // If lesser than 0, it means the value at is lesser than . // If greater than 0, it means the value at is greater than . -func (a *SortedStringArray) binSearch(value string, lock bool) (index int, result int) { +func (a *SortedStrArray) binSearch(value string, lock bool) (index int, result int) { if len(a.array) == 0 { return -1, -2 } @@ -376,7 +376,7 @@ func (a *SortedStringArray) binSearch(value string, lock bool) (index int, resul // SetUnique sets unique mark to the array, // which means it does not contain any repeated items. // It also do unique check, remove all repeated items. -func (a *SortedStringArray) SetUnique(unique bool) *SortedStringArray { +func (a *SortedStrArray) SetUnique(unique bool) *SortedStrArray { oldUnique := a.unique.Val() a.unique.Set(unique) if unique && oldUnique != unique { @@ -386,7 +386,7 @@ func (a *SortedStringArray) SetUnique(unique bool) *SortedStringArray { } // Unique uniques the array, clear repeated items. -func (a *SortedStringArray) Unique() *SortedStringArray { +func (a *SortedStrArray) Unique() *SortedStrArray { a.mu.Lock() i := 0 for { @@ -404,16 +404,16 @@ func (a *SortedStringArray) Unique() *SortedStringArray { } // Clone returns a new array, which is a copy of current array. -func (a *SortedStringArray) Clone() (newArray *SortedStringArray) { +func (a *SortedStrArray) Clone() (newArray *SortedStrArray) { a.mu.RLock() array := make([]string, len(a.array)) copy(array, a.array) a.mu.RUnlock() - return NewSortedStringArrayFrom(array, !a.mu.IsSafe()) + return NewSortedStrArrayFrom(array, !a.mu.IsSafe()) } // Clear deletes all items of current array. -func (a *SortedStringArray) Clear() *SortedStringArray { +func (a *SortedStrArray) Clear() *SortedStrArray { a.mu.Lock() if len(a.array) > 0 { a.array = make([]string, 0) @@ -423,7 +423,7 @@ func (a *SortedStringArray) Clear() *SortedStringArray { } // LockFunc locks writing by callback function . -func (a *SortedStringArray) LockFunc(f func(array []string)) *SortedStringArray { +func (a *SortedStrArray) LockFunc(f func(array []string)) *SortedStrArray { a.mu.Lock() defer a.mu.Unlock() f(a.array) @@ -431,7 +431,7 @@ func (a *SortedStringArray) LockFunc(f func(array []string)) *SortedStringArray } // RLockFunc locks reading by callback function . -func (a *SortedStringArray) RLockFunc(f func(array []string)) *SortedStringArray { +func (a *SortedStrArray) RLockFunc(f func(array []string)) *SortedStrArray { a.mu.RLock() defer a.mu.RUnlock() f(a.array) @@ -442,19 +442,19 @@ func (a *SortedStringArray) RLockFunc(f func(array []string)) *SortedStringArray // The parameter can be any garray or slice type. // The difference between Merge and Append is Append supports only specified slice type, // but Merge supports more parameter types. -func (a *SortedStringArray) Merge(array interface{}) *SortedStringArray { +func (a *SortedStrArray) Merge(array interface{}) *SortedStrArray { switch v := array.(type) { case *Array: a.Add(gconv.Strings(v.Slice())...) case *IntArray: a.Add(gconv.Strings(v.Slice())...) - case *StringArray: + case *StrArray: a.Add(gconv.Strings(v.Slice())...) case *SortedArray: a.Add(gconv.Strings(v.Slice())...) case *SortedIntArray: a.Add(gconv.Strings(v.Slice())...) - case *SortedStringArray: + case *SortedStrArray: a.Add(gconv.Strings(v.Slice())...) default: a.Add(gconv.Strings(array)...) @@ -465,7 +465,7 @@ func (a *SortedStringArray) Merge(array interface{}) *SortedStringArray { // Chunk splits an array into multiple arrays, // the size of each array is determined by . // The last chunk may contain less than size elements. -func (a *SortedStringArray) Chunk(size int) [][]string { +func (a *SortedStrArray) Chunk(size int) [][]string { if size < 1 { return nil } @@ -486,14 +486,14 @@ func (a *SortedStringArray) Chunk(size int) [][]string { } // Rand randomly returns one item from array(no deleting). -func (a *SortedStringArray) Rand() string { +func (a *SortedStrArray) Rand() string { a.mu.RLock() defer a.mu.RUnlock() return a.array[grand.Intn(len(a.array))] } // Rands randomly returns items from array(no deleting). -func (a *SortedStringArray) Rands(size int) []string { +func (a *SortedStrArray) Rands(size int) []string { a.mu.RLock() defer a.mu.RUnlock() if size > len(a.array) { @@ -510,7 +510,7 @@ func (a *SortedStringArray) Rands(size int) []string { } // Join joins array elements with a string . -func (a *SortedStringArray) Join(glue string) string { +func (a *SortedStrArray) Join(glue string) string { a.mu.RLock() defer a.mu.RUnlock() buffer := bytes.NewBuffer(nil) @@ -524,7 +524,7 @@ func (a *SortedStringArray) Join(glue string) string { } // CountValues counts the number of occurrences of all values in the array. -func (a *SortedStringArray) CountValues() map[string]int { +func (a *SortedStrArray) CountValues() map[string]int { m := make(map[string]int) a.mu.RLock() defer a.mu.RUnlock() @@ -535,7 +535,7 @@ func (a *SortedStringArray) CountValues() map[string]int { } // String returns current array as a string. -func (a *SortedStringArray) String() string { +func (a *SortedStrArray) String() string { a.mu.RLock() defer a.mu.RUnlock() jsonContent, _ := json.Marshal(a.array) @@ -543,7 +543,7 @@ func (a *SortedStringArray) String() string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (a *SortedStringArray) MarshalJSON() ([]byte, error) { +func (a *SortedStrArray) MarshalJSON() ([]byte, error) { a.mu.RLock() defer a.mu.RUnlock() return json.Marshal(a.array) diff --git a/container/garray/garray_z_unit_interface_test.go b/container/garray/garray_z_unit_any_test.go similarity index 99% rename from container/garray/garray_z_unit_interface_test.go rename to container/garray/garray_z_unit_any_test.go index 1edc51235..d98c66337 100644 --- a/container/garray/garray_z_unit_interface_test.go +++ b/container/garray/garray_z_unit_any_test.go @@ -149,9 +149,9 @@ func TestArray_Merge(t *testing.T) { s2 := []string{"e", "f"} i3 := garray.NewIntArrayFrom([]int{1, 2, 3}) i4 := garray.NewArrayFrom([]interface{}{3}) - s3 := garray.NewStringArrayFrom([]string{"g", "h"}) + s3 := garray.NewStrArrayFrom([]string{"g", "h"}) s4 := garray.NewSortedArrayFrom([]interface{}{4, 5}, func1) - s5 := garray.NewSortedStringArrayFrom(s2) + s5 := garray.NewSortedStrArrayFrom(s2) s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3}) a1 := garray.NewArrayFrom(i1) @@ -824,9 +824,9 @@ func TestSortedArray_Merge(t *testing.T) { s2 := []string{"e", "f"} i1 := garray.NewIntArrayFrom([]int{1, 2, 3}) i2 := garray.NewArrayFrom([]interface{}{3}) - s3 := garray.NewStringArrayFrom([]string{"g", "h"}) + s3 := garray.NewStrArrayFrom([]string{"g", "h"}) s4 := garray.NewSortedArrayFrom([]interface{}{4, 5}, func1) - s5 := garray.NewSortedStringArrayFrom(s2) + s5 := garray.NewSortedStrArrayFrom(s2) s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3}) a1 := garray.NewSortedArrayFrom(s1, func1) diff --git a/container/garray/garray_z_unit_basic_test.go b/container/garray/garray_z_unit_basic_test.go index 07ed16855..2b6b26a9d 100644 --- a/container/garray/garray_z_unit_basic_test.go +++ b/container/garray/garray_z_unit_basic_test.go @@ -44,10 +44,10 @@ func Test_SortedIntArray2(t *testing.T) { gtest.Assert(array.Slice(), expect) } -func Test_SortedStringArray1(t *testing.T) { +func Test_SortedStrArray1(t *testing.T) { expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} - array1 := garray.NewSortedStringArray() - array2 := garray.NewSortedStringArray(true) + array1 := garray.NewSortedStrArray() + array2 := garray.NewSortedStrArray(true) for i := 10; i > -1; i-- { array1.Add(gconv.String(i)) array2.Add(gconv.String(i)) @@ -57,9 +57,9 @@ func Test_SortedStringArray1(t *testing.T) { } -func Test_SortedStringArray2(t *testing.T) { +func Test_SortedStrArray2(t *testing.T) { expect := []string{"0", "1", "10", "2", "3", "4", "5", "6", "7", "8", "9"} - array := garray.NewSortedStringArray() + array := garray.NewSortedStrArray() for i := 0; i <= 10; i++ { array.Add(gconv.String(i)) } diff --git a/container/garray/garray_z_unit_int_test.go b/container/garray/garray_z_unit_int_test.go index bbbc3dde5..bb2f0838c 100644 --- a/container/garray/garray_z_unit_int_test.go +++ b/container/garray/garray_z_unit_int_test.go @@ -134,12 +134,12 @@ func TestIntArray_Merge(t *testing.T) { a1 := garray.NewIntArrayFrom(n1) a2 := garray.NewIntArrayFrom(n2) a3 := garray.NewArrayFrom(i1) - a4 := garray.NewStringArrayFrom(s1) + a4 := garray.NewStrArrayFrom(s1) - a5 := garray.NewSortedStringArrayFrom(s2) + a5 := garray.NewSortedStrArrayFrom(s2) a6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3}) - a7 := garray.NewSortedStringArrayFrom(s1) + a7 := garray.NewSortedStrArrayFrom(s1) a8 := garray.NewSortedArrayFrom([]interface{}{4, 5}, func1) gtest.Assert(a1.Merge(a2).Slice(), []int{0, 1, 2, 3, 4, 5, 6, 7}) @@ -809,9 +809,9 @@ func TestSortedIntArray_Merge(t *testing.T) { s2 := []string{"e", "f"} i1 := garray.NewIntArrayFrom([]int{1, 2, 3}) i2 := garray.NewArrayFrom([]interface{}{3}) - s3 := garray.NewStringArrayFrom([]string{"g", "h"}) + s3 := garray.NewStrArrayFrom([]string{"g", "h"}) s4 := garray.NewSortedArrayFrom([]interface{}{4, 5}, func1) - s5 := garray.NewSortedStringArrayFrom(s2) + s5 := garray.NewSortedStrArrayFrom(s2) s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3}) a1 := garray.NewSortedIntArrayFrom(i0) diff --git a/container/garray/garray_z_unit_string_test.go b/container/garray/garray_z_unit_str_test.go similarity index 76% rename from container/garray/garray_z_unit_string_test.go rename to container/garray/garray_z_unit_str_test.go index 35462317f..fd3a612b5 100644 --- a/container/garray/garray_z_unit_string_test.go +++ b/container/garray/garray_z_unit_str_test.go @@ -18,12 +18,12 @@ import ( "github.com/gogf/gf/util/gconv" ) -func Test_StringArray_Basic(t *testing.T) { +func Test_StrArray_Basic(t *testing.T) { gtest.Case(t, func() { expect := []string{"0", "1", "2", "3"} - array := garray.NewStringArrayFrom(expect) - array2 := garray.NewStringArrayFrom(expect, true) - array3 := garray.NewStringArrayFrom([]string{}) + array := garray.NewStrArrayFrom(expect) + array2 := garray.NewStrArrayFrom(expect, true) + array3 := garray.NewStrArrayFrom([]string{}) gtest.Assert(array.Slice(), expect) array.Set(0, "100") gtest.Assert(array.Get(0), 100) @@ -46,11 +46,11 @@ func Test_StringArray_Basic(t *testing.T) { }) } -func TestStringArray_Sort(t *testing.T) { +func TestStrArray_Sort(t *testing.T) { gtest.Case(t, func() { expect1 := []string{"0", "1", "2", "3"} expect2 := []string{"3", "2", "1", "0"} - array := garray.NewStringArray() + array := garray.NewStrArray() for i := 3; i >= 0; i-- { array.Append(gconv.String(i)) } @@ -61,18 +61,18 @@ func TestStringArray_Sort(t *testing.T) { }) } -func TestStringArray_Unique(t *testing.T) { +func TestStrArray_Unique(t *testing.T) { gtest.Case(t, func() { expect := []string{"1", "1", "2", "3"} - array := garray.NewStringArrayFrom(expect) + array := garray.NewStrArrayFrom(expect) gtest.Assert(array.Unique().Slice(), []string{"1", "2", "3"}) }) } -func TestStringArray_PushAndPop(t *testing.T) { +func TestStrArray_PushAndPop(t *testing.T) { gtest.Case(t, func() { expect := []string{"0", "1", "2", "3"} - array := garray.NewStringArrayFrom(expect) + array := garray.NewStrArrayFrom(expect) gtest.Assert(array.Slice(), expect) gtest.Assert(array.PopLeft(), "0") gtest.Assert(array.PopRight(), "3") @@ -84,12 +84,12 @@ func TestStringArray_PushAndPop(t *testing.T) { }) } -func TestStringArray_PopLeftsAndPopRights(t *testing.T) { +func TestStrArray_PopLeftsAndPopRights(t *testing.T) { gtest.Case(t, func() { value1 := []string{"0", "1", "2", "3", "4", "5", "6"} value2 := []string{"0", "1", "2", "3", "4", "5", "6"} - array1 := garray.NewStringArrayFrom(value1) - array2 := garray.NewStringArrayFrom(value2) + array1 := garray.NewStrArrayFrom(value1) + array2 := garray.NewStrArrayFrom(value2) gtest.Assert(array1.PopLefts(2), []interface{}{"0", "1"}) gtest.Assert(array1.Slice(), []interface{}{"2", "3", "4", "5", "6"}) gtest.Assert(array1.PopRights(2), []interface{}{"5", "6"}) @@ -104,8 +104,8 @@ func TestStringArray_PopLeftsAndPopRights(t *testing.T) { func TestString_Range(t *testing.T) { gtest.Case(t, func() { value1 := []string{"0", "1", "2", "3", "4", "5", "6"} - array1 := garray.NewStringArrayFrom(value1) - array2 := garray.NewStringArrayFrom(value1, true) + array1 := garray.NewStrArrayFrom(value1) + array2 := garray.NewStrArrayFrom(value1, true) gtest.Assert(array1.Range(0, 1), []interface{}{"0"}) gtest.Assert(array1.Range(1, 2), []interface{}{"1"}) gtest.Assert(array1.Range(0, 2), []interface{}{"0", "1"}) @@ -115,12 +115,12 @@ func TestString_Range(t *testing.T) { }) } -func TestStringArray_Merge(t *testing.T) { +func TestStrArray_Merge(t *testing.T) { gtest.Case(t, func() { a11 := []string{"0", "1", "2", "3"} a21 := []string{"4", "5", "6", "7"} - array1 := garray.NewStringArrayFrom(a11) - array2 := garray.NewStringArrayFrom(a21) + array1 := garray.NewStrArrayFrom(a11) + array2 := garray.NewStrArrayFrom(a21) gtest.Assert(array1.Merge(array2).Slice(), []string{"0", "1", "2", "3", "4", "5", "6", "7"}) func1 := func(v1, v2 interface{}) int { @@ -134,11 +134,11 @@ func TestStringArray_Merge(t *testing.T) { s2 := []string{"e", "f"} i1 := garray.NewIntArrayFrom([]int{1, 2, 3}) i2 := garray.NewArrayFrom([]interface{}{3}) - s3 := garray.NewStringArrayFrom([]string{"g", "h"}) + s3 := garray.NewStrArrayFrom([]string{"g", "h"}) s4 := garray.NewSortedArrayFrom([]interface{}{4, 5}, func1) - s5 := garray.NewSortedStringArrayFrom(s2) + s5 := garray.NewSortedStrArrayFrom(s2) s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3}) - a1 := garray.NewStringArrayFrom(s1) + a1 := garray.NewStrArrayFrom(s1) gtest.Assert(a1.Merge(s2).Len(), 6) gtest.Assert(a1.Merge(i1).Len(), 9) @@ -150,12 +150,12 @@ func TestStringArray_Merge(t *testing.T) { }) } -func TestStringArray_Fill(t *testing.T) { +func TestStrArray_Fill(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0"} a2 := []string{"0"} - array1 := garray.NewStringArrayFrom(a1) - array2 := garray.NewStringArrayFrom(a2) + array1 := garray.NewStrArrayFrom(a1) + array2 := garray.NewStrArrayFrom(a2) gtest.Assert(array1.Fill(1, 2, "100").Slice(), []string{"0", "100", "100"}) gtest.Assert(array2.Fill(0, 2, "100").Slice(), []string{"100", "100"}) s1 := array2.Fill(-1, 2, "100") @@ -163,10 +163,10 @@ func TestStringArray_Fill(t *testing.T) { }) } -func TestStringArray_Chunk(t *testing.T) { +func TestStrArray_Chunk(t *testing.T) { gtest.Case(t, func() { a1 := []string{"1", "2", "3", "4", "5"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) chunks := array1.Chunk(2) gtest.Assert(len(chunks), 3) gtest.Assert(chunks[0], []string{"1", "2"}) @@ -176,21 +176,21 @@ func TestStringArray_Chunk(t *testing.T) { }) } -func TestStringArray_Pad(t *testing.T) { +func TestStrArray_Pad(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) gtest.Assert(array1.Pad(3, "1").Slice(), []string{"0", "1", "1"}) gtest.Assert(array1.Pad(-4, "1").Slice(), []string{"1", "0", "1", "1"}) gtest.Assert(array1.Pad(3, "1").Slice(), []string{"1", "0", "1", "1"}) }) } -func TestStringArray_SubSlice(t *testing.T) { +func TestStrArray_SubSlice(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3", "4", "5", "6"} - array1 := garray.NewStringArrayFrom(a1) - array2 := garray.NewStringArrayFrom(a1, true) + array1 := garray.NewStrArrayFrom(a1) + array2 := garray.NewStrArrayFrom(a1, true) gtest.Assert(array1.SubSlice(0, 2), []string{"0", "1"}) gtest.Assert(array1.SubSlice(2, 2), []string{"2", "3"}) gtest.Assert(array1.SubSlice(5, 8), []string{"5", "6"}) @@ -202,10 +202,10 @@ func TestStringArray_SubSlice(t *testing.T) { }) } -func TestStringArray_Rand(t *testing.T) { +func TestStrArray_Rand(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3", "4", "5", "6"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) gtest.Assert(len(array1.Rands(2)), "2") gtest.Assert(len(array1.Rands(10)), "7") gtest.AssertIN(array1.Rands(1)[0], a1) @@ -214,10 +214,10 @@ func TestStringArray_Rand(t *testing.T) { }) } -func TestStringArray_PopRands(t *testing.T) { +func TestStrArray_PopRands(t *testing.T) { gtest.Case(t, func() { a1 := []string{"a", "b", "c", "d", "e", "f", "g"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) gtest.AssertIN(array1.PopRands(1), []string{"a", "b", "c", "d", "e", "f", "g"}) gtest.AssertIN(array1.PopRands(1), []string{"a", "b", "c", "d", "e", "f", "g"}) gtest.AssertNI(array1.PopRands(1), array1.Slice()) @@ -226,46 +226,46 @@ func TestStringArray_PopRands(t *testing.T) { }) } -func TestStringArray_Shuffle(t *testing.T) { +func TestStrArray_Shuffle(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3", "4", "5", "6"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) gtest.Assert(array1.Shuffle().Len(), 7) }) } -func TestStringArray_Reverse(t *testing.T) { +func TestStrArray_Reverse(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3", "4", "5", "6"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) gtest.Assert(array1.Reverse().Slice(), []string{"6", "5", "4", "3", "2", "1", "0"}) }) } -func TestStringArray_Join(t *testing.T) { +func TestStrArray_Join(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3", "4", "5", "6"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6") }) } -func TestNewStringArrayFromCopy(t *testing.T) { +func TestNewStrArrayFromCopy(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3", "4", "5", "6"} - a2 := garray.NewStringArrayFromCopy(a1) - a3 := garray.NewStringArrayFromCopy(a1, true) + a2 := garray.NewStrArrayFromCopy(a1) + a3 := garray.NewStrArrayFromCopy(a1, true) gtest.Assert(a2.Contains("1"), true) gtest.Assert(a2.Len(), 7) gtest.Assert(a2, a3) }) } -func TestStringArray_SetArray(t *testing.T) { +func TestStrArray_SetArray(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3", "4", "5", "6"} a2 := []string{"a", "b", "c", "d"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) gtest.Assert(array1.Contains("2"), true) gtest.Assert(array1.Len(), 7) @@ -276,12 +276,12 @@ func TestStringArray_SetArray(t *testing.T) { }) } -func TestStringArray_Replace(t *testing.T) { +func TestStrArray_Replace(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3", "4", "5", "6"} a2 := []string{"a", "b", "c", "d"} a3 := []string{"o", "p", "q", "x", "y", "z", "w", "r", "v"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) gtest.Assert(array1.Contains("2"), true) gtest.Assert(array1.Len(), 7) @@ -302,41 +302,41 @@ func TestStringArray_Replace(t *testing.T) { }) } -func TestStringArray_Sum(t *testing.T) { +func TestStrArray_Sum(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3", "4", "5", "6"} a2 := []string{"0", "a", "3", "4", "5", "6"} - array1 := garray.NewStringArrayFrom(a1) - array2 := garray.NewStringArrayFrom(a2) + array1 := garray.NewStrArrayFrom(a1) + array2 := garray.NewStrArrayFrom(a2) gtest.Assert(array1.Sum(), 21) gtest.Assert(array2.Sum(), 18) }) } -func TestStringArray_PopRand(t *testing.T) { +func TestStrArray_PopRand(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3", "4", "5", "6"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) str1 := array1.PopRand() gtest.Assert(strings.Contains("0,1,2,3,4,5,6", str1), true) gtest.Assert(array1.Len(), 6) }) } -func TestStringArray_Clone(t *testing.T) { +func TestStrArray_Clone(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3", "4", "5", "6"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) array2 := array1.Clone() gtest.Assert(array2, array1) gtest.Assert(array2.Len(), 7) }) } -func TestStringArray_CountValues(t *testing.T) { +func TestStrArray_CountValues(t *testing.T) { gtest.Case(t, func() { a1 := []string{"0", "1", "2", "3", "4", "4", "6"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) m1 := array1.CountValues() gtest.Assert(len(m1), 6) @@ -345,30 +345,30 @@ func TestStringArray_CountValues(t *testing.T) { }) } -func TestNewSortedStringArrayFrom(t *testing.T) { +func TestNewSortedStrArrayFrom(t *testing.T) { gtest.Case(t, func() { a1 := []string{"a", "d", "c", "b"} - s1 := garray.NewSortedStringArrayFrom(a1, true) + s1 := garray.NewSortedStrArrayFrom(a1, true) gtest.Assert(s1, []string{"a", "b", "c", "d"}) - s2 := garray.NewSortedStringArrayFrom(a1, false) + s2 := garray.NewSortedStrArrayFrom(a1, false) gtest.Assert(s2, []string{"a", "b", "c", "d"}) }) } -func TestNewSortedStringArrayFromCopy(t *testing.T) { +func TestNewSortedStrArrayFromCopy(t *testing.T) { gtest.Case(t, func() { a1 := []string{"a", "d", "c", "b"} - s1 := garray.NewSortedStringArrayFromCopy(a1, true) + s1 := garray.NewSortedStrArrayFromCopy(a1, true) gtest.Assert(s1.Len(), 4) gtest.Assert(s1, []string{"a", "b", "c", "d"}) }) } -func TestSortedStringArray_SetArray(t *testing.T) { +func TestSortedStrArray_SetArray(t *testing.T) { gtest.Case(t, func() { a1 := []string{"a", "d", "c", "b"} a2 := []string{"f", "g", "h"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) array1.SetArray(a2) gtest.Assert(array1.Len(), 3) gtest.Assert(array1.Contains("d"), false) @@ -377,10 +377,10 @@ func TestSortedStringArray_SetArray(t *testing.T) { }) } -func TestSortedStringArray_Sort(t *testing.T) { +func TestSortedStrArray_Sort(t *testing.T) { gtest.Case(t, func() { a1 := []string{"a", "d", "c", "b"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) gtest.Assert(array1, []string{"a", "b", "c", "d"}) array1.Sort() @@ -390,19 +390,19 @@ func TestSortedStringArray_Sort(t *testing.T) { }) } -func TestSortedStringArray_Get(t *testing.T) { +func TestSortedStrArray_Get(t *testing.T) { gtest.Case(t, func() { a1 := []string{"a", "d", "c", "b"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) gtest.Assert(array1.Get(2), "c") gtest.Assert(array1.Get(0), "a") }) } -func TestSortedStringArray_Remove(t *testing.T) { +func TestSortedStrArray_Remove(t *testing.T) { gtest.Case(t, func() { a1 := []string{"a", "d", "c", "b"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) gtest.Assert(array1.Remove(2), "c") gtest.Assert(array1.Get(2), "d") gtest.Assert(array1.Len(), 3) @@ -418,10 +418,10 @@ func TestSortedStringArray_Remove(t *testing.T) { }) } -func TestSortedStringArray_PopLeft(t *testing.T) { +func TestSortedStrArray_PopLeft(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "c", "b"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) s1 := array1.PopLeft() gtest.Assert(s1, "a") gtest.Assert(array1.Len(), 4) @@ -429,10 +429,10 @@ func TestSortedStringArray_PopLeft(t *testing.T) { }) } -func TestSortedStringArray_PopRight(t *testing.T) { +func TestSortedStrArray_PopRight(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "c", "b"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) s1 := array1.PopRight() gtest.Assert(s1, "e") gtest.Assert(array1.Len(), 4) @@ -440,10 +440,10 @@ func TestSortedStringArray_PopRight(t *testing.T) { }) } -func TestSortedStringArray_PopRand(t *testing.T) { +func TestSortedStrArray_PopRand(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "c", "b"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) s1 := array1.PopRand() gtest.AssertIN(s1, []string{"e", "a", "d", "c", "b"}) gtest.Assert(array1.Len(), 4) @@ -451,10 +451,10 @@ func TestSortedStringArray_PopRand(t *testing.T) { }) } -func TestSortedStringArray_PopRands(t *testing.T) { +func TestSortedStrArray_PopRands(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "c", "b"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) s1 := array1.PopRands(2) gtest.AssertIN(s1, []string{"e", "a", "d", "c", "b"}) gtest.Assert(array1.Len(), 3) @@ -466,10 +466,10 @@ func TestSortedStringArray_PopRands(t *testing.T) { }) } -func TestSortedStringArray_PopLefts(t *testing.T) { +func TestSortedStrArray_PopLefts(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "c", "b"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) s1 := array1.PopLefts(2) gtest.Assert(s1, []string{"a", "b"}) gtest.Assert(array1.Len(), 3) @@ -481,10 +481,10 @@ func TestSortedStringArray_PopLefts(t *testing.T) { }) } -func TestSortedStringArray_PopRights(t *testing.T) { +func TestSortedStrArray_PopRights(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "c", "b", "f", "g"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) s1 := array1.PopRights(2) gtest.Assert(s1, []string{"f", "g"}) gtest.Assert(array1.Len(), 5) @@ -496,11 +496,11 @@ func TestSortedStringArray_PopRights(t *testing.T) { }) } -func TestSortedStringArray_Range(t *testing.T) { +func TestSortedStrArray_Range(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "c", "b", "f", "g"} - array1 := garray.NewSortedStringArrayFrom(a1) - array2 := garray.NewSortedStringArrayFrom(a1, true) + array1 := garray.NewSortedStrArrayFrom(a1) + array2 := garray.NewSortedStrArrayFrom(a1, true) s1 := array1.Range(2, 4) gtest.Assert(len(s1), 2) gtest.Assert(s1, []string{"c", "d"}) @@ -520,21 +520,21 @@ func TestSortedStringArray_Range(t *testing.T) { }) } -func TestSortedStringArray_Sum(t *testing.T) { +func TestSortedStrArray_Sum(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "c", "b", "f", "g"} a2 := []string{"1", "2", "3", "4", "a"} - array1 := garray.NewSortedStringArrayFrom(a1) - array2 := garray.NewSortedStringArrayFrom(a2) + array1 := garray.NewSortedStrArrayFrom(a1) + array2 := garray.NewSortedStrArrayFrom(a2) gtest.Assert(array1.Sum(), 0) gtest.Assert(array2.Sum(), 10) }) } -func TestSortedStringArray_Clone(t *testing.T) { +func TestSortedStrArray_Clone(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "c", "b", "f", "g"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) array2 := array1.Clone() gtest.Assert(array1, array2) array1.Remove(1) @@ -542,20 +542,20 @@ func TestSortedStringArray_Clone(t *testing.T) { }) } -func TestSortedStringArray_Clear(t *testing.T) { +func TestSortedStrArray_Clear(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "c", "b", "f", "g"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) array1.Clear() gtest.Assert(array1.Len(), 0) }) } -func TestSortedStringArray_SubSlice(t *testing.T) { +func TestSortedStrArray_SubSlice(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "c", "b", "f", "g"} - array1 := garray.NewSortedStringArrayFrom(a1) - array2 := garray.NewSortedStringArrayFrom(a1, true) + array1 := garray.NewSortedStrArrayFrom(a1) + array2 := garray.NewSortedStrArrayFrom(a1, true) s1 := array1.SubSlice(1, 3) gtest.Assert(len(s1), 3) gtest.Assert(s1, []string{"b", "c", "d"}) @@ -580,27 +580,27 @@ func TestSortedStringArray_SubSlice(t *testing.T) { }) } -func TestSortedStringArray_Len(t *testing.T) { +func TestSortedStrArray_Len(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "c", "b", "f", "g"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) gtest.Assert(array1.Len(), 7) }) } -func TestSortedStringArray_Rand(t *testing.T) { +func TestSortedStrArray_Rand(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) gtest.AssertIN(array1.Rand(), []string{"e", "a", "d"}) }) } -func TestSortedStringArray_Rands(t *testing.T) { +func TestSortedStrArray_Rands(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) s1 := array1.Rands(2) gtest.AssertIN(s1, []string{"e", "a", "d"}) @@ -612,19 +612,19 @@ func TestSortedStringArray_Rands(t *testing.T) { }) } -func TestSortedStringArray_Join(t *testing.T) { +func TestSortedStrArray_Join(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) gtest.Assert(array1.Join(","), "a,d,e") gtest.Assert(array1.Join("."), "a.d.e") }) } -func TestSortedStringArray_CountValues(t *testing.T) { +func TestSortedStrArray_CountValues(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "a", "c"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) m1 := array1.CountValues() gtest.Assert(m1["a"], 2) gtest.Assert(m1["d"], 1) @@ -632,10 +632,10 @@ func TestSortedStringArray_CountValues(t *testing.T) { }) } -func TestSortedStringArray_Chunk(t *testing.T) { +func TestSortedStrArray_Chunk(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "a", "c"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) array2 := array1.Chunk(2) gtest.Assert(len(array2), 3) gtest.Assert(len(array2[0]), 2) @@ -644,20 +644,20 @@ func TestSortedStringArray_Chunk(t *testing.T) { }) } -func TestSortedStringArray_SetUnique(t *testing.T) { +func TestSortedStrArray_SetUnique(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "a", "c"} - array1 := garray.NewSortedStringArrayFrom(a1) + array1 := garray.NewSortedStrArrayFrom(a1) array2 := array1.SetUnique(true) gtest.Assert(array2.Len(), 4) gtest.Assert(array2, []string{"a", "c", "d", "e"}) }) } -func TestStringArray_Remove(t *testing.T) { +func TestStrArray_Remove(t *testing.T) { gtest.Case(t, func() { a1 := []string{"e", "a", "d", "a", "c"} - array1 := garray.NewStringArrayFrom(a1) + array1 := garray.NewStrArrayFrom(a1) s1 := array1.Remove(1) gtest.Assert(s1, "a") gtest.Assert(array1.Len(), 4) @@ -667,10 +667,10 @@ func TestStringArray_Remove(t *testing.T) { }) } -func TestStringArray_RLockFunc(t *testing.T) { +func TestStrArray_RLockFunc(t *testing.T) { gtest.Case(t, func() { s1 := []string{"a", "b", "c", "d"} - a1 := garray.NewStringArrayFrom(s1, true) + a1 := garray.NewStrArrayFrom(s1, true) ch1 := make(chan int64, 3) ch2 := make(chan int64, 1) @@ -699,10 +699,10 @@ func TestStringArray_RLockFunc(t *testing.T) { }) } -func TestSortedStringArray_LockFunc(t *testing.T) { +func TestSortedStrArray_LockFunc(t *testing.T) { gtest.Case(t, func() { s1 := []string{"a", "b", "c", "d"} - a1 := garray.NewSortedStringArrayFrom(s1, true) + a1 := garray.NewSortedStrArrayFrom(s1, true) ch1 := make(chan int64, 3) ch2 := make(chan int64, 3) @@ -731,10 +731,10 @@ func TestSortedStringArray_LockFunc(t *testing.T) { }) } -func TestSortedStringArray_RLockFunc(t *testing.T) { +func TestSortedStrArray_RLockFunc(t *testing.T) { gtest.Case(t, func() { s1 := []string{"a", "b", "c", "d"} - a1 := garray.NewSortedStringArrayFrom(s1, true) + a1 := garray.NewSortedStrArrayFrom(s1, true) ch1 := make(chan int64, 3) ch2 := make(chan int64, 1) @@ -763,7 +763,7 @@ func TestSortedStringArray_RLockFunc(t *testing.T) { }) } -func TestSortedStringArray_Merge(t *testing.T) { +func TestSortedStrArray_Merge(t *testing.T) { gtest.Case(t, func() { func1 := func(v1, v2 interface{}) int { if gconv.Int(v1) < gconv.Int(v2) { @@ -776,11 +776,11 @@ func TestSortedStringArray_Merge(t *testing.T) { s2 := []string{"e", "f"} i1 := garray.NewIntArrayFrom([]int{1, 2, 3}) i2 := garray.NewArrayFrom([]interface{}{3}) - s3 := garray.NewStringArrayFrom([]string{"g", "h"}) + s3 := garray.NewStrArrayFrom([]string{"g", "h"}) s4 := garray.NewSortedArrayFrom([]interface{}{4, 5}, func1) - s5 := garray.NewSortedStringArrayFrom(s2) + s5 := garray.NewSortedStrArrayFrom(s2) s6 := garray.NewSortedIntArrayFrom([]int{1, 2, 3}) - a1 := garray.NewSortedStringArrayFrom(s1) + a1 := garray.NewSortedStrArrayFrom(s1) gtest.Assert(a1.Merge(s2).Len(), 6) gtest.Assert(a1.Merge(i1).Len(), 9) @@ -792,10 +792,10 @@ func TestSortedStringArray_Merge(t *testing.T) { }) } -func TestStringArray_SortFunc(t *testing.T) { +func TestStrArray_SortFunc(t *testing.T) { gtest.Case(t, func() { s1 := []string{"a", "d", "c", "b"} - a1 := garray.NewStringArrayFrom(s1) + a1 := garray.NewStrArrayFrom(s1) func1 := func(v1, v2 string) bool { return v1 < v2 } @@ -804,10 +804,10 @@ func TestStringArray_SortFunc(t *testing.T) { }) } -func TestStringArray_LockFunc(t *testing.T) { +func TestStrArray_LockFunc(t *testing.T) { gtest.Case(t, func() { s1 := []string{"a", "b", "c", "d"} - a1 := garray.NewStringArrayFrom(s1, true) + a1 := garray.NewStrArrayFrom(s1, true) ch1 := make(chan int64, 3) ch2 := make(chan int64, 3) diff --git a/container/gset/gset_string_set.go b/container/gset/gset_str_set.go similarity index 78% rename from container/gset/gset_string_set.go rename to container/gset/gset_str_set.go index 1d0c1edc8..b009da94b 100644 --- a/container/gset/gset_string_set.go +++ b/container/gset/gset_str_set.go @@ -15,7 +15,7 @@ import ( "github.com/gogf/gf/util/gconv" ) -type StringSet struct { +type StrSet struct { mu *rwmutex.RWMutex m map[string]struct{} } @@ -23,20 +23,20 @@ type StringSet struct { // New create and returns a new set, which contains un-repeated items. // The parameter used to specify whether using set in un-concurrent-safety, // which is false in default. -func NewStringSet(safe ...bool) *StringSet { - return &StringSet{ +func NewStrSet(safe ...bool) *StrSet { + return &StrSet{ m: make(map[string]struct{}), mu: rwmutex.New(safe...), } } -// NewStringSetFrom returns a new set from . -func NewStringSetFrom(items []string, safe ...bool) *StringSet { +// NewStrSetFrom returns a new set from . +func NewStrSetFrom(items []string, safe ...bool) *StrSet { m := make(map[string]struct{}) for _, v := range items { m[v] = struct{}{} } - return &StringSet{ + return &StrSet{ m: m, mu: rwmutex.New(safe...), } @@ -44,7 +44,7 @@ func NewStringSetFrom(items []string, safe ...bool) *StringSet { // Iterator iterates the set with given callback function , // if returns true then continue iterating; or false to stop. -func (set *StringSet) Iterator(f func(v string) bool) *StringSet { +func (set *StrSet) Iterator(f func(v string) bool) *StrSet { set.mu.RLock() defer set.mu.RUnlock() for k, _ := range set.m { @@ -56,7 +56,7 @@ func (set *StringSet) Iterator(f func(v string) bool) *StringSet { } // Add adds one or multiple items to the set. -func (set *StringSet) Add(item ...string) *StringSet { +func (set *StrSet) Add(item ...string) *StrSet { set.mu.Lock() for _, v := range item { set.m[v] = struct{}{} @@ -66,7 +66,7 @@ func (set *StringSet) Add(item ...string) *StringSet { } // Contains checks whether the set contains . -func (set *StringSet) Contains(item string) bool { +func (set *StrSet) Contains(item string) bool { set.mu.RLock() _, exists := set.m[item] set.mu.RUnlock() @@ -74,7 +74,7 @@ func (set *StringSet) Contains(item string) bool { } // Remove deletes from set. -func (set *StringSet) Remove(item string) *StringSet { +func (set *StrSet) Remove(item string) *StrSet { set.mu.Lock() delete(set.m, item) set.mu.Unlock() @@ -82,7 +82,7 @@ func (set *StringSet) Remove(item string) *StringSet { } // Size returns the size of the set. -func (set *StringSet) Size() int { +func (set *StrSet) Size() int { set.mu.RLock() l := len(set.m) set.mu.RUnlock() @@ -90,7 +90,7 @@ func (set *StringSet) Size() int { } // Clear deletes all items of the set. -func (set *StringSet) Clear() *StringSet { +func (set *StrSet) Clear() *StrSet { set.mu.Lock() set.m = make(map[string]struct{}) set.mu.Unlock() @@ -98,7 +98,7 @@ func (set *StringSet) Clear() *StringSet { } // Slice returns the a of items of the set as slice. -func (set *StringSet) Slice() []string { +func (set *StrSet) Slice() []string { set.mu.RLock() ret := make([]string, len(set.m)) i := 0 @@ -112,31 +112,31 @@ func (set *StringSet) Slice() []string { } // Join joins items with a string . -func (set *StringSet) Join(glue string) string { +func (set *StrSet) Join(glue string) string { return strings.Join(set.Slice(), ",") } // String returns items as a string, which are joined by char ','. -func (set *StringSet) String() string { +func (set *StrSet) String() string { return set.Join(",") } // LockFunc locks writing with callback function . -func (set *StringSet) LockFunc(f func(m map[string]struct{})) { +func (set *StrSet) LockFunc(f func(m map[string]struct{})) { set.mu.Lock() defer set.mu.Unlock() f(set.m) } // RLockFunc locks reading with callback function . -func (set *StringSet) RLockFunc(f func(m map[string]struct{})) { +func (set *StrSet) RLockFunc(f func(m map[string]struct{})) { set.mu.RLock() defer set.mu.RUnlock() f(set.m) } // Equal checks whether the two sets equal. -func (set *StringSet) Equal(other *StringSet) bool { +func (set *StrSet) Equal(other *StrSet) bool { if set == other { return true } @@ -156,7 +156,7 @@ func (set *StringSet) Equal(other *StringSet) bool { } // IsSubsetOf checks whether the current set is a sub-set of . -func (set *StringSet) IsSubsetOf(other *StringSet) bool { +func (set *StrSet) IsSubsetOf(other *StrSet) bool { if set == other { return true } @@ -174,8 +174,8 @@ func (set *StringSet) IsSubsetOf(other *StringSet) bool { // Union returns a new set which is the union of and . // Which means, all the items in are in or in . -func (set *StringSet) Union(others ...*StringSet) (newSet *StringSet) { - newSet = NewStringSet(true) +func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet) { + newSet = NewStrSet(true) set.mu.RLock() defer set.mu.RUnlock() for _, other := range others { @@ -200,8 +200,8 @@ func (set *StringSet) Union(others ...*StringSet) (newSet *StringSet) { // Diff returns a new set which is the difference set from to . // Which means, all the items in are in but not in . -func (set *StringSet) Diff(others ...*StringSet) (newSet *StringSet) { - newSet = NewStringSet(true) +func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet) { + newSet = NewStrSet(true) set.mu.RLock() defer set.mu.RUnlock() for _, other := range others { @@ -221,8 +221,8 @@ func (set *StringSet) Diff(others ...*StringSet) (newSet *StringSet) { // Intersect returns a new set which is the intersection from to . // Which means, all the items in are in and also in . -func (set *StringSet) Intersect(others ...*StringSet) (newSet *StringSet) { - newSet = NewStringSet(true) +func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet) { + newSet = NewStrSet(true) set.mu.RLock() defer set.mu.RUnlock() for _, other := range others { @@ -246,8 +246,8 @@ func (set *StringSet) Intersect(others ...*StringSet) (newSet *StringSet) { // // It returns the difference between and // if the given set is not the full set of . -func (set *StringSet) Complement(full *StringSet) (newSet *StringSet) { - newSet = NewStringSet(true) +func (set *StrSet) Complement(full *StrSet) (newSet *StrSet) { + newSet = NewStrSet(true) set.mu.RLock() defer set.mu.RUnlock() if set != full { @@ -263,7 +263,7 @@ func (set *StringSet) Complement(full *StringSet) (newSet *StringSet) { } // Merge adds items from sets into . -func (set *StringSet) Merge(others ...*StringSet) *StringSet { +func (set *StrSet) Merge(others ...*StrSet) *StrSet { set.mu.Lock() defer set.mu.Unlock() for _, other := range others { @@ -283,7 +283,7 @@ func (set *StringSet) Merge(others ...*StringSet) *StringSet { // Sum sums items. // Note: The items should be converted to int type, // or you'd get a result that you unexpected. -func (set *StringSet) Sum() (sum int) { +func (set *StrSet) Sum() (sum int) { set.mu.RLock() defer set.mu.RUnlock() for k, _ := range set.m { @@ -293,7 +293,7 @@ func (set *StringSet) Sum() (sum int) { } // Pops randomly pops an item from set. -func (set *StringSet) Pop() string { +func (set *StrSet) Pop() string { set.mu.RLock() defer set.mu.RUnlock() for k, _ := range set.m { @@ -303,7 +303,7 @@ func (set *StringSet) Pop() string { } // Pops randomly pops items from set. -func (set *StringSet) Pops(size int) []string { +func (set *StrSet) Pops(size int) []string { set.mu.RLock() defer set.mu.RUnlock() if size > len(set.m) { @@ -322,6 +322,6 @@ func (set *StringSet) Pops(size int) []string { } // MarshalJSON implements the interface MarshalJSON for json.Marshal. -func (set *StringSet) MarshalJSON() ([]byte, error) { +func (set *StrSet) MarshalJSON() ([]byte, error) { return json.Marshal(set.Slice()) } diff --git a/container/gset/gset_z_bench_test.go b/container/gset/gset_z_bench_test.go index 6976bcddc..378cbae83 100644 --- a/container/gset/gset_z_bench_test.go +++ b/container/gset/gset_z_bench_test.go @@ -17,10 +17,10 @@ import ( var intSet = gset.NewIntSet(true) var anySet = gset.NewSet(true) -var strSet = gset.NewStringSet(true) +var strSet = gset.NewStrSet(true) var intSetUnsafe = gset.NewIntSet() var anySetUnsafe = gset.NewSet() -var strSetUnsafe = gset.NewStringSet() +var strSetUnsafe = gset.NewStrSet() func Benchmark_IntSet_Add(b *testing.B) { b.RunParallel(func(pb *testing.PB) { diff --git a/container/gset/gset_z_unit_string_test.go b/container/gset/gset_z_unit_str_test.go similarity index 73% rename from container/gset/gset_z_unit_string_test.go rename to container/gset/gset_z_unit_str_test.go index 408e5744e..21cb6188e 100644 --- a/container/gset/gset_z_unit_string_test.go +++ b/container/gset/gset_z_unit_str_test.go @@ -17,9 +17,9 @@ import ( "github.com/gogf/gf/test/gtest" ) -func TestStringSet_Basic(t *testing.T) { +func TestStrSet_Basic(t *testing.T) { gtest.Case(t, func() { - s := gset.NewStringSet() + s := gset.NewStrSet() s.Add("1").Add("1").Add("2") s.Add([]string{"3", "4"}...) gtest.Assert(s.Size(), 4) @@ -37,9 +37,9 @@ func TestStringSet_Basic(t *testing.T) { }) } -func TestStringSet_Iterator(t *testing.T) { +func TestStrSet_Iterator(t *testing.T) { gtest.Case(t, func() { - s := gset.NewStringSet() + s := gset.NewStrSet() s.Add("1").Add("2").Add("3") gtest.Assert(s.Size(), 3) @@ -58,9 +58,9 @@ func TestStringSet_Iterator(t *testing.T) { }) } -func TestStringSet_LockFunc(t *testing.T) { +func TestStrSet_LockFunc(t *testing.T) { gtest.Case(t, func() { - s := gset.NewStringSet() + s := gset.NewStrSet() s.Add("1").Add("2").Add("3") gtest.Assert(s.Size(), 3) s.LockFunc(func(m map[string]struct{}) { @@ -76,11 +76,11 @@ func TestStringSet_LockFunc(t *testing.T) { }) } -func TestStringSet_Equal(t *testing.T) { +func TestStrSet_Equal(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() - s3 := gset.NewStringSet() + s1 := gset.NewStrSet() + s2 := gset.NewStrSet() + s3 := gset.NewStrSet() s1.Add("1").Add("2").Add("3") s2.Add("1").Add("2").Add("3") s3.Add("1").Add("2").Add("3").Add("4") @@ -89,11 +89,11 @@ func TestStringSet_Equal(t *testing.T) { }) } -func TestStringSet_IsSubsetOf(t *testing.T) { +func TestStrSet_IsSubsetOf(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() - s3 := gset.NewStringSet() + s1 := gset.NewStrSet() + s2 := gset.NewStrSet() + s3 := gset.NewStrSet() s1.Add("1").Add("2") s2.Add("1").Add("2").Add("3") s3.Add("1").Add("2").Add("3").Add("4") @@ -105,10 +105,10 @@ func TestStringSet_IsSubsetOf(t *testing.T) { }) } -func TestStringSet_Union(t *testing.T) { +func TestStrSet_Union(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() + s1 := gset.NewStrSet() + s2 := gset.NewStrSet() s1.Add("1").Add("2") s2.Add("3").Add("4") s3 := s1.Union(s2) @@ -119,10 +119,10 @@ func TestStringSet_Union(t *testing.T) { }) } -func TestStringSet_Diff(t *testing.T) { +func TestStrSet_Diff(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() + s1 := gset.NewStrSet() + s2 := gset.NewStrSet() s1.Add("1").Add("2").Add("3") s2.Add("3").Add("4").Add("5") s3 := s1.Diff(s2) @@ -133,10 +133,10 @@ func TestStringSet_Diff(t *testing.T) { }) } -func TestStringSet_Intersect(t *testing.T) { +func TestStrSet_Intersect(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() + s1 := gset.NewStrSet() + s2 := gset.NewStrSet() s1.Add("1").Add("2").Add("3") s2.Add("3").Add("4").Add("5") s3 := s1.Intersect(s2) @@ -147,10 +147,10 @@ func TestStringSet_Intersect(t *testing.T) { }) } -func TestStringSet_Complement(t *testing.T) { +func TestStrSet_Complement(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() + s1 := gset.NewStrSet() + s2 := gset.NewStrSet() s1.Add("1").Add("2").Add("3") s2.Add("3").Add("4").Add("5") s3 := s1.Complement(s2) @@ -172,10 +172,10 @@ func TestNewIntSetFrom(t *testing.T) { }) } -func TestStringSet_Merge(t *testing.T) { +func TestStrSet_Merge(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSet() - s2 := gset.NewStringSet() + s1 := gset.NewStrSet() + s2 := gset.NewStrSet() s1.Add("1").Add("2").Add("3") s2.Add("3").Add("4").Add("5") s3 := s1.Merge(s2) @@ -186,24 +186,24 @@ func TestStringSet_Merge(t *testing.T) { }) } -func TestNewStringSetFrom(t *testing.T) { +func TestNewStrSetFrom(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) gtest.Assert(s1.Contains("b"), true) gtest.Assert(s1.Contains("d"), false) }) } -func TestStringSet_Join(t *testing.T) { - s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) +func TestStrSet_Join(t *testing.T) { + s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) str1 := s1.Join(",") gtest.Assert(strings.Contains(str1, "b"), true) gtest.Assert(strings.Contains(str1, "d"), false) } -func TestStringSet_String(t *testing.T) { +func TestStrSet_String(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) str1 := s1.String() gtest.Assert(strings.Contains(str1, "b"), true) gtest.Assert(strings.Contains(str1, "d"), false) @@ -211,43 +211,43 @@ func TestStringSet_String(t *testing.T) { } -func TestStringSet_Sum(t *testing.T) { +func TestStrSet_Sum(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) s2 := gset.NewIntSetFrom([]int{2, 3, 4}, true) gtest.Assert(s1.Sum(), 0) gtest.Assert(s2.Sum(), 9) }) } -func TestStringSet_Size(t *testing.T) { +func TestStrSet_Size(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) gtest.Assert(s1.Size(), 3) }) } -func TestStringSet_Remove(t *testing.T) { +func TestStrSet_Remove(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) s1 = s1.Remove("b") gtest.Assert(s1.Contains("b"), false) gtest.Assert(s1.Contains("c"), true) }) } -func TestStringSet_Pop(t *testing.T) { +func TestStrSet_Pop(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) str1 := s1.Pop() gtest.Assert(strings.Contains("a,b,c", str1), true) }) } -func TestStringSet_Pops(t *testing.T) { +func TestStrSet_Pops(t *testing.T) { gtest.Case(t, func() { - s1 := gset.NewStringSetFrom([]string{"a", "b", "c"}, true) + s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) strs1 := s1.Pops(2) gtest.AssertIN(strs1, []string{"a", "b", "c"}) gtest.Assert(len(strs1), 2) diff --git a/net/ghttp/ghttp_server.go b/net/ghttp/ghttp_server.go index 504878361..e252784d0 100644 --- a/net/ghttp/ghttp_server.go +++ b/net/ghttp/ghttp_server.go @@ -159,6 +159,17 @@ func init() { } } +// 主要用于开发者在HTTP处理中自定义异常捕获时,判断捕获的异常是否Server抛出的自定义退出异常 +func IsExitError(err interface{}) bool { + errStr := gconv.String(err) + if strings.EqualFold(errStr, gEXCEPTION_EXIT) || + strings.EqualFold(errStr, gEXCEPTION_EXIT_ALL) || + strings.EqualFold(errStr, gEXCEPTION_EXIT_HOOK) { + return true + } + return false +} + // 是否开启平滑重启特性 func SetGraceful(enabled bool) { gracefulEnabled = enabled diff --git a/os/gcfg/gcfg.go b/os/gcfg/gcfg.go index 55988c0ad..918bfdd65 100644 --- a/os/gcfg/gcfg.go +++ b/os/gcfg/gcfg.go @@ -32,10 +32,10 @@ const ( // Configuration struct. type Config struct { - name *gtype.String // Default configuration file name. - paths *garray.StringArray // Searching path array. - jsons *gmap.StrAnyMap // The pared JSON objects for configuration files. - vc *gtype.Bool // Whether do violence check in value index searching. It affects the performance when set true(false in default). + name *gtype.String // Default configuration file name. + paths *garray.StrArray // Searching path array. + jsons *gmap.StrAnyMap // The pared JSON objects for configuration files. + vc *gtype.Bool // Whether do violence check in value index searching. It affects the performance when set true(false in default). } var ( @@ -51,7 +51,7 @@ func New(file ...string) *Config { } c := &Config{ name: gtype.NewString(name), - paths: garray.NewStringArray(true), + paths: garray.NewStrArray(true), jsons: gmap.NewStrAnyMap(true), vc: gtype.NewBool(), } diff --git a/os/gcmd/gcmd.go b/os/gcmd/gcmd.go index 1353ba514..56ed76d4d 100644 --- a/os/gcmd/gcmd.go +++ b/os/gcmd/gcmd.go @@ -5,93 +5,21 @@ // You can obtain one at https://github.com/gogf/gf. // -// Package gcmd provides console operations, like options/values reading and command running. +// Package gcmd provides console operations, like options/arguments reading and command running. package gcmd -import ( - "os" - "regexp" - - "github.com/gogf/gf/os/glog" -) - -// Console values. -type gCmdValue struct { - values []string -} - -// Console options. -type gCmdOption struct { - options map[string]string -} - -var Value = &gCmdValue{} // Console values. -var Option = &gCmdOption{} // Console options. -var cmdFuncMap = make(map[string]func()) // Registered callback functions. - -func init() { - doInit() -} - -// doInit does the initialization for this package. -func doInit() { - Value.values = Value.values[:0] - Option.options = make(map[string]string) - reg := regexp.MustCompile(`^\-{1,2}(\w+)={0,1}(.*)`) - for i := 0; i < len(os.Args); i++ { - result := reg.FindStringSubmatch(os.Args[i]) - if len(result) > 1 { - Option.options[result[1]] = result[2] - } else { - Value.values = append(Value.values, os.Args[i]) - } - } -} - -// BindHandle registers callback function with . -func BindHandle(cmd string, f func()) { - if _, ok := cmdFuncMap[cmd]; ok { - glog.Fatal("duplicated handle for command:" + cmd) - } else { - cmdFuncMap[cmd] = f - } -} - -// RunHandle executes the callback function registered by . -func RunHandle(cmd string) { - if handle, ok := cmdFuncMap[cmd]; ok { - handle() - } else { - glog.Fatal("no handle found for command:" + cmd) - } -} - -// AutoRun automatically recognizes and executes the callback function -// by value of index 0 (the first console parameter). -func AutoRun() { - if cmd := Value.Get(1); cmd != "" { - if handle, ok := cmdFuncMap[cmd]; ok { - handle() - } else { - glog.Fatal("no handle found for command:" + cmd) - } - } else { - glog.Fatal("no command found") - } -} - // BuildOptions builds the options as string. func BuildOptions(m map[string]string, prefix ...string) string { options := "" - leadstr := "-" + leadStr := "-" if len(prefix) > 0 { - leadstr = prefix[0] + leadStr = prefix[0] } for k, v := range m { if len(options) > 0 { options += " " } - options += leadstr + k + options += leadStr + k if v != "" { options += "=" + v } diff --git a/os/gcmd/gcmd_option.go b/os/gcmd/gcmd_option.go deleted file mode 100644 index 02981a0b1..000000000 --- a/os/gcmd/gcmd_option.go +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://github.com/gogf/gf. -// - -package gcmd - -import "github.com/gogf/gf/container/gvar" - -// GetAll returns all option values as map[string]string. -func (c *gCmdOption) GetAll() map[string]string { - return c.options -} - -// BuildOptions builds the options as string. -func (c *gCmdOption) Build(prefix ...string) string { - return BuildOptions(c.options, prefix...) -} - -// Get returns the option value string specified by , -// if value dose not exist, then returns . -func (c *gCmdOption) Get(key string, def ...string) string { - if option, ok := c.options[key]; ok { - return option - } else if len(def) > 0 { - return def[0] - } - return "" -} - -// Contains checks whether the option named exists. -func (c *gCmdOption) Contains(key string) bool { - _, ok := c.options[key] - return ok -} - -// Set sets the option named with value . -func (c *gCmdOption) Set(key string, value string) { - c.options[key] = value -} - -// GetVar returns the option value as *gvar.Var object specified by , -// if value does not exist, then returns as its default value. -func (c *gCmdOption) GetVar(key string, def ...string) *gvar.Var { - return gvar.New(c.Get(key, def...)) -} diff --git a/os/gcmd/gcmd_parser.go b/os/gcmd/gcmd_parser.go new file mode 100644 index 000000000..a0a6fa57d --- /dev/null +++ b/os/gcmd/gcmd_parser.go @@ -0,0 +1,208 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. +// + +package gcmd + +import ( + "encoding/json" + "fmt" + "os" + "strings" + + "github.com/gogf/gf/text/gstr" + + "errors" + + "github.com/gogf/gf/container/gvar" + + "github.com/gogf/gf/text/gregex" +) + +// Parser for arguments. +type Parser struct { + parsedArgs []string // As name described. + parsedOptions map[string]string // As name described. + passedOptions map[string]bool // User passed supported options. + supportedOptions map[string]bool // Option [option name : need argument] + commandFuncMap map[string]func() // Command function map for function handler. +} + +// Parse creates and returns a new Parser with os.Args and supported options. +// +// Note that the parameter is as [option name: need argument], which means +// the value item of indicates whether corresponding option name needs argument or not. +func Parse(supportedOptions map[string]bool) (*Parser, error) { + return ParseWithArgs(os.Args, supportedOptions) +} + +// ParseWithArgs creates and returns a new Parser with given arguments and supported options. +// +// Note that the parameter is as [option name: need argument], which means +// the value item of indicates whether corresponding option name needs argument or not. +func ParseWithArgs(args []string, supportedOptions map[string]bool) (*Parser, error) { + parser := &Parser{ + parsedArgs: make([]string, 0), + parsedOptions: make(map[string]string), + passedOptions: supportedOptions, + supportedOptions: make(map[string]bool), + commandFuncMap: make(map[string]func()), + } + for name, needArgument := range supportedOptions { + for _, v := range strings.Split(name, ",") { + parser.supportedOptions[strings.TrimSpace(v)] = needArgument + } + } + + for i := 0; i < len(args); { + if option := parser.parseOption(args[i]); option != "" { + array, _ := gregex.MatchString(`^(\w+)=(.+)$`, option) + if len(array) == 3 { + if parser.isOptionValid(array[1]) { + parser.setOptionValue(array[1], array[2]) + } + } else { + if parser.isOptionValid(option) { + if parser.isOptionNeedArgument(option) { + if i+1 < len(args) { + parser.setOptionValue(option, args[i+1]) + i += 2 + continue + } + } else { + parser.setOptionValue(option, "") + i++ + continue + } + } else { + // Multiple options? + if array := parser.parseMultiOption(option); len(array) > 0 { + for _, v := range array { + parser.setOptionValue(v, "") + } + i++ + continue + } else { + return nil, errors.New(fmt.Sprintf(`invalid option '%s'`, args[i])) + } + } + } + } else { + parser.parsedArgs = append(parser.parsedArgs, args[i]) + } + i++ + } + return parser, nil +} + +// parseMultiOption parses options to multiple valid options. +// It returns nil if given option is not multi-option. +func (p *Parser) parseMultiOption(option string) []string { + for i := 1; i <= len(option); i++ { + s := option[:i] + if p.isOptionValid(s) && !p.isOptionNeedArgument(s) { + if i == len(option) { + return []string{s} + } + array := p.parseMultiOption(option[i:]) + if len(array) == 0 { + return nil + } + return append(array, s) + } + } + return nil +} + +func (p *Parser) parseOption(argument string) string { + array, _ := gregex.MatchString(`^\-{1,2}(.+)$`, argument) + if len(array) == 2 { + return array[1] + } + return "" +} + +func (p *Parser) isOptionValid(name string) bool { + _, ok := p.supportedOptions[name] + return ok +} + +func (p *Parser) isOptionNeedArgument(name string) bool { + return p.supportedOptions[name] +} + +// setOptionValue sets the option value for name and according alias. +func (p *Parser) setOptionValue(name, value string) { + for optionName, _ := range p.passedOptions { + array := gstr.SplitAndTrimSpace(optionName, ",") + for _, v := range array { + if strings.EqualFold(v, name) { + for _, v := range array { + p.parsedOptions[v] = value + } + return + } + } + } +} + +// GetOpt returns the option value named . +func (p *Parser) GetOpt(name string, def ...string) string { + if v, ok := p.parsedOptions[name]; ok { + return v + } + if len(def) > 0 { + return def[0] + } + return "" +} + +// GetOptVar returns the option value named as *gvar.Var. +func (p *Parser) GetOptVar(name string, def ...string) *gvar.Var { + return gvar.New(p.GetOpt(name, def...)) +} + +// GetOptAll returns all parsed options. +func (p *Parser) GetOptAll() map[string]string { + return p.parsedOptions +} + +// ContainsOpt checks whether option named exist in the arguments. +func (p *Parser) ContainsOpt(name string, def ...string) bool { + _, ok := p.parsedOptions[name] + return ok +} + +// GetArg returns the argument at . +func (p *Parser) GetArg(index int, def ...string) string { + if index < len(p.parsedArgs) { + return p.parsedArgs[index] + } + if len(def) > 0 { + return def[0] + } + return "" +} + +// GetArgVar returns the argument at as *gvar.Var. +func (p *Parser) GetArgVar(index int, def ...string) *gvar.Var { + return gvar.New(p.GetArg(index, def...)) +} + +// GetArgAll returns all parsed arguments. +func (p *Parser) GetArgAll() []string { + return p.parsedArgs +} + +// MarshalJSON implements the interface MarshalJSON for json.Marshal. +func (p *Parser) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]interface{}{ + "parsedArgs": p.parsedArgs, + "parsedOptions": p.parsedOptions, + "passedOptions": p.passedOptions, + "supportedOptions": p.supportedOptions, + }) +} diff --git a/os/gcmd/gcmd_parser_handler.go b/os/gcmd/gcmd_parser_handler.go new file mode 100644 index 000000000..6093ea523 --- /dev/null +++ b/os/gcmd/gcmd_parser_handler.go @@ -0,0 +1,47 @@ +// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. +// + +package gcmd + +import ( + "errors" +) + +// BindHandle registers callback function with . +func (p *Parser) BindHandle(cmd string, f func()) error { + if _, ok := p.commandFuncMap[cmd]; ok { + return errors.New("duplicated handle for command:" + cmd) + } else { + p.commandFuncMap[cmd] = f + } + return nil +} + +// RunHandle executes the callback function registered by . +func (p *Parser) RunHandle(cmd string) error { + if handle, ok := p.commandFuncMap[cmd]; ok { + handle() + } else { + return errors.New("no handle found for command:" + cmd) + } + return nil +} + +// AutoRun automatically recognizes and executes the callback function +// by value of index 0 (the first console parameter). +func (p *Parser) AutoRun() error { + if cmd := p.GetArg(1); cmd != "" { + if handle, ok := p.commandFuncMap[cmd]; ok { + handle() + } else { + return errors.New("no handle found for command:" + cmd) + } + } else { + return errors.New("no command found") + } + return nil +} diff --git a/os/gcmd/gcmd_value.go b/os/gcmd/gcmd_value.go deleted file mode 100644 index bea4390b4..000000000 --- a/os/gcmd/gcmd_value.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://github.com/gogf/gf. -// - -package gcmd - -import "github.com/gogf/gf/container/gvar" - -// GetAll returns all values as a slice of string. -func (c *gCmdValue) GetAll() []string { - return c.values -} - -// Get returns value by index as string, -// if value does not exist, then returns . -func (c *gCmdValue) Get(index int, def ...string) string { - if index < len(c.values) { - return c.values[index] - } else if len(def) > 0 { - return def[0] - } - return "" -} - -// GetVar returns value by index as *gvar.Var object, -// if value does not exist, then returns as its default value. -func (c *gCmdValue) GetVar(index int, def ...string) *gvar.Var { - return gvar.New(c.Get(index, def...)) -} diff --git a/os/gcmd/gcmd_z_unit_func_test.go b/os/gcmd/gcmd_z_unit_func_test.go new file mode 100644 index 000000000..b9ee8b529 --- /dev/null +++ b/os/gcmd/gcmd_z_unit_func_test.go @@ -0,0 +1,34 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +// go test *.go -bench=".*" -benchmem + +package gcmd_test + +import ( + "testing" + + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/os/gcmd" + + "github.com/gogf/gf/test/gtest" +) + +func Test_BuildOptions(t *testing.T) { + gtest.Case(t, func() { + s := gcmd.BuildOptions(g.MapStrStr{ + "n": "john", + }) + gtest.Assert(s, "-n=john") + }) + + gtest.Case(t, func() { + s := gcmd.BuildOptions(g.MapStrStr{ + "n": "john", + }, "-test") + gtest.Assert(s, "-testn=john") + }) +} diff --git a/os/gcmd/gcmd_z_unit_parser_test.go b/os/gcmd/gcmd_z_unit_parser_test.go new file mode 100644 index 000000000..5c44b8010 --- /dev/null +++ b/os/gcmd/gcmd_z_unit_parser_test.go @@ -0,0 +1,136 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +// go test *.go -bench=".*" -benchmem + +package gcmd_test + +import ( + "os" + "testing" + + "github.com/gogf/gf/container/garray" + + "github.com/gogf/gf/os/gcmd" + + "github.com/gogf/gf/test/gtest" +) + +func Test_Parse(t *testing.T) { + gtest.Case(t, func() { + os.Args = []string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"} + p, err := gcmd.Parse(map[string]bool{ + "n, name": true, + "p, prefix": true, + "f,force": false, + "q,quiet": false, + }) + gtest.Assert(err, nil) + gtest.Assert(len(p.GetArgAll()), 3) + gtest.Assert(p.GetArg(0), "gf") + gtest.Assert(p.GetArg(1), "remove") + gtest.Assert(p.GetArg(2), "path") + gtest.Assert(p.GetArgVar(2).String(), "path") + + gtest.Assert(len(p.GetOptAll()), 8) + gtest.Assert(p.GetOpt("n"), "root") + gtest.Assert(p.GetOpt("name"), "root") + gtest.Assert(p.GetOpt("p"), "www") + gtest.Assert(p.GetOpt("prefix"), "www") + gtest.Assert(p.GetOptVar("prefix").String(), "www") + + gtest.Assert(p.ContainsOpt("n"), true) + gtest.Assert(p.ContainsOpt("name"), true) + gtest.Assert(p.ContainsOpt("p"), true) + gtest.Assert(p.ContainsOpt("prefix"), true) + gtest.Assert(p.ContainsOpt("f"), true) + gtest.Assert(p.ContainsOpt("force"), true) + gtest.Assert(p.ContainsOpt("q"), true) + gtest.Assert(p.ContainsOpt("quiet"), true) + gtest.Assert(p.ContainsOpt("none"), false) + }) +} + +func Test_ParseWithArgs(t *testing.T) { + gtest.Case(t, func() { + p, err := gcmd.ParseWithArgs( + []string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"}, + map[string]bool{ + "n, name": true, + "p, prefix": true, + "f,force": false, + "q,quiet": false, + }) + gtest.Assert(err, nil) + gtest.Assert(len(p.GetArgAll()), 3) + gtest.Assert(p.GetArg(0), "gf") + gtest.Assert(p.GetArg(1), "remove") + gtest.Assert(p.GetArg(2), "path") + gtest.Assert(p.GetArgVar(2).String(), "path") + + gtest.Assert(len(p.GetOptAll()), 8) + gtest.Assert(p.GetOpt("n"), "root") + gtest.Assert(p.GetOpt("name"), "root") + gtest.Assert(p.GetOpt("p"), "www") + gtest.Assert(p.GetOpt("prefix"), "www") + gtest.Assert(p.GetOptVar("prefix").String(), "www") + + gtest.Assert(p.ContainsOpt("n"), true) + gtest.Assert(p.ContainsOpt("name"), true) + gtest.Assert(p.ContainsOpt("p"), true) + gtest.Assert(p.ContainsOpt("prefix"), true) + gtest.Assert(p.ContainsOpt("f"), true) + gtest.Assert(p.ContainsOpt("force"), true) + gtest.Assert(p.ContainsOpt("q"), true) + gtest.Assert(p.ContainsOpt("quiet"), true) + gtest.Assert(p.ContainsOpt("none"), false) + }) +} + +func Test_Handler(t *testing.T) { + gtest.Case(t, func() { + p, err := gcmd.ParseWithArgs( + []string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"}, + map[string]bool{ + "n, name": true, + "p, prefix": true, + "f,force": false, + "q,quiet": false, + }) + gtest.Assert(err, nil) + array := garray.New() + err = p.BindHandle("remove", func() { + array.Append(1) + }) + gtest.Assert(err, nil) + + err = p.BindHandle("remove", func() { + array.Append(1) + }) + gtest.AssertNE(err, nil) + + err = p.BindHandle("test", func() { + array.Append(1) + }) + gtest.Assert(err, nil) + + err = p.RunHandle("remove") + gtest.Assert(err, nil) + gtest.Assert(array.Len(), 1) + + err = p.RunHandle("none") + gtest.AssertNE(err, nil) + gtest.Assert(array.Len(), 1) + + err = p.RunHandle("test") + gtest.Assert(err, nil) + gtest.Assert(array.Len(), 2) + + err = p.AutoRun() + gtest.Assert(err, nil) + gtest.Assert(array.Len(), 3) + }) +} diff --git a/os/gcmd/gcmd_z_unit_test.go b/os/gcmd/gcmd_z_unit_test.go deleted file mode 100644 index 56d79d607..000000000 --- a/os/gcmd/gcmd_z_unit_test.go +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://github.com/gogf/gf. - -// go test *.go -bench=".*" -benchmem - -package gcmd - -import ( - "os" - "testing" - - "github.com/gogf/gf/test/gtest" -) - -func Test_ValueAndOption(t *testing.T) { - os.Args = []string{"v1", "v2", "--o1=111", "-o2=222"} - doInit() - gtest.Case(t, func() { - gtest.Assert(Value.GetAll(), []string{"v1", "v2"}) - gtest.Assert(Value.Get(0), "v1") - gtest.Assert(Value.Get(1), "v2") - gtest.Assert(Value.Get(2), "") - gtest.Assert(Value.Get(2, "1"), "1") - gtest.Assert(Value.GetVar(1, "1").String(), "v2") - gtest.Assert(Value.GetVar(2, "1").String(), "1") - - gtest.Assert(Option.GetAll(), map[string]string{"o1": "111", "o2": "222"}) - gtest.Assert(Option.Get("o1"), "111") - gtest.Assert(Option.Get("o2"), "222") - gtest.Assert(Option.Get("o3", "1"), "1") - gtest.Assert(Option.GetVar("o2", "1").String(), "222") - gtest.Assert(Option.GetVar("o3", "1").String(), "1") - - }) -} - -func Test_Handle(t *testing.T) { - os.Args = []string{"gf", "gf"} - doInit() - gtest.Case(t, func() { - num := 1 - BindHandle("gf", func() { - num += 1 - }) - RunHandle("gf") - gtest.AssertEQ(num, 2) - AutoRun() - gtest.AssertEQ(num, 3) - }) -} diff --git a/os/gfile/gfile_search.go b/os/gfile/gfile_search.go index 9e7b88470..a9c473f50 100644 --- a/os/gfile/gfile_search.go +++ b/os/gfile/gfile_search.go @@ -25,7 +25,7 @@ func Search(name string, prioritySearchPaths ...string) (realPath string, err er } // TODO move search paths to internal package variable. // Search paths array. - array := garray.NewStringArray() + array := garray.NewStrArray() array.Append(prioritySearchPaths...) array.Append(Pwd(), SelfDir()) if path := MainPkgPath(); path != "" { diff --git a/os/gfile/gfile_sort.go b/os/gfile/gfile_sort.go index a0d655370..f1a6d4dc8 100644 --- a/os/gfile/gfile_sort.go +++ b/os/gfile/gfile_sort.go @@ -34,7 +34,7 @@ func fileSortFunc(path1, path2 string) int { // SortFiles sorts the in order of: directory -> file. // Note that the item of should be absolute path. func SortFiles(files []string) []string { - array := garray.NewSortedStringArrayComparator(fileSortFunc) + array := garray.NewSortedStrArrayComparator(fileSortFunc) array.Add(files...) return array.Slice() } diff --git a/os/gspath/gspath.go b/os/gspath/gspath.go index 266cc32f8..65c34f9d6 100644 --- a/os/gspath/gspath.go +++ b/os/gspath/gspath.go @@ -26,8 +26,8 @@ import ( // 文件目录搜索管理对象 type SPath struct { - paths *garray.StringArray // 搜索路径,按照优先级进行排序 - cache *gmap.StrStrMap // 搜索结果缓存map(如果未nil表示未启用缓存功能) + paths *garray.StrArray // 搜索路径,按照优先级进行排序 + cache *gmap.StrStrMap // 搜索结果缓存map(如果未nil表示未启用缓存功能) } // 文件搜索缓存项 @@ -45,7 +45,7 @@ var ( // 创建一个搜索对象 func New(path string, cache bool) *SPath { sp := &SPath{ - paths: garray.NewStringArray(true), + paths: garray.NewStrArray(true), } if cache { sp.cache = gmap.NewStrStrMap(true) diff --git a/os/gview/gview.go b/os/gview/gview.go index c6dcc2059..4683331df 100644 --- a/os/gview/gview.go +++ b/os/gview/gview.go @@ -25,7 +25,7 @@ import ( // View object for template engine. type View struct { mu sync.RWMutex - paths *garray.StringArray // Searching path array. + paths *garray.StrArray // Searching path array. data map[string]interface{} // Global template variables. funcMap map[string]interface{} // Global template function map. i18nEnabled bool // Is i18n enabled in this template. @@ -60,7 +60,7 @@ func ParseContent(content string, params Params) (string, error) { // The parameter specifies the template directory path to load template files. func New(path ...string) *View { view := &View{ - paths: garray.NewStringArray(true), + paths: garray.NewStrArray(true), data: make(map[string]interface{}), funcMap: make(map[string]interface{}), i18nEnabled: true, diff --git a/text/gstr/gstr.go b/text/gstr/gstr.go index 1f74d2564..9f64b25c5 100644 --- a/text/gstr/gstr.go +++ b/text/gstr/gstr.go @@ -442,6 +442,26 @@ func Split(str, delimiter string) []string { return strings.Split(str, delimiter) } +// SplitAndTrim splits string by a string to an array, +// and calls Trim to every element of this array. +func SplitAndTrim(str, delimiter, cut string) []string { + array := strings.Split(str, delimiter) + for k, v := range array { + array[k] = strings.Trim(v, cut) + } + return array +} + +// SplitAndTrimSpace splits string by a string to an array, +// and calls TrimSpace to every element of this array. +func SplitAndTrimSpace(str, delimiter string) []string { + array := strings.Split(str, delimiter) + for k, v := range array { + array[k] = strings.TrimSpace(v) + } + return array +} + // Join concatenates the elements of a to create a single string. The separator string // sep is placed between elements in the resulting string. func Join(array []string, sep string) string {