Skip to content

Is string #41

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions string/stristr/stristr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package stristr

import (
"errors"
"fmt"
"strings"
)
//Do https://github.com/php/php-src/blob/master/ext/standard/tests/strings/stristr.phpt
func Do(str string,sep string, before_search bool) (string, error) {
if sep == "" {
return "", errors.New("empty needle param sep")
}
index := strings.Index(strings.ToLower(str), strings.ToLower(sep))
if index == -1 {
return "", errors.New(fmt.Sprintf("param %s is not exits", sep))
}
strArray := make([]string, 0)
if before_search {
strArray = strings.Split(str, "")[:index]
}else{
strArray = strings.Split(str, "")[index:]

}
return strings.Join(strArray, ""), nil


}
90 changes: 90 additions & 0 deletions string/stristr/stristr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package stristr

import "testing"

func TestDo(t *testing.T) {
type args struct {
str string
sep string
before_search bool
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "case-1",
args: args{"tEsT sTrInG", "tEsT", false},
want: "tEsT sTrInG",
wantErr: false,
},
{
name: "case-2",
args: args{"tEsT sTrInG", "stRiNg", false},
want: "sTrInG",
wantErr: false,
},
{
name: "case-3",
args: args{"tEsT sTrInG", "stRiN", false},
want: "sTrInG",
wantErr: false,
},
{
name: "case-4",
args: args{"tEsT sTrInG", "t S", false},
want: "T sTrInG",
wantErr: false,
},
{
name: "case-5",
args: args{"tEsT sTrInG", "g", false},
want: "G",
wantErr: false,
},
{
name: "case-6",
args: args{"", "", false},
want: "",
wantErr: true,
},
{
name: "case-7",
args: args{"a", "", false},
want: "",
wantErr: true,
},
{
name: "case-8",
args: args{"", "a", false},
want: "",
wantErr: true,
},
{
name: "case-9",
args: args{"\\\\a\\", "\\a", false},
want: "\\a\\",
wantErr: false,
},
{
name: "case-10",
args: args{"tEsT sTrInG", " ", false},
want: " sTrInG",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Do(tt.args.str, tt.args.sep, tt.args.before_search)
if (err != nil) != tt.wantErr {
t.Errorf("Do() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Do() got = %v, want %v", got, tt.want)
}
})
}
}
10 changes: 10 additions & 0 deletions variable-handling/isString/is_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package isString

func Do(value interface{}) bool {
switch value.(type) {
case string:
return true
default :
return false
}
}
45 changes: 45 additions & 0 deletions variable-handling/isString/is_string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package isString

import "testing"

func TestDo(t *testing.T) {
type args struct {
value interface{}
}
tests := []struct {
name string
args args
want bool
}{
{
name: "case string true",
args: args{
value: "string",
},
want: true,
},
{
name: "case array false",
args: args{
value: []string{"1", "2"},
},
want: false,
},
{
name: "case map false",
args: args{
value: map[string]string{
"test1":"test1",
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Do(tt.args.value); got != tt.want {
t.Errorf("Do() = %v, want %v", got, tt.want)
}
})
}
}