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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions Sming/Core/Data/CStringArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,25 @@ unsigned CStringArray::count() const
}
return stringCount;
}

String CStringArray::join(const String& separator) const
{
if(isNull()) {
return nullptr;
}
unsigned len = length();
if(len == 0) {
return "";
}
len -= count(); // NUL separators
len += separator.length() * (stringCount - 1);
String s;
s.reserve(len);
for(auto it = begin(); it != end(); ++it) {
if(it.index() != 0) {
s += separator;
}
s += *it;
}
return s;
}
9 changes: 9 additions & 0 deletions Sming/Core/Data/CStringArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,15 @@ class CStringArray : private String
*/
unsigned count() const;

/**
* @brief Get contents of array as delimited string
* @param separator What to join elements with
* @retval String
*
* 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.


/**
* @name Iterator support (forward only)
* @{
Expand Down
19 changes: 19 additions & 0 deletions tests/HostTests/modules/CStringArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,25 @@ class CStringArrayTest : public TestGroup
REQUIRE(csa.back() == nullptr);
REQUIRE(!csa.popBack());
}

TEST_CASE("join")
{
CStringArray csa;
REQUIRE(!csa.join());

csa = "";
REQUIRE(csa.join() == "");

csa.add("a");
REQUIRE(csa.join() == "a");

csa.add("");
REQUIRE(csa.join() == "a,");

csa.add(F("test\0again"));
REQUIRE_EQ(csa.join("}+{"), "a}+{}+{test}+{again");
REQUIRE_EQ(csa.join(nullptr), "atestagain");
}
}
};

Expand Down