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

Strings without null-termination#97

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
aentinger merged 9 commits intomasterfromstringnonull
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
9 commits
Select commitHold shift + click to select a range
184dd3e
Remove strlen in calls like String::concat(s, strlen(s))
matthijskooijmanMar 16, 2014
59dd995
Make string concat, copy and move work on non-terminated strings
matthijskooijmanMar 16, 2014
81a2d51
Simplify String::concat(char)
matthijskooijmanMar 16, 2014
3b88aca
Make String::concat(const char *, unsigned int) public
matthijskooijmanMar 16, 2014
8431488
Add String(char *, unsigned) constructor
matthijskooijmanMar 16, 2014
dd236bf
Don't mess with the original in String::substring
matthijskooijmanMar 16, 2014
eaab14d
Add String::concat(const uint8_t *, unsigned int) version
matthijskooijmanMar 17, 2014
0d83f1a
Add String(const uint8_t *, unsigned int) constructor
matthijskooijmanAug 4, 2015
d6fd84f
fixup! Make string concat, copy and move work on non-terminated strings
matthijskooijmanDec 31, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletionsapi/String.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 deletionapi/String.h
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp