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