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)
{
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
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 *
\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);
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);