From e009c5c6c61da7dcf2683a75e7b17ea4fb3c6e2d Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Tue, 1 Mar 2011 19:52:13 -0500 Subject: Renamed WProgram.h to Arduino.h. --- cores/arduino/WString.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index cadddb9..ff671d8 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -20,7 +20,7 @@ #ifndef String_h #define String_h -//#include "WProgram.h" +//#include "Arduino.h" #include #include #include -- cgit v1.2.3-18-g5258 From 438bca3cb270b0d882b6b3b0bc42d57d72307c76 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Fri, 11 Mar 2011 17:56:10 -0500 Subject: Rewrite of the String class by Paul Stoffregen. http://www.pjrc.com/teensy/string_class_experimental.html --- cores/arduino/WString.h | 243 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 162 insertions(+), 81 deletions(-) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index ff671d8..541a118 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -1,6 +1,8 @@ /* WString.h - String library for Wiring & Arduino + ...mostly rewritten by Paul Stoffregen... Copyright (c) 2009-10 Hernando Barragan. All right reserved. + Copyright 2011, Paul Stoffregen, paul@pjrc.com This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -17,96 +19,175 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef String_h -#define String_h +#ifndef String_class_h +#define String_class_h +#ifdef __cplusplus -//#include "Arduino.h" #include #include #include +#include +// When compiling programs with this class, the following gcc parameters +// dramatically increase performance and memory (RAM) efficiency, typically +// with little or no increase in code size. +// -felide-constructors +// -std=c++0x + +// Brian Cook's "no overhead" Flash String type (message on Dec 14, 2010) +// modified by Mikal Hart for his FlashString library +class __FlashStringHelper; +#ifndef F +#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal))) +#endif + +// An inherited class for holding the result of a concatenation. These +// result objects are assumed to be writable by subsequent concatenations. +class StringSumHelper; + +// The string class class String { - public: - // constructors - String( const char *value = "" ); - String( const String &value ); - String( const char ); - String( const unsigned char ); - String( const int, const int base=10); - String( const unsigned int, const int base=10 ); - String( const long, const int base=10 ); - String( const unsigned long, const int base=10 ); - ~String() { free(_buffer); _length = _capacity = 0;} //added _length = _capacity = 0; - - // operators - const String & operator = ( const String &rhs ); - const String & operator +=( const String &rhs ); - //const String & operator +=( const char ); - int operator ==( const String &rhs ) const; - int operator !=( const String &rhs ) const; - int operator < ( const String &rhs ) const; - int operator > ( const String &rhs ) const; - int operator <=( const String &rhs ) const; - int operator >=( const String &rhs ) const; - char operator []( unsigned int index ) const; - char& operator []( unsigned int index ); - //operator const char *() const { return _buffer; } - - // general methods - char charAt( unsigned int index ) const; - int compareTo( const String &anotherString ) const; - unsigned char endsWith( const String &suffix ) const; - unsigned char equals( const String &anObject ) const; - unsigned char equalsIgnoreCase( const String &anotherString ) const; - int indexOf( char ch ) const; - int indexOf( char ch, unsigned int fromIndex ) const; - int indexOf( const String &str ) const; - int indexOf( const String &str, unsigned int fromIndex ) const; - int lastIndexOf( char ch ) const; - int lastIndexOf( char ch, unsigned int fromIndex ) const; - int lastIndexOf( const String &str ) const; - int lastIndexOf( const String &str, unsigned int fromIndex ) const; - const unsigned int length( ) const { return _length; } - void setCharAt(unsigned int index, const char ch); - unsigned char startsWith( const String &prefix ) const; - unsigned char startsWith( const String &prefix, unsigned int toffset ) const; - String substring( unsigned int beginIndex ) const; - String substring( unsigned int beginIndex, unsigned int endIndex ) const; - String toLowerCase( ) const; - String toUpperCase( ) const; - String trim( ) const; - void getBytes(unsigned char *buf, unsigned int bufsize); - void toCharArray(char *buf, unsigned int bufsize); - long toInt( ); - const String& concat( const String &str ); - String replace( char oldChar, char newChar ); - String replace( const String& match, const String& replace ); - friend String operator + ( String lhs, const String &rhs ); - - protected: - char *_buffer; // the actual char array - unsigned int _capacity; // the array length minus one (for the '\0') - unsigned int _length; // the String length (not counting the '\0') - - void getBuffer(unsigned int maxStrLen); - - private: +public: + // constructors + String(const char *cstr = NULL); + String(const __FlashStringHelper *pgmstr); + String(const String &str); + #ifdef __GXX_EXPERIMENTAL_CXX0X__ + String(String &&rval); + String(StringSumHelper &&rval); + #endif + String(char c); + String(unsigned char c); + String(int, unsigned char base=10); + String(unsigned int, unsigned char base=10); + String(long, unsigned char base=10); + String(unsigned long, unsigned char base=10); + ~String(void); -}; + // memory management + unsigned char reserve(unsigned int size); + inline unsigned int length(void) const {return len;} -// allocate buffer space -inline void String::getBuffer(unsigned int maxStrLen) -{ - _capacity = maxStrLen; - _buffer = (char *) malloc(_capacity + 1); - if (_buffer == NULL) _length = _capacity = 0; -} + // copy and move + String & copy(const char *cstr, unsigned int length); + String & copy(const __FlashStringHelper *pgmstr); + void move(String &rhs); + String & operator = (const String &rhs); + String & operator = (const char *cstr); + String & operator = (const __FlashStringHelper *pgmstr); + #ifdef __GXX_EXPERIMENTAL_CXX0X__ + String & operator = (String &&rval); + String & operator = (StringSumHelper &&rval); + #endif + String & operator = (char c); -inline String operator+( String lhs, const String &rhs ) -{ - return lhs += rhs; -} + // append + String & append(const String &str); + String & append(const char *cstr); + String & append(const __FlashStringHelper *pgmstr); + String & append(char c); + String & append(unsigned char c) {return append((char)c);} + String & append(int num); + String & append(unsigned int num); + String & append(long num); + String & append(unsigned long num); + String & operator += (const String &rhs) {return append(rhs);} + String & operator += (const char *cstr) {return append(cstr);} + String & operator += (const __FlashStringHelper *pgmstr) {return append(pgmstr);} + String & operator += (char c) {return append(c);} + String & operator += (unsigned char c) {return append((char)c);} + String & operator += (int num) {return append(num);} + String & operator += (unsigned int num) {return append(num);} + String & operator += (long num) {return append(num);} + String & operator += (unsigned long num) {return append(num);} + // concatenate + 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 __FlashStringHelper *pgmstr); + friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c); + friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); -#endif + // comparison + int compareTo(const String &s) const; + unsigned char equals(const String &s) const; + unsigned char equals(const char *cstr) const; + unsigned char equals(const __FlashStringHelper *pgmstr) const; + unsigned char operator == (const String &rhs) const {return equals(rhs);} + unsigned char operator == (const char *cstr) const {return equals(cstr);} + unsigned char operator == (const __FlashStringHelper *pgmstr) const {return equals(pgmstr);} + unsigned char operator != (const String &rhs) const {return !equals(rhs);} + unsigned char operator != (const char *cstr) const {return !equals(cstr);} + unsigned char operator != (const __FlashStringHelper *pgmstr) const {return !equals(pgmstr);} + unsigned char operator < (const String &rhs) const; + unsigned char operator > (const String &rhs) const; + unsigned char operator <= (const String &rhs) const; + unsigned char operator >= (const String &rhs) const; + unsigned char equalsIgnoreCase(const String &s) const; + unsigned char startsWith( const String &prefix) const; + unsigned char startsWith(const String &prefix, unsigned int offset) const; + unsigned char endsWith(const String &suffix) const; + + // character acccess + char charAt(unsigned int index) const; + void setCharAt(unsigned int index, char c); + char operator [] (unsigned int index) const; + char& operator [] (unsigned int index); + void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const; + void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const + {getBytes((unsigned char *)buf, bufsize, index);} + + // search + int indexOf( char ch ) const; + int indexOf( char ch, unsigned int fromIndex ) const; + int indexOf( const String &str ) const; + int indexOf( const String &str, unsigned int fromIndex ) const; + int lastIndexOf( char ch ) const; + int lastIndexOf( char ch, int fromIndex ) const; + int lastIndexOf( const String &str ) const; + int lastIndexOf( const String &str, int fromIndex ) const; + String substring( unsigned int beginIndex ) const; + String substring( unsigned int beginIndex, unsigned int endIndex ) const; + + // modification + String & replace(char find, char replace); + String & replace(const String& find, const String& replace); + String & toLowerCase(void); + String & toUpperCase(void); + String & trim(void); + + // parsing/conversion + long toInt(void) const; + +protected: + char *buffer; // the actual char array + unsigned int capacity; // the array length minus one (for the '\0') + unsigned int len; // the String length (not counting the '\0') + unsigned char flags; // unused, for future features +protected: + void init(void); + unsigned char changeBuffer(unsigned int maxStrLen); + String & append(const char *cstr, unsigned int length); +}; + +class StringSumHelper : public String +{ +public: + StringSumHelper(const String &s) : String(s) {} + StringSumHelper(const char *p) : String(p) {} + StringSumHelper(const __FlashStringHelper *pgmstr) : String(pgmstr) {} + StringSumHelper(char c) : String(c) {} + StringSumHelper(unsigned char c) : String(c) {} + StringSumHelper(int num) : String(num, 10) {} + StringSumHelper(unsigned int num) : String(num, 10) {} + StringSumHelper(long num) : String(num, 10) {} + StringSumHelper(unsigned long num) : String(num, 10) {} +}; + +#endif // __cplusplus +#endif // String_class_h -- cgit v1.2.3-18-g5258 From 22786eaed2494bf6fcd9b934f857dec1202b3148 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Fri, 11 Mar 2011 18:01:40 -0500 Subject: Removing F("string") syntax for now. We should probably add something like this back in later, but I want to do one thing at a time. This removes the __FlashStringHelper class as well. --- cores/arduino/WString.h | 17 ----------------- 1 file changed, 17 deletions(-) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 541a118..6111e20 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -34,13 +34,6 @@ // -felide-constructors // -std=c++0x -// Brian Cook's "no overhead" Flash String type (message on Dec 14, 2010) -// modified by Mikal Hart for his FlashString library -class __FlashStringHelper; -#ifndef F -#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal))) -#endif - // An inherited class for holding the result of a concatenation. These // result objects are assumed to be writable by subsequent concatenations. class StringSumHelper; @@ -51,7 +44,6 @@ class String public: // constructors String(const char *cstr = NULL); - String(const __FlashStringHelper *pgmstr); String(const String &str); #ifdef __GXX_EXPERIMENTAL_CXX0X__ String(String &&rval); @@ -71,11 +63,9 @@ public: // copy and move String & copy(const char *cstr, unsigned int length); - String & copy(const __FlashStringHelper *pgmstr); void move(String &rhs); String & operator = (const String &rhs); String & operator = (const char *cstr); - String & operator = (const __FlashStringHelper *pgmstr); #ifdef __GXX_EXPERIMENTAL_CXX0X__ String & operator = (String &&rval); String & operator = (StringSumHelper &&rval); @@ -85,7 +75,6 @@ public: // append String & append(const String &str); String & append(const char *cstr); - String & append(const __FlashStringHelper *pgmstr); String & append(char c); String & append(unsigned char c) {return append((char)c);} String & append(int num); @@ -94,7 +83,6 @@ public: String & append(unsigned long num); String & operator += (const String &rhs) {return append(rhs);} String & operator += (const char *cstr) {return append(cstr);} - String & operator += (const __FlashStringHelper *pgmstr) {return append(pgmstr);} String & operator += (char c) {return append(c);} String & operator += (unsigned char c) {return append((char)c);} String & operator += (int num) {return append(num);} @@ -105,7 +93,6 @@ public: // concatenate 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 __FlashStringHelper *pgmstr); friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c); friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); @@ -117,13 +104,10 @@ public: int compareTo(const String &s) const; unsigned char equals(const String &s) const; unsigned char equals(const char *cstr) const; - unsigned char equals(const __FlashStringHelper *pgmstr) const; unsigned char operator == (const String &rhs) const {return equals(rhs);} unsigned char operator == (const char *cstr) const {return equals(cstr);} - unsigned char operator == (const __FlashStringHelper *pgmstr) const {return equals(pgmstr);} unsigned char operator != (const String &rhs) const {return !equals(rhs);} unsigned char operator != (const char *cstr) const {return !equals(cstr);} - unsigned char operator != (const __FlashStringHelper *pgmstr) const {return !equals(pgmstr);} unsigned char operator < (const String &rhs) const; unsigned char operator > (const String &rhs) const; unsigned char operator <= (const String &rhs) const; @@ -180,7 +164,6 @@ class StringSumHelper : public String public: StringSumHelper(const String &s) : String(s) {} StringSumHelper(const char *p) : String(p) {} - StringSumHelper(const __FlashStringHelper *pgmstr) : String(pgmstr) {} StringSumHelper(char c) : String(c) {} StringSumHelper(unsigned char c) : String(c) {} StringSumHelper(int num) : String(num, 10) {} -- cgit v1.2.3-18-g5258 From 99e0c131fc0bc286e42e848d56beceb3add66e93 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Fri, 11 Mar 2011 18:04:31 -0500 Subject: Renaming append() back to concat(). --- cores/arduino/WString.h | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 6111e20..4a680e2 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -72,23 +72,23 @@ public: #endif String & operator = (char c); - // append - String & append(const String &str); - String & append(const char *cstr); - String & append(char c); - String & append(unsigned char c) {return append((char)c);} - String & append(int num); - String & append(unsigned int num); - String & append(long num); - String & append(unsigned long num); - String & operator += (const String &rhs) {return append(rhs);} - String & operator += (const char *cstr) {return append(cstr);} - String & operator += (char c) {return append(c);} - String & operator += (unsigned char c) {return append((char)c);} - String & operator += (int num) {return append(num);} - String & operator += (unsigned int num) {return append(num);} - String & operator += (long num) {return append(num);} - String & operator += (unsigned long num) {return append(num);} + // concat + String & concat(const String &str); + String & concat(const char *cstr); + String & concat(char c); + String & concat(unsigned char c) {return concat((char)c);} + String & concat(int num); + String & concat(unsigned int num); + String & concat(long num); + String & concat(unsigned long num); + String & operator += (const String &rhs) {return concat(rhs);} + String & operator += (const char *cstr) {return concat(cstr);} + String & operator += (char c) {return concat(c);} + String & operator += (unsigned char c) {return concat((char)c);} + String & operator += (int num) {return concat(num);} + String & operator += (unsigned int num) {return concat(num);} + String & operator += (long num) {return concat(num);} + String & operator += (unsigned long num) {return concat(num);} // concatenate friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); @@ -156,7 +156,7 @@ protected: protected: void init(void); unsigned char changeBuffer(unsigned int maxStrLen); - String & append(const char *cstr, unsigned int length); + String & concat(const char *cstr, unsigned int length); }; class StringSumHelper : public String -- cgit v1.2.3-18-g5258 From b4b32f60f1c156e76117dab69479c193a861be23 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Fri, 11 Mar 2011 18:54:58 -0500 Subject: Don't return the string when modifying its value. Changing toLowerCase(), toUpperCase(), trim() and replace() to return void instead of a reference to the string that's just been changed. That way, it's clear that the functions modify the string they've been called on. --- cores/arduino/WString.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 4a680e2..164eeac 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -139,11 +139,11 @@ public: String substring( unsigned int beginIndex, unsigned int endIndex ) const; // modification - String & replace(char find, char replace); - String & replace(const String& find, const String& replace); - String & toLowerCase(void); - String & toUpperCase(void); - String & trim(void); + void replace(char find, char replace); + void replace(const String& find, const String& replace); + void toLowerCase(void); + void toUpperCase(void); + void trim(void); // parsing/conversion long toInt(void) const; -- cgit v1.2.3-18-g5258 From 98b403114c3d9edcb6b74938f07c68dccc3c2489 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 12 Mar 2011 14:03:34 -0500 Subject: Modifying String.concat() to return success or failure, not this. Which means you can't chain multiple concat() calls together, but you can check if they succeeded or not. --- cores/arduino/WString.h | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 164eeac..f797861 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -73,22 +73,22 @@ public: String & operator = (char c); // concat - String & concat(const String &str); - String & concat(const char *cstr); - String & concat(char c); - String & concat(unsigned char c) {return concat((char)c);} - String & concat(int num); - String & concat(unsigned int num); - String & concat(long num); - String & concat(unsigned long num); - String & operator += (const String &rhs) {return concat(rhs);} - String & operator += (const char *cstr) {return concat(cstr);} - String & operator += (char c) {return concat(c);} - String & operator += (unsigned char c) {return concat((char)c);} - String & operator += (int num) {return concat(num);} - String & operator += (unsigned int num) {return concat(num);} - String & operator += (long num) {return concat(num);} - String & operator += (unsigned long num) {return concat(num);} + unsigned char concat(const String &str); + unsigned char concat(const char *cstr); + unsigned char concat(char c); + unsigned char concat(unsigned char c) {return concat((char)c);} + unsigned char concat(int num); + unsigned char concat(unsigned int num); + unsigned char concat(long num); + unsigned char concat(unsigned long num); + String & operator += (const String &rhs) {concat(rhs); return (*this);} + String & operator += (const char *cstr) {concat(cstr); return (*this);} + String & operator += (char c) {concat(c); return (*this);} + String & operator += (unsigned char c) {concat((char)c); return (*this);} + String & operator += (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 += (unsigned long num) {concat(num); return (*this);} // concatenate friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); @@ -156,7 +156,7 @@ protected: protected: void init(void); unsigned char changeBuffer(unsigned int maxStrLen); - String & concat(const char *cstr, unsigned int length); + unsigned char concat(const char *cstr, unsigned int length); }; class StringSumHelper : public String -- cgit v1.2.3-18-g5258 From 76776e7a46b30ee84f48db146bf8dd642de2927a Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sun, 13 Mar 2011 16:46:06 -0400 Subject: Moving move() to __GXX_EXPERIMENTAL_CXX0X__ only, adding operator bool(). --- cores/arduino/WString.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index f797861..5e78ee5 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -63,7 +63,9 @@ public: // copy and move String & copy(const char *cstr, unsigned int length); + #ifdef __GXX_EXPERIMENTAL_CXX0X__ void move(String &rhs); + #endif String & operator = (const String &rhs); String & operator = (const char *cstr); #ifdef __GXX_EXPERIMENTAL_CXX0X__ @@ -101,6 +103,7 @@ public: friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); // comparison + operator bool() const; int compareTo(const String &s) const; unsigned char equals(const String &s) const; unsigned char equals(const char *cstr) const; -- cgit v1.2.3-18-g5258 From a5f6e6524282e23860ace324dcf81598287f9944 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sun, 13 Mar 2011 19:31:10 -0400 Subject: Adding additional String + operators for disambiguation. The operator bool() means that you could implicitly convert a String to a bool and then add it to it an int, for example. Which means our operator+ has to match exactly or it will be ambiguous. --- cores/arduino/WString.h | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 5e78ee5..5a35101 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -175,5 +175,40 @@ public: StringSumHelper(unsigned long num) : String(num, 10) {} }; +inline StringSumHelper operator + (const String &lhs, const String &rhs) + { return StringSumHelper(lhs) + rhs; } + +inline StringSumHelper operator + (const String &lhs, const char *cstr) + { return StringSumHelper(lhs) + cstr; } +inline StringSumHelper operator + (const String &lhs, char c) + { return StringSumHelper(lhs) + c; } +inline StringSumHelper operator + (const String &lhs, unsigned char c) + { return StringSumHelper(lhs) + c; } +inline StringSumHelper operator + (const String &lhs, int num) + { return StringSumHelper(lhs) + num; } +inline StringSumHelper operator + (const String &lhs, unsigned int num) + { return StringSumHelper(lhs) + num; } +inline StringSumHelper operator + (const String &lhs, long num) + { return StringSumHelper(lhs) + num; } +inline StringSumHelper operator + (const String &lhs, unsigned long num) + { return StringSumHelper(lhs) + num; } + +inline StringSumHelper operator + (const char *cstr, const String &rhs) + { return StringSumHelper(cstr) + rhs; } +inline StringSumHelper operator + (char c, const String &rhs) + { return StringSumHelper(c) + rhs; } +inline StringSumHelper operator + (unsigned char c, const String &rhs) + { return StringSumHelper(c) + rhs; } +inline StringSumHelper operator + (int num, const String &rhs) + { return StringSumHelper(num) + rhs; } +inline StringSumHelper operator + (unsigned int num, const String &rhs) + { return StringSumHelper(num) + rhs; } +inline StringSumHelper operator + (long num, const String &rhs) + { return StringSumHelper(num) + rhs; } +inline StringSumHelper operator + (unsigned long num, const String &rhs) + { return StringSumHelper(num) + rhs; } + + + #endif // __cplusplus #endif // String_class_h -- cgit v1.2.3-18-g5258 From 45884b1231e43acdce51b381975977a3601e77d0 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sun, 13 Mar 2011 19:39:04 -0400 Subject: Protecting String copy() and move(). --- cores/arduino/WString.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 5a35101..6c07427 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -61,11 +61,6 @@ public: unsigned char reserve(unsigned int size); inline unsigned int length(void) const {return len;} - // copy and move - String & copy(const char *cstr, unsigned int length); - #ifdef __GXX_EXPERIMENTAL_CXX0X__ - void move(String &rhs); - #endif String & operator = (const String &rhs); String & operator = (const char *cstr); #ifdef __GXX_EXPERIMENTAL_CXX0X__ @@ -160,6 +155,12 @@ protected: void init(void); unsigned char changeBuffer(unsigned int maxStrLen); unsigned char concat(const char *cstr, unsigned int length); + + // copy and move + String & copy(const char *cstr, unsigned int length); + #ifdef __GXX_EXPERIMENTAL_CXX0X__ + void move(String &rhs); + #endif }; class StringSumHelper : public String -- cgit v1.2.3-18-g5258 From 7dea0522f48641106f92b1511b9e62ee46f8e2dc Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Fri, 18 Mar 2011 21:45:27 -0400 Subject: Starting to distinguish between empty strings and invalid (null) ones. --- cores/arduino/WString.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 6c07427..18b6541 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -43,7 +43,7 @@ class String { public: // constructors - String(const char *cstr = NULL); + String(const char *cstr = ""); String(const String &str); #ifdef __GXX_EXPERIMENTAL_CXX0X__ String(String &&rval); -- cgit v1.2.3-18-g5258 From 58d04ab3a3a0b13524a862168bed1c3d0708766e Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 19 Mar 2011 11:14:17 -0400 Subject: Return an invalid string (not a partial one) when operator+() fails. --- cores/arduino/WString.h | 1 + 1 file changed, 1 insertion(+) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 18b6541..d707309 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -153,6 +153,7 @@ protected: unsigned char flags; // unused, for future features protected: void init(void); + void invalidate(void); unsigned char changeBuffer(unsigned int maxStrLen); unsigned char concat(const char *cstr, unsigned int length); -- cgit v1.2.3-18-g5258 From cedea72273a1d0b70c83f46808342b23ddaddca7 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Wed, 23 Mar 2011 22:42:05 -0400 Subject: Commenting String API behavior. --- cores/arduino/WString.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index d707309..180f58e 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -43,6 +43,10 @@ class String { public: // constructors + // creates a copy of the initial value. + // if the initial value is null or invalid, or if memory allocation + // fails, the string will be marked as invalid (i.e. operator bool() + // will return false). String(const char *cstr = ""); String(const String &str); #ifdef __GXX_EXPERIMENTAL_CXX0X__ @@ -58,9 +62,15 @@ public: ~String(void); // memory management + // return true on success, false on failure (in which case, the string + // is left unchanged). reserve(0), if successful, will validate an + // invalid string (i.e., operator bool() will return true afterwards) unsigned char reserve(unsigned int size); inline unsigned int length(void) const {return len;} + // creates a copy of the assigned value. if the value is null or + // invalid, or if the memory allocation fails, the string will be + // marked as invalid (operator bool() will return false). String & operator = (const String &rhs); String & operator = (const char *cstr); #ifdef __GXX_EXPERIMENTAL_CXX0X__ @@ -70,6 +80,9 @@ public: String & operator = (char c); // concat + // returns true on success, false on failure (in which case, the string + // is left unchanged). if the argument is null or invalid, the + // concatenation is considered unsucessful. unsigned char concat(const String &str); unsigned char concat(const char *cstr); unsigned char concat(char c); @@ -78,6 +91,9 @@ public: unsigned char concat(unsigned int num); unsigned char concat(long num); unsigned char concat(unsigned long num); + + // if there's not enough memory for the concatenated value, the string + // will be left unchanged (but this isn't signalled in any way) String & operator += (const String &rhs) {concat(rhs); return (*this);} String & operator += (const char *cstr) {concat(cstr); return (*this);} String & operator += (char c) {concat(c); return (*this);} -- cgit v1.2.3-18-g5258 From 99e642a26dc2a406cdfa2d43665f45749b63d5c8 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sat, 26 Mar 2011 18:52:54 -0400 Subject: String: removing implicit numeric conversions and new approach to "if (s)". This makes explicit the String constructors that take numeric types and chars and removes the versions of concat() and operator=() and operator+() that accept numberic types. It also replaces the operator bool() with a operator that converts to a function pointer. This allows for uses like "if (s)" but not "s + 123". See: http://www.artima.com/cppsource/safebool.html. This allowed removing the disambiguating operator+() functions and relying solely on StringSumHelper and anonymous temporaries once again. Also, now treating unsigned char's like int when constructing Strings from them, i.e. String(byte(65)) is now "65" not "A". This is consistent with the new behavior of Serial.print(byte). --- cores/arduino/WString.h | 81 +++++++++---------------------------------------- 1 file changed, 14 insertions(+), 67 deletions(-) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 180f58e..bbbe7e8 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -41,6 +41,12 @@ class StringSumHelper; // The string class class String { + // use a function pointer to allow for "if (s)" without the + // complications of an operator bool(). for more information, see: + // http://www.artima.com/cppsource/safebool.html + typedef void (String::*StringIfHelperType)() const; + void StringIfHelper() const {} + public: // constructors // creates a copy of the initial value. @@ -53,12 +59,12 @@ public: String(String &&rval); String(StringSumHelper &&rval); #endif - String(char c); - String(unsigned char c); - String(int, unsigned char base=10); - String(unsigned int, unsigned char base=10); - String(long, unsigned char base=10); - String(unsigned long, unsigned char base=10); + explicit String(char c); + explicit String(unsigned char, unsigned char base=10); + explicit String(int, unsigned char base=10); + explicit String(unsigned int, unsigned char base=10); + explicit String(long, unsigned char base=10); + explicit String(unsigned long, unsigned char base=10); ~String(void); // memory management @@ -77,7 +83,6 @@ public: String & operator = (String &&rval); String & operator = (StringSumHelper &&rval); #endif - String & operator = (char c); // concat // returns true on success, false on failure (in which case, the string @@ -85,36 +90,19 @@ public: // concatenation is considered unsucessful. unsigned char concat(const String &str); unsigned char concat(const char *cstr); - unsigned char concat(char c); - unsigned char concat(unsigned char c) {return concat((char)c);} - unsigned char concat(int num); - unsigned char concat(unsigned int num); - unsigned char concat(long num); - unsigned char concat(unsigned long num); // if there's not enough memory for the concatenated value, the string // will be left unchanged (but this isn't signalled in any way) String & operator += (const String &rhs) {concat(rhs); return (*this);} String & operator += (const char *cstr) {concat(cstr); return (*this);} - String & operator += (char c) {concat(c); return (*this);} - String & operator += (unsigned char c) {concat((char)c); return (*this);} - String & operator += (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 += (unsigned long num) {concat(num); return (*this);} // concatenate friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); - friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); - friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char c); - friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); - friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); - friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); - friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); // comparison - operator bool() const; + operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } + int compareTo(const String &s) const; unsigned char equals(const String &s) const; unsigned char equals(const char *cstr) const; @@ -185,48 +173,7 @@ class StringSumHelper : public String public: StringSumHelper(const String &s) : String(s) {} StringSumHelper(const char *p) : String(p) {} - StringSumHelper(char c) : String(c) {} - StringSumHelper(unsigned char c) : String(c) {} - StringSumHelper(int num) : String(num, 10) {} - StringSumHelper(unsigned int num) : String(num, 10) {} - StringSumHelper(long num) : String(num, 10) {} - StringSumHelper(unsigned long num) : String(num, 10) {} }; -inline StringSumHelper operator + (const String &lhs, const String &rhs) - { return StringSumHelper(lhs) + rhs; } - -inline StringSumHelper operator + (const String &lhs, const char *cstr) - { return StringSumHelper(lhs) + cstr; } -inline StringSumHelper operator + (const String &lhs, char c) - { return StringSumHelper(lhs) + c; } -inline StringSumHelper operator + (const String &lhs, unsigned char c) - { return StringSumHelper(lhs) + c; } -inline StringSumHelper operator + (const String &lhs, int num) - { return StringSumHelper(lhs) + num; } -inline StringSumHelper operator + (const String &lhs, unsigned int num) - { return StringSumHelper(lhs) + num; } -inline StringSumHelper operator + (const String &lhs, long num) - { return StringSumHelper(lhs) + num; } -inline StringSumHelper operator + (const String &lhs, unsigned long num) - { return StringSumHelper(lhs) + num; } - -inline StringSumHelper operator + (const char *cstr, const String &rhs) - { return StringSumHelper(cstr) + rhs; } -inline StringSumHelper operator + (char c, const String &rhs) - { return StringSumHelper(c) + rhs; } -inline StringSumHelper operator + (unsigned char c, const String &rhs) - { return StringSumHelper(c) + rhs; } -inline StringSumHelper operator + (int num, const String &rhs) - { return StringSumHelper(num) + rhs; } -inline StringSumHelper operator + (unsigned int num, const String &rhs) - { return StringSumHelper(num) + rhs; } -inline StringSumHelper operator + (long num, const String &rhs) - { return StringSumHelper(num) + rhs; } -inline StringSumHelper operator + (unsigned long num, const String &rhs) - { return StringSumHelper(num) + rhs; } - - - #endif // __cplusplus #endif // String_class_h -- cgit v1.2.3-18-g5258 From ffe7bc53c1862866bf38e01174bd35d20632608c Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Sun, 27 Mar 2011 15:06:20 -0400 Subject: Adding F("foo") syntax for flash strings. --- cores/arduino/WString.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index bbbe7e8..673e51b 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -34,6 +34,9 @@ // -felide-constructors // -std=c++0x +class __FlashStringHelper; +#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal))) + // An inherited class for holding the result of a concatenation. These // result objects are assumed to be writable by subsequent concatenations. class StringSumHelper; @@ -51,8 +54,8 @@ public: // constructors // creates a copy of the initial value. // if the initial value is null or invalid, or if memory allocation - // fails, the string will be marked as invalid (i.e. operator bool() - // will return false). + // fails, the string will be marked as invalid (i.e. "if (s)" will + // be false). String(const char *cstr = ""); String(const String &str); #ifdef __GXX_EXPERIMENTAL_CXX0X__ @@ -70,13 +73,13 @@ public: // memory management // return true on success, false on failure (in which case, the string // is left unchanged). reserve(0), if successful, will validate an - // invalid string (i.e., operator bool() will return true afterwards) + // invalid string (i.e., "if (s)" will be true afterwards) unsigned char reserve(unsigned int size); inline unsigned int length(void) const {return len;} // creates a copy of the assigned value. if the value is null or // invalid, or if the memory allocation fails, the string will be - // marked as invalid (operator bool() will return false). + // marked as invalid ("if (s)" will be false). String & operator = (const String &rhs); String & operator = (const char *cstr); #ifdef __GXX_EXPERIMENTAL_CXX0X__ -- cgit v1.2.3-18-g5258 From 1cac0f3eb738119f53b8fdd07bcd6d1158235476 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Thu, 31 Mar 2011 10:56:14 -0400 Subject: Restoring concatenation of built-in types with String. --- cores/arduino/WString.h | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'cores/arduino/WString.h') diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 673e51b..a601aca 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -87,25 +87,42 @@ public: String & operator = (StringSumHelper &&rval); #endif - // concat + // concatenate (works w/ built-in types) + // returns true on success, false on failure (in which case, the string // is left unchanged). if the argument is null or invalid, the // concatenation is considered unsucessful. unsigned char concat(const String &str); unsigned char concat(const char *cstr); + unsigned char concat(char c); + unsigned char concat(unsigned char c); + unsigned char concat(int num); + unsigned char concat(unsigned int num); + unsigned char concat(long num); + unsigned char concat(unsigned long num); // if there's not enough memory for the concatenated value, the string // will be left unchanged (but this isn't signalled in any way) String & operator += (const String &rhs) {concat(rhs); return (*this);} String & operator += (const char *cstr) {concat(cstr); return (*this);} + String & operator += (char c) {concat(c); return (*this);} + String & operator += (unsigned char num) {concat(num); return (*this);} + String & operator += (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 += (unsigned long num) {concat(num); return (*this);} - // concatenate friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); - - // comparison + friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); + + // comparison (only works w/ Strings and "strings") operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } - int compareTo(const String &s) const; unsigned char equals(const String &s) const; unsigned char equals(const char *cstr) const; @@ -176,6 +193,12 @@ class StringSumHelper : public String public: StringSumHelper(const String &s) : String(s) {} StringSumHelper(const char *p) : String(p) {} + StringSumHelper(char c) : String(c) {} + StringSumHelper(unsigned char num) : String(num) {} + StringSumHelper(int num) : String(num) {} + StringSumHelper(unsigned int num) : String(num) {} + StringSumHelper(long num) : String(num) {} + StringSumHelper(unsigned long num) : String(num) {} }; #endif // __cplusplus -- cgit v1.2.3-18-g5258