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

Add CStringArray::join() method #2668

Merged
merged 1 commit into from
Sep 25, 2023

Conversation

mikee47
Copy link
Contributor

@mikee47 mikee47 commented Sep 23, 2023

Available in other languages (python, javascript) this provides an efficient way to concatenate array members.

@what-the-diff
Copy link

what-the-diff bot commented Sep 23, 2023

PR Summary

  • Introduction of a Novel Method in CStringArray Class
    A new function called join has been incorporated into the CStringArray class. This function allows arranged items in the array to be conveniently converted into a string separated by a delimiter, which is, by default, a comma. Although the separator is preset to a comma, this can be adjusted depending on the user's preference.

  • Addition of Tests for the New Method
    Essential tests have been added to evaluate the performance and correctness of the recently implemented join function. This is crucial to ensure that the method works as expected and doesn't produce unexpected outcomes.

  • Improved Code Readability in CStringArray.cpp
    Several formatting adjustments have been made to the CStringArray.cpp file. The goal of these changes is to enhance the readability of the code, making it easier for the developers to understand, manage, and maintain the codebase.

@slaff slaff added this to the 5.1.0 milestone Sep 25, 2023
*
* e.g. CStringArray(F("a\0b\0c")).join() returns "a,b,c"
*/
String join(const String& separator = ",") const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikee47 Is there a reason for the separator parameter to be optional? In JavaScript, PHP and Python as far as I can say this parameter is mandatory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't considered PHP but default is same as javascript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join.

Python would be ",".join(cs) which is an interesting construction - clearly there cannot be any 'default' in this case. However, to actually implement this efficiently for Sming would require that the array itself does the join. We could add this as a template method to String easily enough:

class String
{
  ...

  template <class Array> String join(const Array& array)
  {
    return array.join(*this);
  }

  ...
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@mikee47 mikee47 Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to actually implement this efficiently for Sming would require that the array itself does the join

The efficiency here is avoiding reallocations for the returned String as in the general case:

template <class Array> String join(const Array& array, const String& separator)
{
  String s;
  for(auto& e: array) {
    if(s) {
      s += separator;
    }
    s += e;
  }
  return s;
}

This should work for any Vector or other iterable objects with an implicit String() operator. Something that works with HashMaps would also be useful (returning delimited 'name=value' pairs). However the implementation as it stands does not perform any string escaping, for example:

CStringArray cs;
cs.add("first");
cs.add("second,plus,stuff");

Then cs.join() returns "first,second,plus,stuff" as requested. If "first,""second,plus,stuff""" is required that'll have to be done manually. This is same behaviour as python.

@slaff slaff merged commit 69685b1 into SmingHub:develop Sep 25, 2023
32 of 36 checks passed
@mikee47 mikee47 deleted the feature/cstringarray-join branch September 25, 2023 10:35
@slaff slaff mentioned this pull request Oct 24, 2023
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants