@@ -26,12 +26,13 @@ EscapeSeqDecode::EscapeSeqDecode(const std::string &action)
26
26
}
27
27
28
28
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 () ;
33
33
34
- i = count = 0 ;
34
+ bool changed = false ;
35
+ std::string::size_type i = 0 ;
35
36
while (i < input_len) {
36
37
if ((input[i] == ' \\ ' ) && (i + 1 < input_len)) {
37
38
int c = -1 ;
@@ -109,42 +110,28 @@ int EscapeSeqDecode::ansi_c_sequences_decode_inplace(unsigned char *input,
109
110
if (c == -1 ) {
110
111
/* Didn't recognise encoding, copy raw bytes. */
111
112
*d++ = input[i + 1 ];
112
- count++;
113
113
i += 2 ;
114
114
} else {
115
115
/* Converted the encoding. */
116
116
*d++ = c;
117
- count++;
118
117
}
118
+
119
+ changed = true ;
119
120
} else {
120
121
/* Input character not a backslash, copy it. */
121
122
*d++ = input[i++];
122
- count++;
123
123
}
124
124
}
125
125
126
126
*d = ' \0 ' ;
127
127
128
- return count;
128
+ value.resize (d - input);
129
+ return changed;
129
130
}
130
131
131
132
132
133
bool 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);
148
135
}
149
136
150
137
0 commit comments