mirror of
https://github.com/arduino/Arduino.git
synced 2025-01-18 07:52:14 +01:00
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.
This commit is contained in:
parent
f8077a96d7
commit
722b93dce4
@ -45,7 +45,9 @@ void Print::write(const uint8_t *buffer, size_t size)
|
||||
|
||||
void Print::print(const String &s)
|
||||
{
|
||||
print(s.toCharArray());
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
write(s[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void Print::print(const char str[])
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -76,8 +76,8 @@ class String
|
||||
String toLowerCase( ) const;
|
||||
String toUpperCase( ) const;
|
||||
String trim( ) const;
|
||||
const unsigned char *getBytes() const { return (unsigned char*)_buffer; }
|
||||
const char* toCharArray() const { return _buffer; }
|
||||
void getBytes(unsigned char *buf, unsigned int bufsize);
|
||||
void toCharArray(char *buf, unsigned int bufsize);
|
||||
const String& concat( const String &str );
|
||||
String replace( char oldChar, char newChar );
|
||||
String replace( const String& match, const String& replace );
|
||||
|
Loading…
x
Reference in New Issue
Block a user