Skip to content

Commit 555800b

Browse files
Add String(char *, unsigned) constructor
This constructor allows converting a buffer containing a non-nul-terminated string to a String object, by explicitely passing the length.
1 parent e7d934d commit 555800b

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

hardware/arduino/avr/cores/arduino/WString.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ String::String(const char *cstr)
3131
if (cstr) copy(cstr, strlen(cstr));
3232
}
3333

34+
String::String(const char *cstr, unsigned int length)
35+
{
36+
init();
37+
if (cstr) copy(cstr, length);
38+
}
39+
3440
String::String(const String &value)
3541
{
3642
init();

hardware/arduino/avr/cores/arduino/WString.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class String
5757
// fails, the string will be marked as invalid (i.e. "if (s)" will
5858
// be false).
5959
String(const char *cstr = "");
60+
String(const char *cstr, unsigned int length);
6061
String(const String &str);
6162
String(const __FlashStringHelper *str);
6263
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)

hardware/arduino/sam/cores/arduino/WString.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ String::String(const char *cstr)
3333
if (cstr) copy(cstr, strlen(cstr));
3434
}
3535

36+
String::String(const char *cstr, unsigned int length)
37+
{
38+
init();
39+
if (cstr) copy(cstr, length);
40+
}
41+
3642
String::String(const String &value)
3743
{
3844
init();

hardware/arduino/sam/cores/arduino/WString.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class String
5757
// fails, the string will be marked as invalid (i.e. "if (s)" will
5858
// be false).
5959
String(const char *cstr = "");
60+
String(const char *cstr, unsigned int length);
6061
String(const String &str);
6162
String(const __FlashStringHelper *str);
6263
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)

0 commit comments

Comments
 (0)