work.

Dependencies:   Blynk mbed

Committer:
lixianyu
Date:
Thu Jun 16 08:12:33 2016 +0000
Revision:
4:e5018e5ba340
Parent:
0:d8f4c441e032
ok

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:d8f4c441e032 1 /*
lixianyu 0:d8f4c441e032 2 WString.h - String library for Wiring & Arduino
lixianyu 0:d8f4c441e032 3 ...mostly rewritten by Paul Stoffregen...
lixianyu 0:d8f4c441e032 4 Copyright (c) 2009-10 Hernando Barragan. All right reserved.
lixianyu 0:d8f4c441e032 5 Copyright 2011, Paul Stoffregen, paul@pjrc.com
lixianyu 0:d8f4c441e032 6
lixianyu 0:d8f4c441e032 7 This library is free software; you can redistribute it and/or
lixianyu 0:d8f4c441e032 8 modify it under the terms of the GNU Lesser General Public
lixianyu 0:d8f4c441e032 9 License as published by the Free Software Foundation; either
lixianyu 0:d8f4c441e032 10 version 2.1 of the License, or (at your option) any later version.
lixianyu 0:d8f4c441e032 11
lixianyu 0:d8f4c441e032 12 This library is distributed in the hope that it will be useful,
lixianyu 0:d8f4c441e032 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
lixianyu 0:d8f4c441e032 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
lixianyu 0:d8f4c441e032 15 Lesser General Public License for more details.
lixianyu 0:d8f4c441e032 16
lixianyu 0:d8f4c441e032 17 You should have received a copy of the GNU Lesser General Public
lixianyu 0:d8f4c441e032 18 License along with this library; if not, write to the Free Software
lixianyu 0:d8f4c441e032 19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
lixianyu 0:d8f4c441e032 20 */
lixianyu 0:d8f4c441e032 21
lixianyu 0:d8f4c441e032 22 #ifndef String_class_h
lixianyu 0:d8f4c441e032 23 #define String_class_h
lixianyu 0:d8f4c441e032 24 #ifdef __cplusplus
lixianyu 0:d8f4c441e032 25
lixianyu 0:d8f4c441e032 26 #include <stdlib.h>
lixianyu 0:d8f4c441e032 27 #include <string.h>
lixianyu 0:d8f4c441e032 28 #include <ctype.h>
lixianyu 0:d8f4c441e032 29 //#include <avr/pgmspace.h>
lixianyu 0:d8f4c441e032 30
lixianyu 0:d8f4c441e032 31 // When compiling programs with this class, the following gcc parameters
lixianyu 0:d8f4c441e032 32 // dramatically increase performance and memory (RAM) efficiency, typically
lixianyu 0:d8f4c441e032 33 // with little or no increase in code size.
lixianyu 0:d8f4c441e032 34 // -felide-constructors
lixianyu 0:d8f4c441e032 35 // -std=c++0x
lixianyu 0:d8f4c441e032 36
lixianyu 0:d8f4c441e032 37 //class __FlashStringHelper;
lixianyu 0:d8f4c441e032 38 #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
lixianyu 0:d8f4c441e032 39
lixianyu 0:d8f4c441e032 40 // An inherited class for holding the result of a concatenation. These
lixianyu 0:d8f4c441e032 41 // result objects are assumed to be writable by subsequent concatenations.
lixianyu 0:d8f4c441e032 42 class StringSumHelper;
lixianyu 0:d8f4c441e032 43
lixianyu 0:d8f4c441e032 44 // The string class
lixianyu 0:d8f4c441e032 45 class String
lixianyu 0:d8f4c441e032 46 {
lixianyu 0:d8f4c441e032 47 // use a function pointer to allow for "if (s)" without the
lixianyu 0:d8f4c441e032 48 // complications of an operator bool(). for more information, see:
lixianyu 0:d8f4c441e032 49 // http://www.artima.com/cppsource/safebool.html
lixianyu 0:d8f4c441e032 50 typedef void (String::*StringIfHelperType)() const;
lixianyu 0:d8f4c441e032 51 void StringIfHelper() const {}
lixianyu 0:d8f4c441e032 52
lixianyu 0:d8f4c441e032 53 public:
lixianyu 0:d8f4c441e032 54 // constructors
lixianyu 0:d8f4c441e032 55 // creates a copy of the initial value.
lixianyu 0:d8f4c441e032 56 // if the initial value is null or invalid, or if memory allocation
lixianyu 0:d8f4c441e032 57 // fails, the string will be marked as invalid (i.e. "if (s)" will
lixianyu 0:d8f4c441e032 58 // be false).
lixianyu 0:d8f4c441e032 59 String(const char *cstr = "");
lixianyu 0:d8f4c441e032 60 String(const String &str);
lixianyu 0:d8f4c441e032 61 #if 0
lixianyu 0:d8f4c441e032 62 String(const __FlashStringHelper *str);
lixianyu 0:d8f4c441e032 63 #endif
lixianyu 0:d8f4c441e032 64 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
lixianyu 0:d8f4c441e032 65 String(String &&rval);
lixianyu 0:d8f4c441e032 66 String(StringSumHelper &&rval);
lixianyu 0:d8f4c441e032 67 #endif
lixianyu 0:d8f4c441e032 68 explicit String(char c);
lixianyu 0:d8f4c441e032 69 explicit String(unsigned char, unsigned char base=10);
lixianyu 0:d8f4c441e032 70 explicit String(int, unsigned char base=10);
lixianyu 0:d8f4c441e032 71 explicit String(unsigned int, unsigned char base=10);
lixianyu 0:d8f4c441e032 72 explicit String(long, unsigned char base=10);
lixianyu 0:d8f4c441e032 73 explicit String(unsigned long, unsigned char base=10);
lixianyu 0:d8f4c441e032 74 explicit String(float, unsigned char decimalPlaces=2);
lixianyu 0:d8f4c441e032 75 explicit String(double, unsigned char decimalPlaces=2);
lixianyu 0:d8f4c441e032 76 ~String(void);
lixianyu 0:d8f4c441e032 77
lixianyu 0:d8f4c441e032 78 // memory management
lixianyu 0:d8f4c441e032 79 // return true on success, false on failure (in which case, the string
lixianyu 0:d8f4c441e032 80 // is left unchanged). reserve(0), if successful, will validate an
lixianyu 0:d8f4c441e032 81 // invalid string (i.e., "if (s)" will be true afterwards)
lixianyu 0:d8f4c441e032 82 unsigned char reserve(unsigned int size);
lixianyu 0:d8f4c441e032 83 inline unsigned int length(void) const {
lixianyu 0:d8f4c441e032 84 return len;
lixianyu 0:d8f4c441e032 85 }
lixianyu 0:d8f4c441e032 86
lixianyu 0:d8f4c441e032 87 // creates a copy of the assigned value. if the value is null or
lixianyu 0:d8f4c441e032 88 // invalid, or if the memory allocation fails, the string will be
lixianyu 0:d8f4c441e032 89 // marked as invalid ("if (s)" will be false).
lixianyu 0:d8f4c441e032 90 String & operator = (const String &rhs);
lixianyu 0:d8f4c441e032 91 String & operator = (const char *cstr);
lixianyu 0:d8f4c441e032 92 #if 0
lixianyu 0:d8f4c441e032 93 String & operator = (const __FlashStringHelper *str);
lixianyu 0:d8f4c441e032 94 #endif
lixianyu 0:d8f4c441e032 95 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
lixianyu 0:d8f4c441e032 96 String & operator = (String &&rval);
lixianyu 0:d8f4c441e032 97 String & operator = (StringSumHelper &&rval);
lixianyu 0:d8f4c441e032 98 #endif
lixianyu 0:d8f4c441e032 99
lixianyu 0:d8f4c441e032 100 // concatenate (works w/ built-in types)
lixianyu 0:d8f4c441e032 101
lixianyu 0:d8f4c441e032 102 // returns true on success, false on failure (in which case, the string
lixianyu 0:d8f4c441e032 103 // is left unchanged). if the argument is null or invalid, the
lixianyu 0:d8f4c441e032 104 // concatenation is considered unsucessful.
lixianyu 0:d8f4c441e032 105 unsigned char concat(const String &str);
lixianyu 0:d8f4c441e032 106 unsigned char concat(const char *cstr);
lixianyu 0:d8f4c441e032 107 unsigned char concat(char c);
lixianyu 0:d8f4c441e032 108 unsigned char concat(unsigned char c);
lixianyu 0:d8f4c441e032 109 unsigned char concat(int num);
lixianyu 0:d8f4c441e032 110 unsigned char concat(unsigned int num);
lixianyu 0:d8f4c441e032 111 unsigned char concat(long num);
lixianyu 0:d8f4c441e032 112 unsigned char concat(unsigned long num);
lixianyu 0:d8f4c441e032 113 unsigned char concat(float num);
lixianyu 0:d8f4c441e032 114 unsigned char concat(double num);
lixianyu 0:d8f4c441e032 115 #if 0
lixianyu 0:d8f4c441e032 116 unsigned char concat(const __FlashStringHelper * str);
lixianyu 0:d8f4c441e032 117 #endif
lixianyu 0:d8f4c441e032 118 // if there's not enough memory for the concatenated value, the string
lixianyu 0:d8f4c441e032 119 // will be left unchanged (but this isn't signalled in any way)
lixianyu 0:d8f4c441e032 120 String & operator += (const String &rhs) {
lixianyu 0:d8f4c441e032 121 concat(rhs);
lixianyu 0:d8f4c441e032 122 return (*this);
lixianyu 0:d8f4c441e032 123 }
lixianyu 0:d8f4c441e032 124 String & operator += (const char *cstr) {
lixianyu 0:d8f4c441e032 125 concat(cstr);
lixianyu 0:d8f4c441e032 126 return (*this);
lixianyu 0:d8f4c441e032 127 }
lixianyu 0:d8f4c441e032 128 String & operator += (char c) {
lixianyu 0:d8f4c441e032 129 concat(c);
lixianyu 0:d8f4c441e032 130 return (*this);
lixianyu 0:d8f4c441e032 131 }
lixianyu 0:d8f4c441e032 132 String & operator += (unsigned char num) {
lixianyu 0:d8f4c441e032 133 concat(num);
lixianyu 0:d8f4c441e032 134 return (*this);
lixianyu 0:d8f4c441e032 135 }
lixianyu 0:d8f4c441e032 136 String & operator += (int num) {
lixianyu 0:d8f4c441e032 137 concat(num);
lixianyu 0:d8f4c441e032 138 return (*this);
lixianyu 0:d8f4c441e032 139 }
lixianyu 0:d8f4c441e032 140 String & operator += (unsigned int num) {
lixianyu 0:d8f4c441e032 141 concat(num);
lixianyu 0:d8f4c441e032 142 return (*this);
lixianyu 0:d8f4c441e032 143 }
lixianyu 0:d8f4c441e032 144 String & operator += (long num) {
lixianyu 0:d8f4c441e032 145 concat(num);
lixianyu 0:d8f4c441e032 146 return (*this);
lixianyu 0:d8f4c441e032 147 }
lixianyu 0:d8f4c441e032 148 String & operator += (unsigned long num) {
lixianyu 0:d8f4c441e032 149 concat(num);
lixianyu 0:d8f4c441e032 150 return (*this);
lixianyu 0:d8f4c441e032 151 }
lixianyu 0:d8f4c441e032 152 String & operator += (float num) {
lixianyu 0:d8f4c441e032 153 concat(num);
lixianyu 0:d8f4c441e032 154 return (*this);
lixianyu 0:d8f4c441e032 155 }
lixianyu 0:d8f4c441e032 156 String & operator += (double num) {
lixianyu 0:d8f4c441e032 157 concat(num);
lixianyu 0:d8f4c441e032 158 return (*this);
lixianyu 0:d8f4c441e032 159 }
lixianyu 0:d8f4c441e032 160 #if 0
lixianyu 0:d8f4c441e032 161 String & operator += (const __FlashStringHelper *str) {
lixianyu 0:d8f4c441e032 162 concat(str);
lixianyu 0:d8f4c441e032 163 return (*this);
lixianyu 0:d8f4c441e032 164 }
lixianyu 0:d8f4c441e032 165 #endif
lixianyu 0:d8f4c441e032 166
lixianyu 0:d8f4c441e032 167 friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
lixianyu 0:d8f4c441e032 168 friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
lixianyu 0:d8f4c441e032 169 friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
lixianyu 0:d8f4c441e032 170 friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
lixianyu 0:d8f4c441e032 171 friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
lixianyu 0:d8f4c441e032 172 friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
lixianyu 0:d8f4c441e032 173 friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
lixianyu 0:d8f4c441e032 174 friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
lixianyu 0:d8f4c441e032 175 friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
lixianyu 0:d8f4c441e032 176 friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
lixianyu 0:d8f4c441e032 177 #if 0
lixianyu 0:d8f4c441e032 178 friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs);
lixianyu 0:d8f4c441e032 179 #endif
lixianyu 0:d8f4c441e032 180 // comparison (only works w/ Strings and "strings")
lixianyu 0:d8f4c441e032 181 operator StringIfHelperType() const {
lixianyu 0:d8f4c441e032 182 return buffer ? &String::StringIfHelper : 0;
lixianyu 0:d8f4c441e032 183 }
lixianyu 0:d8f4c441e032 184 int compareTo(const String &s) const;
lixianyu 0:d8f4c441e032 185 unsigned char equals(const String &s) const;
lixianyu 0:d8f4c441e032 186 unsigned char equals(const char *cstr) const;
lixianyu 0:d8f4c441e032 187 unsigned char operator == (const String &rhs) const {
lixianyu 0:d8f4c441e032 188 return equals(rhs);
lixianyu 0:d8f4c441e032 189 }
lixianyu 0:d8f4c441e032 190 unsigned char operator == (const char *cstr) const {
lixianyu 0:d8f4c441e032 191 return equals(cstr);
lixianyu 0:d8f4c441e032 192 }
lixianyu 0:d8f4c441e032 193 unsigned char operator != (const String &rhs) const {
lixianyu 0:d8f4c441e032 194 return !equals(rhs);
lixianyu 0:d8f4c441e032 195 }
lixianyu 0:d8f4c441e032 196 unsigned char operator != (const char *cstr) const {
lixianyu 0:d8f4c441e032 197 return !equals(cstr);
lixianyu 0:d8f4c441e032 198 }
lixianyu 0:d8f4c441e032 199 unsigned char operator < (const String &rhs) const;
lixianyu 0:d8f4c441e032 200 unsigned char operator > (const String &rhs) const;
lixianyu 0:d8f4c441e032 201 unsigned char operator <= (const String &rhs) const;
lixianyu 0:d8f4c441e032 202 unsigned char operator >= (const String &rhs) const;
lixianyu 0:d8f4c441e032 203 unsigned char equalsIgnoreCase(const String &s) const;
lixianyu 0:d8f4c441e032 204 unsigned char startsWith( const String &prefix) const;
lixianyu 0:d8f4c441e032 205 unsigned char startsWith(const String &prefix, unsigned int offset) const;
lixianyu 0:d8f4c441e032 206 unsigned char endsWith(const String &suffix) const;
lixianyu 0:d8f4c441e032 207
lixianyu 0:d8f4c441e032 208 // character acccess
lixianyu 0:d8f4c441e032 209 char charAt(unsigned int index) const;
lixianyu 0:d8f4c441e032 210 void setCharAt(unsigned int index, char c);
lixianyu 0:d8f4c441e032 211 char operator [] (unsigned int index) const;
lixianyu 0:d8f4c441e032 212 char& operator [] (unsigned int index);
lixianyu 0:d8f4c441e032 213 void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
lixianyu 0:d8f4c441e032 214 void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const {
lixianyu 0:d8f4c441e032 215 getBytes((unsigned char *)buf, bufsize, index);
lixianyu 0:d8f4c441e032 216 }
lixianyu 0:d8f4c441e032 217 const char * c_str() const {
lixianyu 0:d8f4c441e032 218 return buffer;
lixianyu 0:d8f4c441e032 219 }
lixianyu 0:d8f4c441e032 220 const char* begin() {
lixianyu 0:d8f4c441e032 221 return c_str();
lixianyu 0:d8f4c441e032 222 }
lixianyu 0:d8f4c441e032 223 const char* end() {
lixianyu 0:d8f4c441e032 224 return c_str() + length();
lixianyu 0:d8f4c441e032 225 }
lixianyu 0:d8f4c441e032 226
lixianyu 0:d8f4c441e032 227 // search
lixianyu 0:d8f4c441e032 228 int indexOf( char ch ) const;
lixianyu 0:d8f4c441e032 229 int indexOf( char ch, unsigned int fromIndex ) const;
lixianyu 0:d8f4c441e032 230 int indexOf( const String &str ) const;
lixianyu 0:d8f4c441e032 231 int indexOf( const String &str, unsigned int fromIndex ) const;
lixianyu 0:d8f4c441e032 232 int lastIndexOf( char ch ) const;
lixianyu 0:d8f4c441e032 233 int lastIndexOf( char ch, unsigned int fromIndex ) const;
lixianyu 0:d8f4c441e032 234 int lastIndexOf( const String &str ) const;
lixianyu 0:d8f4c441e032 235 int lastIndexOf( const String &str, unsigned int fromIndex ) const;
lixianyu 0:d8f4c441e032 236 String substring( unsigned int beginIndex ) const {
lixianyu 0:d8f4c441e032 237 return substring(beginIndex, len);
lixianyu 0:d8f4c441e032 238 };
lixianyu 0:d8f4c441e032 239 String substring( unsigned int beginIndex, unsigned int endIndex ) const;
lixianyu 0:d8f4c441e032 240
lixianyu 0:d8f4c441e032 241 // modification
lixianyu 0:d8f4c441e032 242 void replace(char find, char replace);
lixianyu 0:d8f4c441e032 243 void replace(const String& find, const String& replace);
lixianyu 0:d8f4c441e032 244 void remove(unsigned int index);
lixianyu 0:d8f4c441e032 245 void remove(unsigned int index, unsigned int count);
lixianyu 0:d8f4c441e032 246 void toLowerCase(void);
lixianyu 0:d8f4c441e032 247 void toUpperCase(void);
lixianyu 0:d8f4c441e032 248 void trim(void);
lixianyu 0:d8f4c441e032 249
lixianyu 0:d8f4c441e032 250 // parsing/conversion
lixianyu 0:d8f4c441e032 251 long toInt(void) const;
lixianyu 0:d8f4c441e032 252 float toFloat(void) const;
lixianyu 0:d8f4c441e032 253
lixianyu 0:d8f4c441e032 254 protected:
lixianyu 0:d8f4c441e032 255 char *buffer; // the actual char array
lixianyu 0:d8f4c441e032 256 unsigned int capacity; // the array length minus one (for the '\0')
lixianyu 0:d8f4c441e032 257 unsigned int len; // the String length (not counting the '\0')
lixianyu 0:d8f4c441e032 258 protected:
lixianyu 0:d8f4c441e032 259 void init(void);
lixianyu 0:d8f4c441e032 260 void invalidate(void);
lixianyu 0:d8f4c441e032 261 unsigned char changeBuffer(unsigned int maxStrLen);
lixianyu 0:d8f4c441e032 262 unsigned char concat(const char *cstr, unsigned int length);
lixianyu 0:d8f4c441e032 263
lixianyu 0:d8f4c441e032 264 // copy and move
lixianyu 0:d8f4c441e032 265 String & copy(const char *cstr, unsigned int length);
lixianyu 0:d8f4c441e032 266 #if 0
lixianyu 0:d8f4c441e032 267 String & copy(const __FlashStringHelper *pstr, unsigned int length);
lixianyu 0:d8f4c441e032 268 #endif
lixianyu 0:d8f4c441e032 269 #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
lixianyu 0:d8f4c441e032 270 void move(String &rhs);
lixianyu 0:d8f4c441e032 271 #endif
lixianyu 0:d8f4c441e032 272 };
lixianyu 0:d8f4c441e032 273
lixianyu 0:d8f4c441e032 274 class StringSumHelper : public String
lixianyu 0:d8f4c441e032 275 {
lixianyu 0:d8f4c441e032 276 public:
lixianyu 0:d8f4c441e032 277 StringSumHelper(const String &s) : String(s) {}
lixianyu 0:d8f4c441e032 278 StringSumHelper(const char *p) : String(p) {}
lixianyu 0:d8f4c441e032 279 StringSumHelper(char c) : String(c) {}
lixianyu 0:d8f4c441e032 280 StringSumHelper(unsigned char num) : String(num) {}
lixianyu 0:d8f4c441e032 281 StringSumHelper(int num) : String(num) {}
lixianyu 0:d8f4c441e032 282 StringSumHelper(unsigned int num) : String(num) {}
lixianyu 0:d8f4c441e032 283 StringSumHelper(long num) : String(num) {}
lixianyu 0:d8f4c441e032 284 StringSumHelper(unsigned long num) : String(num) {}
lixianyu 0:d8f4c441e032 285 StringSumHelper(float num) : String(num) {}
lixianyu 0:d8f4c441e032 286 StringSumHelper(double num) : String(num) {}
lixianyu 0:d8f4c441e032 287 };
lixianyu 0:d8f4c441e032 288
lixianyu 0:d8f4c441e032 289 #endif // __cplusplus
lixianyu 0:d8f4c441e032 290 #endif // String_class_h
lixianyu 0:d8f4c441e032 291