PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
9:754a7af30890
direct lcd stuff used by sensitive

Who changed what in which revision?

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