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