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

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

Copy link
Copy link
Open
@TobiasKnauss

Description

@TobiasKnauss
Issue body actions

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: CoreRelated to the code for the standard Arduino APIRelated to the code for the standard Arduino APIfeature requestA request to make an enhancement (not a bug fix)A request to make an enhancement (not a bug fix)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

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