Skip to content

Commit 5a8b47c

Browse files
committed
update
1 parent 006126c commit 5a8b47c

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

slice.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,59 @@ func SliceRemoveCallback(length int, callback func(int) func(bool) error) error
558558
}
559559
return nil
560560
}
561+
562+
func SplitKVRows(rows string, seperator ...string) map[string]string {
563+
sep := `=`
564+
if len(seperator) > 0 {
565+
sep = seperator[0]
566+
}
567+
res := map[string]string{}
568+
for _, row := range strings.Split(rows, StrLF) {
569+
parts := strings.SplitN(row, sep, 2)
570+
if len(parts) != 2 {
571+
continue
572+
}
573+
parts[0] = strings.TrimSpace(parts[0])
574+
if len(parts[0]) == 0 {
575+
continue
576+
}
577+
parts[1] = strings.TrimSpace(parts[1])
578+
res[parts[0]] = parts[1]
579+
}
580+
return res
581+
}
582+
583+
func SplitKVRowsCallback(rows string, callback func(k, v string) error, seperator ...string) (err error) {
584+
sep := `=`
585+
if len(seperator) > 0 {
586+
sep = seperator[0]
587+
}
588+
for _, row := range strings.Split(rows, StrLF) {
589+
parts := strings.SplitN(row, sep, 2)
590+
if len(parts) != 2 {
591+
continue
592+
}
593+
parts[0] = strings.TrimSpace(parts[0])
594+
if len(parts[0]) == 0 {
595+
continue
596+
}
597+
parts[1] = strings.TrimSpace(parts[1])
598+
err = callback(parts[0], parts[1])
599+
if err != nil {
600+
return
601+
}
602+
}
603+
return
604+
}
605+
606+
func TrimSpaceForRows(rows string) []string {
607+
res := []string{}
608+
for _, row := range strings.Split(rows, StrLF) {
609+
row = strings.TrimSpace(row)
610+
if len(row) == 0 {
611+
continue
612+
}
613+
res = append(res, row)
614+
}
615+
return res
616+
}

0 commit comments

Comments
 (0)