sanguino/cores/arduino/WString.h

Fri, 17 Nov 2017 10:13:31 +0100

author
mdd
date
Fri, 17 Nov 2017 10:13:31 +0100
changeset 3
770b218a4931
parent 2
b373b0288715
permissions
-rw-r--r--

proper configuration, homing and planner optimization

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

mercurial