Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

#5 string helpers #12

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
AfterLast Working
Wulfheart committed Feb 20, 2021
commit c9ffa56aa2bb687286e9146297a9b84d9be7053b
21 changes: 17 additions & 4 deletions str/str.go
Original file line number Diff line number Diff line change
@@ -25,27 +25,40 @@ func InSlice(input interface{}, expects ...interface{}) bool {

// Return the remainder of a string after the first occurrence of a given value.
func After(subject string, search string) string {
// TODO
l := len(search)
if l == 0 {
return subject
}


byteIndex := strings.Index(subject, search)
if byteIndex == -1 {
return subject
}

byteSubject := []byte(subject)
byteSearch := []byte(search)

result := string(byteSubject[byteIndex+ len(byteSearch):])
return result
}

// Return the remainder of a string after the last occurrence of a given value.
func AfterLast(subject string, search string) string {
// TODO
return ""
l := len(search)
if l == 0 {
return subject
}

byteIndex := strings.LastIndex(subject, search)
if byteIndex == -1 {
return subject
}

byteSubject := []byte(subject)
byteSearch := []byte(search)

result := string(byteSubject[byteIndex+ len(byteSearch):])
return result
}

func Before(subject string, search string) string {
13 changes: 13 additions & 0 deletions str/str_test.go
Original file line number Diff line number Diff line change
@@ -16,3 +16,16 @@ func TestAfter(t *testing.T) {
assert.Equal(t, "nah", After("han0nah", "0"))
assert.Equal(t, "nah", After("han2nah", "2"))
}

func TestAfterLast(t *testing.T) {
assert.Equal(t,"tte", AfterLast("yvette", "yve"))
assert.Equal(t,"e", AfterLast("yvette", "t"))
assert.Equal(t,"e", AfterLast("ééé yvette", "t"))
assert.Equal(t,"", AfterLast("yvette", "tte"))
assert.Equal(t,"yvette", AfterLast("yvette", "xxxx"))
assert.Equal(t,"yvette", AfterLast("yvette", ""))
assert.Equal(t,"te", AfterLast("yv0et0te", "0"))
assert.Equal(t,"te", AfterLast("yv0et0te", "0"))
assert.Equal(t,"te", AfterLast("yv2et2te", "2"))
assert.Equal(t,"foo", AfterLast("----foo", "---"))
}