From 0408dbc8812c0bc845f55934a7e7f9fb74b3c4aa Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sun, 4 Jul 2021 09:43:09 -0700 Subject: [PATCH] lexer: Move lex_ellipsize() into string module, as str_ellipsize(). --- src/language/lexer/lexer.c | 37 +------------------------------------ src/libpspp/str.c | 33 +++++++++++++++++++++++++++++++++ src/libpspp/str.h | 4 ++++ 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/language/lexer/lexer.c b/src/language/lexer/lexer.c index 4467042e14..21309f5f97 100644 --- a/src/language/lexer/lexer.c +++ b/src/language/lexer/lexer.c @@ -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 diff --git a/src/libpspp/str.c b/src/libpspp/str.c index e06f626571..99602fba68 100644 --- a/src/libpspp/str.c +++ b/src/libpspp/str.c @@ -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 * diff --git a/src/libpspp/str.h b/src/libpspp/str.h index 7917d58a70..57783e0ea5 100644 --- a/src/libpspp/str.h +++ b/src/libpspp/str.h @@ -34,6 +34,8 @@ /* 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); -- 2.30.2