]> andersk Git - splint.git/blobdiff - src/cstring.c
Fixed bug reproted by Jim Francis. Bug was triggered by running splint on typedef...
[splint.git] / src / cstring.c
index 134d6e1841ba1dcba6f45581b0db53768578ddac..04fa8a299c9d642ff4b7d198678b5e026e9463ab 100644 (file)
@@ -17,8 +17,8 @@
 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 ** MA 02111-1307, USA.
 **
-** For information on splint: splint@cs.virginia.edu
-** To report a bug: splint-bug@cs.virginia.edu
+** For information on splint: info@splint.org
+** To report a bug: splint-bug@splint.org
 ** For more information: http://www.splint.org
 */
 /*
@@ -212,11 +212,11 @@ void cstring_replaceAll (cstring s, char old, char snew)
           }
 }
 
-void cstring_replaceLit (/*@unique@*/ cstring s, char *old, char *snew) /*@requires maxRead(snew) >= 0 /\ maxRead(old) >= 0 /\ maxRead(old) >= maxRead(snew) @*/
+void cstring_replaceLit (/*@unique@*/ cstring s, char *old, char *snew) 
+   /*@requires maxRead(snew) >= 0 /\ maxRead(old) >= 0 /\ maxRead(old) >= maxRead(snew) @*/
 {
-  
   llassert (strlen (old) >= strlen (snew));
-
+  
   if (cstring_isDefined (s))
     {
       char *sp = strstr (s, old);
@@ -225,12 +225,15 @@ void cstring_replaceLit (/*@unique@*/ cstring s, char *old, char *snew) /*@requi
        {
          int lendiff = size_toInt (strlen (old) - strlen (snew));
          char *tsnew = snew;
-         
+
+         llassert (lendiff >= 0);
+
          while (*tsnew != '\0')
            {
+             llassert (*sp != '\0');
              *sp++ = *tsnew++;
            }
-
+         
          if (lendiff > 0)
            {
              while (*(sp + lendiff) != '\0')
@@ -897,6 +900,174 @@ extern /*@observer@*/ cstring cstring_advanceWhiteSpace (cstring s)
   
   return cstring_undefined;
 }
-    
+
+static mstring doExpandEscapes (cstring s, /*@out@*/ int * len)
+{
+  char *ptr;
+  mstring ret;
+  char * retPtr;
+
+  
+  llassert(cstring_isDefined (s));
+  
+  ret = mstring_create (cstring_length(s) );
+
+  ptr = s;
+
+  retPtr = ret;
+  while (*ptr != '\0')
+    {
+      if (*ptr != '\\')
+       {
+         *retPtr = *ptr;
+         retPtr++;
+         ptr++;
+         continue;
+       }
+      
+      if (*ptr == '\\')
+       {
+         ptr++;
+         if (*ptr == '\0')
+           {
+             /*not a legal escape sequence but try to handle it in a sesible way*/
+             *retPtr = '\\';
+             retPtr++;
+           }
+         
+         /* Handle Octal escapes  */
+         else if (*ptr >= '0' && *ptr <= '9' )
+           {
+             int total;
+             total = (int)(*ptr - '0');
+             ptr++;
+             /*octal can only be 3 characters long */
+             if (*ptr != '\0' &&  (*ptr >= '0' && *ptr <= '9' ) )
+               {
+                 total *= 8;
+                 ptr++;
+                 if (*ptr != '\0' &&  (*ptr >= '0' && *ptr <= '9' ) )
+                   {
+                     total *= 8;
+                     total += (int) (*ptr - '0');
+                     ptr++;
+                   }
+               }
+             
+             *retPtr =  (char) total;
+             retPtr++;
+           }
+         
+         else if (*ptr == 'x')
+           {
+             int total;
+             total = 0;
+             ptr++;
+             if (!(*ptr != '\0' &&
+                   ( (*ptr >= '0' && *ptr <= '9' ) ||
+                     (toupper(*ptr) >= (int)('A') && toupper(*ptr) <= (int)('F') ) )
+                     ))
+               {
+                 total = (int)'x';
+               }
+             else
+               {
+                 while (*ptr != '\0' &&
+                   ( (*ptr >= '0' && *ptr <= '9' ) ||
+                     (toupper(*ptr) >= ((int)('A')) && toupper(*ptr) <= ((int)'F') ) )
+                        )
+                   {
+                     total *= 16;
+                     if (*ptr >= '0' && *ptr <= '9' )
+                       total += (int)(*ptr - '0');
+                     else
+                       total += ( (toupper(*ptr) - 'A') + 10);
+                     ptr++;
+                   }
+               }
+             *retPtr =  (char) total;
+             retPtr++;
+           }
+         else
+           {
+             switch ( *ptr )
+               {
+               case 'a':
+                 *retPtr = '\a';
+                 retPtr++;
+                 /*@switchbreak@*/ break;
+
+               case 'b':
+                 *retPtr = '\b';
+                 retPtr++;
+                 /*@switchbreak@*/ break;
+
+               case 'f':
+                 *retPtr = '\f';
+                 retPtr++;
+                 /*@switchbreak@*/ break;
+
+               case 'n':
+                 *retPtr = '\n';
+                 retPtr++;
+                 /*@switchbreak@*/ break;
+
+               case 'r':
+                 *retPtr = '\r';
+                 retPtr++;
+                 /*@switchbreak@*/ break;
+
+               case 't':
+                 *retPtr = '\t';
+                 retPtr++;
+                 /*@switchbreak@*/ break;
+                 /* ' " ? \ */
+                 /* we assume invalid sequences are handled somewhere else
+                    so we handle an invalid sequence of the form \char by replacing
+                    it with char (this is what gcc does) the C standard says a diagnostic is
+                    required..*/
+               default:
+                 *retPtr = *ptr;
+                 retPtr++;
+               }
+             ptr++;
+           }
+         
+       }/*end outer if*/
+      
+    }/*end while */
+
+  /* add the null character */
+  *retPtr = '\0';
+
+  *len = retPtr - ret;
+  return ret;
+}
+
+
+/*this function is like sctring_expandEscapses */
+mstring cstring_expandEscapes (cstring s)
+{
+  int len;
+
+  mstring ret;
+  
+  ret = doExpandEscapes (s, &len);
+  return ret;
+}
+
+int  cstring_lengthExpandEscapes (cstring s)
+{
+  int len;
+
+  mstring tmpStr;
+  
+  tmpStr = doExpandEscapes (s, &len);
+
+  cstring_free(tmpStr);
+
+  return len;
+}
+
 
 
This page took 0.043038 seconds and 4 git commands to generate.