Skip to content

Commit

Permalink
Implement strstr, strcasestr.
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed May 6, 2018
1 parent b0b5721 commit fbacfc8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
26 changes: 26 additions & 0 deletions noarch/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package noarch
import (
"bytes"
"reflect"
"strings"
"unsafe"
)

Expand Down Expand Up @@ -66,6 +67,18 @@ func Strncpy(dest, src []byte, len int32) []byte {
return dest
}

// Strcasestr - function is similar to Strstr(),
// but ignores the case of both strings.
func Strcasestr(str1, str2 []byte) []byte {
a := strings.ToLower(CStringToString(str1))
b := strings.ToLower(CStringToString(str2))
index := strings.Index(a, b)
if index == -1 {
return []byte{0}
}
return str1[index : index+len(b)]
}

// Strcat - concatenate strings
// Appends a copy of the source string to the destination string.
// The terminating null character in destination is overwritten by the first
Expand Down Expand Up @@ -93,6 +106,19 @@ func Strncmp(str1, str2 []byte, n int32) int32 {
return int32(bytes.Compare(a, b))
}

// Strstr - locate a substring in a string
// function locates the first occurrence of the null-terminated string needle
// in the null-terminated string haystack.
func Strstr(str1, str2 []byte) []byte {
a := CStringToString(str1)
b := CStringToString(str2)
index := strings.Index(a, b)
if index == -1 {
return []byte{0}
}
return str1[index : index+len(b)]
}

func min(a, b int) int {
if a < b {
return a
Expand Down
3 changes: 3 additions & 0 deletions program/function_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ var builtInFunctionDefinitions = map[string][]string{
},
"string.h": []string{
// string.h
"char* strcasestr(const char*, const char*) -> noarch.Strcasestr",
"char* strcat(char *, const char *) -> noarch.Strcat",
"int strcmp(const char *, const char *) -> noarch.Strcmp",

Expand All @@ -195,6 +196,8 @@ var builtInFunctionDefinitions = map[string][]string{
// in according to noarch.Strlen
"int strlen(const char*) -> noarch.Strlen",

"char* strstr(const char*, const char*) -> noarch.Strstr",

// should be: "void* memset(void *, int, size_t) -> noarch.Memset"
"void* memset(void *, int, int) -> noarch.Memset",

Expand Down
20 changes: 19 additions & 1 deletion tests/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ typedef struct mem {

int main()
{
plan(70);
plan(72);

diag("TODO: __builtin_object_size")
// https://github.com/elliotchance/c2go/issues/359
Expand Down Expand Up @@ -268,6 +268,24 @@ int main()
is_true(memcmp(a,b,3) == 0);
}
}
{
diag("strstr");
{
char* a = "needle in a haystack";
char* b = "haystack";
char* res = strstr(a,b);
is_streq(res, "haystack");
}
}
{
diag("strcasestr");
{
char* a = "needle in a haystack";
char* b = "HayStack";
char* res = strcasestr(a,b);
is_streq(res, "haystack");
}
}

done_testing();
}

0 comments on commit fbacfc8

Please sign in to comment.