For test

Dependencies:   mbed

Committer:
shennongmin
Date:
Fri Jan 30 12:19:57 2015 +0000
Revision:
5:218b5decd1ea
Child:
6:a8cf39c768c4
Add WString class and ready to transplant.

Who changed what in which revision?

UserRevisionLine numberNew 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 5:218b5decd1ea 22 #ifndef String_class_h
shennongmin 5:218b5decd1ea 23 #define String_class_h
shennongmin 5:218b5decd1ea 24 #ifdef __cplusplus
shennongmin 5:218b5decd1ea 25
shennongmin 5:218b5decd1ea 26 #include <stdlib.h>
shennongmin 5:218b5decd1ea 27 #include <string.h>
shennongmin 5:218b5decd1ea 28 #include <ctype.h>
shennongmin 5:218b5decd1ea 29
shennongmin 5:218b5decd1ea 30
shennongmin 5:218b5decd1ea 31 // An inherited class for holding the result of a concatenation. These
shennongmin 5:218b5decd1ea 32 // result objects are assumed to be writable by subsequent concatenations.
shennongmin 5:218b5decd1ea 33 class StringSumHelper;
shennongmin 5:218b5decd1ea 34
shennongmin 5:218b5decd1ea 35 // The string class
shennongmin 5:218b5decd1ea 36 class String
shennongmin 5:218b5decd1ea 37 {
shennongmin 5:218b5decd1ea 38 // use a function pointer to allow for "if (s)" without the
shennongmin 5:218b5decd1ea 39 // complications of an operator bool(). for more information, see:
shennongmin 5:218b5decd1ea 40 // http://www.artima.com/cppsource/safebool.html
shennongmin 5:218b5decd1ea 41 typedef void (String::*StringIfHelperType)() const;
shennongmin 5:218b5decd1ea 42 void StringIfHelper() const {}
shennongmin 5:218b5decd1ea 43
shennongmin 5:218b5decd1ea 44 public:
shennongmin 5:218b5decd1ea 45 // constructors
shennongmin 5:218b5decd1ea 46 // creates a copy of the initial value.
shennongmin 5:218b5decd1ea 47 // if the initial value is null or invalid, or if memory allocation
shennongmin 5:218b5decd1ea 48 // fails, the string will be marked as invalid (i.e. "if (s)" will
shennongmin 5:218b5decd1ea 49 // be false).
shennongmin 5:218b5decd1ea 50 String(const char *cstr = "");
shennongmin 5:218b5decd1ea 51 String(const String &str);
shennongmin 5:218b5decd1ea 52 #ifdef __GXX_EXPERIMENTAL_CXX0X__
shennongmin 5:218b5decd1ea 53 String(String &&rval);
shennongmin 5:218b5decd1ea 54 String(StringSumHelper &&rval);
shennongmin 5:218b5decd1ea 55 #endif
shennongmin 5:218b5decd1ea 56 explicit String(char c);
shennongmin 5:218b5decd1ea 57 explicit String(unsigned char, unsigned char base=10);
shennongmin 5:218b5decd1ea 58 explicit String(int, unsigned char base=10);
shennongmin 5:218b5decd1ea 59 explicit String(unsigned int, unsigned char base=10);
shennongmin 5:218b5decd1ea 60 explicit String(long, unsigned char base=10);
shennongmin 5:218b5decd1ea 61 explicit String(unsigned long, unsigned char base=10);
shennongmin 5:218b5decd1ea 62 ~String(void);
shennongmin 5:218b5decd1ea 63
shennongmin 5:218b5decd1ea 64 // memory management
shennongmin 5:218b5decd1ea 65 // return true on success, false on failure (in which case, the string
shennongmin 5:218b5decd1ea 66 // is left unchanged). reserve(0), if successful, will validate an
shennongmin 5:218b5decd1ea 67 // invalid string (i.e., "if (s)" will be true afterwards)
shennongmin 5:218b5decd1ea 68 unsigned char reserve(unsigned int size);
shennongmin 5:218b5decd1ea 69 inline unsigned int length(void) const {return len;}
shennongmin 5:218b5decd1ea 70
shennongmin 5:218b5decd1ea 71 // creates a copy of the assigned value. if the value is null or
shennongmin 5:218b5decd1ea 72 // invalid, or if the memory allocation fails, the string will be
shennongmin 5:218b5decd1ea 73 // marked as invalid ("if (s)" will be false).
shennongmin 5:218b5decd1ea 74 String & operator = (const String &rhs);
shennongmin 5:218b5decd1ea 75 String & operator = (const char *cstr);
shennongmin 5:218b5decd1ea 76 #ifdef __GXX_EXPERIMENTAL_CXX0X__
shennongmin 5:218b5decd1ea 77 String & operator = (String &&rval);
shennongmin 5:218b5decd1ea 78 String & operator = (StringSumHelper &&rval);
shennongmin 5:218b5decd1ea 79 #endif
shennongmin 5:218b5decd1ea 80
shennongmin 5:218b5decd1ea 81 // concatenate (works w/ built-in types)
shennongmin 5:218b5decd1ea 82
shennongmin 5:218b5decd1ea 83 // returns true on success, false on failure (in which case, the string
shennongmin 5:218b5decd1ea 84 // is left unchanged). if the argument is null or invalid, the
shennongmin 5:218b5decd1ea 85 // concatenation is considered unsucessful.
shennongmin 5:218b5decd1ea 86 unsigned char concat(const String &str);
shennongmin 5:218b5decd1ea 87 unsigned char concat(const char *cstr);
shennongmin 5:218b5decd1ea 88 unsigned char concat(char c);
shennongmin 5:218b5decd1ea 89 unsigned char concat(unsigned char c);
shennongmin 5:218b5decd1ea 90 unsigned char concat(int num);
shennongmin 5:218b5decd1ea 91 unsigned char concat(unsigned int num);
shennongmin 5:218b5decd1ea 92 unsigned char concat(long num);
shennongmin 5:218b5decd1ea 93 unsigned char concat(unsigned long num);
shennongmin 5:218b5decd1ea 94
shennongmin 5:218b5decd1ea 95 // if there's not enough memory for the concatenated value, the string
shennongmin 5:218b5decd1ea 96 // will be left unchanged (but this isn't signalled in any way)
shennongmin 5:218b5decd1ea 97 String & operator += (const String &rhs) {concat(rhs); return (*this);}
shennongmin 5:218b5decd1ea 98 String & operator += (const char *cstr) {concat(cstr); return (*this);}
shennongmin 5:218b5decd1ea 99 String & operator += (char c) {concat(c); return (*this);}
shennongmin 5:218b5decd1ea 100 String & operator += (unsigned char num) {concat(num); return (*this);}
shennongmin 5:218b5decd1ea 101 String & operator += (int num) {concat(num); return (*this);}
shennongmin 5:218b5decd1ea 102 String & operator += (unsigned int num) {concat(num); return (*this);}
shennongmin 5:218b5decd1ea 103 String & operator += (long num) {concat(num); return (*this);}
shennongmin 5:218b5decd1ea 104 String & operator += (unsigned long num) {concat(num); return (*this);}
shennongmin 5:218b5decd1ea 105
shennongmin 5:218b5decd1ea 106 friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
shennongmin 5:218b5decd1ea 107 friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
shennongmin 5:218b5decd1ea 108 friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
shennongmin 5:218b5decd1ea 109 friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
shennongmin 5:218b5decd1ea 110 friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
shennongmin 5:218b5decd1ea 111 friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
shennongmin 5:218b5decd1ea 112 friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
shennongmin 5:218b5decd1ea 113 friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
shennongmin 5:218b5decd1ea 114
shennongmin 5:218b5decd1ea 115 // comparison (only works w/ Strings and "strings")
shennongmin 5:218b5decd1ea 116 operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
shennongmin 5:218b5decd1ea 117 int compareTo(const String &s) const;
shennongmin 5:218b5decd1ea 118 unsigned char equals(const String &s) const;
shennongmin 5:218b5decd1ea 119 unsigned char equals(const char *cstr) const;
shennongmin 5:218b5decd1ea 120 unsigned char operator == (const String &rhs) const {return equals(rhs);}
shennongmin 5:218b5decd1ea 121 unsigned char operator == (const char *cstr) const {return equals(cstr);}
shennongmin 5:218b5decd1ea 122 unsigned char operator != (const String &rhs) const {return !equals(rhs);}
shennongmin 5:218b5decd1ea 123 unsigned char operator != (const char *cstr) const {return !equals(cstr);}
shennongmin 5:218b5decd1ea 124 unsigned char operator < (const String &rhs) const;
shennongmin 5:218b5decd1ea 125 unsigned char operator > (const String &rhs) const;
shennongmin 5:218b5decd1ea 126 unsigned char operator <= (const String &rhs) const;
shennongmin 5:218b5decd1ea 127 unsigned char operator >= (const String &rhs) const;
shennongmin 5:218b5decd1ea 128 unsigned char equalsIgnoreCase(const String &s) const;
shennongmin 5:218b5decd1ea 129 unsigned char startsWith( const String &prefix) const;
shennongmin 5:218b5decd1ea 130 unsigned char startsWith(const String &prefix, unsigned int offset) const;
shennongmin 5:218b5decd1ea 131 unsigned char endsWith(const String &suffix) const;
shennongmin 5:218b5decd1ea 132
shennongmin 5:218b5decd1ea 133 // character acccess
shennongmin 5:218b5decd1ea 134 char charAt(unsigned int index) const;
shennongmin 5:218b5decd1ea 135 void setCharAt(unsigned int index, char c);
shennongmin 5:218b5decd1ea 136 char operator [] (unsigned int index) const;
shennongmin 5:218b5decd1ea 137 char& operator [] (unsigned int index);
shennongmin 5:218b5decd1ea 138 void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
shennongmin 5:218b5decd1ea 139 void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
shennongmin 5:218b5decd1ea 140 {getBytes((unsigned char *)buf, bufsize, index);}
shennongmin 5:218b5decd1ea 141 const char * c_str() const { return buffer; }
shennongmin 5:218b5decd1ea 142
shennongmin 5:218b5decd1ea 143 // search
shennongmin 5:218b5decd1ea 144 int indexOf( char ch ) const;
shennongmin 5:218b5decd1ea 145 int indexOf( char ch, unsigned int fromIndex ) const;
shennongmin 5:218b5decd1ea 146 int indexOf( const String &str ) const;
shennongmin 5:218b5decd1ea 147 int indexOf( const String &str, unsigned int fromIndex ) const;
shennongmin 5:218b5decd1ea 148 int lastIndexOf( char ch ) const;
shennongmin 5:218b5decd1ea 149 int lastIndexOf( char ch, unsigned int fromIndex ) const;
shennongmin 5:218b5decd1ea 150 int lastIndexOf( const String &str ) const;
shennongmin 5:218b5decd1ea 151 int lastIndexOf( const String &str, unsigned int fromIndex ) const;
shennongmin 5:218b5decd1ea 152 String substring( unsigned int beginIndex ) const;
shennongmin 5:218b5decd1ea 153 String substring( unsigned int beginIndex, unsigned int endIndex ) const;
shennongmin 5:218b5decd1ea 154
shennongmin 5:218b5decd1ea 155 // modification
shennongmin 5:218b5decd1ea 156 void replace(char find, char replace);
shennongmin 5:218b5decd1ea 157 void replace(const String& find, const String& replace);
shennongmin 5:218b5decd1ea 158 void toLowerCase(void);
shennongmin 5:218b5decd1ea 159 void toUpperCase(void);
shennongmin 5:218b5decd1ea 160 void trim(void);
shennongmin 5:218b5decd1ea 161
shennongmin 5:218b5decd1ea 162 // parsing/conversion
shennongmin 5:218b5decd1ea 163 long toInt(void) const;
shennongmin 5:218b5decd1ea 164
shennongmin 5:218b5decd1ea 165 protected:
shennongmin 5:218b5decd1ea 166 char *buffer; // the actual char array
shennongmin 5:218b5decd1ea 167 unsigned int capacity; // the array length minus one (for the '\0')
shennongmin 5:218b5decd1ea 168 unsigned int len; // the String length (not counting the '\0')
shennongmin 5:218b5decd1ea 169 unsigned char flags; // unused, for future features
shennongmin 5:218b5decd1ea 170 protected:
shennongmin 5:218b5decd1ea 171 void init(void);
shennongmin 5:218b5decd1ea 172 void invalidate(void);
shennongmin 5:218b5decd1ea 173 unsigned char changeBuffer(unsigned int maxStrLen);
shennongmin 5:218b5decd1ea 174 unsigned char concat(const char *cstr, unsigned int length);
shennongmin 5:218b5decd1ea 175
shennongmin 5:218b5decd1ea 176 // copy and move
shennongmin 5:218b5decd1ea 177 String & copy(const char *cstr, unsigned int length);
shennongmin 5:218b5decd1ea 178 #ifdef __GXX_EXPERIMENTAL_CXX0X__
shennongmin 5:218b5decd1ea 179 void move(String &rhs);
shennongmin 5:218b5decd1ea 180 #endif
shennongmin 5:218b5decd1ea 181 };
shennongmin 5:218b5decd1ea 182
shennongmin 5:218b5decd1ea 183 class StringSumHelper : public String
shennongmin 5:218b5decd1ea 184 {
shennongmin 5:218b5decd1ea 185 public:
shennongmin 5:218b5decd1ea 186 StringSumHelper(const String &s) : String(s) {}
shennongmin 5:218b5decd1ea 187 StringSumHelper(const char *p) : String(p) {}
shennongmin 5:218b5decd1ea 188 StringSumHelper(char c) : String(c) {}
shennongmin 5:218b5decd1ea 189 StringSumHelper(unsigned char num) : String(num) {}
shennongmin 5:218b5decd1ea 190 StringSumHelper(int num) : String(num) {}
shennongmin 5:218b5decd1ea 191 StringSumHelper(unsigned int num) : String(num) {}
shennongmin 5:218b5decd1ea 192 StringSumHelper(long num) : String(num) {}
shennongmin 5:218b5decd1ea 193 StringSumHelper(unsigned long num) : String(num) {}
shennongmin 5:218b5decd1ea 194 };
shennongmin 5:218b5decd1ea 195
shennongmin 5:218b5decd1ea 196 #endif // __cplusplus
shennongmin 5:218b5decd1ea 197 #endif // String_class_h