about summary refs log tree commit diff
path: root/support/support_quote_blob.c
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2021-10-12 13:48:39 +0000
committerJoseph Myers <joseph@codesourcery.com>2021-10-12 13:48:39 +0000
commitde82cb0da4b8fa5b3d56c457438d2568c67ab1b1 (patch)
tree396836ad8aa0a466b0cc8a379c6933b972780ce9 /support/support_quote_blob.c
parent4912c738fcbc6def723370ec3a7ab4a732361322 (diff)
downloadglibc-de82cb0da4b8fa5b3d56c457438d2568c67ab1b1.tar.gz
glibc-de82cb0da4b8fa5b3d56c457438d2568c67ab1b1.tar.xz
glibc-de82cb0da4b8fa5b3d56c457438d2568c67ab1b1.zip
Add TEST_COMPARE_STRING_WIDE to support/check.h
I'd like to be able to test narrow and wide string interfaces, with
the narrow string tests using TEST_COMPARE_STRING and the wide string
tests using something analogous (possibly generated using macros from
a common test template for both the narrow and wide string tests where
appropriate).

Add such a TEST_COMPARE_STRING_WIDE, along with functions
support_quote_blob_wide and support_test_compare_string_wide that it
builds on.  Those functions are built using macros from common
templates shared by the narrow and wide string implementations, though
I didn't do that for the tests of test functions.  In
support_quote_blob_wide, I chose to use the \x{} delimited escape
sequence syntax proposed for C2X in N2785, rather than e.g. trying to
generate the end of a string and the start of a new string when
ambiguity would result from undelimited \x (when the next character
after such an escape sequence is valid hex) or forcing an escape
sequence to be used for the next character in the case of such
ambiguity.

Tested for x86_64.
Diffstat (limited to 'support/support_quote_blob.c')
-rw-r--r--support/support_quote_blob.c71
1 files changed, 6 insertions, 65 deletions
diff --git a/support/support_quote_blob.c b/support/support_quote_blob.c
index b5e70125f1..611980c9a2 100644
--- a/support/support_quote_blob.c
+++ b/support/support_quote_blob.c
@@ -1,4 +1,4 @@
-/* Quote a blob so that it can be used in C literals.
+/* Quote a narrow string blob so that it can be used in C literals.
    Copyright (C) 2018-2021 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
@@ -16,68 +16,9 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <support/support.h>
-#include <support/xmemstream.h>
+#define CHAR unsigned char
+#define L_(C) C
+#define SUPPORT_QUOTE_BLOB support_quote_blob
+#define WIDE 0
 
-char *
-support_quote_blob (const void *blob, size_t length)
-{
-  struct xmemstream out;
-  xopen_memstream (&out);
-
-  const unsigned char *p = blob;
-  for (size_t i = 0; i < length; ++i)
-    {
-      unsigned char ch = p[i];
-
-      /* Use C backslash escapes for those control characters for
-         which they are defined.  */
-      switch (ch)
-        {
-          case '\a':
-            putc_unlocked ('\\', out.out);
-            putc_unlocked ('a', out.out);
-            break;
-          case '\b':
-            putc_unlocked ('\\', out.out);
-            putc_unlocked ('b', out.out);
-            break;
-          case '\f':
-            putc_unlocked ('\\', out.out);
-            putc_unlocked ('f', out.out);
-            break;
-          case '\n':
-            putc_unlocked ('\\', out.out);
-            putc_unlocked ('n', out.out);
-            break;
-          case '\r':
-            putc_unlocked ('\\', out.out);
-            putc_unlocked ('r', out.out);
-            break;
-          case '\t':
-            putc_unlocked ('\\', out.out);
-            putc_unlocked ('t', out.out);
-            break;
-          case '\v':
-            putc_unlocked ('\\', out.out);
-            putc_unlocked ('v', out.out);
-            break;
-          case '\\':
-          case '\'':
-          case '\"':
-            putc_unlocked ('\\', out.out);
-            putc_unlocked (ch, out.out);
-            break;
-        default:
-          if (ch < ' ' || ch > '~')
-            /* Use octal sequences because they are fixed width,
-               unlike hexadecimal sequences.  */
-            fprintf (out.out, "\\%03o", ch);
-          else
-            putc_unlocked (ch, out.out);
-        }
-    }
-
-  xfclose_memstream (&out);
-  return out.buffer;
-}
+#include "support_quote_blob_main.c"