COSA
An Object-Oriented Platform for Arduino Programming
String.hh
Go to the documentation of this file.
1 
23 #ifndef COSA_STRING_HH
24 #define COSA_STRING_HH
25 
26 #include "Cosa/Types.h"
27 #include "Cosa/IOStream.hh"
28 
32 class __StringSumHelper;
33 
37 class String {
38 
44  typedef void (String::*StringIfHelperType)() const;
45  void StringIfHelper() const {}
46 
47 public:
53  String(const char *cstr = "");
54  String(const String &str);
55  String(str_P str);
56 #if (ARDUINO >= 150)
57  String(String &&rval);
58  String(__StringSumHelper &&rval);
59 #endif
60 
61  explicit String(char c);
62  explicit String(unsigned char, unsigned char base=10);
63  explicit String(int, unsigned char base=10);
64  explicit String(unsigned int, unsigned char base=10);
65  explicit String(long, unsigned char base=10);
66  explicit String(unsigned long, unsigned char base=10);
67  explicit String(float, unsigned char decimalPlaces=2);
68  explicit String(double, unsigned char decimalPlaces=2);
69 
70  ~String(void);
71 
78  unsigned char reserve(unsigned int size);
79 
83  inline unsigned int length(void) const
84  {
85  return (m_length);
86  }
87 
93  String& operator=(const String &rhs);
94  String& operator=(const char *cstr);
95  String& operator=(str_P str);
96 
97 #if (ARDUINO >= 150)
98  String& operator=(String &&rval);
100 #endif
101 
107  unsigned char concat(const String &str);
108  unsigned char concat(const char *cstr);
109  unsigned char concat(char c);
110  unsigned char concat(unsigned char c);
111  unsigned char concat(int num);
112  unsigned char concat(unsigned int num);
113  unsigned char concat(long num);
114  unsigned char concat(unsigned long num);
115  unsigned char concat(float num);
116  unsigned char concat(double num);
117  unsigned char concat(str_P str);
118 
124  String& operator+=(const String &rhs) { concat(rhs); return (*this); }
125  String& operator+=(const char *cstr) { concat(cstr); return (*this); }
126  String& operator+=(char c) { concat(c); return (*this); }
127  String& operator+=(unsigned char num) { concat(num); return (*this); }
128  String& operator+=(int num) { concat(num); return (*this); }
129  String& operator+=(unsigned int num) {concat(num); return (*this); }
130  String& operator+=(long num) { concat(num); return (*this); }
131  String& operator+=(unsigned long num) { concat(num); return (*this); }
132  String& operator+=(float num) { concat(num); return (*this); }
133  String& operator+=(double num) { concat(num); return (*this); }
134  String& operator+=(str_P str) { concat(str); return (*this);}
135 
136  friend __StringSumHelper& operator+(const __StringSumHelper &lhs, const String &rhs);
137  friend __StringSumHelper& operator+(const __StringSumHelper &lhs, const char *cstr);
138  friend __StringSumHelper& operator+(const __StringSumHelper &lhs, char c);
139  friend __StringSumHelper& operator+(const __StringSumHelper &lhs, unsigned char num);
140  friend __StringSumHelper& operator+(const __StringSumHelper &lhs, int num);
141  friend __StringSumHelper& operator+(const __StringSumHelper &lhs, unsigned int num);
142  friend __StringSumHelper& operator+(const __StringSumHelper &lhs, long num);
143  friend __StringSumHelper& operator+(const __StringSumHelper &lhs, unsigned long num);
144  friend __StringSumHelper& operator+(const __StringSumHelper &lhs, float num);
145  friend __StringSumHelper& operator+(const __StringSumHelper &lhs, double num);
146  friend __StringSumHelper& operator+(const __StringSumHelper &lhs, str_P rhs);
147 
151  operator StringIfHelperType() const
152  {
153  return m_buffer ? &String::StringIfHelper : 0;
154  }
155 
156  int compareTo(const String &s) const;
157  unsigned char equals(const String &s) const;
158  unsigned char equals(const char *cstr) const;
159  unsigned char operator==(const String &rhs) const { return equals(rhs); }
160  unsigned char operator==(const char *cstr) const { return equals(cstr); }
161  unsigned char operator!=(const String &rhs) const { return !equals(rhs); }
162  unsigned char operator!=(const char *cstr) const { return !equals(cstr); }
163  unsigned char operator<(const String &rhs) const;
164  unsigned char operator>(const String &rhs) const;
165  unsigned char operator<=(const String &rhs) const;
166  unsigned char operator>=(const String &rhs) const;
167  unsigned char equalsIgnoreCase(const String &s) const;
168  unsigned char startsWith( const String &prefix) const;
169  unsigned char startsWith(const String &prefix, unsigned int offset) const;
170  unsigned char endsWith(const String &suffix) const;
171 
175  char charAt(unsigned int index) const;
176  void setCharAt(unsigned int index, char c);
177  char operator[](unsigned int index) const;
178  char& operator[](unsigned int index);
179  void getBytes(unsigned char* buf, unsigned int bufsize, unsigned int index = 0) const;
180 
181  void toCharArray(char* buf, unsigned int bufsize, unsigned int index = 0) const
182  {
183  getBytes((unsigned char*) buf, bufsize, index);
184  }
185 
186  const char* c_str() const { return (m_buffer); }
187 
191  int indexOf(char ch) const;
192  int indexOf(char ch, unsigned int fromIndex) const;
193  int indexOf(const String &str) const;
194  int indexOf(const String &str, unsigned int fromIndex) const;
195  int lastIndexOf(char ch) const;
196  int lastIndexOf(char ch, unsigned int fromIndex) const;
197  int lastIndexOf(const String &str) const;
198  int lastIndexOf(const String &str, unsigned int fromIndex) const;
199 
203  String substring(unsigned int beginIndex, unsigned int endIndex) const;
204  String substring(unsigned int beginIndex) const
205  {
206  return substring(beginIndex, m_length);
207  };
208 
212  void replace(char find, char replace);
213  void replace(const String& find, const String& replace);
214  void remove(unsigned int index);
215  void remove(unsigned int index, unsigned int count);
216  void toLowerCase(void);
217  void toUpperCase(void);
218  void trim(void);
219 
223  long toInt(void) const;
224  float toFloat(void) const;
225 
226 protected:
227  char* m_buffer;
228  unsigned int m_capacity;
229  unsigned int m_length;
230 
231  void init(void);
232  void invalidate(void);
233  unsigned char changeBuffer(unsigned int maxStrLen);
234  unsigned char concat(const char *cstr, unsigned int length);
235 
239  String& copy(const char *cstr, unsigned int length);
240  String& copy(str_P pstr, unsigned int length);
241  void move(String &rhs);
242 };
243 
249 class __StringSumHelper : public String
250 {
251 public:
252  __StringSumHelper(const String& s) : String(s) {}
253  __StringSumHelper(const char* p) : String(p) {}
254  __StringSumHelper(char c) : String(c) {}
255  __StringSumHelper(unsigned char num) : String(num) {}
256  __StringSumHelper(int num) : String(num) {}
257  __StringSumHelper(unsigned int num) : String(num) {}
258  __StringSumHelper(long num) : String(num) {}
259  __StringSumHelper(unsigned long num) : String(num) {}
260  __StringSumHelper(float num) : String(num) {}
261  __StringSumHelper(double num) : String(num) {}
262 };
263 
269 inline IOStream& operator<<(IOStream& outs, String& s)
270 {
271  outs.print((char*) s.c_str());
272  return (outs);
273 }
274 #endif
String substring(unsigned int beginIndex) const
Definition: String.hh:204
void init(void)
Definition: String.cpp:127
__StringSumHelper(float num)
Definition: String.hh:260
__StringSumHelper(char c)
Definition: String.hh:254
unsigned char equalsIgnoreCase(const String &s) const
Definition: String.cpp:496
String & operator+=(float num)
Definition: String.hh:132
char charAt(unsigned int index) const
Definition: String.cpp:535
String substring(unsigned int beginIndex, unsigned int endIndex) const
Definition: String.cpp:649
int compareTo(const String &s) const
Definition: String.cpp:443
float toFloat(void) const
Definition: String.cpp:777
String & operator+=(unsigned char num)
Definition: String.hh:127
const char * c_str() const
Definition: String.hh:186
void print(int value, Base base=dec)
Definition: IOStream.cpp:46
int lastIndexOf(char ch) const
Definition: String.cpp:609
unsigned char equals(const String &s) const
Definition: String.cpp:456
__StringSumHelper(unsigned int num)
Definition: String.hh:257
String & operator+=(const String &rhs)
Definition: String.hh:124
unsigned char changeBuffer(unsigned int maxStrLen)
Definition: String.cpp:153
void toUpperCase(void)
Definition: String.cpp:748
String & operator=(const String &rhs)
Definition: String.cpp:209
unsigned char operator>=(const String &rhs) const
Definition: String.cpp:490
long toInt(void) const
Definition: String.cpp:770
unsigned char reserve(unsigned int size)
Definition: String.cpp:144
String & operator+=(str_P str)
Definition: String.hh:134
unsigned char operator<=(const String &rhs) const
Definition: String.cpp:484
unsigned int m_capacity
the array length minus one (for the &#39;\0&#39;)
Definition: String.hh:228
char operator[](unsigned int index) const
Definition: String.cpp:558
String & copy(const char *cstr, unsigned int length)
Definition: String.cpp:163
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const
Definition: String.cpp:565
String(const char *cstr="")
Definition: String.cpp:26
String & operator+=(const char *cstr)
Definition: String.hh:125
Definition: String.hh:37
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
Definition: String.hh:181
__StringSumHelper(const String &s)
Definition: String.hh:252
unsigned char operator==(const char *cstr) const
Definition: String.hh:160
__StringSumHelper(const char *p)
Definition: String.hh:253
unsigned int length(void) const
Definition: String.hh:83
String & operator+=(double num)
Definition: String.hh:133
void move(String &rhs)
Definition: String.cpp:188
unsigned char operator<(const String &rhs) const
Definition: String.cpp:472
unsigned char operator!=(const char *cstr) const
Definition: String.hh:162
const class prog_str * str_P
Definition: Types.h:187
void invalidate(void)
Definition: String.cpp:135
String & operator+=(unsigned long num)
Definition: String.hh:131
void setCharAt(unsigned int index, char c)
Definition: String.cpp:541
unsigned char endsWith(const String &suffix) const
Definition: String.cpp:526
String & operator+=(long num)
Definition: String.hh:130
__StringSumHelper(long num)
Definition: String.hh:258
int indexOf(char ch) const
Definition: String.cpp:579
String & operator+=(int num)
Definition: String.hh:128
char * m_buffer
the actual char array
Definition: String.hh:227
unsigned char operator==(const String &rhs) const
Definition: String.hh:159
unsigned int m_length
the String length (not counting the &#39;\0&#39;)
Definition: String.hh:229
String & operator+=(unsigned int num)
Definition: String.hh:129
void toLowerCase(void)
Definition: String.cpp:739
void trim(void)
Definition: String.cpp:757
__StringSumHelper(unsigned long num)
Definition: String.hh:259
unsigned char operator>(const String &rhs) const
Definition: String.cpp:478
unsigned char concat(const String &str)
Definition: String.cpp:252
__StringSumHelper(unsigned char num)
Definition: String.hh:255
unsigned char operator!=(const String &rhs) const
Definition: String.hh:161
void replace(char find, char replace)
Definition: String.cpp:667
String & operator+=(char c)
Definition: String.hh:126
IOStream & operator<<(IOStream &outs, String &s)
Definition: String.hh:269
~String(void)
Definition: String.cpp:121
__StringSumHelper(int num)
Definition: String.hh:256
__StringSumHelper(double num)
Definition: String.hh:261
friend __StringSumHelper & operator+(const __StringSumHelper &lhs, const String &rhs)
Definition: String.cpp:355
unsigned char startsWith(const String &prefix) const
Definition: String.cpp:510