fixed sstrtrim and some warnings

Fri, 21 Jun 2013 10:27:03 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 21 Jun 2013 10:27:03 +0200
changeset 104
9d3dea320d8e
parent 103
08018864fb91
child 105
f08fa6308d1f

fixed sstrtrim and some warnings

.hgignore file | annotate | diff | comparison | revisions
osx-debug.mk file | annotate | diff | comparison | revisions
osx.mk file | annotate | diff | comparison | revisions
test/Makefile file | annotate | diff | comparison | revisions
test/string_tests.c file | annotate | diff | comparison | revisions
ucx/Makefile file | annotate | diff | comparison | revisions
ucx/string.c file | annotate | diff | comparison | revisions
--- a/.hgignore	Thu Feb 28 08:50:24 2013 +0100
+++ b/.hgignore	Fri Jun 21 10:27:03 2013 +0200
@@ -2,5 +2,6 @@
 ^nbproject/.*$
 ^build/.*$
 core$
+DS_Store$
 ^.c?project$
 ^.settings/.*$
--- a/osx-debug.mk	Thu Feb 28 08:50:24 2013 +0100
+++ b/osx-debug.mk	Fri Jun 21 10:27:03 2013 +0200
@@ -26,12 +26,12 @@
 # POSSIBILITY OF SUCH DAMAGE.
 #
 
-CC = gcc
-LD = gcc
+CC = cc
+LD = cc
 AR = ar
 RM = rm
 
-CFLAGS  = -std=gnu99 -g -c
+CFLAGS  = -g -c
 COFLAGS = -o
 LDFLAGS = 
 LOFLAGS = -o
--- a/osx.mk	Thu Feb 28 08:50:24 2013 +0100
+++ b/osx.mk	Fri Jun 21 10:27:03 2013 +0200
@@ -26,12 +26,12 @@
 # POSSIBILITY OF SUCH DAMAGE.
 #
 
-CC = gcc
-LD = gcc
+CC = cc
+LD = cc
 AR = ar
 RM = rm
 
-CFLAGS  = -std=gnu99 -O2 -c
+CFLAGS  = -O2 -c
 COFLAGS = -o
 LDFLAGS = 
 LOFLAGS = -o
--- a/test/Makefile	Thu Feb 28 08:50:24 2013 +0100
+++ b/test/Makefile	Fri Jun 21 10:27:03 2013 +0200
@@ -42,11 +42,11 @@
 all: ../build/test1
 
 ../build/test1: $(OBJ)
-	$(LD) $(LDFLAGS) $(LOFLAGS)../build/test$(APP_EXT) $(OBJ) \
+	$(LD) $(LDFLAGS) $(LOFLAGS) ../build/test$(APP_EXT) $(OBJ) \
 		../build/libucx.$(LIB_EXT)
 
 ../build/%.$(OBJ_EXT): %.c ../build
-	$(CC) $(CFLAGS) -I../ $(COFLAGS)$@ $<
+	$(CC) $(CFLAGS) -I../ $(COFLAGS) $@ $<
 
 ../build:
 	mkdir -p build
--- a/test/string_tests.c	Thu Feb 28 08:50:24 2013 +0100
+++ b/test/string_tests.c	Fri Jun 21 10:27:03 2013 +0200
@@ -194,10 +194,18 @@
 }
 
 UCX_TEST_IMPLEMENT(test_sstrtrim) {
-    sstr_t test = sstrtrim(sstr((char*)"  ein test   "));
+    sstr_t t1 = sstrtrim(sstr((char*)"  ein test   "));
+    sstr_t t2 = sstrtrim(sstr((char*)"abc"));
+    sstr_t t3 = sstrtrim(sstr((char*)" 123"));
+    sstr_t t4 = sstrtrim(sstr((char*)"xyz "));
+    sstr_t t5 = sstrtrim(sstr((char*)"   "));
     sstr_t empty = sstrtrim(sstr((char*)""));
     UCX_TEST_BEGIN
-    UCX_TEST_ASSERT(strncmp(test.ptr, "ein test", test.length) == 0, "failed");
+    UCX_TEST_ASSERT(strncmp(t1.ptr, "ein test", t1.length) == 0, "failed");
+    UCX_TEST_ASSERT(strncmp(t2.ptr, "abc", t2.length) == 0, "failed");
+    UCX_TEST_ASSERT(strncmp(t3.ptr, "123", t3.length) == 0, "failed");
+    UCX_TEST_ASSERT(strncmp(t4.ptr, "xyz", t4.length) == 0, "failed");
+    UCX_TEST_ASSERT(t5.length == 0, "string t5 not empty");
     UCX_TEST_ASSERT(empty.length == 0, "empty string failed");
     UCX_TEST_END
 }
--- a/ucx/Makefile	Thu Feb 28 08:50:24 2013 +0100
+++ b/ucx/Makefile	Fri Jun 21 10:27:03 2013 +0200
@@ -45,10 +45,10 @@
 all: libucx
 
 libucx: $(OBJ)
-	$(AR) $(ARFLAGS) $(AOFLAGS)../build/libucx.$(LIB_EXT) $(OBJ)
+	$(AR) $(ARFLAGS) $(AOFLAGS) ../build/libucx.$(LIB_EXT) $(OBJ)
 
 ../build/%.$(OBJ_EXT): %.c ../build
-	$(CC) $(CFLAGS) $(COFLAGS)$@ $<
+	$(CC) $(CFLAGS) $(COFLAGS) $@ $<
 
 ../build:
 	mkdir -p ../build
--- a/ucx/string.c	Thu Feb 28 08:50:24 2013 +0100
+++ b/ucx/string.c	Fri Jun 21 10:27:03 2013 +0200
@@ -95,7 +95,7 @@
 
 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
     sstr_t new_sstr;
-    if (start < 0 || start >= s.length || length < 0) {
+    if (start >= s.length) {
         return s;
     }
     if (length > s.length-start) {
@@ -197,11 +197,21 @@
     newstr.ptr = &string.ptr[i];
     newstr.length = string.length - i;
     
-    for(i=newstr.length-1;i>=0;i--) {
+    if(newstr.length == 0) {
+        return newstr;
+    }
+    
+    i = newstr.length - 1;
+    for(;;) {
         char c = newstr.ptr[i];
         if(c > 32) {
             break;
         }
+        if(i > 0) {
+            i--;
+        } else {
+            break;
+        }
     }
     newstr.length = i + 1;
     

mercurial