]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: Python: Make more use of mixed_string_buffer.
authorBruno Haible <bruno@clisp.org>
Sun, 4 Nov 2018 19:23:18 +0000 (20:23 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 4 Nov 2018 23:25:34 +0000 (00:25 +0100)
* gettext-tools/src/x-python.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-python.c

index 5ab722f34dca988d3960d4c223288bab03c89e19..5109d569a3bb397332b69f65884eff5de1b8fab5 100644 (file)
@@ -501,76 +501,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.  ======================== */
@@ -578,31 +509,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 ()
 {
-  char *buffer = unicode_string_buffer_result (&comment_buffer);
+  char *buffer = mixed_string_buffer_result (&comment_buffer);
   size_t buflen = strlen (buffer);
 
   while (buflen >= 1