public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 2/5] Don't pre-size result string in ada_decode
Date: Mon, 28 Feb 2022 11:33:01 -0700	[thread overview]
Message-ID: <20220228183304.1162089-3-tromey@adacore.com> (raw)
In-Reply-To: <20220228183304.1162089-1-tromey@adacore.com>

Currently, ada_decode pre-sizes the output string, filling it with 'X'
characters.  However, it's a bit simpler and more flexible to let
std::string do the work here, and simply append characters to the
string as we go.  This turns out to be useful for a subsequent patch.
---
 gdb/ada-lang.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index d44b0906e6d..9a7ab72f0e5 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -1004,7 +1004,7 @@ remove_compiler_suffix (const char *encoded, int *len)
 std::string
 ada_decode (const char *encoded, bool wrap)
 {
-  int i, j;
+  int i;
   int len0;
   const char *p;
   int at_start_name;
@@ -1068,10 +1068,6 @@ ada_decode (const char *encoded, bool wrap)
   if (len0 > 1 && startswith (encoded + len0 - 1, "B"))
     len0 -= 1;
 
-  /* Make decoded big enough for possible expansion by operator name.  */
-
-  decoded.resize (2 * len0 + 1, 'X');
-
   /* Remove trailing __{digit}+ or trailing ${digit}+.  */
 
   if (len0 > 1 && isdigit (encoded[len0 - 1]))
@@ -1089,8 +1085,8 @@ ada_decode (const char *encoded, bool wrap)
   /* The first few characters that are not alphabetic are not part
      of any encoding we use, so we can copy them over verbatim.  */
 
-  for (i = 0, j = 0; i < len0 && !isalpha (encoded[i]); i += 1, j += 1)
-    decoded[j] = encoded[i];
+  for (i = 0; i < len0 && !isalpha (encoded[i]); i += 1)
+    decoded.push_back (encoded[i]);
 
   at_start_name = 1;
   while (i < len0)
@@ -1107,10 +1103,9 @@ ada_decode (const char *encoded, bool wrap)
 			    op_len - 1) == 0)
 		  && !isalnum (encoded[i + op_len]))
 		{
-		  strcpy (&decoded.front() + j, ada_opname_table[k].decoded);
+		  decoded.append (ada_opname_table[k].decoded);
 		  at_start_name = 0;
 		  i += op_len;
-		  j += strlen (ada_opname_table[k].decoded);
 		  break;
 		}
 	    }
@@ -1214,21 +1209,18 @@ ada_decode (const char *encoded, bool wrap)
       else if (i < len0 - 2 && encoded[i] == '_' && encoded[i + 1] == '_')
 	{
 	 /* Replace '__' by '.'.  */
-	  decoded[j] = '.';
+	  decoded.push_back ('.');
 	  at_start_name = 1;
 	  i += 2;
-	  j += 1;
 	}
       else
 	{
 	  /* It's a character part of the decoded name, so just copy it
 	     over.  */
-	  decoded[j] = encoded[i];
+	  decoded.push_back (encoded[i]);
 	  i += 1;
-	  j += 1;
 	}
     }
-  decoded.resize (j);
 
   /* Decoded names should never contain any uppercase character.
      Double-check this, and abort the decoding if we find one.  */
-- 
2.31.1


  parent reply	other threads:[~2022-02-28 18:33 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-28 18:32 [PATCH 0/5] Handle non-ASCII identifiers in Ada Tom Tromey
2022-02-28 18:33 ` [PATCH 1/5] Simplify a regular expression in ada-lex.l Tom Tromey
2022-02-28 18:33 ` Tom Tromey [this message]
2022-02-28 18:33 ` [PATCH 3/5] Let phex and phex_nz handle sizeof_l==1 Tom Tromey
2022-03-01 14:26   ` Simon Marchi
2022-03-01 14:32     ` Tom Tromey
2022-02-28 18:33 ` [PATCH 4/5] Define HOST_UTF32 in charset.h Tom Tromey
2022-02-28 18:33 ` [PATCH 5/5] Handle non-ASCII identifiers in Ada Tom Tromey
2022-02-28 18:59   ` Eli Zaretskii
2022-02-28 20:59     ` Tom Tromey
2022-03-01  3:28       ` Eli Zaretskii
2022-03-01 14:49         ` Tom Tromey
2022-03-01 15:17           ` Eli Zaretskii
2022-03-01 15:33   ` Tom Tromey
2022-03-07 14:52 ` [PATCH 0/5] " Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220228183304.1162089-3-tromey@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).