@@ -26,12 +26,13 @@ EscapeSeqDecode::EscapeSeqDecode(const std::string &action)
2626}
2727
2828
29- int EscapeSeqDecode:: ansi_c_sequences_decode_inplace (unsigned char *input,
30- int input_len) {
31- unsigned char *d = input ;
32- int i, count ;
29+ static inline int ansi_c_sequences_decode_inplace (std::string &value) {
30+ auto d = reinterpret_cast < unsigned char *>(value. data ());
31+ const unsigned char * input = d ;
32+ const auto input_len = value. length () ;
3333
34- i = count = 0 ;
34+ bool changed = false ;
35+ std::string::size_type i = 0 ;
3536 while (i < input_len) {
3637 if ((input[i] == ' \\ ' ) && (i + 1 < input_len)) {
3738 int c = -1 ;
@@ -109,42 +110,28 @@ int EscapeSeqDecode::ansi_c_sequences_decode_inplace(unsigned char *input,
109110 if (c == -1 ) {
110111 /* Didn't recognise encoding, copy raw bytes. */
111112 *d++ = input[i + 1 ];
112- count++;
113113 i += 2 ;
114114 } else {
115115 /* Converted the encoding. */
116116 *d++ = c;
117- count++;
118117 }
118+
119+ changed = true ;
119120 } else {
120121 /* Input character not a backslash, copy it. */
121122 *d++ = input[i++];
122- count++;
123123 }
124124 }
125125
126126 *d = ' \0 ' ;
127127
128- return count;
128+ value.resize (d - input);
129+ return changed;
129130}
130131
131132
132133bool EscapeSeqDecode::transform (std::string &value, const Transaction *trans) const {
133-
134- unsigned char *tmp = (unsigned char *) malloc (sizeof (char )
135- * value.size () + 1 );
136- memcpy (tmp, value.c_str (), value.size () + 1 );
137- tmp[value.size ()] = ' \0 ' ;
138-
139- int size = ansi_c_sequences_decode_inplace (tmp, value.size ());
140-
141- std::string ret (" " );
142- ret.assign (reinterpret_cast <char *>(tmp), size);
143- free (tmp);
144-
145- const auto changed = ret != value;
146- value = ret;
147- return changed;
134+ return ansi_c_sequences_decode_inplace (value);
148135}
149136
150137
0 commit comments