Skip to content
Merged
41 changes: 22 additions & 19 deletions api/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ String::String(const char *cstr)
if (cstr) copy(cstr, strlen(cstr));
}

String::String(const char *cstr, unsigned int length)
{
init();
if (cstr) copy(cstr, length);
}

String::String(const String &value)
{
init();
Expand Down Expand Up @@ -192,7 +198,8 @@ String & String::copy(const char *cstr, unsigned int length)
return *this;
}
len = length;
strcpy(buffer, cstr);
memcpy(buffer, cstr, length);
buffer[len] = '\0';
return *this;
}

Expand All @@ -212,8 +219,9 @@ void String::move(String &rhs)
{
if (buffer) {
if (rhs && capacity >= rhs.len) {
strcpy(buffer, rhs.buffer);
memcpy(buffer, rhs.buffer, rhs.len);
len = rhs.len;
buffer[len] = '\0';
rhs.len = 0;
return;
} else {
Expand Down Expand Up @@ -284,8 +292,9 @@ unsigned char String::concat(const char *cstr, unsigned int length)
if (!cstr) return 0;
if (length == 0) return 1;
if (!reserve(newlen)) return 0;
strcpy(buffer + len, cstr);
memcpy(buffer + len, cstr, length);
len = newlen;
buffer[len] = '\0';
return 1;
}

Expand All @@ -297,59 +306,56 @@ unsigned char String::concat(const char *cstr)

unsigned char String::concat(char c)
{
char buf[2];
buf[0] = c;
buf[1] = 0;
return concat(buf, 1);
return concat(&c, 1);
}

unsigned char String::concat(unsigned char num)
{
char buf[1 + 3 * sizeof(unsigned char)];
itoa(num, buf, 10);
return concat(buf, strlen(buf));
return concat(buf);
}

unsigned char String::concat(int num)
{
char buf[2 + 3 * sizeof(int)];
itoa(num, buf, 10);
return concat(buf, strlen(buf));
return concat(buf);
}

unsigned char String::concat(unsigned int num)
{
char buf[1 + 3 * sizeof(unsigned int)];
utoa(num, buf, 10);
return concat(buf, strlen(buf));
return concat(buf);
}

unsigned char String::concat(long num)
{
char buf[2 + 3 * sizeof(long)];
ltoa(num, buf, 10);
return concat(buf, strlen(buf));
return concat(buf);
}

unsigned char String::concat(unsigned long num)
{
char buf[1 + 3 * sizeof(unsigned long)];
ultoa(num, buf, 10);
return concat(buf, strlen(buf));
return concat(buf);
}

unsigned char String::concat(float num)
{
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string));
return concat(string);
}

unsigned char String::concat(double num)
{
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
return concat(string, strlen(string));
return concat(string);
}

unsigned char String::concat(const __FlashStringHelper * str)
Expand Down Expand Up @@ -378,7 +384,7 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
{
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
if (!cstr || !a.concat(cstr)) a.invalidate();
return a;
}

Expand Down Expand Up @@ -629,10 +635,7 @@ String String::substring(unsigned int left, unsigned int right) const
String out;
if (left >= len) return out;
if (right > len) right = len;
char temp = buffer[right]; // save the replaced character
buffer[right] = '\0';
out = buffer + left; // pointer arithmetic
buffer[right] = temp; //restore character
out.copy(buffer + left, right - left);
return out;
}

Expand Down
5 changes: 4 additions & 1 deletion api/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class String
// fails, the string will be marked as invalid (i.e. "if (s)" will
// be false).
String(const char *cstr = "");
String(const char *cstr, unsigned int length);
String(const uint8_t *cstr, unsigned int length) : String((const char*)cstr, length) {}
String(const String &str);
String(const __FlashStringHelper *str);
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
Expand Down Expand Up @@ -109,6 +111,8 @@ class String
// concatenation is considered unsucessful.
unsigned char concat(const String &str);
unsigned char concat(const char *cstr);
unsigned char concat(const char *cstr, unsigned int length);
unsigned char concat(const uint8_t *cstr, unsigned int length) {return concat((const char*)cstr, length);}
unsigned char concat(char c);
unsigned char concat(unsigned char num);
unsigned char concat(int num);
Expand Down Expand Up @@ -225,7 +229,6 @@ class String
void init(void);
void invalidate(void);
unsigned char changeBuffer(unsigned int maxStrLen);
unsigned char concat(const char *cstr, unsigned int length);

// copy and move
String & copy(const char *cstr, unsigned int length);
Expand Down