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