Skip to content

Commit e95b531

Browse files
committed
Few more fixes related to the String transition
1 parent 877fdc0 commit e95b531

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

libraries/BLE/examples/Beacon_Scanner/Beacon_Scanner.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
4242
String strManufacturerData = advertisedDevice.getManufacturerData();
4343

4444
uint8_t cManufacturerData[100];
45-
strManufacturerData.copy((char *)cManufacturerData, strManufacturerData.length(), 0);
45+
memcpy(cManufacturerData, strManufacturerData.c_str(), strManufacturerData.length());
4646

4747
if (strManufacturerData.length() == 25 && cManufacturerData[0] == 0x4C && cManufacturerData[1] == 0x00)
4848
{

libraries/BLE/src/BLEEddystoneURL.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,11 @@ int BLEEddystoneURL::setSmartURL(String url) {
248248
}
249249

250250
for(uint8_t i = 0; i < 0x0E; ++i){
251-
String std_url(url.c_str());
251+
std::string std_url(url.c_str());
252252
std::string std_suffix(EDDYSTONE_URL_SUFFIX[i].c_str());
253253
size_t found_pos = std_url.find(std_suffix);
254254
//log_d("check if in url \"%s\" can find suffix \"%s\": found_pos = %d", std_url.c_str(), std_suffix.c_str(), found_pos);
255-
if(found_pos != String::npos){
255+
if(found_pos != std::string::npos){
256256
hasSuffix = true;
257257
suffix = i;
258258
break;

libraries/BLE/src/GeneralUtils.cpp

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,24 @@ static void a4_to_a3(unsigned char* a3, unsigned char* a4) {
5656
* @param [out] out
5757
*/
5858
bool GeneralUtils::base64Encode(const String& in, String* out) {
59+
std::string std_in(in.c_str());
60+
std::string std_out(out->c_str());
5961
int i = 0, j = 0;
6062
size_t enc_len = 0;
6163
unsigned char a3[3];
6264
unsigned char a4[4];
65+
std_out.resize(base64EncodedLength(in));
6366

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();
6869

6970
while (input_len--) {
7071
a3[i++] = *(input++);
7172
if (i == 3) {
7273
a3_to_a4(a4, a3);
7374

7475
for (i = 0; i < 4; i++) {
75-
(*out)[enc_len++] = kBase64Alphabet[a4[i]];
76+
(std_out)[enc_len++] = kBase64Alphabet[a4[i]];
7677
}
7778

7879
i = 0;
@@ -87,15 +88,16 @@ bool GeneralUtils::base64Encode(const String& in, String* out) {
8788
a3_to_a4(a4, a3);
8889

8990
for (j = 0; j < i + 1; j++) {
90-
(*out)[enc_len++] = kBase64Alphabet[a4[j]];
91+
(std_out)[enc_len++] = kBase64Alphabet[a4[j]];
9192
}
9293

9394
while ((i++ < 3)) {
94-
(*out)[enc_len++] = '=';
95+
(std_out)[enc_len++] = '=';
9596
}
9697
}
98+
*out = String(std_out.c_str());
9799

98-
return (enc_len == out->size());
100+
return (enc_len == out->length());
99101
} // base64Encode
100102

101103

@@ -122,26 +124,27 @@ void GeneralUtils::dumpInfo() {
122124
* @return True if the string ends with the given character.
123125
*/
124126
bool GeneralUtils::endsWith(String str, char c) {
125-
if (str.empty()) {
127+
if (str.length() == 0) {
126128
return false;
127129
}
128-
if (str.at(str.length() - 1) == c) {
130+
if (str.charAt(str.length() - 1) == c) {
129131
return true;
130132
}
131133
return false;
132134
} // endsWidth
133135

134-
136+
/*
135137
static int DecodedLength(const String& in) {
136138
int numEq = 0;
137-
int n = (int) in.size();
139+
int n = (int) in.length();
138140
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) {
140143
++numEq;
141144
}
142145
return ((6 * n) / 8) - numEq;
143146
} // DecodedLength
144-
147+
*/
145148

146149
static unsigned char b64_lookup(unsigned char c) {
147150
if(c >='A' && c <='Z') return c - 'A';
@@ -164,17 +167,18 @@ bool GeneralUtils::base64Decode(const String& in, String* out) {
164167
unsigned char a3[3];
165168
unsigned char a4[4];
166169

167-
int input_len = in.size();
168-
String::const_iterator input = in.begin();
170+
int input_len = in.length();
171+
int input_iterator = 0;
169172

170-
out->resize(DecodedLength(in));
173+
//out->resize(DecodedLength(in));
171174

172175
while (input_len--) {
173-
if (*input == '=') {
176+
//if (*input == '=') {
177+
if (in[input_iterator] == '=') {
174178
break;
175179
}
176180

177-
a4[i++] = *(input++);
181+
a4[i++] = in[input_iterator++];
178182
if (i == 4) {
179183
for (i = 0; i <4; i++) {
180184
a4[i] = b64_lookup(a4[i]);
@@ -183,7 +187,8 @@ bool GeneralUtils::base64Decode(const String& in, String* out) {
183187
a4_to_a3(a3,a4);
184188

185189
for (i = 0; i < 3; i++) {
186-
(*out)[dec_len++] = a3[i];
190+
out->concat(a3[i]);
191+
dec_len++;
187192
}
188193

189194
i = 0;
@@ -206,7 +211,7 @@ bool GeneralUtils::base64Decode(const String& in, String* out) {
206211
}
207212
}
208213

209-
return (dec_len == out->size());
214+
return (dec_len == out->length());
210215
} // base64Decode
211216

212217
/*
@@ -351,13 +356,14 @@ std::vector<String> GeneralUtils::split(String source, char delimiter) {
351356
// See also: https://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g
352357
std::vector<String> strings;
353358
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)));
357363
previous = current + 1;
358-
current = source.find(delimiter, previous);
364+
current = std_source.find(delimiter, previous);
359365
}
360-
strings.push_back(trim(source.substr(previous, current - previous)));
366+
strings.push_back(trim(source.substring(previous, current)));
361367
return strings;
362368
} // split
363369

@@ -535,8 +541,9 @@ String GeneralUtils::toLower(String& value) {
535541
* @brief Remove white space from a string.
536542
*/
537543
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));
542549
} // trim

libraries/BLE/src/GeneralUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#ifndef COMPONENTS_CPP_UTILS_GENERALUTILS_H_
99
#define COMPONENTS_CPP_UTILS_GENERALUTILS_H_
10+
#include "Arduino.h"
1011
#include <stdint.h>
1112
#include <string>
1213
#include <esp_err.h>

0 commit comments

Comments
 (0)