diff options
| author | ayyansea <ayyansea@gmail.com> | 2024-11-15 19:47:46 +0300 |
|---|---|---|
| committer | ayyansea <ayyansea@gmail.com> | 2024-11-15 19:47:46 +0300 |
| commit | 22e051662cadb657fab8e98561cdcf13b6b71d8b (patch) | |
| tree | fb23c51bddd673996ea46daa389005a7c4d846b0 /internal/split/split.go | |
initial commit
Diffstat (limited to 'internal/split/split.go')
| -rw-r--r-- | internal/split/split.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/internal/split/split.go b/internal/split/split.go new file mode 100644 index 0000000..65e1a14 --- /dev/null +++ b/internal/split/split.go @@ -0,0 +1,44 @@ +package split + +import ( + "strings" +) + +func checkArrayElementsEmpty(array []string) bool { + for _, str := range array { + if str != "" { + return false + } + } + return true +} + +func NewArrayWithSplit(initialArray []string, index int, token string, delimeter string) (result []string) { + split := strings.Split(token, delimeter) + splitLength := len(split) + + /* + When a token only consists of delimeter * N (N >= 0), + the resulting split consists of N empty elements. + Here we check if it is so and essentialy remove that token + from resulting array. + */ + splitIsEmpty := checkArrayElementsEmpty(split) + if splitIsEmpty { + result = append(initialArray[:index], initialArray[index+1:]...) + return result + } + + if splitLength > 1 { + if split[splitLength-1] == "" { + split = split[:splitLength-1] + } + + result = append(initialArray[:index], append(split, initialArray[index+1:]...)...) + } + if splitLength == 1 { + result = initialArray + } + + return result +} |
