1 /*
2 WString.h - String library for Wiring & Arduino
3 Copyright (c) 2009-10 Hernando Barragan. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifndef String_h
21 #define String_h
22
23 //#include "WProgram.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27
28 class String
29 {
30 public:
31 // constructors
32 String( const char *value = "" );
33 String( const String &value );
34 String( const char );
35 String( const unsigned char );
36 String( const int, const int base=10);
37 String( const unsigned int, const int base=10 );
38 String( const long, const int base=10 );
39 String( const unsigned long, const int base=10 );
~String()40 ~String() { free(_buffer); _length = _capacity = 0;} //added _length = _capacity = 0;
41
42 // operators
43 const String & operator = ( const String &rhs );
44 const String & operator +=( const String &rhs );
45 //const String & operator +=( const char );
46 int operator ==( const String &rhs ) const;
47 int operator !=( const String &rhs ) const;
48 int operator < ( const String &rhs ) const;
49 int operator > ( const String &rhs ) const;
50 int operator <=( const String &rhs ) const;
51 int operator >=( const String &rhs ) const;
52 char operator []( unsigned int index ) const;
53 char& operator []( unsigned int index );
54 //operator const char *() const { return _buffer; }
55
56 // general methods
57 char charAt( unsigned int index ) const;
58 int compareTo( const String &anotherString ) const;
59 unsigned char endsWith( const String &suffix ) const;
60 unsigned char equals( const String &anObject ) const;
61 unsigned char equalsIgnoreCase( const String &anotherString ) const;
62 int indexOf( char ch ) const;
63 int indexOf( char ch, unsigned int fromIndex ) const;
64 int indexOf( const String &str ) const;
65 int indexOf( const String &str, unsigned int fromIndex ) const;
66 int lastIndexOf( char ch ) const;
67 int lastIndexOf( char ch, unsigned int fromIndex ) const;
68 int lastIndexOf( const String &str ) const;
69 int lastIndexOf( const String &str, unsigned int fromIndex ) const;
length()70 const unsigned int length( ) const { return _length; }
71 void setCharAt(unsigned int index, const char ch);
72 unsigned char startsWith( const String &prefix ) const;
73 unsigned char startsWith( const String &prefix, unsigned int toffset ) const;
74 String substring( unsigned int beginIndex ) const;
75 String substring( unsigned int beginIndex, unsigned int endIndex ) const;
76 String toLowerCase( ) const;
77 String toUpperCase( ) const;
78 String trim( ) const;
79 void getBytes(unsigned char *buf, unsigned int bufsize);
80 void toCharArray(char *buf, unsigned int bufsize);
81 long toInt( );
82 const String& concat( const String &str );
83 String replace( char oldChar, char newChar );
84 String replace( const String& match, const String& replace );
85 friend String operator + ( String lhs, const String &rhs );
86
87 protected:
88 char *_buffer; // the actual char array
89 unsigned int _capacity; // the array length minus one (for the '\0')
90 unsigned int _length; // the String length (not counting the '\0')
91
92 void getBuffer(unsigned int maxStrLen);
93
94 private:
95
96 };
97
98 // allocate buffer space
getBuffer(unsigned int maxStrLen)99 inline void String::getBuffer(unsigned int maxStrLen)
100 {
101 _capacity = maxStrLen;
102 _buffer = (char *) malloc(_capacity + 1);
103 if (_buffer == NULL) _length = _capacity = 0;
104 }
105
106 inline String operator+( String lhs, const String &rhs )
107 {
108 return lhs += rhs;
109 }
110
111
112 #endif
113