@@ -56,23 +56,24 @@ static void a4_to_a3(unsigned char* a3, unsigned char* a4) {
56
56
* @param [out] out
57
57
*/
58
58
bool GeneralUtils::base64Encode (const String& in, String* out) {
59
+ std::string std_in (in.c_str ());
60
+ std::string std_out (out->c_str ());
59
61
int i = 0 , j = 0 ;
60
62
size_t enc_len = 0 ;
61
63
unsigned char a3[3 ];
62
64
unsigned char a4[4 ];
65
+ std_out.resize (base64EncodedLength (in));
63
66
64
- out->resize (base64EncodedLength (in));
65
-
66
- int input_len = in.size ();
67
- String::const_iterator input = in.begin ();
67
+ int input_len = std_in.length ();
68
+ std::string::const_iterator input = std_in.begin ();
68
69
69
70
while (input_len--) {
70
71
a3[i++] = *(input++);
71
72
if (i == 3 ) {
72
73
a3_to_a4 (a4, a3);
73
74
74
75
for (i = 0 ; i < 4 ; i++) {
75
- (*out )[enc_len++] = kBase64Alphabet [a4[i]];
76
+ (std_out )[enc_len++] = kBase64Alphabet [a4[i]];
76
77
}
77
78
78
79
i = 0 ;
@@ -87,15 +88,16 @@ bool GeneralUtils::base64Encode(const String& in, String* out) {
87
88
a3_to_a4 (a4, a3);
88
89
89
90
for (j = 0 ; j < i + 1 ; j++) {
90
- (*out )[enc_len++] = kBase64Alphabet [a4[j]];
91
+ (std_out )[enc_len++] = kBase64Alphabet [a4[j]];
91
92
}
92
93
93
94
while ((i++ < 3 )) {
94
- (*out )[enc_len++] = ' =' ;
95
+ (std_out )[enc_len++] = ' =' ;
95
96
}
96
97
}
98
+ *out = String (std_out.c_str ());
97
99
98
- return (enc_len == out->size ());
100
+ return (enc_len == out->length ());
99
101
} // base64Encode
100
102
101
103
@@ -122,26 +124,27 @@ void GeneralUtils::dumpInfo() {
122
124
* @return True if the string ends with the given character.
123
125
*/
124
126
bool GeneralUtils::endsWith (String str, char c) {
125
- if (str.empty () ) {
127
+ if (str.length () == 0 ) {
126
128
return false ;
127
129
}
128
- if (str.at (str.length () - 1 ) == c) {
130
+ if (str.charAt (str.length () - 1 ) == c) {
129
131
return true ;
130
132
}
131
133
return false ;
132
134
} // endsWidth
133
135
134
-
136
+ /*
135
137
static int DecodedLength(const String& in) {
136
138
int numEq = 0;
137
- int n = (int ) in.size ();
139
+ int n = (int) in.length ();
138
140
139
- for (String::const_reverse_iterator it = in.rbegin (); *it == ' =' ; ++it) {
141
+ //for (String::const_reverse_iterator it = in.rbegin(); *it == '='; ++it) {
142
+ for (int it = in.length()-1; in.charAt(it) == '='; --it) {
140
143
++numEq;
141
144
}
142
145
return ((6 * n) / 8) - numEq;
143
146
} // DecodedLength
144
-
147
+ */
145
148
146
149
static unsigned char b64_lookup (unsigned char c) {
147
150
if (c >=' A' && c <=' Z' ) return c - ' A' ;
@@ -164,17 +167,18 @@ bool GeneralUtils::base64Decode(const String& in, String* out) {
164
167
unsigned char a3[3 ];
165
168
unsigned char a4[4 ];
166
169
167
- int input_len = in.size ();
168
- String::const_iterator input = in. begin () ;
170
+ int input_len = in.length ();
171
+ int input_iterator = 0 ;
169
172
170
- out->resize (DecodedLength (in));
173
+ // out->resize(DecodedLength(in));
171
174
172
175
while (input_len--) {
173
- if (*input == ' =' ) {
176
+ // if (*input == '=') {
177
+ if (in[input_iterator] == ' =' ) {
174
178
break ;
175
179
}
176
180
177
- a4[i++] = *(input++) ;
181
+ a4[i++] = in[input_iterator++] ;
178
182
if (i == 4 ) {
179
183
for (i = 0 ; i <4 ; i++) {
180
184
a4[i] = b64_lookup (a4[i]);
@@ -183,7 +187,8 @@ bool GeneralUtils::base64Decode(const String& in, String* out) {
183
187
a4_to_a3 (a3,a4);
184
188
185
189
for (i = 0 ; i < 3 ; i++) {
186
- (*out)[dec_len++] = a3[i];
190
+ out->concat (a3[i]);
191
+ dec_len++;
187
192
}
188
193
189
194
i = 0 ;
@@ -206,7 +211,7 @@ bool GeneralUtils::base64Decode(const String& in, String* out) {
206
211
}
207
212
}
208
213
209
- return (dec_len == out->size ());
214
+ return (dec_len == out->length ());
210
215
} // base64Decode
211
216
212
217
/*
@@ -351,13 +356,14 @@ std::vector<String> GeneralUtils::split(String source, char delimiter) {
351
356
// See also: https://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g
352
357
std::vector<String> strings;
353
358
std::size_t current, previous = 0 ;
354
- current = source.find (delimiter);
355
- while (current != String::npos) {
356
- strings.push_back (trim (source.substr (previous, current - previous)));
359
+ std::string std_source (source.c_str ());
360
+ current = std_source.find (delimiter);
361
+ while (current != std::string::npos) {
362
+ strings.push_back (trim (source.substring (previous, current)));
357
363
previous = current + 1 ;
358
- current = source .find (delimiter, previous);
364
+ current = std_source .find (delimiter, previous);
359
365
}
360
- strings.push_back (trim (source.substr (previous, current - previous )));
366
+ strings.push_back (trim (source.substring (previous, current)));
361
367
return strings;
362
368
} // split
363
369
@@ -535,8 +541,9 @@ String GeneralUtils::toLower(String& value) {
535
541
* @brief Remove white space from a string.
536
542
*/
537
543
String GeneralUtils::trim (const String& str) {
538
- size_t first = str.find_first_not_of (' ' );
539
- if (String::npos == first) return str;
540
- size_t last = str.find_last_not_of (' ' );
541
- return str.substr (first, (last - first + 1 ));
544
+ std::string std_str (str.c_str ());
545
+ size_t first = std_str.find_first_not_of (' ' );
546
+ if (std::string::npos == first) return str;
547
+ size_t last = std_str.find_last_not_of (' ' );
548
+ return str.substring (first, (last + 1 ));
542
549
} // trim
0 commit comments