mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-20 14:54:31 +01:00
Don't return the string when modifying its value.
Changing toLowerCase(), toUpperCase(), trim() and replace() to return void instead of a reference to the string that's just been changed. That way, it's clear that the functions modify the string they've been called on.
This commit is contained in:
parent
7fa866ffea
commit
6727c8a831
@ -550,18 +550,17 @@ String String::substring(unsigned int left, unsigned int right) const
|
|||||||
/* Modification */
|
/* Modification */
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
|
|
||||||
String & String::replace(char find, char replace)
|
void String::replace(char find, char replace)
|
||||||
{
|
{
|
||||||
if (!buffer) return *this;
|
if (!buffer) return;
|
||||||
for (char *p = buffer; *p; p++) {
|
for (char *p = buffer; *p; p++) {
|
||||||
if (*p == find) *p = replace;
|
if (*p == find) *p = replace;
|
||||||
}
|
}
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String & String::replace(const String& find, const String& replace)
|
void String::replace(const String& find, const String& replace)
|
||||||
{
|
{
|
||||||
if (len == 0 || find.len == 0) return *this;
|
if (len == 0 || find.len == 0) return;
|
||||||
int diff = replace.len - find.len;
|
int diff = replace.len - find.len;
|
||||||
char *readFrom = buffer;
|
char *readFrom = buffer;
|
||||||
char *foundAt;
|
char *foundAt;
|
||||||
@ -588,8 +587,8 @@ String & String::replace(const String& find, const String& replace)
|
|||||||
readFrom = foundAt + find.len;
|
readFrom = foundAt + find.len;
|
||||||
size += diff;
|
size += diff;
|
||||||
}
|
}
|
||||||
if (size == len) return *this;
|
if (size == len) return;
|
||||||
if (size > capacity && !changeBuffer(size)) return *this;
|
if (size > capacity && !changeBuffer(size)) return; // XXX: tell user!
|
||||||
int index = len - 1;
|
int index = len - 1;
|
||||||
while ((index = lastIndexOf(find, index)) >= 0) {
|
while ((index = lastIndexOf(find, index)) >= 0) {
|
||||||
readFrom = buffer + index + find.len;
|
readFrom = buffer + index + find.len;
|
||||||
@ -600,30 +599,27 @@ String & String::replace(const String& find, const String& replace)
|
|||||||
index--;
|
index--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String & String::toLowerCase(void)
|
void String::toLowerCase(void)
|
||||||
{
|
{
|
||||||
if (!buffer) return *this;
|
if (!buffer) return;
|
||||||
for (char *p = buffer; *p; p++) {
|
for (char *p = buffer; *p; p++) {
|
||||||
*p = tolower(*p);
|
*p = tolower(*p);
|
||||||
}
|
}
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String & String::toUpperCase(void)
|
void String::toUpperCase(void)
|
||||||
{
|
{
|
||||||
if (!buffer) return *this;
|
if (!buffer) return;
|
||||||
for (char *p = buffer; *p; p++) {
|
for (char *p = buffer; *p; p++) {
|
||||||
*p = toupper(*p);
|
*p = toupper(*p);
|
||||||
}
|
}
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String & String::trim(void)
|
void String::trim(void)
|
||||||
{
|
{
|
||||||
if (!buffer || len == 0) return *this;
|
if (!buffer || len == 0) return;
|
||||||
char *begin = buffer;
|
char *begin = buffer;
|
||||||
while (isspace(*begin)) begin++;
|
while (isspace(*begin)) begin++;
|
||||||
char *end = buffer + len - 1;
|
char *end = buffer + len - 1;
|
||||||
@ -631,7 +627,6 @@ String & String::trim(void)
|
|||||||
len = end + 1 - begin;
|
len = end + 1 - begin;
|
||||||
if (begin > buffer) memcpy(buffer, begin, len);
|
if (begin > buffer) memcpy(buffer, begin, len);
|
||||||
buffer[len] = 0;
|
buffer[len] = 0;
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*********************************************/
|
/*********************************************/
|
||||||
|
@ -139,11 +139,11 @@ public:
|
|||||||
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
|
String substring( unsigned int beginIndex, unsigned int endIndex ) const;
|
||||||
|
|
||||||
// modification
|
// modification
|
||||||
String & replace(char find, char replace);
|
void replace(char find, char replace);
|
||||||
String & replace(const String& find, const String& replace);
|
void replace(const String& find, const String& replace);
|
||||||
String & toLowerCase(void);
|
void toLowerCase(void);
|
||||||
String & toUpperCase(void);
|
void toUpperCase(void);
|
||||||
String & trim(void);
|
void trim(void);
|
||||||
|
|
||||||
// parsing/conversion
|
// parsing/conversion
|
||||||
long toInt(void) const;
|
long toInt(void) const;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user