15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/* 25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) WString.h - String library for Wiring & Arduino 35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) Copyright (c) 2009-10 Hernando Barragan. All right reserved. 45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) This library is free software; you can redistribute it and/or 65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) modify it under the terms of the GNU Lesser General Public 75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) License as published by the Free Software Foundation; either 85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) version 2.1 of the License, or (at your option) any later version. 95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) This library is distributed in the hope that it will be useful, 115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) but WITHOUT ANY WARRANTY; without even the implied warranty of 125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) Lesser General Public License for more details. 145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) You should have received a copy of the GNU Lesser General Public 165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) License along with this library; if not, write to the Free Software 175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*/ 195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef String_h 215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define String_h 225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 2381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)//#include "WProgram.h" 245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include <stdlib.h> 255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include <string.h> 26f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include <ctype.h> 27f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu 28f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liuclass String 295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 3053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) public: 3153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) // constructors 325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String( const char *value = "" ); 335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 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 ); 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; 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 99inline 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 106inline String operator+( String lhs, const String &rhs ) 107{ 108 return lhs += rhs; 109} 110 111 112#endif 113