[v3,7/8] Rewrite Pascal character printer

Message ID 20251228-string-printing-2-v3-7-ec5cc9dd0c71@tromey.com
State New
Headers
Series Refactor character printing |

Commit Message

Tom Tromey Dec. 28, 2025, 8:13 p.m. UTC
  This adds a Pascal-specific subclass of wchar_printer and arranges to
use it.
---
 gdb/p-lang.c | 137 ++++++++---------------------------------------------------
 gdb/p-lang.h |  17 --------
 2 files changed, 18 insertions(+), 136 deletions(-)
  

Patch

diff --git a/gdb/p-lang.c b/gdb/p-lang.c
index 1dc1a34663cbcf2d11f44f1ffbbbfb8d8bd6ea0d..72c6e30e6ffec455c4626143ea3abfb4151918fc 100644
--- a/gdb/p-lang.c
+++ b/gdb/p-lang.c
@@ -33,6 +33,7 @@ 
 #include "c-lang.h"
 #include "gdbarch.h"
 #include "cli/cli-style.h"
+#include "char-print.h"
 
 /* All GPC versions until now (2007-09-27) also define a symbol called
    '_p_initialize'.  Check for the presence of this symbol first.  */
@@ -144,32 +145,20 @@  pascal_is_string_type (struct type *type,int *length_pos, int *length_size,
   return 0;
 }
 
-/* See p-lang.h.  */
-
-void
-pascal_language::print_one_char (int c, struct ui_file *stream,
-				 int *in_quotes) const
+class pascal_wchar_printer : public wchar_printer
 {
-  if (c == '\'' || ((unsigned int) c <= 0xff && (PRINT_LITERAL_FORM (c))))
-    {
-      if (!(*in_quotes))
-	gdb_puts ("'", stream);
-      *in_quotes = 1;
-      if (c == '\'')
-	{
-	  gdb_puts ("''", stream);
-	}
-      else
-	gdb_printf (stream, "%c", c);
-    }
-  else
-    {
-      if (*in_quotes)
-	gdb_puts ("'", stream);
-      *in_quotes = 0;
-      gdb_printf (stream, "#%d", (unsigned int) c);
-    }
-}
+public:
+
+  using wchar_printer::wchar_printer;
+
+  void print_char (gdb_wchar_t w) override
+  {
+    if (w == LCST ('\''))
+      m_file.write (LCST ("''"));
+    else
+      wchar_printer::print_char (w);
+  }
+};
 
 /* See language.h.  */
 
@@ -177,11 +166,8 @@  void
 pascal_language::printchar (int c, struct type *type,
 			    struct ui_file *stream) const
 {
-  int in_quotes = 0;
-
-  print_one_char (c, stream, &in_quotes);
-  if (in_quotes)
-    gdb_puts ("'", stream);
+  pascal_wchar_printer printer (type, '\'');
+  printer.print (c, stream);
 }
 
 
@@ -229,95 +215,8 @@  pascal_language::printstr (struct ui_file *stream, struct type *elttype,
 			   const char *encoding, int force_ellipses,
 			   const struct value_print_options *options) const
 {
-  enum bfd_endian byte_order = type_byte_order (elttype);
-  unsigned int i;
-  unsigned int things_printed = 0;
-  int in_quotes = 0;
-  int need_comma = 0;
-  int width;
-
-  /* Preserve ELTTYPE's original type, just set its LENGTH.  */
-  check_typedef (elttype);
-  width = elttype->length ();
-
-  /* If the string was not truncated due to `set print elements', and
-     the last byte of it is a null, we don't print that, in traditional C
-     style.  */
-  if ((!force_ellipses) && length > 0
-      && extract_unsigned_integer (string + (length - 1) * width, width,
-				   byte_order) == 0)
-    length--;
-
-  if (length == 0)
-    {
-      gdb_puts ("''", stream);
-      return;
-    }
-
-  unsigned int print_max_chars = get_print_max_chars (options);
-  for (i = 0; i < length && things_printed < print_max_chars; ++i)
-    {
-      /* Position of the character we are examining
-	 to see whether it is repeated.  */
-      unsigned int rep1;
-      /* Number of repetitions we have detected so far.  */
-      unsigned int reps;
-      unsigned long int current_char;
-
-      QUIT;
-
-      if (need_comma)
-	{
-	  gdb_puts (", ", stream);
-	  need_comma = 0;
-	}
-
-      current_char = extract_unsigned_integer (string + i * width, width,
-					       byte_order);
-
-      rep1 = i + 1;
-      reps = 1;
-      while (rep1 < length
-	     && extract_unsigned_integer (string + rep1 * width, width,
-					  byte_order) == current_char)
-	{
-	  ++rep1;
-	  ++reps;
-	}
-
-      if (reps > options->repeat_count_threshold)
-	{
-	  if (in_quotes)
-	    {
-	      gdb_puts ("', ", stream);
-	      in_quotes = 0;
-	    }
-	  printchar (current_char, elttype, stream);
-	  gdb_printf (stream, " %p[<repeats %u times>%p]",
-		      metadata_style.style ().ptr (),
-		      reps, nullptr);
-	  i = rep1 - 1;
-	  things_printed += options->repeat_count_threshold;
-	  need_comma = 1;
-	}
-      else
-	{
-	  if ((!in_quotes) && (PRINT_LITERAL_FORM (current_char)))
-	    {
-	      gdb_puts ("'", stream);
-	      in_quotes = 1;
-	    }
-	  print_one_char (current_char, stream, &in_quotes);
-	  ++things_printed;
-	}
-    }
-
-  /* Terminate the quotes if necessary.  */
-  if (in_quotes)
-    gdb_puts ("'", stream);
-
-  if (force_ellipses || i < length)
-    gdb_puts ("...", stream);
+  pascal_wchar_printer printer (elttype, '\'', encoding);
+  printer.print (stream, string, length, force_ellipses, 0, options);
 }
 
 /* Single instance of the Pascal language class.  */
diff --git a/gdb/p-lang.h b/gdb/p-lang.h
index ab8e4bfa7f654240646e2b54dec212fc65a638a6..4b1c0a3a70d42b4ed947305853b20dcc9b49bf1c 100644
--- a/gdb/p-lang.h
+++ b/gdb/p-lang.h
@@ -109,18 +109,6 @@  class pascal_language : public language_defn
 
   /* See language.h.  */
 
-  void emitchar (int ch, struct type *chtype,
-		 struct ui_file *stream, int quoter) const
-  {
-    int in_quotes = 0;
-
-    print_one_char (ch, stream, &in_quotes);
-    if (in_quotes)
-      gdb_puts ("'", stream);
-  }
-
-  /* See language.h.  */
-
   void printchar (int ch, struct type *chtype,
 		  struct ui_file *stream) const override;
 
@@ -159,11 +147,6 @@  class pascal_language : public language_defn
 
 private:
 
-  /* Print the character C on STREAM as part of the contents of a literal
-     string.  IN_QUOTES is reset to 0 if a char is written with #4 notation.  */
-
-  void print_one_char (int c, struct ui_file *stream, int *in_quotes) const;
-
   /* Print the name of the type (or the ultimate pointer target,
      function value or array element), or the description of a
      structure or union.