pengfei wu
/
ESP8266Program
For test
WString.h@35:90be2bc2a492, 2015-02-05 (annotated)
- Committer:
- shennongmin
- Date:
- Thu Feb 05 06:48:45 2015 +0000
- Revision:
- 35:90be2bc2a492
- Parent:
- 10:9d4ec0359a5c
compile okay and test okay
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shennongmin | 5:218b5decd1ea | 1 | /* |
shennongmin | 5:218b5decd1ea | 2 | WString.h - String library for Wiring & Arduino |
shennongmin | 5:218b5decd1ea | 3 | ...mostly rewritten by Paul Stoffregen... |
shennongmin | 5:218b5decd1ea | 4 | Copyright (c) 2009-10 Hernando Barragan. All right reserved. |
shennongmin | 5:218b5decd1ea | 5 | Copyright 2011, Paul Stoffregen, paul@pjrc.com |
shennongmin | 5:218b5decd1ea | 6 | |
shennongmin | 5:218b5decd1ea | 7 | This library is free software; you can redistribute it and/or |
shennongmin | 5:218b5decd1ea | 8 | modify it under the terms of the GNU Lesser General Public |
shennongmin | 5:218b5decd1ea | 9 | License as published by the Free Software Foundation; either |
shennongmin | 5:218b5decd1ea | 10 | version 2.1 of the License, or (at your option) any later version. |
shennongmin | 5:218b5decd1ea | 11 | |
shennongmin | 5:218b5decd1ea | 12 | This library is distributed in the hope that it will be useful, |
shennongmin | 5:218b5decd1ea | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
shennongmin | 5:218b5decd1ea | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
shennongmin | 5:218b5decd1ea | 15 | Lesser General Public License for more details. |
shennongmin | 5:218b5decd1ea | 16 | |
shennongmin | 5:218b5decd1ea | 17 | You should have received a copy of the GNU Lesser General Public |
shennongmin | 5:218b5decd1ea | 18 | License along with this library; if not, write to the Free Software |
shennongmin | 5:218b5decd1ea | 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
shennongmin | 5:218b5decd1ea | 20 | */ |
shennongmin | 5:218b5decd1ea | 21 | |
shennongmin | 10:9d4ec0359a5c | 22 | #ifndef __WSTRING_H__ |
shennongmin | 10:9d4ec0359a5c | 23 | #define __WSTRING_H__ |
shennongmin | 5:218b5decd1ea | 24 | #ifdef __cplusplus |
shennongmin | 5:218b5decd1ea | 25 | |
shennongmin | 6:a8cf39c768c4 | 26 | #include "mbed.h" |
shennongmin | 6:a8cf39c768c4 | 27 | |
shennongmin | 6:a8cf39c768c4 | 28 | #include "itoas.h" |
shennongmin | 6:a8cf39c768c4 | 29 | |
shennongmin | 5:218b5decd1ea | 30 | #include <ctype.h> |
shennongmin | 6:a8cf39c768c4 | 31 | #include <cstddef> |
shennongmin | 6:a8cf39c768c4 | 32 | #include <cstdlib> |
shennongmin | 6:a8cf39c768c4 | 33 | #include <cstdio> |
shennongmin | 6:a8cf39c768c4 | 34 | #include <cstring> |
shennongmin | 5:218b5decd1ea | 35 | |
shennongmin | 5:218b5decd1ea | 36 | |
shennongmin | 5:218b5decd1ea | 37 | // An inherited class for holding the result of a concatenation. These |
shennongmin | 5:218b5decd1ea | 38 | // result objects are assumed to be writable by subsequent concatenations. |
shennongmin | 5:218b5decd1ea | 39 | class StringSumHelper; |
shennongmin | 5:218b5decd1ea | 40 | |
shennongmin | 5:218b5decd1ea | 41 | // The string class |
shennongmin | 5:218b5decd1ea | 42 | class String |
shennongmin | 5:218b5decd1ea | 43 | { |
shennongmin | 5:218b5decd1ea | 44 | // use a function pointer to allow for "if (s)" without the |
shennongmin | 5:218b5decd1ea | 45 | // complications of an operator bool(). for more information, see: |
shennongmin | 5:218b5decd1ea | 46 | // http://www.artima.com/cppsource/safebool.html |
shennongmin | 5:218b5decd1ea | 47 | typedef void (String::*StringIfHelperType)() const; |
shennongmin | 5:218b5decd1ea | 48 | void StringIfHelper() const {} |
shennongmin | 5:218b5decd1ea | 49 | |
shennongmin | 5:218b5decd1ea | 50 | public: |
shennongmin | 5:218b5decd1ea | 51 | // constructors |
shennongmin | 5:218b5decd1ea | 52 | // creates a copy of the initial value. |
shennongmin | 5:218b5decd1ea | 53 | // if the initial value is null or invalid, or if memory allocation |
shennongmin | 5:218b5decd1ea | 54 | // fails, the string will be marked as invalid (i.e. "if (s)" will |
shennongmin | 5:218b5decd1ea | 55 | // be false). |
shennongmin | 5:218b5decd1ea | 56 | String(const char *cstr = ""); |
shennongmin | 5:218b5decd1ea | 57 | String(const String &str); |
shennongmin | 5:218b5decd1ea | 58 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
shennongmin | 5:218b5decd1ea | 59 | String(String &&rval); |
shennongmin | 5:218b5decd1ea | 60 | String(StringSumHelper &&rval); |
shennongmin | 5:218b5decd1ea | 61 | #endif |
shennongmin | 5:218b5decd1ea | 62 | explicit String(char c); |
shennongmin | 5:218b5decd1ea | 63 | explicit String(unsigned char, unsigned char base=10); |
shennongmin | 5:218b5decd1ea | 64 | explicit String(int, unsigned char base=10); |
shennongmin | 5:218b5decd1ea | 65 | explicit String(unsigned int, unsigned char base=10); |
shennongmin | 5:218b5decd1ea | 66 | explicit String(long, unsigned char base=10); |
shennongmin | 5:218b5decd1ea | 67 | explicit String(unsigned long, unsigned char base=10); |
shennongmin | 5:218b5decd1ea | 68 | ~String(void); |
shennongmin | 5:218b5decd1ea | 69 | |
shennongmin | 5:218b5decd1ea | 70 | // memory management |
shennongmin | 5:218b5decd1ea | 71 | // return true on success, false on failure (in which case, the string |
shennongmin | 5:218b5decd1ea | 72 | // is left unchanged). reserve(0), if successful, will validate an |
shennongmin | 5:218b5decd1ea | 73 | // invalid string (i.e., "if (s)" will be true afterwards) |
shennongmin | 5:218b5decd1ea | 74 | unsigned char reserve(unsigned int size); |
shennongmin | 5:218b5decd1ea | 75 | inline unsigned int length(void) const {return len;} |
shennongmin | 5:218b5decd1ea | 76 | |
shennongmin | 5:218b5decd1ea | 77 | // creates a copy of the assigned value. if the value is null or |
shennongmin | 5:218b5decd1ea | 78 | // invalid, or if the memory allocation fails, the string will be |
shennongmin | 5:218b5decd1ea | 79 | // marked as invalid ("if (s)" will be false). |
shennongmin | 5:218b5decd1ea | 80 | String & operator = (const String &rhs); |
shennongmin | 5:218b5decd1ea | 81 | String & operator = (const char *cstr); |
shennongmin | 5:218b5decd1ea | 82 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
shennongmin | 5:218b5decd1ea | 83 | String & operator = (String &&rval); |
shennongmin | 5:218b5decd1ea | 84 | String & operator = (StringSumHelper &&rval); |
shennongmin | 5:218b5decd1ea | 85 | #endif |
shennongmin | 5:218b5decd1ea | 86 | |
shennongmin | 5:218b5decd1ea | 87 | // concatenate (works w/ built-in types) |
shennongmin | 5:218b5decd1ea | 88 | |
shennongmin | 5:218b5decd1ea | 89 | // returns true on success, false on failure (in which case, the string |
shennongmin | 5:218b5decd1ea | 90 | // is left unchanged). if the argument is null or invalid, the |
shennongmin | 5:218b5decd1ea | 91 | // concatenation is considered unsucessful. |
shennongmin | 5:218b5decd1ea | 92 | unsigned char concat(const String &str); |
shennongmin | 5:218b5decd1ea | 93 | unsigned char concat(const char *cstr); |
shennongmin | 5:218b5decd1ea | 94 | unsigned char concat(char c); |
shennongmin | 5:218b5decd1ea | 95 | unsigned char concat(unsigned char c); |
shennongmin | 5:218b5decd1ea | 96 | unsigned char concat(int num); |
shennongmin | 5:218b5decd1ea | 97 | unsigned char concat(unsigned int num); |
shennongmin | 5:218b5decd1ea | 98 | unsigned char concat(long num); |
shennongmin | 5:218b5decd1ea | 99 | unsigned char concat(unsigned long num); |
shennongmin | 5:218b5decd1ea | 100 | |
shennongmin | 5:218b5decd1ea | 101 | // if there's not enough memory for the concatenated value, the string |
shennongmin | 5:218b5decd1ea | 102 | // will be left unchanged (but this isn't signalled in any way) |
shennongmin | 5:218b5decd1ea | 103 | String & operator += (const String &rhs) {concat(rhs); return (*this);} |
shennongmin | 5:218b5decd1ea | 104 | String & operator += (const char *cstr) {concat(cstr); return (*this);} |
shennongmin | 5:218b5decd1ea | 105 | String & operator += (char c) {concat(c); return (*this);} |
shennongmin | 5:218b5decd1ea | 106 | String & operator += (unsigned char num) {concat(num); return (*this);} |
shennongmin | 5:218b5decd1ea | 107 | String & operator += (int num) {concat(num); return (*this);} |
shennongmin | 5:218b5decd1ea | 108 | String & operator += (unsigned int num) {concat(num); return (*this);} |
shennongmin | 5:218b5decd1ea | 109 | String & operator += (long num) {concat(num); return (*this);} |
shennongmin | 5:218b5decd1ea | 110 | String & operator += (unsigned long num) {concat(num); return (*this);} |
shennongmin | 5:218b5decd1ea | 111 | |
shennongmin | 5:218b5decd1ea | 112 | friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); |
shennongmin | 5:218b5decd1ea | 113 | friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); |
shennongmin | 5:218b5decd1ea | 114 | friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); |
shennongmin | 5:218b5decd1ea | 115 | friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num); |
shennongmin | 5:218b5decd1ea | 116 | friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); |
shennongmin | 5:218b5decd1ea | 117 | friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); |
shennongmin | 5:218b5decd1ea | 118 | friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); |
shennongmin | 5:218b5decd1ea | 119 | friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); |
shennongmin | 5:218b5decd1ea | 120 | |
shennongmin | 5:218b5decd1ea | 121 | // comparison (only works w/ Strings and "strings") |
shennongmin | 5:218b5decd1ea | 122 | operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } |
shennongmin | 5:218b5decd1ea | 123 | int compareTo(const String &s) const; |
shennongmin | 5:218b5decd1ea | 124 | unsigned char equals(const String &s) const; |
shennongmin | 5:218b5decd1ea | 125 | unsigned char equals(const char *cstr) const; |
shennongmin | 5:218b5decd1ea | 126 | unsigned char operator == (const String &rhs) const {return equals(rhs);} |
shennongmin | 5:218b5decd1ea | 127 | unsigned char operator == (const char *cstr) const {return equals(cstr);} |
shennongmin | 5:218b5decd1ea | 128 | unsigned char operator != (const String &rhs) const {return !equals(rhs);} |
shennongmin | 5:218b5decd1ea | 129 | unsigned char operator != (const char *cstr) const {return !equals(cstr);} |
shennongmin | 5:218b5decd1ea | 130 | unsigned char operator < (const String &rhs) const; |
shennongmin | 5:218b5decd1ea | 131 | unsigned char operator > (const String &rhs) const; |
shennongmin | 5:218b5decd1ea | 132 | unsigned char operator <= (const String &rhs) const; |
shennongmin | 5:218b5decd1ea | 133 | unsigned char operator >= (const String &rhs) const; |
shennongmin | 5:218b5decd1ea | 134 | unsigned char equalsIgnoreCase(const String &s) const; |
shennongmin | 5:218b5decd1ea | 135 | unsigned char startsWith( const String &prefix) const; |
shennongmin | 5:218b5decd1ea | 136 | unsigned char startsWith(const String &prefix, unsigned int offset) const; |
shennongmin | 5:218b5decd1ea | 137 | unsigned char endsWith(const String &suffix) const; |
shennongmin | 5:218b5decd1ea | 138 | |
shennongmin | 5:218b5decd1ea | 139 | // character acccess |
shennongmin | 5:218b5decd1ea | 140 | char charAt(unsigned int index) const; |
shennongmin | 5:218b5decd1ea | 141 | void setCharAt(unsigned int index, char c); |
shennongmin | 5:218b5decd1ea | 142 | char operator [] (unsigned int index) const; |
shennongmin | 5:218b5decd1ea | 143 | char& operator [] (unsigned int index); |
shennongmin | 5:218b5decd1ea | 144 | void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const; |
shennongmin | 5:218b5decd1ea | 145 | void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const |
shennongmin | 5:218b5decd1ea | 146 | {getBytes((unsigned char *)buf, bufsize, index);} |
shennongmin | 5:218b5decd1ea | 147 | const char * c_str() const { return buffer; } |
shennongmin | 5:218b5decd1ea | 148 | |
shennongmin | 5:218b5decd1ea | 149 | // search |
shennongmin | 5:218b5decd1ea | 150 | int indexOf( char ch ) const; |
shennongmin | 5:218b5decd1ea | 151 | int indexOf( char ch, unsigned int fromIndex ) const; |
shennongmin | 5:218b5decd1ea | 152 | int indexOf( const String &str ) const; |
shennongmin | 5:218b5decd1ea | 153 | int indexOf( const String &str, unsigned int fromIndex ) const; |
shennongmin | 5:218b5decd1ea | 154 | int lastIndexOf( char ch ) const; |
shennongmin | 5:218b5decd1ea | 155 | int lastIndexOf( char ch, unsigned int fromIndex ) const; |
shennongmin | 5:218b5decd1ea | 156 | int lastIndexOf( const String &str ) const; |
shennongmin | 5:218b5decd1ea | 157 | int lastIndexOf( const String &str, unsigned int fromIndex ) const; |
shennongmin | 5:218b5decd1ea | 158 | String substring( unsigned int beginIndex ) const; |
shennongmin | 5:218b5decd1ea | 159 | String substring( unsigned int beginIndex, unsigned int endIndex ) const; |
shennongmin | 5:218b5decd1ea | 160 | |
shennongmin | 5:218b5decd1ea | 161 | // modification |
shennongmin | 5:218b5decd1ea | 162 | void replace(char find, char replace); |
shennongmin | 5:218b5decd1ea | 163 | void replace(const String& find, const String& replace); |
shennongmin | 5:218b5decd1ea | 164 | void toLowerCase(void); |
shennongmin | 5:218b5decd1ea | 165 | void toUpperCase(void); |
shennongmin | 5:218b5decd1ea | 166 | void trim(void); |
shennongmin | 5:218b5decd1ea | 167 | |
shennongmin | 5:218b5decd1ea | 168 | // parsing/conversion |
shennongmin | 5:218b5decd1ea | 169 | long toInt(void) const; |
shennongmin | 5:218b5decd1ea | 170 | |
shennongmin | 5:218b5decd1ea | 171 | protected: |
shennongmin | 5:218b5decd1ea | 172 | char *buffer; // the actual char array |
shennongmin | 5:218b5decd1ea | 173 | unsigned int capacity; // the array length minus one (for the '\0') |
shennongmin | 5:218b5decd1ea | 174 | unsigned int len; // the String length (not counting the '\0') |
shennongmin | 5:218b5decd1ea | 175 | unsigned char flags; // unused, for future features |
shennongmin | 5:218b5decd1ea | 176 | protected: |
shennongmin | 5:218b5decd1ea | 177 | void init(void); |
shennongmin | 5:218b5decd1ea | 178 | void invalidate(void); |
shennongmin | 5:218b5decd1ea | 179 | unsigned char changeBuffer(unsigned int maxStrLen); |
shennongmin | 5:218b5decd1ea | 180 | unsigned char concat(const char *cstr, unsigned int length); |
shennongmin | 5:218b5decd1ea | 181 | |
shennongmin | 5:218b5decd1ea | 182 | // copy and move |
shennongmin | 5:218b5decd1ea | 183 | String & copy(const char *cstr, unsigned int length); |
shennongmin | 5:218b5decd1ea | 184 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ |
shennongmin | 5:218b5decd1ea | 185 | void move(String &rhs); |
shennongmin | 5:218b5decd1ea | 186 | #endif |
shennongmin | 5:218b5decd1ea | 187 | }; |
shennongmin | 5:218b5decd1ea | 188 | |
shennongmin | 5:218b5decd1ea | 189 | class StringSumHelper : public String |
shennongmin | 5:218b5decd1ea | 190 | { |
shennongmin | 5:218b5decd1ea | 191 | public: |
shennongmin | 5:218b5decd1ea | 192 | StringSumHelper(const String &s) : String(s) {} |
shennongmin | 5:218b5decd1ea | 193 | StringSumHelper(const char *p) : String(p) {} |
shennongmin | 5:218b5decd1ea | 194 | StringSumHelper(char c) : String(c) {} |
shennongmin | 5:218b5decd1ea | 195 | StringSumHelper(unsigned char num) : String(num) {} |
shennongmin | 5:218b5decd1ea | 196 | StringSumHelper(int num) : String(num) {} |
shennongmin | 5:218b5decd1ea | 197 | StringSumHelper(unsigned int num) : String(num) {} |
shennongmin | 5:218b5decd1ea | 198 | StringSumHelper(long num) : String(num) {} |
shennongmin | 5:218b5decd1ea | 199 | StringSumHelper(unsigned long num) : String(num) {} |
shennongmin | 5:218b5decd1ea | 200 | }; |
shennongmin | 5:218b5decd1ea | 201 | |
shennongmin | 5:218b5decd1ea | 202 | #endif // __cplusplus |
shennongmin | 10:9d4ec0359a5c | 203 | #endif // __WSTRING_H__ |