2010-07-04 09:27:23 -07:00
|
|
|
/*
|
2010-07-27 03:06:49 -07:00
|
|
|
WString.h - String library for Wiring & Arduino
|
|
|
|
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
|
2010-07-27 03:09:09 -07:00
|
|
|
Changes for version 1.0 by Xiaoyang Feng
|
|
|
|
|
2010-07-27 03:06:49 -07:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2010-07-04 09:27:23 -07:00
|
|
|
|
|
|
|
#ifndef String_h
|
|
|
|
#define String_h
|
|
|
|
|
2010-07-04 16:22:34 -07:00
|
|
|
//#include "WProgram.h"
|
|
|
|
#include <stdlib.h>
|
2010-07-04 09:27:23 -07:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
class String
|
|
|
|
{
|
2010-07-27 03:06:49 -07:00
|
|
|
public:
|
2010-07-04 09:27:23 -07:00
|
|
|
// constructors
|
2010-07-27 03:06:49 -07:00
|
|
|
String( const char* value = "" );
|
2010-07-04 09:27:23 -07:00
|
|
|
String( const String &value );
|
|
|
|
explicit String( const char );
|
|
|
|
explicit String( const unsigned char );
|
|
|
|
explicit String( const int, const int base=10);
|
2010-07-27 03:06:49 -07:00
|
|
|
explicit String( const unsigned int, const int base=10 );//negative sign only works on decimal
|
2010-07-04 09:27:23 -07:00
|
|
|
explicit String( const long, const int base=10 );
|
2010-07-27 03:06:49 -07:00
|
|
|
explicit String( const unsigned long, const int base=10 );//need add function to detect if number is bigger than the limit
|
|
|
|
~String() { free(_buffer); _length = _capacity = 0;} //added _length = _capacity = 0;
|
|
|
|
|
2010-07-04 09:27:23 -07:00
|
|
|
// operators
|
|
|
|
const String & operator = ( const String &rhs );
|
|
|
|
const String & operator +=( const String &rhs );
|
|
|
|
const String & operator +=( const char );
|
2010-07-27 03:06:49 -07:00
|
|
|
int operator ==( const String &rhs ) const;
|
2010-07-04 09:27:23 -07:00
|
|
|
int operator !=( const String &rhs ) const;
|
2010-07-27 03:06:49 -07:00
|
|
|
//is this useful?
|
2010-07-04 09:27:23 -07:00
|
|
|
int operator < ( const String &rhs ) const;
|
|
|
|
int operator > ( const String &rhs ) const;
|
|
|
|
int operator <=( const String &rhs ) const;
|
|
|
|
int operator >=( const String &rhs ) const;
|
2010-07-27 03:06:49 -07:00
|
|
|
|
|
|
|
friend String operator + ( String lhs, const String &rhs );
|
|
|
|
|
2010-07-04 09:27:23 -07:00
|
|
|
char operator []( unsigned int index ) const;
|
|
|
|
char& operator []( unsigned int index );
|
2010-07-27 03:06:49 -07:00
|
|
|
|
2010-07-04 09:27:23 -07:00
|
|
|
// general methods
|
|
|
|
char charAt( unsigned int index ) const;
|
2010-07-27 03:06:49 -07:00
|
|
|
void setCharAt(unsigned int index, const char ch);
|
|
|
|
boolean startsWith( const String &prefix ) const;
|
|
|
|
boolean startsWith( const String &prefix, unsigned int toffset ) const;
|
2010-07-04 09:27:23 -07:00
|
|
|
boolean endsWith( const String &suffix ) const;
|
2010-07-27 03:06:49 -07:00
|
|
|
boolean contains( const String &str ) const; //added
|
|
|
|
boolean equals( const String &str ) const;
|
|
|
|
boolean equalsIgnoreCase( const String &str ) const;
|
|
|
|
|
|
|
|
String trim( ) const;
|
|
|
|
String toLowerCase( ) const;
|
|
|
|
String toUpperCase( ) const;
|
|
|
|
String replace( char oldChar, char newChar );
|
|
|
|
String replace( const String& match, const String& replace );
|
|
|
|
String substring( unsigned int beginIndex ) const;
|
|
|
|
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
|
|
|
|
|
|
|
|
int compareTo( const String &str ) const;
|
|
|
|
int compareTo( const char* str ) const; //added
|
|
|
|
|
2010-07-04 09:27:23 -07:00
|
|
|
int indexOf( char ch ) const;
|
|
|
|
int indexOf( char ch, unsigned int fromIndex ) const;
|
|
|
|
int indexOf( const String &str ) const;
|
|
|
|
int indexOf( const String &str, unsigned int fromIndex ) const;
|
|
|
|
int lastIndexOf( char ch ) const;
|
2010-07-27 03:06:49 -07:00
|
|
|
int lastIndexOf( char ch, unsigned int fromIndex ) const; //from right to left?
|
2010-07-04 09:27:23 -07:00
|
|
|
int lastIndexOf( const String &str ) const;
|
|
|
|
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
|
2010-07-27 03:06:49 -07:00
|
|
|
|
2010-07-04 09:27:23 -07:00
|
|
|
const unsigned int length( ) const { return _length; }
|
|
|
|
const byte *getBytes() const { return (byte*)_buffer; }
|
|
|
|
const char* toCharArray() const { return _buffer; }
|
2010-07-27 03:06:49 -07:00
|
|
|
|
|
|
|
//version checking
|
|
|
|
String version(void); // the version number
|
|
|
|
|
|
|
|
protected:
|
2010-07-04 09:27:23 -07:00
|
|
|
char *_buffer; // the actual char array
|
2010-07-04 09:36:52 -07:00
|
|
|
unsigned int _capacity; // the array length minus one (for the '\0')
|
|
|
|
unsigned int _length; // the String length (not counting the '\0')
|
2010-07-27 03:06:49 -07:00
|
|
|
|
2010-07-04 09:27:23 -07:00
|
|
|
void getBuffer(unsigned int maxStrLen);
|
2010-07-27 03:06:49 -07:00
|
|
|
//void doubleBuffer( );
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2010-07-04 09:27:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// allocate buffer space
|
|
|
|
inline void String::getBuffer(unsigned int maxStrLen)
|
|
|
|
{
|
2010-07-27 03:06:49 -07:00
|
|
|
_capacity = maxStrLen;
|
|
|
|
_buffer = (char *) malloc(_capacity + 1);
|
|
|
|
if(_buffer == NULL){ //added for memory alloc error
|
|
|
|
_length = _capacity = 0;
|
|
|
|
}
|
2010-07-04 09:27:23 -07:00
|
|
|
}
|
|
|
|
|
2010-07-27 03:06:49 -07:00
|
|
|
/* used only once, really not effcient
|
|
|
|
// double the buffer size
|
|
|
|
inline void String::doubleBuffer( )
|
|
|
|
{
|
|
|
|
char *temp = _buffer;
|
|
|
|
getBuffer( ++_capacity * 2 );
|
|
|
|
strcpy( _buffer, temp );
|
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2010-07-04 09:27:23 -07:00
|
|
|
|
|
|
|
inline String operator+( String lhs, const String &rhs )
|
|
|
|
{
|
2010-07-27 03:06:49 -07:00
|
|
|
return lhs += rhs;
|
2010-07-04 09:27:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-27 03:06:49 -07:00
|
|
|
#endif
|