Repository: Konstantin8105/c4go
Pull request #284 (2019-02-26 09:51:20)

prepare kilo editor


Patch:
diff --git a/noarch/stdio.go b/noarch/stdio.go
index 0d7cc8e..265a0d5 100644
--- a/noarch/stdio.go
+++ b/noarch/stdio.go
@@ -831,8 +831,16 @@ func Getline(line [][]byte, len []uint, f *File) SsizeT {
 		if err != nil {
 			break
 		}
+		if buf[0] == '\n' {
+			break
+		}
 		line[0] = append(line[0], buf...)
 		counter++
 	}
+	if counter == 0 {
+		return SsizeT(-1)
+	}
+	line[0] = append(line[0], '\x00')
+	counter++
 	return SsizeT(counter)
 }
diff --git a/noarch/string.go b/noarch/string.go
index 84097fc..bc3fabb 100644
--- a/noarch/string.go
+++ b/noarch/string.go
@@ -2,6 +2,7 @@ package noarch
 
 import (
 	"bytes"
+	"reflect"
 )
 
 // Strlen returns the length of a string.
@@ -116,26 +117,20 @@ func Strstr(str, subStr []byte) []byte {
 	if subStr[0] == '\x00' {
 		return str
 	}
-	i, j, k := 0, 0, 0
-	j++
-	for ; subStr[j] != '\x00'; j++ {
-		k++
-	}
-
-	for str[k] != '\x00' {
-		j, l := 0, i
 
-		for str[i] != '\x00' && subStr[j] != '\x00' && str[i] == subStr[j] {
-			i++
-			j++
-		}
-		if subStr[j] == '\x00' {
-			return str[l:]
+	k := 0
+	for i := range subStr {
+		if subStr[i] == '\x00' {
+			k = i
+			break
 		}
-		i = l + 1
-		k++
 	}
-	return nil
+
+	index := bytes.Index(str, subStr[:k])
+	if index < 0 {
+		return nil
+	}
+	return str[index:]
 }
 
 // Memset sets the first num bytes of the block of memory pointed by ptr to
@@ -170,11 +165,20 @@ func Memcmp(lhs []byte, rhs []byte, count uint32) int {
 	return 0
 }
 
-func Memcpy(dest, source []byte, num uint) []byte {
-	for i := 0; i < int(num); i++ {
-		dest[i] = source[i]
+func Memcpy(dst, src interface{}, size uint) interface{} {
+	switch reflect.TypeOf(src).Kind() {
+	case reflect.Slice:
+		s := reflect.ValueOf(src)
+		d := reflect.ValueOf(dst)
+		size /= uint(int(s.Index(0).Type().Size()))
+		for i := 0; i < int(size); i++ {
+			if i >= s.Len() {
+				break
+			}
+			d.Index(i).Set(s.Index(i))
+		}
 	}
-	return nil
+	return dst
 }
 
 func Strrchr(source []byte, c int) []byte {


Test patch:
diff --git a/tests/stdio.c b/tests/stdio.c
index 648ad0b..93e5116 100644
--- a/tests/stdio.c
+++ b/tests/stdio.c
@@ -550,7 +550,9 @@ void test_getline()
 	char *line = NULL;
 	char **pnt = &line;
 	size_t *l = &len;
-	is_true(getline(pnt,l,pFile) == filesize);
+	ssize_t pos = getline(pnt,l,pFile) ;
+	printf("getline = %d\n", pos);
+	is_true(pos == filesize);
 }
 
 void test_sscanf()
diff --git a/tests/string.c b/tests/string.c
index dd8a826..69811bc 100644
--- a/tests/string.c
+++ b/tests/string.c
@@ -157,8 +157,14 @@ int main()
     {
         diag("strstr");
         char str[] = "one two three";
+
+		printf("%s\n",strstr(str, "one"));
         is_streq(strstr(str, "one"), "one two three");
+
+		printf("%s\n",strstr(str, "two"));
         is_streq(strstr(str, "two"), "two three");
+
+		printf("%s\n",strstr(str, "three"));
         is_streq(strstr(str, "three"), "three");
         //TODO: NULL check in is_eq(strstr(str, "four") - str, 0);
     }
@@ -168,6 +174,7 @@ int main()
 		char name[40]; 
 		memcpy ( name, myname, strlen(myname)+1 );
 		is_streq(name, myname);
+		printf("name = `%s`\n",name);
 	}
 	{
 		diag("strrchr");