Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitfdccb19

Browse files
authored
Merge pull request#97 from arduino/stringnonull
Strings without null-termination
2 parentsd5790a0 +d6fd84f commitfdccb19

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

‎api/String.cpp‎

Lines 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(constchar *cstr,unsignedint 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)return0;
285293
if (length ==0)return1;
286294
if (!reserve(newlen))return0;
287-
strcpy(buffer + len, cstr);
295+
memcpy(buffer + len, cstr, length);
288296
len = newlen;
297+
buffer[len] ='\0';
289298
return1;
290299
}
291300

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

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

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

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

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

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

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

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

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

355361
unsignedcharString::concat(const __FlashStringHelper * str)
@@ -378,7 +384,7 @@ StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
378384
StringSumHelper &operator + (const StringSumHelper &lhs,constchar *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‎

Lines 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(constchar *cstr ="");
71+
String(constchar *cstr,unsignedint length);
72+
String(constuint8_t *cstr,unsignedint length) : String((constchar*)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
unsignedcharconcat(const String &str);
111113
unsignedcharconcat(constchar *cstr);
114+
unsignedcharconcat(constchar *cstr,unsignedint length);
115+
unsignedcharconcat(constuint8_t *cstr,unsignedint length) {returnconcat((constchar*)cstr, length);}
112116
unsignedcharconcat(char c);
113117
unsignedcharconcat(unsignedchar num);
114118
unsignedcharconcat(int num);
@@ -225,7 +229,6 @@ class String
225229
voidinit(void);
226230
voidinvalidate(void);
227231
unsignedcharchangeBuffer(unsignedint maxStrLen);
228-
unsignedcharconcat(constchar *cstr,unsignedint length);
229232

230233
// copy and move
231234
String &copy(constchar *cstr,unsignedint length);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp