Skip to content

Strings without null-termination #1936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
40 changes: 20 additions & 20 deletions hardware/arduino/avr/cores/arduino/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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 @@ -174,7 +180,7 @@ String & String::copy(const char *cstr, unsigned int length)
return *this;
}
len = length;
strcpy(buffer, cstr);
memcpy(buffer, cstr, length);
return *this;
}

Expand All @@ -194,7 +200,7 @@ void String::move(String &rhs)
{
if (buffer) {
if (capacity >= rhs.len) {
strcpy(buffer, rhs.buffer);
memcpy(buffer, rhs.buffer, rhs.len);
len = rhs.len;
rhs.len = 0;
return;
Expand Down Expand Up @@ -266,7 +272,7 @@ 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;
return 1;
}
Expand All @@ -279,59 +285,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 @@ -360,7 +363,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 @@ -621,10 +624,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 Expand Up @@ -696,7 +696,7 @@ void String::remove(unsigned int index, unsigned int count){
if (count > len - index) { count = len - index; }
char *writeTo = buffer + index;
len = len - count;
strncpy(writeTo, buffer + index + count,len - index);
memmove(writeTo, buffer + index + count, len - index);
buffer[len] = 0;
}

Expand Down
5 changes: 4 additions & 1 deletion hardware/arduino/avr/cores/arduino/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 @@ -98,6 +100,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 c);
unsigned char concat(int num);
Expand Down Expand Up @@ -195,7 +199,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
40 changes: 20 additions & 20 deletions hardware/arduino/sam/cores/arduino/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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 @@ -176,7 +182,7 @@ String & String::copy(const char *cstr, unsigned int length)
return *this;
}
len = length;
strcpy(buffer, cstr);
memcpy(buffer, cstr, length);
return *this;
}

Expand All @@ -196,7 +202,7 @@ void String::move(String &rhs)
{
if (buffer) {
if (capacity >= rhs.len) {
strcpy(buffer, rhs.buffer);
memcpy(buffer, rhs.buffer, rhs.len);
len = rhs.len;
rhs.len = 0;
return;
Expand Down Expand Up @@ -268,7 +274,7 @@ 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;
return 1;
}
Expand All @@ -281,59 +287,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 @@ -362,7 +365,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 @@ -623,10 +626,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 Expand Up @@ -698,7 +698,7 @@ void String::remove(unsigned int index, unsigned int count){
if (count > len - index) { count = len - index; }
char *writeTo = buffer + index;
len = len - count;
strncpy(writeTo, buffer + index + count,len - index);
memmove(writeTo, buffer + index + count, len - index);
buffer[len] = 0;
}

Expand Down
5 changes: 4 additions & 1 deletion hardware/arduino/sam/cores/arduino/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 @@ -98,6 +100,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 c);
unsigned char concat(int num);
Expand Down Expand Up @@ -195,7 +199,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