summaryrefslogtreecommitdiff
path: root/internal/split/split.go
diff options
context:
space:
mode:
authorayyansea <ayyansea@gmail.com>2024-11-18 21:53:29 +0300
committerayyansea <ayyansea@gmail.com>2024-11-18 21:53:29 +0300
commit574e76ae935c4931ec50b14a94dee930ed6f3d5a (patch)
treea289a98536930cd90a0d8e7e05420f3a35934923 /internal/split/split.go
parentd741a3187f0e8abb00ee34356b3e61ab9087c0d9 (diff)
feat: restructure project + add a basic cli arg parser
Diffstat (limited to 'internal/split/split.go')
-rw-r--r--internal/split/split.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/internal/split/split.go b/internal/split/split.go
new file mode 100644
index 0000000..e03335c
--- /dev/null
+++ b/internal/split/split.go
@@ -0,0 +1,55 @@
+package split
+
+import (
+ "strings"
+)
+
+func FormatInput(tokenArray []string, delimeterArray []string) []string {
+ for _, delimeter := range delimeterArray {
+ for index, element := range tokenArray {
+ tokenArray = NewArrayWithSplit(tokenArray, index, element, delimeter)
+ }
+ }
+
+ return tokenArray
+}
+
+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
+}