-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 a string splitter class #27154
Merged
Merged
Add a string splitter class #27154
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f055777
Add a string splitter class
andreilitvin c41a3f1
Do not fail on stringop-truncation in tests
andreilitvin 1e78cf6
Fix initialization of mData for null cases for stringsplitter
andreilitvin 7be1d55
Restyled by clang-format
restyled-commits 9c9ed81
Add more unit tests and ensure final empty element is emitted
andreilitvin 71b704a
Restyled by clang-format
restyled-commits 03cc684
separator should be const
andreilitvin 3ced41d
Merge branch 'master' into string_splitter
andreilitvin b3ab8c2
Review comment: use char span
andreilitvin a9aa2d5
Restyled by clang-format
restyled-commits 1523a44
Force a static cast for size compares
andreilitvin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* | ||
* Copyright (c) 2023 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#pragma once | ||
|
||
#include <lib/support/Span.h> | ||
|
||
namespace chip { | ||
|
||
/// Provides the ability to split a given string by a character. | ||
/// | ||
/// Converts things like: | ||
/// "a,b,c" split by ',': "a", "b", "c" | ||
/// ",b,c" split by ',': "", "b", "c" | ||
/// "a,,c" split by ',': "a", "", "c" | ||
/// "a," split by ',': "a", "" | ||
/// ",a" split by ',': "", "a" | ||
/// | ||
/// | ||
/// WARNING: WILL DESTRUCTIVELY MODIFY THE STRING IN PLACE | ||
/// | ||
class StringSplitter | ||
{ | ||
public: | ||
StringSplitter(const char * s, char separator) : mNext(s), mSeparator(separator) | ||
{ | ||
if ((mNext != nullptr) && (*mNext == '\0')) | ||
{ | ||
mNext = nullptr; // end of string right away | ||
} | ||
} | ||
|
||
/// Returns the next character san | ||
/// | ||
/// out - contains the next element or a nullptr/0 sized span if | ||
/// no elements available | ||
/// | ||
/// Returns true if an element is available, false otherwise. | ||
bool Next(CharSpan & out) | ||
{ | ||
if (mNext == nullptr) | ||
{ | ||
out = CharSpan(); | ||
return false; // nothing left | ||
} | ||
|
||
const char * end = mNext; | ||
while ((*end != '\0') && (*end != mSeparator)) | ||
{ | ||
end++; | ||
} | ||
|
||
if (*end != '\0') | ||
{ | ||
// intermediate element | ||
out = CharSpan(mNext, static_cast<size_t>(end - mNext)); | ||
mNext = end + 1; | ||
} | ||
else | ||
{ | ||
// last element | ||
out = CharSpan::fromCharString(mNext); | ||
mNext = nullptr; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected: | ||
const char * mNext; // start of next element to return by Next() | ||
const char mSeparator; | ||
}; | ||
|
||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* | ||
* Copyright (c) 2023 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include <lib/support/StringSplitter.h> | ||
#include <lib/support/UnitTestRegistration.h> | ||
|
||
#include <nlunit-test.h> | ||
|
||
namespace { | ||
|
||
using namespace chip; | ||
|
||
void TestStrdupSplitter(nlTestSuite * inSuite, void * inContext) | ||
{ | ||
CharSpan out; | ||
|
||
// empty string handling | ||
{ | ||
StringSplitter splitter("", ','); | ||
|
||
// next stays at nullptr | ||
NL_TEST_ASSERT(inSuite, !splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data() == nullptr); | ||
NL_TEST_ASSERT(inSuite, !splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data() == nullptr); | ||
NL_TEST_ASSERT(inSuite, !splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data() == nullptr); | ||
} | ||
andy31415 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// single item | ||
{ | ||
StringSplitter splitter("single", ','); | ||
|
||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("single"))); | ||
|
||
// next stays at nullptr also after valid data | ||
NL_TEST_ASSERT(inSuite, !splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data() == nullptr); | ||
NL_TEST_ASSERT(inSuite, !splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data() == nullptr); | ||
} | ||
|
||
// multi-item | ||
{ | ||
StringSplitter splitter("one,two,three", ','); | ||
|
||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("one"))); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("two"))); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("three"))); | ||
NL_TEST_ASSERT(inSuite, !splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data() == nullptr); | ||
} | ||
|
||
// mixed | ||
{ | ||
StringSplitter splitter("a**bc*d,e*f", '*'); | ||
|
||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("a"))); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("bc"))); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("d,e"))); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("f"))); | ||
NL_TEST_ASSERT(inSuite, !splitter.Next(out)); | ||
} | ||
|
||
// some edge cases | ||
{ | ||
StringSplitter splitter(",", ','); | ||
// Note that even though "" is nullptr right away, "," becomes two empty strings | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); | ||
NL_TEST_ASSERT(inSuite, !splitter.Next(out)); | ||
} | ||
{ | ||
StringSplitter splitter("log,", ','); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("log"))); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); | ||
NL_TEST_ASSERT(inSuite, !splitter.Next(out)); | ||
} | ||
{ | ||
StringSplitter splitter(",log", ','); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("log"))); | ||
NL_TEST_ASSERT(inSuite, !splitter.Next(out)); | ||
} | ||
{ | ||
StringSplitter splitter(",,,", ','); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); | ||
NL_TEST_ASSERT(inSuite, splitter.Next(out)); | ||
NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); | ||
NL_TEST_ASSERT(inSuite, !splitter.Next(out)); | ||
} | ||
} | ||
|
||
void TestNullResilience(nlTestSuite * inSuite, void * inContext) | ||
{ | ||
{ | ||
StringSplitter splitter(nullptr, ','); | ||
CharSpan span; | ||
NL_TEST_ASSERT(inSuite, !splitter.Next(span)); | ||
NL_TEST_ASSERT(inSuite, span.data() == nullptr); | ||
} | ||
} | ||
|
||
const nlTest sTests[] = { | ||
NL_TEST_DEF("TestSplitter", TestStrdupSplitter), // | ||
NL_TEST_DEF("TestNullResilience", TestNullResilience), // | ||
NL_TEST_SENTINEL() // | ||
}; | ||
|
||
} // namespace | ||
|
||
int TestStringSplitter() | ||
{ | ||
nlTestSuite theSuite = { "StringSplitter", sTests, nullptr, nullptr }; | ||
nlTestRunner(&theSuite, nullptr); | ||
return nlTestRunnerStats(&theSuite); | ||
} | ||
|
||
CHIP_REGISTER_TEST_SUITE(TestStringSplitter) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"span", not "san"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops ... patched into #27156 because small delta.