Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String class (WString.cpp/.h) new functions #4890

Closed
wants to merge 8 commits into from

Conversation

TobiasKnauss
Copy link

See
https://groups.google.com/a/arduino.cc/forum/#!topic/developers/ryrMAJiqwf0

summary:

  • constructors enhanced with "unsigned int bufferSize = 0" to allow a preset size for the internal char array malloc.
  • void String::setReservePercentage (unsigned char percentage)
    adds a reserve on the realloc so that further small String concats don't have to realloc every time.
  • void String::setDefaultReservePercentage (unsigned char percentage)
    sets a default value for reservePercentage, that is used when creating new Strings.

These 3 functions operate in init() and reserve().

Added String manipulation functions:
// modification
// notice: void function (...) operates on the object and returns void
// String functionC (...) operates on a copy of the object and returns the copy (C=Copy)
// String* functionS (...) operates on the object and returns a pointer to the object (S=Self)

existing before #### void replace (char find, char replace);

String replaceC(char find, char replace);
String* replaceS(char find, char replace);

existing before #### void replace (const String& find, const String& replace);

String replaceC(const String& find, const String& replace);
String* replaceS(const String& find, const String& replace);

existing before #### void remove (unsigned int index);

String removeC(unsigned int index);
String* removeS(unsigned int index);

existing before #### void remove (unsigned int index, unsigned int count);

String removeC(unsigned int index, unsigned int count);
String* removeS(unsigned int index, unsigned int count);

existing before #### void toLowerCase (void);

String toLowerCaseC(void);
String* toLowerCaseS(void);

existing before #### void toUpperCase (void);

String toUpperCaseC(void);
String* toUpperCaseS(void);

existing before #### void trim (void);

String trimC(void);
String* trimS(void);
void trim (char remove);
String trimC(char remove);
String* trimS(char remove);
void trimStart (void);
String trimStartC(void);
String* trimStartS(void);
void trimStart (char remove);
String trimStartC(char remove);
String* trimStartS(char remove);
void trimEnd (void);
String trimEndC(void);
String* trimEndS(void);
void trimEnd (char remove);
String trimEndC(char remove);
String* trimEndS(char remove);
void padLeft (unsigned int newlength);
String padLeftC(unsigned int newlength);
String* padLeftS(unsigned int newlength);
void padLeft (unsigned int newlength, char add);
String padLeftC(unsigned int newlength, char add);
String* padLeftS(unsigned int newlength, char add);
void padRight (unsigned int newlength);
String padRightC(unsigned int newlength);
String* padRightS(unsigned int newlength);
void padRight (unsigned int newlength, char add);
String padRightC(unsigned int newlength, char add);
String* padRightS(unsigned int newlength, char add);
void cropLeft (unsigned int count);
String cropLeftC(unsigned int count);
String* cropLeftS(unsigned int count);
void cropRight (unsigned int count);
String cropRightC(unsigned int count);
String* cropRightS(unsigned int count);
void keepLeft (unsigned int count);
String keepLeftC(unsigned int count);
String* keepLeftS(unsigned int count);
void keepRight (unsigned int count);
String keepRightC(unsigned int count);
String* keepRightS(unsigned int count);

detailed descriptions at
#4870

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

also:
  // modification
  // notice:  void    function  (...) operates on           the object and returns void
  //          String  functionC (...) operates on a copy of the object and returns the copy (C=Copy)
  //          String* functionS (...) operates on           the object and returns a pointer to the object (S=Self)
  void    replace (char find, char replace);
  String  replaceC(char find, char replace);
  String* replaceS(char find, char replace);
  void    replace (const
When the String instance is created and/or increased, a new variable "reserverPercentage" defines how much reserve to keep when resizing.
The constructors of the String class are modified to take an additional parameter 'bufferSize' which defines the minimum capacity when the memory of the buffer array is allocated.
The constructors of the String class are modified to take an additional parameter 'bufferSize' which defines the minimum capacity when the memory of the buffer array is allocated.
Whitespace tabs converted to spaces.
@cousteaulecommandant
Copy link
Contributor

cousteaulecommandant commented May 6, 2016

Instead of adding the -S versions, wouldn't it be better to just replace the functions returning void with functions returning String&? That way you would be able to do things like myString.trim().replace('\\','/').toLowerCase().
Also I think it would be simpler to just add a String copy(void) method for when you want to explicitly operate on a copy of the String rather than adding a lot of new -C functions potentially increasing the library size (plus I think the -C suffix is a bit cryptic).
...well, maybe not copy(), as this already seems to be used for something different; maybe dup() or something like that.

@facchinm facchinm added the Print and Stream class The Arduino core library's Print and Stream classes label Jan 20, 2017
@BillyDonahue
Copy link

@cousteaulecommandant, can't the proposed s.dup() already be expressed as String(s)?

@cousteaulecommandant
Copy link
Contributor

cousteaulecommandant commented Jan 23, 2018

@cousteaulecommandant, can't the proposed s.dup() already be expressed as String(s)?

Yes, but I think string2 = string1.dup().trim().replace('\\','/').toLowerCase() might look more consistent than string2 = String(string1).trim().replace('\\','/').toLowerCase(), if this "chain of methods" approach is to be followed.
Then again, maybe an explicit string creation is a better idea than a "method"-based copy, so you might be right. I was just proposing a direct alternative to the functions proposed here.

@facchinm
Copy link
Member

I agree with #4890 (comment) , and this discussion should be migrated to https://github.com/arduino/ArduinoCore-api since it allows very interesting stuff. We are not going to add C and S versions so I'm closing this PR

@facchinm facchinm closed this Sep 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Print and Stream class The Arduino core library's Print and Stream classes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants