From 0d1c55a06124640020baf3810d02183ac7e6a1a4 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 4 Nov 2018 20:23:25 +0100 Subject: [PATCH] xgettext: JavaScript: Make more use of mixed_string_buffer. * gettext-tools/src/x-javascript.c (struct unicode_string_buffer): Remove type. (init_unicode_string_buffer, unicode_string_buffer_append_unicode_grow, unicode_string_buffer_append_unicode, unicode_string_buffer_result, free_unicode_string_buffer): Remove functions. (comment_buffer, comment_start, comment_at_start, comment_add, comment_line_end): Use mixed_string_buffer API. --- gettext-tools/src/x-javascript.c | 83 +++----------------------------- 1 file changed, 7 insertions(+), 76 deletions(-) diff --git a/gettext-tools/src/x-javascript.c b/gettext-tools/src/x-javascript.c index d69157440..2879837c0 100644 --- a/gettext-tools/src/x-javascript.c +++ b/gettext-tools/src/x-javascript.c @@ -447,76 +447,7 @@ phase2_ungetc (int c) /* ========================= Accumulating strings. ======================== */ -/* A string buffer type that allows appending Unicode characters. - Returns the entire string in UTF-8 encoding. */ - -struct unicode_string_buffer -{ - /* The part of the string that has already been converted to UTF-8. */ - char *utf8_buffer; - size_t utf8_buflen; - size_t utf8_allocated; -}; - -/* Initialize a 'struct unicode_string_buffer' to empty. */ -static inline void -init_unicode_string_buffer (struct unicode_string_buffer *bp) -{ - bp->utf8_buffer = NULL; - bp->utf8_buflen = 0; - bp->utf8_allocated = 0; -} - -/* Auxiliary function: Ensure count more bytes are available in bp->utf8. */ -static inline void -unicode_string_buffer_append_unicode_grow (struct unicode_string_buffer *bp, - size_t count) -{ - if (bp->utf8_buflen + count > bp->utf8_allocated) - { - size_t new_allocated = 2 * bp->utf8_allocated + 10; - if (new_allocated < bp->utf8_buflen + count) - new_allocated = bp->utf8_buflen + count; - bp->utf8_allocated = new_allocated; - bp->utf8_buffer = xrealloc (bp->utf8_buffer, new_allocated); - } -} - -/* Auxiliary function: Append a Unicode character to bp->utf8. - uc must be < 0x110000. */ -static inline void -unicode_string_buffer_append_unicode (struct unicode_string_buffer *bp, - unsigned int uc) -{ - unsigned char utf8buf[6]; - int count = u8_uctomb (utf8buf, uc, 6); - - if (count < 0) - /* The caller should have ensured that uc is not out-of-range. */ - abort (); - - unicode_string_buffer_append_unicode_grow (bp, count); - memcpy (bp->utf8_buffer + bp->utf8_buflen, utf8buf, count); - bp->utf8_buflen += count; -} - -/* Return the string buffer's contents. */ -static char * -unicode_string_buffer_result (struct unicode_string_buffer *bp) -{ - /* NUL-terminate it. */ - unicode_string_buffer_append_unicode_grow (bp, 1); - bp->utf8_buffer[bp->utf8_buflen] = '\0'; - /* Return it. */ - return bp->utf8_buffer; -} - -/* Free the memory pointed to by a 'struct unicode_string_buffer'. */ -static inline void -free_unicode_string_buffer (struct unicode_string_buffer *bp) -{ - free (bp->utf8_buffer); -} +/* See xg-mixed-string.h for the API. */ /* ======================== Accumulating comments. ======================== */ @@ -524,31 +455,31 @@ free_unicode_string_buffer (struct unicode_string_buffer *bp) /* Accumulating a single comment line. */ -static struct unicode_string_buffer comment_buffer; +static struct mixed_string_buffer comment_buffer; static inline void comment_start () { - lexical_context = lc_comment; - comment_buffer.utf8_buflen = 0; + mixed_string_buffer_init (&comment_buffer, lc_comment, + logical_file_name, line_number); } static inline bool comment_at_start () { - return (comment_buffer.utf8_buflen == 0); + return mixed_string_buffer_is_empty (&comment_buffer); } static inline void comment_add (int c) { - unicode_string_buffer_append_unicode (&comment_buffer, c); + mixed_string_buffer_append_unicode (&comment_buffer, c); } static inline const char * comment_line_end (size_t chars_to_remove) { - char *buffer = unicode_string_buffer_result (&comment_buffer); + char *buffer = mixed_string_buffer_result (&comment_buffer); size_t buflen = strlen (buffer) - chars_to_remove; while (buflen >= 1 -- 2.47.2