arduino / arduino/Arduino

New functionality in String class (WString.cpp/.h)

Open
#4,870 5 comments 0 reactions 0 assignees View on GitHub
Component: Core feature request
Dominant language
Java
Stars
14.6k
Forks
7k
PR merge metrics
No merged PRs in 30d

Description

I need some new functions in the the String class.
I had created 2 posts about that in the arduino developers google group some hours ago, but the posts never appeared. Maybe the moderator is currently N/A...?

While waiting for the posts to appear, I have created the functionality and tested it.
It's ready to use and I'd be happy to see it implemented in the official version of the library:

```
WString.h:
void trim(void);
void trim(char remove);
void trimStart(void);
void trimStart(char remove);
void trimEnd(void);
void trimEnd(char remove);
void padLeft(unsigned int newlength);
void padLeft(unsigned int newlength, char add);
void padRight(unsigned int newlength);
void padRight(unsigned int newlength, char add);
void cropLeft(unsigned int count);
void cropRight(unsigned int count);
void keepLeft(unsigned int count);
void keepRight(unsigned int count);

WString.cpp:
void String::trim(void)
{
trim (0x20);
}

void String::trim(char remove)
{
trimStart(remove);
trimEnd (remove);
}

void String::trimStart(void)
{
trimStart(0x20);
}

void String::trimStart(char remove)
{
if (!buffer || len == 0 || remove == 0) return;
char *begin = buffer;
while ((*begin) == remove) begin++;
len = buffer + len - begin;
if (begin > buffer) memmove(buffer, begin, len);
buffer[len] = 0;
}

void String::trimEnd(void)
{
trimEnd(0x20);
}

void String::trimEnd(char remove)
{
if (!buffer || len == 0 || remove == 0) return;
char *end = buffer + len - 1;
while ((*end) == remove && end >= buffer) end--;
len = end + 1 - buffer;
buffer[len] = 0;
}

void String::padLeft(unsigned int newlength)
{
padLeft(newlength, 0x20);
}

void String::padLeft(unsigned int newlength, char add)
{
if (newlength < len) newlength = len;
if (!reserve(newlength)) return;
unsigned int lenAdd = newlength - len;
char *begin = buffer + lenAdd;
memmove (begin, buffer, len);
memset (buffer, add, lenAdd);
len = newlength;
buffer[len] = 0;
}

void String::padRight(unsigned int newlength)
{
padRight(newlength, 0x20);
}

void String::padRight(unsigned int newlength, char add)
{
if (newlength < len) newlength = len;
if (!reserve(newlength)) return;
unsigned int lenAdd = newlength - len;
char *begin = buffer + len;
memset (begin, add, lenAdd);
len = newlength;
buffer[len] = 0;
}

void String::cropLeft(unsigned int count)
{
unsigned int lenKeep = len - count;
keepRight(lenKeep);
}

void String::cropRight(unsigned int count)
{
unsigned int lenKeep = len - count;
keepLeft(lenKeep);
}

void String::keepLeft(unsigned int count)
{
if (!buffer || len == 0) return;
if (count <= len) len = count;
buffer[len] = 0;
}

void String::keepRight(unsigned int count)
{
if (!buffer || len == 0) return;
if (count > len) count = len;
char *begin = buffer + len - count;
len = count;
if (begin > buffer) memmove(buffer, begin, len);
buffer[len] = 0;
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the proposed API in WString.h and the corresponding String implementations in WString.cpp. Check the existing String behavior and available test coverage, then verify that trimming, padding, cropping, and keeping produce the requested results without breaking existing callers.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
embedded-iot
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.