/* ========================= 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. ======================== */
/* 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