mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-01 12:24:14 +01:00
Import WString from 1.5.6
This commit is contained in:
parent
5bc28a178f
commit
8a1fffdb8e
@ -21,7 +21,6 @@
|
|||||||
|
|
||||||
#include "WString.h"
|
#include "WString.h"
|
||||||
|
|
||||||
|
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
/* Constructors */
|
/* Constructors */
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
@ -38,6 +37,12 @@ String::String(const String &value)
|
|||||||
*this = value;
|
*this = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String::String(const __FlashStringHelper *pstr)
|
||||||
|
{
|
||||||
|
init();
|
||||||
|
*this = pstr;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
String::String(String &&rval)
|
String::String(String &&rval)
|
||||||
{
|
{
|
||||||
@ -63,7 +68,7 @@ String::String(char c)
|
|||||||
String::String(unsigned char value, unsigned char base)
|
String::String(unsigned char value, unsigned char base)
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
char buf[9];
|
char buf[1 + 8 * sizeof(unsigned char)];
|
||||||
utoa(value, buf, base);
|
utoa(value, buf, base);
|
||||||
*this = buf;
|
*this = buf;
|
||||||
}
|
}
|
||||||
@ -71,7 +76,7 @@ String::String(unsigned char value, unsigned char base)
|
|||||||
String::String(int value, unsigned char base)
|
String::String(int value, unsigned char base)
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
char buf[18];
|
char buf[2 + 8 * sizeof(int)];
|
||||||
itoa(value, buf, base);
|
itoa(value, buf, base);
|
||||||
*this = buf;
|
*this = buf;
|
||||||
}
|
}
|
||||||
@ -79,7 +84,7 @@ String::String(int value, unsigned char base)
|
|||||||
String::String(unsigned int value, unsigned char base)
|
String::String(unsigned int value, unsigned char base)
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
char buf[17];
|
char buf[1 + 8 * sizeof(unsigned int)];
|
||||||
utoa(value, buf, base);
|
utoa(value, buf, base);
|
||||||
*this = buf;
|
*this = buf;
|
||||||
}
|
}
|
||||||
@ -87,7 +92,7 @@ String::String(unsigned int value, unsigned char base)
|
|||||||
String::String(long value, unsigned char base)
|
String::String(long value, unsigned char base)
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
char buf[34];
|
char buf[2 + 8 * sizeof(long)];
|
||||||
ltoa(value, buf, base);
|
ltoa(value, buf, base);
|
||||||
*this = buf;
|
*this = buf;
|
||||||
}
|
}
|
||||||
@ -95,7 +100,7 @@ String::String(long value, unsigned char base)
|
|||||||
String::String(unsigned long value, unsigned char base)
|
String::String(unsigned long value, unsigned char base)
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
char buf[33];
|
char buf[1 + 8 * sizeof(unsigned long)];
|
||||||
ultoa(value, buf, base);
|
ultoa(value, buf, base);
|
||||||
*this = buf;
|
*this = buf;
|
||||||
}
|
}
|
||||||
@ -113,6 +118,7 @@ String::String(double value, unsigned char decimalPlaces)
|
|||||||
char buf[33];
|
char buf[33];
|
||||||
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
|
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
String::~String()
|
String::~String()
|
||||||
{
|
{
|
||||||
free(buffer);
|
free(buffer);
|
||||||
@ -127,7 +133,6 @@ inline void String::init(void)
|
|||||||
buffer = NULL;
|
buffer = NULL;
|
||||||
capacity = 0;
|
capacity = 0;
|
||||||
len = 0;
|
len = 0;
|
||||||
flags = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void String::invalidate(void)
|
void String::invalidate(void)
|
||||||
@ -173,6 +178,17 @@ String & String::copy(const char *cstr, unsigned int length)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
|
||||||
|
{
|
||||||
|
if (!reserve(length)) {
|
||||||
|
invalidate();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
len = length;
|
||||||
|
strcpy_P(buffer, (PGM_P)pstr);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
void String::move(String &rhs)
|
void String::move(String &rhs)
|
||||||
{
|
{
|
||||||
@ -227,6 +243,14 @@ String & String::operator = (const char *cstr)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String & String::operator = (const __FlashStringHelper *pstr)
|
||||||
|
{
|
||||||
|
if (pstr) copy(pstr, strlen_P((PGM_P)pstr));
|
||||||
|
else invalidate();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
/* concat */
|
/* concat */
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
@ -263,35 +287,35 @@ unsigned char String::concat(char c)
|
|||||||
|
|
||||||
unsigned char String::concat(unsigned char num)
|
unsigned char String::concat(unsigned char num)
|
||||||
{
|
{
|
||||||
char buf[4];
|
char buf[1 + 3 * sizeof(unsigned char)];
|
||||||
itoa(num, buf, 10);
|
itoa(num, buf, 10);
|
||||||
return concat(buf, strlen(buf));
|
return concat(buf, strlen(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(int num)
|
unsigned char String::concat(int num)
|
||||||
{
|
{
|
||||||
char buf[7];
|
char buf[2 + 3 * sizeof(int)];
|
||||||
itoa(num, buf, 10);
|
itoa(num, buf, 10);
|
||||||
return concat(buf, strlen(buf));
|
return concat(buf, strlen(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(unsigned int num)
|
unsigned char String::concat(unsigned int num)
|
||||||
{
|
{
|
||||||
char buf[6];
|
char buf[1 + 3 * sizeof(unsigned int)];
|
||||||
utoa(num, buf, 10);
|
utoa(num, buf, 10);
|
||||||
return concat(buf, strlen(buf));
|
return concat(buf, strlen(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(long num)
|
unsigned char String::concat(long num)
|
||||||
{
|
{
|
||||||
char buf[12];
|
char buf[2 + 3 * sizeof(long)];
|
||||||
ltoa(num, buf, 10);
|
ltoa(num, buf, 10);
|
||||||
return concat(buf, strlen(buf));
|
return concat(buf, strlen(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(unsigned long num)
|
unsigned char String::concat(unsigned long num)
|
||||||
{
|
{
|
||||||
char buf[11];
|
char buf[1 + 3 * sizeof(unsigned long)];
|
||||||
ultoa(num, buf, 10);
|
ultoa(num, buf, 10);
|
||||||
return concat(buf, strlen(buf));
|
return concat(buf, strlen(buf));
|
||||||
}
|
}
|
||||||
@ -299,17 +323,29 @@ unsigned char String::concat(unsigned long num)
|
|||||||
unsigned char String::concat(float num)
|
unsigned char String::concat(float num)
|
||||||
{
|
{
|
||||||
char buf[20];
|
char buf[20];
|
||||||
char* string = dtostrf(num, 8, 6, buf);
|
char* string = dtostrf(num, 4, 2, buf);
|
||||||
return concat(string, strlen(string));
|
return concat(string, strlen(string));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char String::concat(double num)
|
unsigned char String::concat(double num)
|
||||||
{
|
{
|
||||||
char buf[20];
|
char buf[20];
|
||||||
char* string = dtostrf(num, 8, 6, buf);
|
char* string = dtostrf(num, 4, 2, buf);
|
||||||
return concat(string, strlen(string));
|
return concat(string, strlen(string));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned char String::concat(const __FlashStringHelper * str)
|
||||||
|
{
|
||||||
|
if (!str) return 0;
|
||||||
|
int length = strlen_P((const char *) str);
|
||||||
|
if (length == 0) return 1;
|
||||||
|
unsigned int newlen = len + length;
|
||||||
|
if (!reserve(newlen)) return 0;
|
||||||
|
strcpy_P(buffer + len, (const char *) str);
|
||||||
|
len = newlen;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
/* Concatenate */
|
/* Concatenate */
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
@ -383,6 +419,14 @@ StringSumHelper & operator + (const StringSumHelper &lhs, double num)
|
|||||||
if (!a.concat(num)) a.invalidate();
|
if (!a.concat(num)) a.invalidate();
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs)
|
||||||
|
{
|
||||||
|
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
|
||||||
|
if (!a.concat(rhs)) a.invalidate();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
/* Comparison */
|
/* Comparison */
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
@ -567,11 +611,6 @@ int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
|
|||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::substring( unsigned int left ) const
|
|
||||||
{
|
|
||||||
return substring(left, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
String String::substring(unsigned int left, unsigned int right) const
|
String String::substring(unsigned int left, unsigned int right) const
|
||||||
{
|
{
|
||||||
if (left > right) {
|
if (left > right) {
|
||||||
@ -698,7 +737,6 @@ long String::toInt(void) const
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
float String::toFloat(void) const
|
float String::toFloat(void) const
|
||||||
{
|
{
|
||||||
if (buffer) return float(atof(buffer));
|
if (buffer) return float(atof(buffer));
|
||||||
|
@ -58,6 +58,7 @@ public:
|
|||||||
// be false).
|
// be false).
|
||||||
String(const char *cstr = "");
|
String(const char *cstr = "");
|
||||||
String(const String &str);
|
String(const String &str);
|
||||||
|
String(const __FlashStringHelper *str);
|
||||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
String(String &&rval);
|
String(String &&rval);
|
||||||
String(StringSumHelper &&rval);
|
String(StringSumHelper &&rval);
|
||||||
@ -68,8 +69,8 @@ public:
|
|||||||
explicit String(unsigned int, unsigned char base=10);
|
explicit String(unsigned int, unsigned char base=10);
|
||||||
explicit String(long, unsigned char base=10);
|
explicit String(long, unsigned char base=10);
|
||||||
explicit String(unsigned long, unsigned char base=10);
|
explicit String(unsigned long, unsigned char base=10);
|
||||||
explicit String(float, unsigned char decimalPlaces=6);
|
explicit String(float, unsigned char decimalPlaces=2);
|
||||||
explicit String(double, unsigned char decimalPlaces=6);
|
explicit String(double, unsigned char decimalPlaces=2);
|
||||||
~String(void);
|
~String(void);
|
||||||
|
|
||||||
// memory management
|
// memory management
|
||||||
@ -84,6 +85,7 @@ public:
|
|||||||
// marked as invalid ("if (s)" will be false).
|
// marked as invalid ("if (s)" will be false).
|
||||||
String & operator = (const String &rhs);
|
String & operator = (const String &rhs);
|
||||||
String & operator = (const char *cstr);
|
String & operator = (const char *cstr);
|
||||||
|
String & operator = (const __FlashStringHelper *str);
|
||||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
String & operator = (String &&rval);
|
String & operator = (String &&rval);
|
||||||
String & operator = (StringSumHelper &&rval);
|
String & operator = (StringSumHelper &&rval);
|
||||||
@ -104,6 +106,7 @@ public:
|
|||||||
unsigned char concat(unsigned long num);
|
unsigned char concat(unsigned long num);
|
||||||
unsigned char concat(float num);
|
unsigned char concat(float num);
|
||||||
unsigned char concat(double num);
|
unsigned char concat(double num);
|
||||||
|
unsigned char concat(const __FlashStringHelper * str);
|
||||||
|
|
||||||
// if there's not enough memory for the concatenated value, the string
|
// if there's not enough memory for the concatenated value, the string
|
||||||
// will be left unchanged (but this isn't signalled in any way)
|
// will be left unchanged (but this isn't signalled in any way)
|
||||||
@ -115,6 +118,9 @@ public:
|
|||||||
String & operator += (unsigned int num) {concat(num); return (*this);}
|
String & operator += (unsigned int num) {concat(num); return (*this);}
|
||||||
String & operator += (long num) {concat(num); return (*this);}
|
String & operator += (long num) {concat(num); return (*this);}
|
||||||
String & operator += (unsigned long num) {concat(num); return (*this);}
|
String & operator += (unsigned long num) {concat(num); return (*this);}
|
||||||
|
String & operator += (float num) {concat(num); return (*this);}
|
||||||
|
String & operator += (double num) {concat(num); return (*this);}
|
||||||
|
String & operator += (const __FlashStringHelper *str){concat(str); return (*this);}
|
||||||
|
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
|
||||||
@ -126,6 +132,7 @@ public:
|
|||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
|
||||||
friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
|
||||||
|
friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
|
||||||
|
|
||||||
// comparison (only works w/ Strings and "strings")
|
// comparison (only works w/ Strings and "strings")
|
||||||
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
|
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
|
||||||
@ -164,7 +171,7 @@ public:
|
|||||||
int lastIndexOf( char ch, unsigned int fromIndex ) const;
|
int lastIndexOf( char ch, unsigned int fromIndex ) const;
|
||||||
int lastIndexOf( const String &str ) const;
|
int lastIndexOf( const String &str ) const;
|
||||||
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
|
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
|
||||||
String substring( unsigned int beginIndex ) const;
|
String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); };
|
||||||
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
|
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
|
||||||
|
|
||||||
// modification
|
// modification
|
||||||
@ -184,7 +191,6 @@ protected:
|
|||||||
char *buffer; // the actual char array
|
char *buffer; // the actual char array
|
||||||
unsigned int capacity; // the array length minus one (for the '\0')
|
unsigned int capacity; // the array length minus one (for the '\0')
|
||||||
unsigned int len; // the String length (not counting the '\0')
|
unsigned int len; // the String length (not counting the '\0')
|
||||||
unsigned char flags; // unused, for future features
|
|
||||||
protected:
|
protected:
|
||||||
void init(void);
|
void init(void);
|
||||||
void invalidate(void);
|
void invalidate(void);
|
||||||
@ -193,6 +199,7 @@ protected:
|
|||||||
|
|
||||||
// copy and move
|
// copy and move
|
||||||
String & copy(const char *cstr, unsigned int length);
|
String & copy(const char *cstr, unsigned int length);
|
||||||
|
String & copy(const __FlashStringHelper *pstr, unsigned int length);
|
||||||
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
#ifdef __GXX_EXPERIMENTAL_CXX0X__
|
||||||
void move(String &rhs);
|
void move(String &rhs);
|
||||||
#endif
|
#endif
|
||||||
@ -209,6 +216,8 @@ public:
|
|||||||
StringSumHelper(unsigned int num) : String(num) {}
|
StringSumHelper(unsigned int num) : String(num) {}
|
||||||
StringSumHelper(long num) : String(num) {}
|
StringSumHelper(long num) : String(num) {}
|
||||||
StringSumHelper(unsigned long num) : String(num) {}
|
StringSumHelper(unsigned long num) : String(num) {}
|
||||||
|
StringSumHelper(float num) : String(num) {}
|
||||||
|
StringSumHelper(double num) : String(num) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
|
Loading…
Reference in New Issue
Block a user