aboutsummaryrefslogtreecommitdiff
path: root/cores/arduino/WString.cpp
diff options
context:
space:
mode:
authorDavid A. Mellis <d.mellis@arduino.cc>2010-08-28 10:23:54 +0000
committerDavid A. Mellis <d.mellis@arduino.cc>2010-08-28 10:23:54 +0000
commit4bbd4f7448907ed0133ef737fc694751f9f97ea3 (patch)
treedd2455545cc79ad5b09c598f7fdbb0d4f4e4b4b4 /cores/arduino/WString.cpp
parent1362ca26c198082235f71192861656385a1eab20 (diff)
Changing String::toCharArray() and getBytes() to accept a buffer, rather than return one. That way they don't expose the internal representation of the String class, allowing future optimization. Thanks to Paul Stoffregen.
Diffstat (limited to 'cores/arduino/WString.cpp')
-rw-r--r--cores/arduino/WString.cpp27
1 files changed, 22 insertions, 5 deletions
diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp
index fd07c2d..2718956 100644
--- a/cores/arduino/WString.cpp
+++ b/cores/arduino/WString.cpp
@@ -169,7 +169,7 @@ int String::operator==( const String &rhs ) const
int String::operator!=( const String &rhs ) const
{
- return ( _length != rhs.length() || strcmp( _buffer, rhs.toCharArray() ) != 0 );
+ return ( _length != rhs.length() || strcmp( _buffer, rhs._buffer ) != 0 );
}
int String::operator<( const String &rhs ) const
@@ -213,7 +213,7 @@ boolean String::endsWith( const String &s2 ) const
if ( _length < s2._length )
return 0;
- return strcmp( &_buffer[ _length - s2._length], s2.toCharArray() ) == 0;
+ return strcmp( &_buffer[ _length - s2._length], s2._buffer ) == 0;
}
boolean String::equals( const String &s2 ) const
@@ -228,7 +228,7 @@ boolean String::equalsIgnoreCase( const String &s2 ) const
else if ( _length != s2._length )
return false; //0;
- return strcmp(toLowerCase().toCharArray(), s2.toLowerCase().toCharArray()) == 0;
+ return strcmp(toLowerCase()._buffer, s2.toLowerCase()._buffer) == 0;
}
String String::replace( char findChar, char replaceChar )
@@ -285,7 +285,7 @@ int String::indexOf( const String &s2, unsigned int fromIndex ) const
if ( fromIndex >= _length )
return -1;
- const char *theFind = strstr( &_buffer[ fromIndex ], s2.toCharArray() );
+ const char *theFind = strstr( &_buffer[ fromIndex ], s2._buffer );
if ( theFind == NULL )
return -1;
@@ -349,7 +349,7 @@ 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;
+ return strncmp( &_buffer[offset], s2._buffer, s2._length ) == 0;
}
String String::substring( unsigned int left ) const
@@ -417,3 +417,20 @@ String String::trim() const
return temp.substring( i, j + 1);
}
+void String::getBytes(unsigned char *buf, unsigned int bufsize)
+{
+ if (!bufsize || !buf) return;
+ unsigned int len = bufsize - 1;
+ if (len > _length) len = _length;
+ strncpy((char *)buf, _buffer, len);
+ buf[len] = 0;
+}
+
+void String::toCharArray(char *buf, unsigned int bufsize)
+{
+ if (!bufsize || !buf) return;
+ unsigned int len = bufsize - 1;
+ if (len > _length) len = _length;
+ strncpy(buf, _buffer, len);
+ buf[len] = 0;
+}