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