Inline the smaller string-handling functions
[exim.git] / src / src / string.c
index d0b8db4ae52bfa834b834c03aed9536364c4438e..fed210080971806fdc0b4641d5cb7de9ad3cab05 100644 (file)
@@ -37,7 +37,6 @@ Returns:    0 if the string is not a textual representation of an IP address
 int
 string_is_ip_address(const uschar *s, int *maskptr)
 {
-int i;
 int yield = 4;
 
 /* If an optional mask is permitted, check for it. If found, pass back the
@@ -60,7 +59,6 @@ if (Ustrchr(s, ':') != NULL)
   {
   BOOL had_double_colon = FALSE;
   BOOL v4end = FALSE;
-  int count = 0;
 
   yield = 6;
 
@@ -73,7 +71,7 @@ if (Ustrchr(s, ':') != NULL)
   may be one and only one appearance of double colon, which implies any number
   of binary zero bits. The number of preceding components is held in count. */
 
-  for (count = 0; count < 8; count++)
+  for (int count = 0; count < 8; count++)
     {
     /* If the end of the string is reached before reading 8 components, the
     address is valid provided a double colon has been read. This also applies
@@ -134,7 +132,7 @@ if (Ustrchr(s, ':') != NULL)
 
 /* Test for IPv4 address, which may be the tail-end of an IPv6 address. */
 
-for (i = 0; i < 4; i++)
+for (int i = 0; i < 4; i++)
   {
   long n;
   uschar * end;
@@ -409,6 +407,7 @@ return ss;
 
 
 
+#ifdef HAVE_LOCAL_SCAN
 /*************************************************
 *            Copy and save string                *
 *************************************************/
@@ -420,7 +419,7 @@ Returns:  copy of string in new store
 */
 
 uschar *
-string_copy(const uschar *s)
+string_copy_function(const uschar *s)
 {
 int len = Ustrlen(s) + 1;
 uschar *ss = store_get(len);
@@ -429,49 +428,6 @@ return ss;
 }
 
 
-
-/*************************************************
-*     Copy and save string in malloc'd store     *
-*************************************************/
-
-/* This function assumes that memcpy() is faster than strcpy().
-
-Argument: string to copy
-Returns:  copy of string in new store
-*/
-
-uschar *
-string_copy_malloc(const uschar *s)
-{
-int len = Ustrlen(s) + 1;
-uschar *ss = store_malloc(len);
-memcpy(ss, s, len);
-return ss;
-}
-
-
-
-/*************************************************
-*       Copy, lowercase and save string          *
-*************************************************/
-
-/*
-Argument: string to copy
-Returns:  copy of string in new store, with letters lowercased
-*/
-
-uschar *
-string_copylc(const uschar *s)
-{
-uschar *ss = store_get(Ustrlen(s) + 1);
-uschar *p = ss;
-while (*s != 0) *p++ = tolower(*s++);
-*p = 0;
-return ss;
-}
-
-
-
 /*************************************************
 *       Copy and save string, given length       *
 *************************************************/
@@ -487,36 +443,32 @@ Returns:    copy of string in new store
 */
 
 uschar *
-string_copyn(const uschar *s, int n)
+string_copyn_function(const uschar *s, int n)
 {
 uschar *ss = store_get(n + 1);
 Ustrncpy(ss, s, n);
 ss[n] = 0;
 return ss;
 }
+#endif
 
 
 /*************************************************
-* Copy, lowercase, and save string, given length *
+*     Copy and save string in malloc'd store     *
 *************************************************/
 
-/* It is assumed the data contains no zeros. A zero is added
-onto the end.
-
-Arguments:
-  s         string to copy
-  n         number of characters
+/* This function assumes that memcpy() is faster than strcpy().
 
-Returns:    copy of string in new store, with letters lowercased
+Argument: string to copy
+Returns:  copy of string in new store
 */
 
 uschar *
-string_copynlc(uschar *s, int n)
+string_copy_malloc(const uschar *s)
 {
-uschar *ss = store_get(n + 1);
-uschar *p = ss;
-while (n-- > 0) *p++ = tolower(*s++);
-*p = 0;
+int len = Ustrlen(s) + 1;
+uschar *ss = store_malloc(len);
+memcpy(ss, s, len);
 return ss;
 }
 
@@ -651,18 +603,16 @@ uschar *t, *yield;
 /* First find the end of the string */
 
 if (*s != '\"')
-  {
   while (*s != 0 && !isspace(*s)) s++;
-  }
 else
   {
   s++;
-  while (*s != 0 && *s != '\"')
+  while (*s && *s != '\"')
     {
     if (*s == '\\') (void)string_interpret_escape(&s);
     s++;
     }
-  if (*s != 0) s++;
+  if (*s) s++;
   }
 
 /* Get enough store to copy into */
@@ -740,7 +690,7 @@ if (!gp2)
 #ifdef COMPILE_UTILITY
 return string_copy(gp->s);
 #else
-gstring_reset_unused(gp);
+gstring_release_unused(gp);
 return gp->s;
 #endif
 }
@@ -907,7 +857,7 @@ int sep = *separator;
 const uschar *s = *listptr;
 BOOL sep_is_special;
 
-if (s == NULL) return NULL;
+if (!s) return NULL;
 
 /* This allows for a fixed specified separator to be an iscntrl() character,
 but at the time of implementation, this is never the case. However, it's best
@@ -923,19 +873,17 @@ if (sep <= 0)
   if (*s == '<' && (ispunct(s[1]) || iscntrl(s[1])))
     {
     sep = s[1];
-    s += 2;
+    if (*++s) ++s;
     while (isspace(*s) && *s != sep) s++;
     }
   else
-    {
-    sep = (sep == 0)? ':' : -sep;
-    }
+    sep = sep ? -sep : ':';
   *separator = sep;
   }
 
 /* An empty string has no list elements */
 
-if (*s == 0) return NULL;
+if (!*s) return NULL;
 
 /* Note whether whether or not the separator is an iscntrl() character. */
 
@@ -946,20 +894,19 @@ sep_is_special = iscntrl(sep);
 if (buffer)
   {
   int p = 0;
-  for (; *s != 0; s++)
+  for (; *s; s++)
     {
     if (*s == sep && (*(++s) != sep || sep_is_special)) break;
     if (p < buflen - 1) buffer[p++] = *s;
     }
   while (p > 0 && isspace(buffer[p-1])) p--;
-  buffer[p] = 0;
+  buffer[p] = '\0';
   }
 
 /* Handle the case when a buffer is not provided. */
 
 else
   {
-  const uschar *ss;
   gstring * g = NULL;
 
   /* We know that *s != 0 at this point. However, it might be pointing to a
@@ -982,14 +929,15 @@ else
 
   for (;;)
     {
-    for (ss = s + 1; *ss != 0 && *ss != sep; ss++) ;
+    const uschar * ss;
+    for (ss = s + 1; *ss && *ss != sep; ) ss++;
     g = string_catn(g, s, ss-s);
     s = ss;
-    if (*s == 0 || *(++s) != sep || sep_is_special) break;
+    if (!*s || *++s != sep || sep_is_special) break;
     }
   while (g->ptr > 0 && isspace(g->s[g->ptr-1])) g->ptr--;
   buffer = string_from_gstring(g);
-  gstring_reset_unused(g);
+  gstring_release_unused(g);
   }
 
 /* Update the current pointer and return the new string */
@@ -1099,35 +1047,6 @@ return list;
 
 
 /************************************************/
-/* Create a growable-string with some preassigned space */
-
-gstring *
-string_get(unsigned size)
-{
-gstring * g = store_get(sizeof(gstring) + size);
-g->size = size;
-g->ptr = 0;
-g->s = US(g + 1);
-return g;
-}
-
-/* NUL-terminate the C string in the growable-string, and return it. */
-
-uschar *
-string_from_gstring(gstring * g)
-{
-if (!g) return NULL;
-g->s[g->ptr] = '\0';
-return g->s;
-}
-
-void
-gstring_reset_unused(gstring * g)
-{
-store_reset(g->s + (g->size = g->ptr + 1));
-}
-
-
 /* Add more space to a growable-string.
 
 Arguments:
@@ -1358,7 +1277,11 @@ while (*fp)
     {
     /* Avoid string_copyn() due to COMPILE_UTILITY */
     if (g->ptr >= lim - 1)
-      if (extend) gstring_grow(g, g->ptr, 1); else return NULL;
+      {
+      if (!extend) return NULL;
+      gstring_grow(g, g->ptr, 1);
+      lim = g->size - 1;
+      }
     g->s[g->ptr++] = (uschar) *fp++;
     continue;
     }
@@ -1426,7 +1349,12 @@ while (*fp)
     case 'X':
       width = length > L_LONG ? 24 : 12;
       if (g->ptr >= lim - width)
-       if (extend) gstring_grow(g, g->ptr, width); else return NULL;
+       {
+       if (!extend) return NULL;
+       gstring_grow(g, g->ptr, width);
+       lim = g->size - 1;
+       gp = CS g->s + g->ptr;
+       }
       strncpy(newformat, item_start, fp - item_start);
       newformat[fp - item_start] = 0;
 
@@ -1451,7 +1379,12 @@ while (*fp)
       {
       void * ptr;
       if (g->ptr >= lim - 24)
-       if (extend) gstring_grow(g, g->ptr, 24); else return NULL;
+       {
+       if (!extend) return NULL;
+       gstring_grow(g, g->ptr, 24);
+       lim = g->size - 1;
+       gp = CS g->s + g->ptr;
+       }
       /* sprintf() saying "(nil)" for a null pointer seems unreliable.
       Handle it explicitly. */
       if ((ptr = va_arg(ap, void *)))
@@ -1479,7 +1412,12 @@ while (*fp)
     case 'G':
       if (precision < 0) precision = 6;
       if (g->ptr >= lim - precision - 8)
-       if (extend) gstring_grow(g, g->ptr, precision+8); else return NULL;
+       {
+       if (!extend) return NULL;
+       gstring_grow(g, g->ptr, precision+8);
+       lim = g->size - 1;
+       gp = CS g->s + g->ptr;
+       }
       strncpy(newformat, item_start, fp - item_start);
       newformat[fp-item_start] = 0;
       if (length == L_LONGDOUBLE)
@@ -1492,13 +1430,21 @@ while (*fp)
 
     case '%':
       if (g->ptr >= lim - 1)
-       if (extend) gstring_grow(g, g->ptr, 1); else return NULL;
+       {
+       if (!extend) return NULL;
+       gstring_grow(g, g->ptr, 1);
+       lim = g->size - 1;
+       }
       g->s[g->ptr++] = (uschar) '%';
       break;
 
     case 'c':
       if (g->ptr >= lim - 1)
-       if (extend) gstring_grow(g, g->ptr, 1); else return NULL;
+       {
+       if (!extend) return NULL;
+       gstring_grow(g, g->ptr, 1);
+       lim = g->size - 1;
+       }
       g->s[g->ptr++] = (uschar) va_arg(ap, int);
       break;
 
@@ -1563,7 +1509,11 @@ while (*fp)
          }
        }
       else if (g->ptr >= lim - width)
-       gstring_grow(g, g->ptr, width);
+       {
+       gstring_grow(g, g->ptr, width - (lim - g->ptr));
+       lim = g->size - 1;
+       gp = CS g->s + g->ptr;
+       }
 
       g->ptr += sprintf(gp, "%*.*s", width, precision, s);
       if (fp[-1] == 'S')
@@ -1637,7 +1587,7 @@ doesn't seem much we can do about that. */
 va_start(ap, format);
 (void) string_vformat(g, FALSE, format, ap);
 string_from_gstring(g);
-gstring_reset_unused(g);
+gstring_release_unused(g);
 va_end(ap);
 
 return eno == EACCES