Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/chunk split #79

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions go/chunk_split.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package pehape

import (
"errors"
"fmt"
"strconv"
"strings"
)

var (
ErrLengthNotValid = errors.New("length is not valid")
ErrLengthMustGreaterThanZero = errors.New("length must be greater than zero")

ErrSeparatorNotValid = errors.New("separator is not valid")
)

// ChunkSplit - Split a string into smaller chunks
// php doc: https://www.php.net/manual/en/function.chunk-split
func ChunkSplit(str string, args ...any) (result string, err error) {
if len(args) > 2 {
return "", ErrTooManyArgs
}

length := 76
separator := "\r\n"

switch len(args) {
case 1:
length, err = getChunkSplitLength(args[0])
if err != nil {
return "", err
}
case 2:
length, err = getChunkSplitLength(args[0])
if err != nil {
return "", err
}
separator, err = getChunkSplitSeparator(args[1])
if err != nil {
return "", err
}
}

if len(str) < length {
return str, nil
}

var sb strings.Builder

var index int
for {
if len(str) <= index {
sb.WriteString(str[index:])
return sb.String(), nil
}

if index+length > len(str) {
length = len(str) - index
}

sb.WriteString(str[index : index+length])
sb.WriteString(separator)

index += length

}
}

func getChunkSplitLength(arg any) (int, error) {
switch v := arg.(type) {
case int:
return v, nil
case float64: // DEPRECATED
return 0, ErrLengthNotValid
case string:
i, err := strconv.Atoi(v)
if err != nil {
return 0, ErrLengthNotValid
}
return i, nil
case bool:
if v {
return 1, nil
}
return 0, ErrLengthMustGreaterThanZero
}
return 0, ErrLengthNotValid
}

func getChunkSplitSeparator(arg any) (string, error) {
switch v := arg.(type) {
case string:
return v, nil
case bool:
if v {
return "1", nil
}
return "", nil
case float64, int:
return fmt.Sprintf("%v", v), nil
}
return "", ErrSeparatorNotValid
}
44 changes: 44 additions & 0 deletions go/chunk_split_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package pehape

import "testing"

func TestChunkSplit(t *testing.T) {
tests := []struct {
name string
given string
args []any
expectedString string
expectedError error
}{
{"should return as is 1", "hello", nil, "hello", nil},
{"should return as is 2", "hello", []any{10}, "hello", nil},
{"should return as is 3", "hello", []any{10, "oke"}, "hello", nil},
{"should return chunk 1", "hello", []any{2}, "he\r\nll\r\no\r\n", nil},
{"should return chunk 2", "hello", []any{2, "oke"}, "heokellokeooke", nil},
{"should return error len args > 2", "hello", []any{2, "oke", "random"}, "", ErrTooManyArgs},
{"should return error length not valid because float64", "hello", []any{2.3}, "", ErrLengthNotValid},
{"should return success with length string", "hello", []any{"2"}, "he\r\nll\r\no\r\n", nil},
{"should return error because length string float64", "hello", []any{"2.5"}, "", ErrLengthNotValid},
{"should return success with length bool true", "hello", []any{true}, "h\r\ne\r\nl\r\nl\r\no\r\n", nil},
{"should return error because string length bool false", "hello", []any{false}, "", ErrLengthMustGreaterThanZero},
{"should return success with separator bool true", "hello", []any{2, true}, "he1ll1o1", nil},
{"should return success with separator bool false", "hello", []any{2, false}, "hello", nil},
{"should return success with separator int", "hello", []any{2, 1}, "he1ll1o1", nil},
{"should return success with separator float64", "hello", []any{2, 1.5}, "he1.5ll1.5o1.5", nil},
{"should return error because separator not valid", "hello", []any{2, func() {}}, "", ErrSeparatorNotValid},
{"should return error because length not valid", "hello", []any{func() {}, func() {}}, "", ErrLengthNotValid},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
actual, err := ChunkSplit(tt.given, tt.args...)
if actual != tt.expectedString {
t.Errorf("(%s): expected %s, actual %s", tt.given, tt.expectedString, actual)
}

if err != tt.expectedError {
t.Errorf("(%s): expected %s, actual %s", tt.given, tt.expectedError, err)
}
})
}
}
11 changes: 10 additions & 1 deletion go/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,13 @@ shuffle := StrShuffle("abcdef")

//This will print something like: "bfdaec"
fmt.Println(shuffle)
```
```

### `ChunkSplit`

```go
chunk, err := ChunkSplit("hello", 2, "oke")

//This will print: "<nil> heokellokeooke"
fmt.Println(err, chunk)
```