Don't pre-size result string in ada_decode

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.
This commit is contained in:
Tom Tromey
2022-02-03 12:24:12 -07:00
parent a320f135dd
commit 36f5ca535d

View File

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