Skip to content

Commit d15959a

Browse files
authored
Merge pull request #11 from Jason2866/opt_wstring
Opt wstring
2 parents f18a251 + 4f94abc commit d15959a

File tree

4 files changed

+69
-116
lines changed

4 files changed

+69
-116
lines changed

cores/esp8266/WString.cpp

+34-78
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
Copyright 2011, Paul Stoffregen, [email protected]
66
Modified by Ivan Grokhotkov, 2014 - esp8266 support
77
Modified by Michael C. Miller, 2015 - esp8266 progmem support
8-
8+
99
This library is free software; you can redistribute it and/or
1010
modify it under the terms of the GNU Lesser General Public
1111
License as published by the Free Software Foundation; either
1212
version 2.1 of the License, or (at your option) any later version.
13-
1413
This library is distributed in the hope that it will be useful,
1514
but WITHOUT ANY WARRANTY; without even the implied warranty of
1615
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1716
Lesser General Public License for more details.
18-
17+
1918
You should have received a copy of the GNU Lesser General Public
2019
License along with this library; if not, write to the Free Software
2120
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
@@ -45,17 +44,15 @@ String::String(const __FlashStringHelper *pstr) {
4544
*this = pstr; // see operator =
4645
}
4746

48-
#ifdef __GXX_EXPERIMENTAL_CXX0X__
49-
String::String(String &&rval) {
47+
String::String(String &&rval) noexcept {
5048
init();
5149
move(rval);
5250
}
5351

54-
String::String(StringSumHelper &&rval) {
52+
String::String(StringSumHelper &&rval) noexcept {
5553
init();
5654
move(rval);
5755
}
58-
#endif
5956

6057
String::String(char c) {
6158
init();
@@ -120,19 +117,9 @@ String::String(double value, unsigned char decimalPlaces) {
120117
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
121118
}
122119

123-
String::~String() {
124-
invalidate();
125-
}
126-
127-
// /*********************************************/
128-
// /* Memory Management */
129-
// /*********************************************/
130-
131-
inline void String::init(void) {
132-
setSSO(true);
133-
setLen(0);
134-
wbuffer()[0] = 0;
135-
}
120+
/*********************************************/
121+
/* Memory Management */
122+
/*********************************************/
136123

137124
void String::invalidate(void) {
138125
if(!isSSO() && wbuffer())
@@ -199,9 +186,9 @@ unsigned char String::changeBuffer(unsigned int maxStrLen) {
199186
return 0;
200187
}
201188

202-
// /*********************************************/
203-
// /* Copy and Move */
204-
// /*********************************************/
189+
/*********************************************/
190+
/* Copy and Move */
191+
/*********************************************/
205192

206193
String & String::copy(const char *cstr, unsigned int length) {
207194
if (!reserve(length)) {
@@ -223,36 +210,11 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length) {
223210
return *this;
224211
}
225212

226-
#ifdef __GXX_EXPERIMENTAL_CXX0X__
227-
void String::move(String &rhs) {
228-
if (buffer()) {
229-
if (capacity() >= rhs.len()) {
230-
memmove_P(wbuffer(), rhs.buffer(), rhs.length() + 1);
231-
setLen(rhs.len());
232-
rhs.invalidate();
233-
return;
234-
} else {
235-
if (!isSSO()) {
236-
free(wbuffer());
237-
setBuffer(nullptr);
238-
}
239-
}
240-
}
241-
if (rhs.isSSO()) {
242-
setSSO(true);
243-
memmove_P(sso.buff, rhs.sso.buff, sizeof(sso.buff));
244-
} else {
245-
setSSO(false);
246-
setBuffer(rhs.wbuffer());
247-
}
248-
setCapacity(rhs.capacity());
249-
setLen(rhs.len());
250-
rhs.setSSO(false);
251-
rhs.setCapacity(0);
252-
rhs.setLen(0);
253-
rhs.setBuffer(nullptr);
213+
void String::move(String &rhs) noexcept {
214+
invalidate();
215+
sso = rhs.sso;
216+
rhs.init();
254217
}
255-
#endif
256218

257219
String & String::operator =(const String &rhs) {
258220
if (this == &rhs)
@@ -266,19 +228,17 @@ String & String::operator =(const String &rhs) {
266228
return *this;
267229
}
268230

269-
#ifdef __GXX_EXPERIMENTAL_CXX0X__
270-
String & String::operator =(String &&rval) {
231+
String & String::operator =(String &&rval) noexcept {
271232
if (this != &rval)
272233
move(rval);
273234
return *this;
274235
}
275236

276-
String & String::operator =(StringSumHelper &&rval) {
237+
String & String::operator =(StringSumHelper &&rval) noexcept {
277238
if (this != &rval)
278239
move(rval);
279240
return *this;
280241
}
281-
#endif
282242

283243
String & String::operator =(const char *cstr) {
284244
if (cstr)
@@ -297,9 +257,9 @@ String & String::operator = (const __FlashStringHelper *pstr)
297257
return *this;
298258
}
299259

300-
// /*********************************************/
301-
// /* concat */
302-
// /*********************************************/
260+
/*********************************************/
261+
/* concat */
262+
/*********************************************/
303263

304264
unsigned char String::concat(const String &s) {
305265
// Special case if we're concatting ourself (s += s;) since we may end up
@@ -483,9 +443,9 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHel
483443
return a;
484444
}
485445

486-
// /*********************************************/
487-
// /* Comparison */
488-
// /*********************************************/
446+
/*********************************************/
447+
/* Comparison */
448+
/*********************************************/
489449

490450
int String::compareTo(const String &s) const {
491451
if(!buffer() || !s.buffer()) {
@@ -587,13 +547,9 @@ unsigned char String::endsWith(const String &s2) const {
587547
return strcmp(&buffer()[len() - s2.len()], s2.buffer()) == 0;
588548
}
589549

590-
// /*********************************************/
591-
// /* Character Access */
592-
// /*********************************************/
593-
594-
char String::charAt(unsigned int loc) const {
595-
return operator[](loc);
596-
}
550+
/*********************************************/
551+
/* Character Access */
552+
/*********************************************/
597553

598554
void String::setCharAt(unsigned int loc, char c) {
599555
if (loc < len())
@@ -629,9 +585,9 @@ void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int ind
629585
buf[n] = 0;
630586
}
631587

632-
// /*********************************************/
633-
// /* Search */
634-
// /*********************************************/
588+
/*********************************************/
589+
/* Search */
590+
/*********************************************/
635591

636592
int String::indexOf(char c) const {
637593
return indexOf(c, 0);
@@ -713,9 +669,9 @@ String String::substring(unsigned int left, unsigned int right) const {
713669
return out;
714670
}
715671

716-
// /*********************************************/
717-
// /* Modification */
718-
// /*********************************************/
672+
/*********************************************/
673+
/* Modification */
674+
/*********************************************/
719675

720676
void String::replace(char find, char replace) {
721677
if (!buffer())
@@ -828,9 +784,9 @@ void String::trim(void) {
828784
wbuffer()[newlen] = 0;
829785
}
830786

831-
// /*********************************************/
832-
// /* Parsing / Conversion */
833-
// /*********************************************/
787+
/*********************************************/
788+
/* Parsing / Conversion */
789+
/*********************************************/
834790

835791
long String::toInt(void) const {
836792
if (buffer())

cores/esp8266/WString.h

+30-25
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
...mostly rewritten by Paul Stoffregen...
44
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
55
Copyright 2011, Paul Stoffregen, [email protected]
6-
6+
77
This library is free software; you can redistribute it and/or
88
modify it under the terms of the GNU Lesser General Public
99
License as published by the Free Software Foundation; either
1010
version 2.1 of the License, or (at your option) any later version.
11-
11+
1212
This library is distributed in the hope that it will be useful,
1313
but WITHOUT ANY WARRANTY; without even the implied warranty of
1414
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1515
Lesser General Public License for more details.
16-
16+
1717
You should have received a copy of the GNU Lesser General Public
1818
License along with this library; if not, write to the Free Software
1919
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
@@ -53,13 +53,14 @@ class String {
5353
// if the initial value is null or invalid, or if memory allocation
5454
// fails, the string will be marked as invalid (i.e. "if (s)" will
5555
// be false).
56-
String(const char *cstr = nullptr);
56+
String() {
57+
init();
58+
}
59+
String(const char *cstr);
5760
String(const String &str);
5861
String(const __FlashStringHelper *str);
59-
#ifdef __GXX_EXPERIMENTAL_CXX0X__
60-
String(String &&rval);
61-
String(StringSumHelper &&rval);
62-
#endif
62+
String(String &&rval) noexcept;
63+
String(StringSumHelper &&rval) noexcept;
6364
explicit String(char c);
6465
explicit String(unsigned char, unsigned char base = 10);
6566
explicit String(int, unsigned char base = 10);
@@ -68,7 +69,9 @@ class String {
6869
explicit String(unsigned long, unsigned char base = 10);
6970
explicit String(float, unsigned char decimalPlaces = 2);
7071
explicit String(double, unsigned char decimalPlaces = 2);
71-
~String(void);
72+
~String() {
73+
invalidate();
74+
}
7275

7376
// memory management
7477
// return true on success, false on failure (in which case, the string
@@ -95,10 +98,8 @@ class String {
9598
String & operator =(const String &rhs);
9699
String & operator =(const char *cstr);
97100
String & operator = (const __FlashStringHelper *str);
98-
#ifdef __GXX_EXPERIMENTAL_CXX0X__
99-
String & operator =(String &&rval);
100-
String & operator =(StringSumHelper &&rval);
101-
#endif
101+
String & operator =(String &&rval) noexcept;
102+
String & operator =(StringSumHelper &&rval) noexcept;
102103

103104
// concatenate (works w/ built-in types)
104105

@@ -219,7 +220,9 @@ class String {
219220
}
220221

221222
// character access
222-
char charAt(unsigned int index) const;
223+
char charAt(unsigned int index) const {
224+
return operator[](index);
225+
}
223226
void setCharAt(unsigned int index, char c);
224227
char operator [](unsigned int index) const;
225228
char& operator [](unsigned int index);
@@ -275,11 +278,11 @@ class String {
275278
// parsing/conversion
276279
long toInt(void) const;
277280
float toFloat(void) const;
278-
double toDouble(void) const;
281+
double toDouble(void) const;
279282

280283
protected:
281284
// Contains the string info when we're not in SSO mode
282-
struct _ptr {
285+
struct _ptr {
283286
char * buff;
284287
uint16_t cap;
285288
uint16_t len;
@@ -288,37 +291,39 @@ class String {
288291
enum { SSOSIZE = sizeof(struct _ptr) + 4 - 1 }; // Characters to allocate space for SSO, must be 12 or more
289292
struct _sso {
290293
char buff[SSOSIZE];
291-
unsigned char len : 7; // Ensure only one byte is allocated by GCC for the bitfields
292-
unsigned char isSSO : 1;
294+
unsigned char len : 7; // Ensure only one byte is allocated by GCC for the bitfields
295+
unsigned char isHeap : 1;
293296
} __attribute__((packed)); // Ensure that GCC doesn't expand the flag byte to a 32-bit word for alignment issues
294297
enum { CAPACITY_MAX = 65535 }; // If typeof(cap) changed from uint16_t, be sure to update this enum to the max value storable in the type
295298
union {
296299
struct _ptr ptr;
297300
struct _sso sso;
298301
};
299302
// Accessor functions
300-
inline bool isSSO() const { return sso.isSSO; }
303+
inline bool isSSO() const { return !sso.isHeap; }
301304
inline unsigned int len() const { return isSSO() ? sso.len : ptr.len; }
302305
inline unsigned int capacity() const { return isSSO() ? (unsigned int)SSOSIZE - 1 : ptr.cap; } // Size of max string not including terminal NUL
303-
inline void setSSO(bool set) { sso.isSSO = set; }
306+
inline void setSSO(bool set) { sso.isHeap = !set; }
304307
inline void setLen(int len) { if (isSSO()) sso.len = len; else ptr.len = len; }
305308
inline void setCapacity(int cap) { if (!isSSO()) ptr.cap = cap; }
306-
inline void setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; }
309+
inline void setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; }
307310
// Buffer accessor functions
308311
inline const char *buffer() const { return (const char *)(isSSO() ? sso.buff : ptr.buff); }
309312
inline char *wbuffer() const { return isSSO() ? const_cast<char *>(sso.buff) : ptr.buff; } // Writable version of buffer
310313

311314
protected:
312-
void init(void);
315+
void init(void) {
316+
sso.isHeap = 0;
317+
sso.len = 0;
318+
sso.buff[0] = 0;
319+
}
313320
void invalidate(void);
314321
unsigned char changeBuffer(unsigned int maxStrLen);
315322

316323
// copy and move
317324
String & copy(const char *cstr, unsigned int length);
318325
String & copy(const __FlashStringHelper *pstr, unsigned int length);
319-
#ifdef __GXX_EXPERIMENTAL_CXX0X__
320-
void move(String &rhs);
321-
#endif
326+
void move(String &rhs) noexcept;
322327
};
323328

324329
class StringSumHelper: public String {

cores/esp8266/core_version.h

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
#ifndef ARDUINO_ESP8266_GIT_VER
2-
#define ARDUINO_ESP8266_GIT_VER 0x00000000
3-
#endif
4-
5-
#ifndef ARDUINO_ESP8266_GIT_DESC
6-
#define ARDUINO_ESP8266_GIT_DESC unspecified
7-
#endif
8-
9-
// ARDUINO_ESP8266_RELEASE is defined for released versions as a string containing the version name, i.e. "2_3_0_RC1"
10-
// ARDUINO_ESP8266_RELEASE is used in the core internally. Please use ESP.getCoreVersion() function instead.
11-
12-
// ARDUINO_ESP8266_RELEASE_<version number> are defined for releases, for use in #ifdef... constructs
1+
#define ARDUINO_ESP8266_GIT_VER 0x2843a5ac
2+
#define ARDUINO_ESP8266_GIT_DESC 2.7.3-3-g2843a5ac
3+
#define ARDUINO_ESP8266_RELEASE_2_7_4_5
4+
#define ARDUINO_ESP8266_RELEASE "2_7_4_5"

cores/esp8266/libb64/cencode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ int base64_encode_blockend(char* code_out, base64_encodestate* state_in){
117117

118118
int base64_encode_chars(const char* plaintext_in, int length_in, char* code_out){
119119
base64_encodestate _state;
120-
base64_init_encodestate(&_state);
120+
base64_init_encodestate_nonewlines(&_state);
121121
int len = base64_encode_block(plaintext_in, length_in, code_out, &_state);
122122
return len + base64_encode_blockend((code_out + len), &_state);
123123
}

0 commit comments

Comments
 (0)