Skip to content

Commit 4099263

Browse files
committed
Few more fixes related to the String transition
1 parent a77e3df commit 4099263

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

Diff for: libraries/BLE/examples/Beacon_Scanner/Beacon_Scanner.ino

+1-1
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
{

Diff for: libraries/BLE/src/BLEEddystoneURL.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,11 @@ int BLEEddystoneURL::setSmartURL(String url) {
245245
}
246246

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

Diff for: libraries/BLE/src/GeneralUtils.cpp

+37-30
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,24 @@ static void a4_to_a3(unsigned char* a3, unsigned char* a4) {
5555
* @param [out] out
5656
*/
5757
bool GeneralUtils::base64Encode(const String& in, String* out) {
58+
std::string std_in(in.c_str());
59+
std::string std_out(out->c_str());
5860
int i = 0, j = 0;
5961
size_t enc_len = 0;
6062
unsigned char a3[3];
6163
unsigned char a4[4];
64+
std_out.resize(base64EncodedLength(in));
6265

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

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

7374
for (i = 0; i < 4; i++) {
74-
(*out)[enc_len++] = kBase64Alphabet[a4[i]];
75+
(std_out)[enc_len++] = kBase64Alphabet[a4[i]];
7576
}
7677

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

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

9293
while ((i++ < 3)) {
93-
(*out)[enc_len++] = '=';
94+
(std_out)[enc_len++] = '=';
9495
}
9596
}
97+
*out = String(std_out.c_str());
9698

97-
return (enc_len == out->size());
99+
return (enc_len == out->length());
98100
} // base64Encode
99101

100102

@@ -121,26 +123,27 @@ void GeneralUtils::dumpInfo() {
121123
* @return True if the string ends with the given character.
122124
*/
123125
bool GeneralUtils::endsWith(String str, char c) {
124-
if (str.empty()) {
126+
if (str.length() == 0) {
125127
return false;
126128
}
127-
if (str.at(str.length() - 1) == c) {
129+
if (str.charAt(str.length() - 1) == c) {
128130
return true;
129131
}
130132
return false;
131133
} // endsWidth
132134

133-
135+
/*
134136
static int DecodedLength(const String& in) {
135137
int numEq = 0;
136-
int n = (int) in.size();
138+
int n = (int) in.length();
137139
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) {
139142
++numEq;
140143
}
141144
return ((6 * n) / 8) - numEq;
142145
} // DecodedLength
143-
146+
*/
144147

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

166-
int input_len = in.size();
167-
String::const_iterator input = in.begin();
169+
int input_len = in.length();
170+
int input_iterator = 0;
168171

169-
out->resize(DecodedLength(in));
172+
//out->resize(DecodedLength(in));
170173

171174
while (input_len--) {
172-
if (*input == '=') {
175+
//if (*input == '=') {
176+
if (in[input_iterator] == '=') {
173177
break;
174178
}
175179

176-
a4[i++] = *(input++);
180+
a4[i++] = in[input_iterator++];
177181
if (i == 4) {
178182
for (i = 0; i <4; i++) {
179183
a4[i] = b64_lookup(a4[i]);
@@ -182,7 +186,8 @@ bool GeneralUtils::base64Decode(const String& in, String* out) {
182186
a4_to_a3(a3,a4);
183187

184188
for (i = 0; i < 3; i++) {
185-
(*out)[dec_len++] = a3[i];
189+
out->concat(a3[i]);
190+
dec_len++;
186191
}
187192

188193
i = 0;
@@ -205,7 +210,7 @@ bool GeneralUtils::base64Decode(const String& in, String* out) {
205210
}
206211
}
207212

208-
return (dec_len == out->size());
213+
return (dec_len == out->length());
209214
} // base64Decode
210215

211216
/*
@@ -350,13 +355,14 @@ std::vector<String> GeneralUtils::split(String source, char delimiter) {
350355
// See also: https://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g
351356
std::vector<String> strings;
352357
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)));
356362
previous = current + 1;
357-
current = source.find(delimiter, previous);
363+
current = std_source.find(delimiter, previous);
358364
}
359-
strings.push_back(trim(source.substr(previous, current - previous)));
365+
strings.push_back(trim(source.substring(previous, current)));
360366
return strings;
361367
} // split
362368

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

Diff for: libraries/BLE/src/GeneralUtils.h

+1
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)