[jbossnative-commits] JBoss Native SVN: r962 - trunk/sight/native/share.

jbossnative-commits at lists.jboss.org jbossnative-commits at lists.jboss.org
Mon Sep 3 02:38:58 EDT 2007


Author: mladen.turk at jboss.com
Date: 2007-09-03 02:38:58 -0400 (Mon, 03 Sep 2007)
New Revision: 962

Modified:
   trunk/sight/native/share/jnu.c
Log:
Make trim functions return NULL for empty trimmed strings.

Modified: trunk/sight/native/share/jnu.c
===================================================================
--- trunk/sight/native/share/jnu.c	2007-09-03 06:37:05 UTC (rev 961)
+++ trunk/sight/native/share/jnu.c	2007-09-03 06:38:58 UTC (rev 962)
@@ -121,20 +121,30 @@
     size_t i;
     /* check for empty strings */
     if (!s || !*s)
-        return s;
-    for (i = strlen(s) - 1; i >= 0 && apr_isspace(s[i]); i--)
+        return NULL;
+    for (i = strlen(s) - 1; i >= 0 && (apr_iscntrl(s[i]) ||
+         apr_isspace(s[i])); i--)
         ;
     s[i + 1] = '\0';
-    return s;
+    if (!s[0])
+        return NULL;
+    else
+        return s;
 }
 
 char *sight_ltrim(char *s)
 {
     size_t i;
+    /* check for empty strings */
+    if (!s || !*s)
+        return NULL;
     for (i = 0; s[i] != '\0' && apr_isspace(s[i]); i++)
         ;
 
-    return s + i;
+    if (!s[i])
+        return NULL;
+    else
+        return s + i;
 }
 
 char *sight_trim(char *s)
@@ -142,14 +152,21 @@
     size_t i;
     /* check for empty strings */
     if (!s || !*s)
-        return s;
-    for (i = strlen(s) - 1; i >= 0 && apr_isspace(s[i]); i--)
+        return NULL;
+    for (i = strlen(s) - 1; i >= 0 && (apr_iscntrl(s[i]) ||
+         apr_isspace(s[i])); i--)
         ;
     s[i + 1] = '\0';
+    /* Don't check if the sting is empty */
+    if (!s[0])
+        return NULL;
     for (i = 0; s[i] != '\0' && apr_isspace(s[i]); i++)
         ;
 
-    return s + i;
+    if (!s[i])
+        return NULL;
+    else
+        return s + i;
 }
 
 
@@ -157,9 +174,9 @@
 {
     size_t i;
     /* check for empty strings */
-    if (!(i = strlen(s)))
+    if (!s || !(i = strlen(s)))
         return 0;
-    if (s[i-1] == chr)
+    if (s[i - 1] == chr)
         return -1;
     else
         return 0;
@@ -207,8 +224,10 @@
     if ((b = malloc(rd))) {
         rd = fread(b, 1, rd - 2, f);
 
-        if (rd > 0) {
-            for (i = rd - 1; i >= 0 && apr_isspace(b[i]); i--)
+        if (rd > 0) {            
+            /* Remove all trailing zero and space characters */
+            for (rd = i - 1; i >= 0 && (apr_iscntrl(b[i]) ||
+                 apr_isspace(b[i])); i--)
                 ;
             b[i + 1] = '\0';
             b[i + 2] = '\0';
@@ -547,12 +566,12 @@
      *  Copy the input (bytewise) array into a wordwise array.
      *  Find the longest run of 0x00's in src[] for :: shorthanding.
      */
-    next_src = src;
-    src_end = src + IN6ADDRSZ;
+    next_src  = src;
+    src_end   = src + IN6ADDRSZ;
     next_dest = words;
     best.base = -1;
-    cur.base = -1;
-    cur.len = best.len = 0; /* silence gcc4 warning */
+    cur.base  = -1;
+    cur.len   = best.len = 0; /* silence gcc4 warning */
     i = 0;
     do {
         unsigned int next_word = (unsigned int)*next_src++;
@@ -760,7 +779,7 @@
     while (fgets(&buf[0], 8192, file)) {
         char *pline = sight_trim(&buf[0]);
         /* Skip empty lines */
-        if (*pline) {
+        if (pline) {
             if (sight_arr_add(array, pline))
                 break;
         }
@@ -787,7 +806,7 @@
     while (fgets(&buf[0], 8192, file)) {
         char *pline = sight_trim(&buf[0]);
         /* Skip empty lines */
-        if (*pline) {
+        if (pline) {
             /* Skip comments */
             int    ic = 0;
             size_t ci;




More information about the jbossnative-commits mailing list