Takumi Odashima / ArduinoUsbHostShield

Dependents:   USBHOST_PS5

Committer:
robo_ichinoseki_a
Date:
Sat May 02 05:56:48 2020 +0000
Revision:
1:da31140f2a1c
Parent:
0:b1ce54272580
update

Who changed what in which revision?

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