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