-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: [help] Common functions for strings and arrays
- Loading branch information
Showing
7 changed files
with
162 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package help | ||
|
||
import ( | ||
"math/rand" | ||
) | ||
|
||
func Reverse[T any](v []T) { | ||
for n, m := 0, len(v)-1; n < len(v)/2; n, m = n+1, m-1 { | ||
v[n], v[m] = v[m], v[n] | ||
} | ||
} | ||
|
||
func Shuffle[T any](v []T) { | ||
m := 0 | ||
for n := len(v) - 1; n > 0; n-- { | ||
m = rand.Intn(n + 1) | ||
if n != m { | ||
v[n], v[m] = v[m], v[n] | ||
} | ||
} | ||
} | ||
|
||
func ReverseString(v string) string { | ||
runes := []rune(v) | ||
for n, m := 0, len(runes)-1; n < len(runes)/2; n, m = n+1, m-1 { | ||
runes[n], runes[m] = runes[m], runes[n] | ||
} | ||
return string(runes) | ||
} | ||
|
||
func ShuffleString(v string) string { | ||
runes, m := []rune(v), 0 | ||
for n := len(runes) - 1; n > 0; n-- { | ||
m = rand.Intn(n + 1) | ||
if n != m { | ||
runes[n], runes[m] = runes[m], runes[n] | ||
} | ||
} | ||
return string(runes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package help_test | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/weplanx/go/help" | ||
"testing" | ||
) | ||
|
||
func TestReverse(t *testing.T) { | ||
v := []string{"a", "b", "c"} | ||
help.Reverse(v) | ||
assert.Equal(t, []string{"c", "b", "a"}, v) | ||
t.Log(v) | ||
} | ||
|
||
func TestShuffle(t *testing.T) { | ||
v := []int{1, 2, 3, 4, 5, 6, 7} | ||
help.Shuffle(v) | ||
t.Log(v) | ||
} | ||
|
||
func TestReverseString(t *testing.T) { | ||
v := help.ReverseString("abcdefg") | ||
assert.Equal(t, "gfedcba", v) | ||
t.Log(v) | ||
} | ||
|
||
func TestShuffleString(t *testing.T) { | ||
v := help.ShuffleString("abcdefg") | ||
t.Log(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package help | ||
|
||
import "github.com/google/uuid" | ||
|
||
func Uuid() string { | ||
return uuid.New().String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package help_test | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/weplanx/go/help" | ||
"testing" | ||
) | ||
|
||
func TestUuid(t *testing.T) { | ||
v := help.Uuid() | ||
_, err := uuid.Parse(v) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package help | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cloudwego/hertz/pkg/app/server/binding" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"reflect" | ||
) | ||
|
||
func Ptr[T any](i T) *T { | ||
return &i | ||
} | ||
|
||
func IsEmpty(i any) bool { | ||
if i == nil || i == "" || i == false { | ||
return true | ||
} | ||
|
||
v := reflect.ValueOf(i) | ||
switch v.Kind() { | ||
case reflect.Invalid: | ||
return true | ||
case reflect.String, reflect.Array: | ||
return v.Len() == 0 | ||
case reflect.Map, reflect.Slice: | ||
return v.Len() == 0 || v.IsNil() | ||
case reflect.Bool: | ||
return !v.Bool() | ||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
return v.Int() == 0 | ||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
return v.Uint() == 0 | ||
case reflect.Float32, reflect.Float64: | ||
return v.Float() == 0 | ||
case reflect.Interface, reflect.Ptr, reflect.Func, reflect.Chan: | ||
return v.IsNil() | ||
} | ||
|
||
return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface()) | ||
} | ||
|
||
func RegValidate() { | ||
binding.MustRegValidateFunc("mongoId", func(args ...interface{}) error { | ||
if len(args) != 1 { | ||
return fmt.Errorf("the args must be one") | ||
} | ||
if _, e := primitive.ObjectIDFromHex(args[0].(string)); e != nil { | ||
return e | ||
} | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package help_test | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"github.com/weplanx/go/help" | ||
"testing" | ||
) | ||
|
||
func TestHelp(t *testing.T) { | ||
assert.True(t, help.IsEmpty(nil)) | ||
assert.True(t, help.IsEmpty("")) | ||
assert.True(t, help.IsEmpty(0)) | ||
assert.False(t, help.IsEmpty(help.Ptr(0))) | ||
var a *string | ||
assert.True(t, help.IsEmpty(a)) | ||
var b struct{} | ||
assert.True(t, help.IsEmpty(b)) | ||
} |
This file was deleted.
Oops, something went wrong.