Microduino

Dependencies:   mbed

Fork of LED_DZ by Li Weiyi

Committer:
lixianyu
Date:
Tue May 31 15:32:03 2016 +0000
Revision:
0:5ca227682ee7
???????(Pass compile!)

Who changed what in which revision?

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