Skip to content

Commit

Permalink
code refactor for IdsString
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed Aug 7, 2021
1 parent 1726881 commit 68608e4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 0 additions & 1 deletion slice/contains.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func ContainsString(sl []string, v string) bool {
return false
}


func ContainsSlice(smallSlice, bigSlice []string) bool {
for i := 0; i < len(smallSlice); i++ {
if !ContainsString(bigSlice, smallSlice[i]) {
Expand Down
25 changes: 21 additions & 4 deletions str/ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ import (
"strings"
)

func IdsInt64(ids string) []int64 {
func IdsInt64(ids string, sep ...string) []int64 {
if ids == "" {
return []int64{}
}

arr := strings.Split(ids, ",")
s := ","
if len(sep) > 0 {
s = sep[0]
}

var arr []string

if s == " " {
arr = strings.Fields(ids)
} else {
arr = strings.Split(ids, s)
}

count := len(arr)
ret := make([]int64, 0, count)
for i := 0; i < count; i++ {
Expand All @@ -26,11 +38,16 @@ func IdsInt64(ids string) []int64 {
return ret
}

func IdsString(ids []int64) string {
func IdsString(ids []int64, sep ...string) string {
count := len(ids)
arr := make([]string, count)
for i := 0; i < count; i++ {
arr[i] = fmt.Sprintf("%d", ids[i])
arr[i] = fmt.Sprint(ids[i])
}

if len(sep) > 0 {
return strings.Join(arr, sep[0])
}

return strings.Join(arr, ",")
}
2 changes: 1 addition & 1 deletion str/str.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ func TrimStringSlice(raw []string) []string {
}

return arr
}
}

0 comments on commit 68608e4

Please sign in to comment.