mirror of
https://github.com/arduino/Arduino.git
synced 2024-12-10 21:24:12 +01:00
Reverting changes to String class and modifying to allow + and += to work on more types.
This commit is contained in:
parent
cd4c0e56e2
commit
4e33e6cb9f
@ -1,7 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
WString.cpp - String library for Wiring & Arduino
|
WString.cpp - String library for Wiring & Arduino
|
||||||
Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
|
Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
|
||||||
Changes for version 1.0 by Xiaoyang Feng
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
@ -16,25 +15,24 @@
|
|||||||
You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU Lesser General Public
|
||||||
License along with this library; if not, write to the Free Software
|
License along with this library; if not, write to the Free Software
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "WProgram.h"
|
#include "WProgram.h"
|
||||||
#include "WString.h"
|
#include "WString.h"
|
||||||
|
|
||||||
|
|
||||||
String::String( const char *value )
|
String::String( const char *value )
|
||||||
{
|
{
|
||||||
if ( value == NULL )
|
if ( value == NULL )
|
||||||
value = "";
|
value = "";
|
||||||
getBuffer( _length = strlen( value ) );
|
getBuffer( _length = strlen( value ) );
|
||||||
if(_buffer != NULL) //added
|
|
||||||
strcpy( _buffer, value );
|
strcpy( _buffer, value );
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const String &value )
|
String::String( const String &value )
|
||||||
{
|
{
|
||||||
getBuffer( _length = value._length );
|
getBuffer( _length = value._length );
|
||||||
if(_buffer != NULL) //added
|
|
||||||
strcpy( _buffer, value._buffer );
|
strcpy( _buffer, value._buffer );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,70 +40,72 @@ String::String( const char value )
|
|||||||
{
|
{
|
||||||
_length = 1;
|
_length = 1;
|
||||||
getBuffer(1);
|
getBuffer(1);
|
||||||
if(_buffer != NULL){ //added
|
|
||||||
_buffer[0] = value;
|
_buffer[0] = value;
|
||||||
_buffer[1] = '\0'; //_buffer[1] = 0;
|
_buffer[1] = 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const unsigned char value )
|
String::String( const unsigned char value )
|
||||||
{
|
{
|
||||||
_length = 1;
|
_length = 1;
|
||||||
getBuffer(1);
|
getBuffer(1);
|
||||||
if(_buffer != NULL){ //added
|
|
||||||
_buffer[0] = value;
|
_buffer[0] = value;
|
||||||
_buffer[1] = '\0'; //_buffer[1] = 0;
|
_buffer[1] = 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const int value, const int base )
|
String::String( const int value, const int base )
|
||||||
{
|
{
|
||||||
//char buf[33];
|
char buf[33];
|
||||||
char* buf = (char *) malloc(33);
|
itoa((signed long)value, buf, base);
|
||||||
if(buf != NULL){ //added
|
|
||||||
itoa(value,buf,base);
|
|
||||||
getBuffer( _length = strlen(buf) );
|
getBuffer( _length = strlen(buf) );
|
||||||
strcpy( _buffer, buf );
|
strcpy( _buffer, buf );
|
||||||
free(buf); //added
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const unsigned int value, const int base )
|
String::String( const unsigned int value, const int base )
|
||||||
{
|
{
|
||||||
//char buf[33];
|
char buf[33];
|
||||||
char* buf = (char *) malloc(33);
|
|
||||||
if(buf != NULL){ //added
|
|
||||||
ultoa((unsigned long)value, buf, base);
|
ultoa((unsigned long)value, buf, base);
|
||||||
getBuffer( _length = strlen(buf) );
|
getBuffer( _length = strlen(buf) );
|
||||||
strcpy( _buffer, buf );
|
strcpy( _buffer, buf );
|
||||||
free(buf); //added
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const long value, const int base )
|
String::String( const long value, const int base )
|
||||||
{
|
{
|
||||||
//char buf[33];
|
char buf[33];
|
||||||
char* buf = (char *) malloc(33);
|
|
||||||
if(buf != NULL){ //added
|
|
||||||
ltoa(value, buf, base);
|
ltoa(value, buf, base);
|
||||||
getBuffer( _length = strlen(buf) );
|
getBuffer( _length = strlen(buf) );
|
||||||
strcpy( _buffer, buf );
|
strcpy( _buffer, buf );
|
||||||
free(buf); //added
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String::String( const unsigned long value, const int base )
|
String::String( const unsigned long value, const int base )
|
||||||
{
|
{
|
||||||
//char buf[33];
|
char buf[33];
|
||||||
char* buf = (char *) malloc(33);
|
|
||||||
if(buf != NULL){ //added
|
|
||||||
ultoa(value, buf, 10);
|
ultoa(value, buf, 10);
|
||||||
getBuffer( _length = strlen(buf) );
|
getBuffer( _length = strlen(buf) );
|
||||||
strcpy( _buffer, buf );
|
strcpy( _buffer, buf );
|
||||||
free(buf); //added
|
}
|
||||||
|
|
||||||
|
char String::charAt( unsigned int loc ) const
|
||||||
|
{
|
||||||
|
return operator[]( loc );
|
||||||
|
}
|
||||||
|
|
||||||
|
void String::setCharAt( unsigned int loc, const char aChar )
|
||||||
|
{
|
||||||
|
if(_length > loc) {
|
||||||
|
_buffer[loc] = aChar;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int String::compareTo( const String &s2 ) const
|
||||||
|
{
|
||||||
|
return strcmp( _buffer, s2._buffer );
|
||||||
|
}
|
||||||
|
|
||||||
|
const String & String::concat( const String &s2 )
|
||||||
|
{
|
||||||
|
return (*this) += s2;
|
||||||
|
}
|
||||||
|
|
||||||
const String & String::operator=( const String &rhs )
|
const String & String::operator=( const String &rhs )
|
||||||
{
|
{
|
||||||
if ( this == &rhs )
|
if ( this == &rhs )
|
||||||
@ -121,36 +121,31 @@ const String & String::operator=( const String &rhs )
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
const String & String::operator+=( const char ch )
|
//const String & String::operator+=( const char aChar )
|
||||||
|
//{
|
||||||
|
// if ( _length == _capacity )
|
||||||
|
// doubleBuffer();
|
||||||
|
//
|
||||||
|
// _buffer[ _length++ ] = aChar;
|
||||||
|
// _buffer[ _length ] = '\0';
|
||||||
|
// return *this;
|
||||||
|
//}
|
||||||
|
|
||||||
|
const String & String::operator+=( const String &other )
|
||||||
{
|
{
|
||||||
if ( _length == _capacity )
|
_length += other._length;
|
||||||
{ //doubleBuffer(); //only place to use
|
|
||||||
char *temp = _buffer;
|
|
||||||
_length++;
|
|
||||||
getBuffer(_length);
|
|
||||||
if(_buffer == NULL)
|
|
||||||
return "/*NOT ENOUGH MEMORY*/";
|
|
||||||
strcpy(_buffer,temp);
|
|
||||||
}
|
|
||||||
_buffer[ _length-1 ] = ch;
|
|
||||||
_buffer[ _length ] = '\0';
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
const String & String::operator+=( const String &rhs )
|
|
||||||
{
|
|
||||||
_length += rhs._length;
|
|
||||||
if ( _length > _capacity )
|
if ( _length > _capacity )
|
||||||
{
|
{
|
||||||
char *temp = _buffer;
|
char *temp = _buffer;
|
||||||
getBuffer( _length );
|
getBuffer( _length );
|
||||||
strcpy( _buffer, temp );
|
strcpy( _buffer, temp );
|
||||||
_buffer[_length] = '\0';
|
|
||||||
free(temp);
|
free(temp);
|
||||||
}
|
}
|
||||||
strcat( _buffer, rhs._buffer );
|
strcat( _buffer, other._buffer );
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int String::operator==( const String &rhs ) const
|
int String::operator==( const String &rhs ) const
|
||||||
{
|
{
|
||||||
return ( _length == rhs._length && strcmp( _buffer, rhs._buffer ) == 0 );
|
return ( _length == rhs._length && strcmp( _buffer, rhs._buffer ) == 0 );
|
||||||
@ -158,8 +153,7 @@ int String::operator==( const String &rhs ) const
|
|||||||
|
|
||||||
int String::operator!=( const String &rhs ) const
|
int String::operator!=( const String &rhs ) const
|
||||||
{
|
{
|
||||||
return ( _length != rhs._length || strcmp( _buffer, rhs._buffer ) != 0 );
|
return ( _length != rhs.length() || strcmp( _buffer, rhs.toCharArray() ) != 0 );
|
||||||
//return ( _length != rhs.length() || strcmp( _buffer, rhs.toCharArray() ) != 0 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int String::operator<( const String &rhs ) const
|
int String::operator<( const String &rhs ) const
|
||||||
@ -184,120 +178,182 @@ int String::operator>=( const String & rhs ) const
|
|||||||
|
|
||||||
char & String::operator[]( unsigned int index )
|
char & String::operator[]( unsigned int index )
|
||||||
{
|
{
|
||||||
if(index < 0 || index >= _length) exit(1); //added
|
// need to check for valid index, to do later
|
||||||
return _buffer[ index ];
|
return _buffer[ index ];
|
||||||
}
|
}
|
||||||
|
|
||||||
char String::operator[]( unsigned int index ) const
|
char String::operator[]( unsigned int index ) const
|
||||||
{
|
{
|
||||||
if(index < 0 || index >= _length) exit(1); //added
|
// need to check for valid index, to do later
|
||||||
return _buffer[ index ];
|
return _buffer[ index ];
|
||||||
}
|
}
|
||||||
|
|
||||||
char String::charAt( unsigned int index ) const
|
boolean String::endsWith( const String &s2 ) const
|
||||||
{
|
{
|
||||||
return operator[]( index );
|
if ( _length < s2._length )
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return strcmp( &_buffer[ _length - s2._length], s2.toCharArray() ) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void String::setCharAt( unsigned int index, const char ch )
|
boolean String::equals( const String &s2 ) const
|
||||||
{
|
{
|
||||||
if(_length > index) {
|
return ( _length == s2._length && strcmp( _buffer,s2._buffer ) == 0 );
|
||||||
_buffer[index] = ch;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
exit(1); //added
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean String::startsWith( const String &prefix ) const
|
boolean String::equalsIgnoreCase( const String &s2 ) const
|
||||||
{
|
{
|
||||||
if ( _length < prefix._length )
|
if ( this == &s2 )
|
||||||
return false;
|
return true; //1;
|
||||||
|
else if ( _length != s2._length )
|
||||||
|
return false; //0;
|
||||||
|
|
||||||
return startsWith( prefix, 0 );
|
return strcmp(toLowerCase().toCharArray(), s2.toLowerCase().toCharArray()) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean String::startsWith( const String &prefix, unsigned int offset ) const
|
String String::replace( char findChar, char replaceChar )
|
||||||
{
|
{
|
||||||
if ( offset > _length - prefix._length )
|
String theReturn = _buffer;
|
||||||
return false;
|
char* temp = theReturn._buffer;
|
||||||
|
while( (temp = strchr( temp, findChar )) != 0 )
|
||||||
|
*temp = replaceChar;
|
||||||
|
|
||||||
return (strncmp( &_buffer[offset], prefix._buffer, prefix._length ) == 0);
|
return theReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean String::endsWith( const String &suffix ) const
|
String String::replace( const String& match, const String& replace )
|
||||||
{
|
{
|
||||||
if ( _length < suffix._length )
|
String temp = _buffer, newString;
|
||||||
return false;
|
|
||||||
|
|
||||||
return (strcmp( &_buffer[ _length - suffix._length], suffix._buffer ) == 0);
|
int loc;
|
||||||
}
|
while ( (loc = temp.indexOf( match )) != -1 )
|
||||||
|
|
||||||
boolean String::contains( const String &str ) const
|
|
||||||
{
|
|
||||||
return (strstr(_buffer, str._buffer) != NULL );
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean String::equals( const String &str ) const
|
|
||||||
{
|
|
||||||
return ( _length == str._length && strcmp( _buffer,str._buffer ) == 0 );
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean String::equalsIgnoreCase( const String &str ) const
|
|
||||||
{
|
|
||||||
if ( this == &str )
|
|
||||||
return true;
|
|
||||||
else if ( _length != str._length )
|
|
||||||
return false;
|
|
||||||
//return strcmp(toLowerCase().toCharArray(), s2.toLowerCase().toCharArray()) == 0;
|
|
||||||
for(unsigned int i = 0; i < _length; i++ ){
|
|
||||||
if(tolower( _buffer[i]) != tolower(str._buffer[i]))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
String String::trim() const
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
String temp = _buffer;
|
|
||||||
unsigned int i,j;
|
|
||||||
|
|
||||||
for ( i = 0; i < _length; i++ )
|
|
||||||
{
|
{
|
||||||
if ( !isspace(_buffer[i]) )
|
newString += temp.substring( 0, loc );
|
||||||
break;
|
newString += replace;
|
||||||
|
temp = temp.substring( loc + match._length );
|
||||||
|
}
|
||||||
|
newString += temp;
|
||||||
|
return newString;
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::indexOf( char temp ) const
|
||||||
|
{
|
||||||
|
return indexOf( temp, 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::indexOf( char ch, unsigned int fromIndex ) const
|
||||||
|
{
|
||||||
|
if ( fromIndex >= _length )
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
const char* temp = strchr( &_buffer[fromIndex], ch );
|
||||||
|
if ( temp == NULL )
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return temp - _buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::indexOf( const String &s2 ) const
|
||||||
|
{
|
||||||
|
return indexOf( s2, 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::indexOf( const String &s2, unsigned int fromIndex ) const
|
||||||
|
{
|
||||||
|
if ( fromIndex >= _length )
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
const char *theFind = strstr( &_buffer[ fromIndex ], s2.toCharArray() );
|
||||||
|
|
||||||
|
if ( theFind == NULL )
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return theFind - _buffer; // pointer subtraction
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::lastIndexOf( char theChar ) const
|
||||||
|
{
|
||||||
|
return lastIndexOf( theChar, _length - 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::lastIndexOf( char ch, unsigned int fromIndex ) const
|
||||||
|
{
|
||||||
|
if ( fromIndex >= _length )
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
char tempchar = _buffer[fromIndex + 1];
|
||||||
|
_buffer[fromIndex + 1] = '\0';
|
||||||
|
char* temp = strrchr( _buffer, ch );
|
||||||
|
_buffer[fromIndex + 1] = tempchar;
|
||||||
|
|
||||||
|
if ( temp == NULL )
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return temp - _buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::lastIndexOf( const String &s2 ) const
|
||||||
|
{
|
||||||
|
return lastIndexOf( s2, _length - s2._length );
|
||||||
|
}
|
||||||
|
|
||||||
|
int String::lastIndexOf( const String &s2, unsigned int fromIndex ) const
|
||||||
|
{
|
||||||
|
// check for empty strings
|
||||||
|
if ( s2._length == 0 || s2._length - 1 > fromIndex || fromIndex >= _length )
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// matching first character
|
||||||
|
char temp = s2[ 0 ];
|
||||||
|
|
||||||
|
for ( int i = fromIndex; i >= 0; i-- )
|
||||||
|
{
|
||||||
|
if ( _buffer[ i ] == temp && (*this).substring( i, i + s2._length ).equals( s2 ) )
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean String::startsWith( const String &s2 ) const
|
||||||
|
{
|
||||||
|
if ( _length < s2._length )
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return startsWith( s2, 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean String::startsWith( const String &s2, unsigned int offset ) const
|
||||||
|
{
|
||||||
|
if ( offset > _length - s2._length )
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return strncmp( &_buffer[offset], s2.toCharArray(), s2._length ) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
String String::substring( unsigned int left ) const
|
||||||
|
{
|
||||||
|
return substring( left, _length );
|
||||||
|
}
|
||||||
|
|
||||||
|
String String::substring( unsigned int left, unsigned int right ) const
|
||||||
|
{
|
||||||
|
if ( left > right )
|
||||||
|
{
|
||||||
|
int temp = right;
|
||||||
|
right = left;
|
||||||
|
left = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( j = temp._length - 1; j > i; j-- )
|
if ( right > _length )
|
||||||
{
|
{
|
||||||
if ( !isspace(_buffer[j]) )
|
right = _length;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return temp.substring( i, j + 1);
|
char temp = _buffer[ right ]; // save the replaced character
|
||||||
*/
|
_buffer[ right ] = '\0';
|
||||||
String temp = _buffer;
|
String outPut = ( _buffer + left ); // pointer arithmetic
|
||||||
unsigned int index = 0;
|
_buffer[ right ] = temp; //restore character
|
||||||
unsigned int tempIndex = 0;
|
return outPut;
|
||||||
while( index < _length )
|
|
||||||
{
|
|
||||||
if(_buffer[index] == '\t' ||
|
|
||||||
_buffer[index] == '\r' ||
|
|
||||||
_buffer[index] == '\n' ||
|
|
||||||
_buffer[index] == ' ' ||
|
|
||||||
_buffer[index] == 0x11)
|
|
||||||
{
|
|
||||||
memcpy(temp._buffer+tempIndex, _buffer+index+1, strlen(_buffer)-index-1);
|
|
||||||
temp._buffer[temp._length-1] = '\0';
|
|
||||||
temp._length--;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
tempIndex++;
|
|
||||||
}
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
return temp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::toLowerCase() const
|
String String::toLowerCase() const
|
||||||
@ -318,199 +374,23 @@ String String::toUpperCase() const
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
String String::replace( char oldChar, char newChar )
|
String String::trim() const
|
||||||
{
|
{
|
||||||
String newString = _buffer;
|
|
||||||
char* temp = newString._buffer;
|
|
||||||
while( temp = strchr( temp, oldChar ) )
|
|
||||||
*temp = newChar;
|
|
||||||
|
|
||||||
return newString;
|
|
||||||
}
|
|
||||||
|
|
||||||
String String::replace( const String& match, const String& replace )
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
String newString;
|
|
||||||
String temp = _buffer;
|
String temp = _buffer;
|
||||||
int loc;
|
unsigned int i,j;
|
||||||
while ( (loc = temp.indexOf( match )) != -1 )
|
|
||||||
|
for ( i = 0; i < _length; i++ )
|
||||||
{
|
{
|
||||||
newString += temp.substring( 0, loc );
|
if ( !isspace(_buffer[i]) )
|
||||||
newString += replace;
|
break;
|
||||||
temp = temp.substring( loc + match._length );
|
|
||||||
}
|
}
|
||||||
newString += temp;
|
|
||||||
return newString;
|
|
||||||
*/
|
|
||||||
|
|
||||||
const char* temp = strstr( _buffer, match._buffer);
|
for ( j = temp._length - 1; j > i; j-- )
|
||||||
if(temp == NULL || match._length == 0){
|
|
||||||
String newString = _buffer;
|
|
||||||
return newString;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
//get new buffer
|
|
||||||
int count = 0;
|
|
||||||
int lastIndex = 0;
|
|
||||||
while(temp != NULL){
|
|
||||||
count ++;
|
|
||||||
lastIndex = temp - _buffer;
|
|
||||||
temp = strstr( &_buffer[ lastIndex + match._length ], match._buffer );
|
|
||||||
}
|
|
||||||
String newString;
|
|
||||||
newString._length = newString._capacity = _length + (replace._length - match._length)*count;
|
|
||||||
char* _buf = (char*)malloc(newString._length + 1);
|
|
||||||
if(_buf == NULL)
|
|
||||||
exit(1);
|
|
||||||
newString._buffer = _buf;
|
|
||||||
//string copy
|
|
||||||
int fromIndex = 0;
|
|
||||||
int tempLength = 0;
|
|
||||||
lastIndex = 0;
|
|
||||||
temp = strstr( &_buffer[ lastIndex ], match._buffer);
|
|
||||||
while (temp != NULL){
|
|
||||||
fromIndex = temp - _buffer;
|
|
||||||
memcpy(newString._buffer+tempLength, _buffer+lastIndex, fromIndex-lastIndex);
|
|
||||||
memcpy(newString._buffer+tempLength+fromIndex-lastIndex, replace._buffer, replace._length);
|
|
||||||
tempLength += (fromIndex-lastIndex + replace._length);
|
|
||||||
lastIndex = fromIndex + match._length;
|
|
||||||
temp = strstr( &_buffer[ lastIndex ], match._buffer);
|
|
||||||
}
|
|
||||||
memcpy(newString._buffer+tempLength, _buffer+lastIndex, _length-lastIndex);
|
|
||||||
tempLength += (_length - lastIndex);
|
|
||||||
newString._buffer[tempLength] = '\0';
|
|
||||||
newString._length = newString._capacity = strlen(newString._buffer);
|
|
||||||
return newString;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String String::substring( unsigned int beginIndex ) const
|
|
||||||
{
|
|
||||||
return substring( beginIndex, _length );
|
|
||||||
}
|
|
||||||
|
|
||||||
String String::substring( unsigned int beginIndex, unsigned int endIndex ) const
|
|
||||||
{
|
|
||||||
if ( beginIndex > endIndex )
|
|
||||||
{
|
{
|
||||||
int temp = endIndex;
|
if ( !isspace(_buffer[j]) )
|
||||||
endIndex = beginIndex;
|
break;
|
||||||
beginIndex = temp;
|
|
||||||
}
|
|
||||||
if ( endIndex > _length )
|
|
||||||
{
|
|
||||||
endIndex = _length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char temp = _buffer[ endIndex ]; // save the replaced character
|
return temp.substring( i, j + 1);
|
||||||
_buffer[ endIndex ] = '\0';
|
|
||||||
String outPut = ( _buffer + beginIndex ); // pointer arithmetic
|
|
||||||
_buffer[ endIndex ] = temp; //restore character
|
|
||||||
return outPut;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int String::compareTo( const String &str ) const
|
|
||||||
{
|
|
||||||
return strcmp( _buffer, str._buffer );
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::compareTo( const char* str ) const
|
|
||||||
{
|
|
||||||
return strcmp( _buffer, str );
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::indexOf( char ch ) const
|
|
||||||
{
|
|
||||||
return indexOf( ch, 0 );
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::indexOf( char ch, unsigned int fromIndex ) const
|
|
||||||
{
|
|
||||||
if ( fromIndex >= _length )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
const char* temp = strchr( &_buffer[fromIndex], ch );
|
|
||||||
if ( temp == NULL )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return temp - _buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::indexOf( const String &str ) const
|
|
||||||
{
|
|
||||||
return indexOf( str, 0 );
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::indexOf( const String &str, unsigned int fromIndex ) const
|
|
||||||
{
|
|
||||||
if ( fromIndex >= _length )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
const char *temp = strstr( &_buffer[ fromIndex ], str._buffer );
|
|
||||||
|
|
||||||
if ( temp == NULL )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return temp - _buffer; // pointer subtraction
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::lastIndexOf( char ch ) const
|
|
||||||
{
|
|
||||||
return lastIndexOf( ch, _length - 1 );
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::lastIndexOf( char ch, unsigned int fromIndex ) const
|
|
||||||
{
|
|
||||||
if ( fromIndex >= _length )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
char tempchar = _buffer[fromIndex + 1];
|
|
||||||
_buffer[fromIndex + 1] = '\0';
|
|
||||||
char* temp = strrchr( _buffer, ch );
|
|
||||||
_buffer[fromIndex + 1] = tempchar;
|
|
||||||
|
|
||||||
if ( temp == NULL )
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return temp - _buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::lastIndexOf( const String &str ) const
|
|
||||||
{
|
|
||||||
return lastIndexOf( str, _length - str._length );
|
|
||||||
}
|
|
||||||
|
|
||||||
int String::lastIndexOf( const String &str, unsigned int fromIndex ) const
|
|
||||||
{
|
|
||||||
// check for empty strings
|
|
||||||
if ( str._length == 0 || str._length - 1 > fromIndex || fromIndex >= _length )
|
|
||||||
return -1;
|
|
||||||
/*
|
|
||||||
// matching first character
|
|
||||||
char temp = str[ 0 ];
|
|
||||||
|
|
||||||
for ( unsigned int i = fromIndex; i >= 0; i-- )
|
|
||||||
{
|
|
||||||
if ( _buffer[ i ] == temp && (*this).substring( i, i + str._length ).equals( str ) )
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
*/
|
|
||||||
char *temp = strstr(&_buffer[_length-fromIndex], str._buffer);
|
|
||||||
if(temp == NULL)
|
|
||||||
return -1;
|
|
||||||
int index;
|
|
||||||
while( temp != NULL){
|
|
||||||
index = temp - _buffer;
|
|
||||||
temp = strstr(&_buffer[index+1],str._buffer);
|
|
||||||
}
|
|
||||||
return index-_length+fromIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
//version() returns the version of the library:
|
|
||||||
String String::version(void)
|
|
||||||
{
|
|
||||||
return "1.0";
|
|
||||||
}
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
WString.h - String library for Wiring & Arduino
|
WString.h - String library for Wiring & Arduino
|
||||||
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
|
Copyright (c) 2009-10 Hernando Barragan. All right reserved.
|
||||||
Changes for version 1.0 by Xiaoyang Feng
|
|
||||||
|
|
||||||
|
|
||||||
This library is free software; you can redistribute it and/or
|
This library is free software; you can redistribute it and/or
|
||||||
modify it under the terms of the GNU Lesser General Public
|
modify it under the terms of the GNU Lesser General Public
|
||||||
@ -17,7 +15,7 @@
|
|||||||
You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU Lesser General Public
|
||||||
License along with this library; if not, write to the Free Software
|
License along with this library; if not, write to the Free Software
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef String_h
|
#ifndef String_h
|
||||||
#define String_h
|
#define String_h
|
||||||
@ -29,81 +27,71 @@
|
|||||||
|
|
||||||
class String
|
class String
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// constructors
|
// constructors
|
||||||
String( const char* value = "" );
|
String( const char *value = "" );
|
||||||
String( const String &value );
|
String( const String &value );
|
||||||
explicit String( const char );
|
String( const char );
|
||||||
explicit String( const unsigned char );
|
String( const unsigned char );
|
||||||
explicit String( const int, const int base=10);
|
String( const int, const int base=10);
|
||||||
explicit String( const unsigned int, const int base=10 );//negative sign only works on decimal
|
String( const unsigned int, const int base=10 );
|
||||||
explicit String( const long, const int base=10 );
|
String( const long, const int base=10 );
|
||||||
explicit String( const unsigned long, const int base=10 );//need add function to detect if number is bigger than the limit
|
String( const unsigned long, const int base=10 );
|
||||||
~String() { free(_buffer); _length = _capacity = 0;} //added _length = _capacity = 0;
|
~String() { free(_buffer); }
|
||||||
|
|
||||||
// operators
|
// operators
|
||||||
const String & operator = ( const String &rhs );
|
const String & operator = ( const String &rhs );
|
||||||
const String & operator +=( const String &rhs );
|
const String & operator +=( const String &rhs );
|
||||||
const String & operator +=( const char );
|
//const String & operator +=( const char );
|
||||||
int operator ==( const String &rhs ) const;
|
int operator ==( const String &rhs ) const;
|
||||||
int operator !=( const String &rhs ) const;
|
int operator !=( const String &rhs ) const;
|
||||||
//is this useful?
|
|
||||||
int operator < ( const String &rhs ) const;
|
int operator < ( const String &rhs ) const;
|
||||||
int operator > ( const String &rhs ) const;
|
int operator > ( const String &rhs ) const;
|
||||||
int operator <=( const String &rhs ) const;
|
int operator <=( const String &rhs ) const;
|
||||||
int operator >=( const String &rhs ) const;
|
int operator >=( const String &rhs ) const;
|
||||||
|
|
||||||
friend String operator + ( String lhs, const String &rhs );
|
|
||||||
|
|
||||||
char operator []( unsigned int index ) const;
|
char operator []( unsigned int index ) const;
|
||||||
char& operator []( unsigned int index );
|
char& operator []( unsigned int index );
|
||||||
|
//operator const char *() const { return _buffer; }
|
||||||
|
|
||||||
// general methods
|
// general methods
|
||||||
char charAt( unsigned int index ) const;
|
char charAt( unsigned int index ) const;
|
||||||
void setCharAt(unsigned int index, const char ch);
|
int compareTo( const String &anotherString ) const;
|
||||||
boolean startsWith( const String &prefix ) const;
|
|
||||||
boolean startsWith( const String &prefix, unsigned int toffset ) const;
|
|
||||||
boolean endsWith( const String &suffix ) const;
|
boolean endsWith( const String &suffix ) const;
|
||||||
boolean contains( const String &str ) const; //added
|
boolean equals( const String &anObject ) const;
|
||||||
boolean equals( const String &str ) const;
|
boolean equalsIgnoreCase( const String &anotherString ) 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
|
|
||||||
|
|
||||||
int indexOf( char ch ) const;
|
int indexOf( char ch ) const;
|
||||||
int indexOf( char ch, unsigned int fromIndex ) const;
|
int indexOf( char ch, unsigned int fromIndex ) const;
|
||||||
int indexOf( const String &str ) const;
|
int indexOf( const String &str ) const;
|
||||||
int indexOf( const String &str, unsigned int fromIndex ) const;
|
int indexOf( const String &str, unsigned int fromIndex ) const;
|
||||||
int lastIndexOf( char ch ) const;
|
int lastIndexOf( char ch ) const;
|
||||||
int lastIndexOf( char ch, unsigned int fromIndex ) const; //from right to left?
|
int lastIndexOf( char ch, unsigned int fromIndex ) const;
|
||||||
int lastIndexOf( const String &str ) const;
|
int lastIndexOf( const String &str ) const;
|
||||||
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
|
int lastIndexOf( const String &str, unsigned int fromIndex ) const;
|
||||||
|
|
||||||
const unsigned int length( ) const { return _length; }
|
const unsigned int length( ) const { return _length; }
|
||||||
|
void setCharAt(unsigned int index, const char ch);
|
||||||
|
boolean startsWith( const String &prefix ) const;
|
||||||
|
boolean startsWith( const String &prefix, unsigned int toffset ) const;
|
||||||
|
String substring( unsigned int beginIndex ) const;
|
||||||
|
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
|
||||||
|
String toLowerCase( ) const;
|
||||||
|
String toUpperCase( ) const;
|
||||||
|
String trim( ) const;
|
||||||
const byte *getBytes() const { return (byte*)_buffer; }
|
const byte *getBytes() const { return (byte*)_buffer; }
|
||||||
const char* toCharArray() const { return _buffer; }
|
const char* toCharArray() const { return _buffer; }
|
||||||
|
const String& concat( const String &str );
|
||||||
|
String replace( char oldChar, char newChar );
|
||||||
|
String replace( const String& match, const String& replace );
|
||||||
|
friend String operator + ( String lhs, const String &rhs );
|
||||||
|
|
||||||
//version checking
|
protected:
|
||||||
String version(void); // the version number
|
|
||||||
|
|
||||||
protected:
|
|
||||||
char *_buffer; // the actual char array
|
char *_buffer; // the actual char array
|
||||||
unsigned int _capacity; // the array length minus one (for the '\0')
|
unsigned int _capacity; // the array length minus one (for the '\0')
|
||||||
unsigned int _length; // the String length (not counting the '\0')
|
unsigned int _length; // the String length (not counting the '\0')
|
||||||
|
|
||||||
void getBuffer(unsigned int maxStrLen);
|
void getBuffer(unsigned int maxStrLen);
|
||||||
//void doubleBuffer( );
|
void doubleBuffer( );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -112,22 +100,16 @@ inline void String::getBuffer(unsigned int maxStrLen)
|
|||||||
{
|
{
|
||||||
_capacity = maxStrLen;
|
_capacity = maxStrLen;
|
||||||
_buffer = (char *) malloc(_capacity + 1);
|
_buffer = (char *) malloc(_capacity + 1);
|
||||||
if(_buffer == NULL){ //added for memory alloc error
|
|
||||||
_length = _capacity = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* used only once, really not effcient
|
// double the buffer size
|
||||||
// double the buffer size
|
inline void String::doubleBuffer( )
|
||||||
inline void String::doubleBuffer( )
|
{
|
||||||
{
|
|
||||||
char *temp = _buffer;
|
char *temp = _buffer;
|
||||||
getBuffer( ++_capacity * 2 );
|
getBuffer( ++_capacity * 2 );
|
||||||
strcpy( _buffer, temp );
|
strcpy( _buffer, temp );
|
||||||
free(temp);
|
free(temp);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
inline String operator+( String lhs, const String &rhs )
|
inline String operator+( String lhs, const String &rhs )
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user