lexer: Move lex_ellipsize() into string module, as str_ellipsize().
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 4 Jul 2021 16:43:09 +0000 (09:43 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 5 Jul 2021 01:22:12 +0000 (18:22 -0700)
src/language/lexer/lexer.c
src/libpspp/str.c
src/libpspp/str.h

index 4467042e1464bc91195eb37a5fd7281ad94f74eb..21309f5f975f688693e0ea09534cd750cf29dab9 100644 (file)
@@ -1369,41 +1369,6 @@ lex_source_get_syntax__ (const struct lex_source *src, int n0, int n1)
                                   lex_source_next__ (src, MAX (n0, n1)));
 }
 
-static void
-lex_ellipsize__ (struct substring in, char *out, size_t out_size)
-{
-  size_t out_maxlen;
-  size_t out_len;
-  int mblen;
-
-  assert (out_size >= 16);
-  out_maxlen = out_size - 1;
-  if (in.length > out_maxlen - 3)
-    out_maxlen -= 3;
-
-  for (out_len = 0; out_len < in.length; out_len += mblen)
-    {
-      if (in.string[out_len] == '\n'
-          || in.string[out_len] == '\0'
-          || (in.string[out_len] == '\r'
-              && out_len + 1 < in.length
-              && in.string[out_len + 1] == '\n'))
-        break;
-
-      mblen = u8_mblen (CHAR_CAST (const uint8_t *, in.string + out_len),
-                        in.length - out_len);
-
-      if (mblen < 0)
-        break;
-
-      if (out_len + mblen > out_maxlen)
-        break;
-    }
-
-  memcpy (out, in.string, out_len);
-  strcpy (&out[out_len], out_len < in.length ? "..." : "");
-}
-
 static void
 lex_source_error_valist (struct lex_source *src, int n0, int n1,
                          const char *format, va_list args)
@@ -1423,7 +1388,7 @@ lex_source_error_valist (struct lex_source *src, int n0, int n1,
         {
           char syntax_cstr[64];
 
-          lex_ellipsize__ (syntax, syntax_cstr, sizeof syntax_cstr);
+          str_ellipsize (syntax, syntax_cstr, sizeof syntax_cstr);
           ds_put_format (&s, _("Syntax error at `%s'"), syntax_cstr);
         }
       else
index e06f6265712a0a8399050615431e6c6536fbf83f..99602fba681e699146f2ddb4922b2e90ac1caadc 100644 (file)
@@ -301,6 +301,39 @@ overflow:
   return false;
 }
 
+/* Copies IN to buffer OUT with size OUT_SIZE, appending a null terminator.  If
+   IN is too long for OUT, or if IN contains a new-line, replaces the tail with
+   "...".
+
+   OUT_SIZE must be at least 16. */
+void
+str_ellipsize (struct substring in, char *out, size_t out_size)
+{
+  assert (out_size >= 16);
+
+  size_t out_maxlen = out_size - 1;
+  if (in.length > out_maxlen - 3)
+    out_maxlen -= 3;
+
+  size_t out_len = 0;
+  while (out_len < in.length
+         && in.string[out_len] != '\n'
+         && in.string[out_len] != '\0'
+         && (in.string[out_len] != '\r'
+             || out_len + 1 >= in.length
+             || in.string[out_len + 1] != '\n'))
+    {
+      int mblen = u8_mblen (CHAR_CAST (const uint8_t *, in.string + out_len),
+                            in.length - out_len);
+      if (mblen < 0 || out_len + mblen > out_maxlen)
+        break;
+      out_len += mblen;
+    }
+
+  memcpy (out, in.string, out_len);
+  strcpy (&out[out_len], out_len < in.length ? "..." : "");
+}
+
 /* Sets the SIZE bytes starting at BLOCK to C,
    and returns the byte following BLOCK. */
 void *
index 7917d58a70dd3a62e305ff84c6884aa85e7061cb..57783e0ea57a7615aa31315f07160d91be978caf 100644 (file)
@@ -34,6 +34,8 @@
 \f
 /* Miscellaneous. */
 
+struct substring;
+
 void buf_reverse (char *, size_t);
 int buf_compare_case (const char *, const char *, size_t);
 int buf_compare_rpad (const char *, size_t, const char *, size_t);
@@ -52,6 +54,8 @@ void str_lowercase (char *);
 bool str_format_26adic (unsigned long int number, bool uppercase,
                         char buffer[], size_t);
 
+void str_ellipsize (struct substring in, char *out, size_t out_size);
+
 static inline char *xstrdup_if_nonnull (const char *);
 
 void *mempset (void *, int, size_t);