Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fdccb19

Browse filesBrowse files
authored
Merge pull request #97 from arduino/stringnonull
Strings without null-termination
2 parents d5790a0 + d6fd84f commit fdccb19
Copy full SHA for fdccb19

File tree

2 files changed

+26
-20
lines changed
Filter options

2 files changed

+26
-20
lines changed

‎api/String.cpp

Copy file name to clipboardExpand all lines: api/String.cpp
+22-19Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ String::String(const char *cstr)
4545
if (cstr) copy(cstr, strlen(cstr));
4646
}
4747

48+
String::String(const char *cstr, unsigned int length)
49+
{
50+
init();
51+
if (cstr) copy(cstr, length);
52+
}
53+
4854
String::String(const String &value)
4955
{
5056
init();
@@ -192,7 +198,8 @@ String & String::copy(const char *cstr, unsigned int length)
192198
return *this;
193199
}
194200
len = length;
195-
strcpy(buffer, cstr);
201+
memcpy(buffer, cstr, length);
202+
buffer[len] = '\0';
196203
return *this;
197204
}
198205

@@ -212,8 +219,9 @@ void String::move(String &rhs)
212219
{
213220
if (buffer) {
214221
if (rhs && capacity >= rhs.len) {
215-
strcpy(buffer, rhs.buffer);
222+
memcpy(buffer, rhs.buffer, rhs.len);
216223
len = rhs.len;
224+
buffer[len] = '\0';
217225
rhs.len = 0;
218226
return;
219227
} else {
@@ -284,8 +292,9 @@ unsigned char String::concat(const char *cstr, unsigned int length)
284292
if (!cstr) return 0;
285293
if (length == 0) return 1;
286294
if (!reserve(newlen)) return 0;
287-
strcpy(buffer + len, cstr);
295+
memcpy(buffer + len, cstr, length);
288296
len = newlen;
297+
buffer[len] = '\0';
289298
return 1;
290299
}
291300

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

298307
unsigned char String::concat(char c)
299308
{
300-
char buf[2];
301-
buf[0] = c;
302-
buf[1] = 0;
303-
return concat(buf, 1);
309+
return concat(&c, 1);
304310
}
305311

306312
unsigned char String::concat(unsigned char num)
307313
{
308314
char buf[1 + 3 * sizeof(unsigned char)];
309315
itoa(num, buf, 10);
310-
return concat(buf, strlen(buf));
316+
return concat(buf);
311317
}
312318

313319
unsigned char String::concat(int num)
314320
{
315321
char buf[2 + 3 * sizeof(int)];
316322
itoa(num, buf, 10);
317-
return concat(buf, strlen(buf));
323+
return concat(buf);
318324
}
319325

320326
unsigned char String::concat(unsigned int num)
321327
{
322328
char buf[1 + 3 * sizeof(unsigned int)];
323329
utoa(num, buf, 10);
324-
return concat(buf, strlen(buf));
330+
return concat(buf);
325331
}
326332

327333
unsigned char String::concat(long num)
328334
{
329335
char buf[2 + 3 * sizeof(long)];
330336
ltoa(num, buf, 10);
331-
return concat(buf, strlen(buf));
337+
return concat(buf);
332338
}
333339

334340
unsigned char String::concat(unsigned long num)
335341
{
336342
char buf[1 + 3 * sizeof(unsigned long)];
337343
ultoa(num, buf, 10);
338-
return concat(buf, strlen(buf));
344+
return concat(buf);
339345
}
340346

341347
unsigned char String::concat(float num)
342348
{
343349
char buf[20];
344350
char* string = dtostrf(num, 4, 2, buf);
345-
return concat(string, strlen(string));
351+
return concat(string);
346352
}
347353

348354
unsigned char String::concat(double num)
349355
{
350356
char buf[20];
351357
char* string = dtostrf(num, 4, 2, buf);
352-
return concat(string, strlen(string));
358+
return concat(string);
353359
}
354360

355361
unsigned char String::concat(const __FlashStringHelper * str)
@@ -378,7 +384,7 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
378384
StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
379385
{
380386
StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
381-
if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
387+
if (!cstr || !a.concat(cstr)) a.invalidate();
382388
return a;
383389
}
384390

@@ -629,10 +635,7 @@ String String::substring(unsigned int left, unsigned int right) const
629635
String out;
630636
if (left >= len) return out;
631637
if (right > len) right = len;
632-
char temp = buffer[right]; // save the replaced character
633-
buffer[right] = '\0';
634-
out = buffer + left; // pointer arithmetic
635-
buffer[right] = temp; //restore character
638+
out.copy(buffer + left, right - left);
636639
return out;
637640
}
638641

‎api/String.h

Copy file name to clipboardExpand all lines: api/String.h
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class String
6868
// fails, the string will be marked as invalid (i.e. "if (s)" will
6969
// be false).
7070
String(const char *cstr = "");
71+
String(const char *cstr, unsigned int length);
72+
String(const uint8_t *cstr, unsigned int length) : String((const char*)cstr, length) {}
7173
String(const String &str);
7274
String(const __FlashStringHelper *str);
7375
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
@@ -109,6 +111,8 @@ class String
109111
// concatenation is considered unsucessful.
110112
unsigned char concat(const String &str);
111113
unsigned char concat(const char *cstr);
114+
unsigned char concat(const char *cstr, unsigned int length);
115+
unsigned char concat(const uint8_t *cstr, unsigned int length) {return concat((const char*)cstr, length);}
112116
unsigned char concat(char c);
113117
unsigned char concat(unsigned char num);
114118
unsigned char concat(int num);
@@ -225,7 +229,6 @@ class String
225229
void init(void);
226230
void invalidate(void);
227231
unsigned char changeBuffer(unsigned int maxStrLen);
228-
unsigned char concat(const char *cstr, unsigned int length);
229232

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.