1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:28:11 +00:00

AK: Add AKString::split_limit to split strings with a limit

This is a small change to the existing split() functionality to support
the case of splitting a string and stopping at a certain number of
tokens. This is useful for parsing e.g. key/value pairs, where the value
may contain the delimiter you're splitting on.
This commit is contained in:
Conrad Pankoff 2019-06-04 18:13:07 +10:00 committed by Andreas Kling
parent ccc6e69a29
commit 042895317d
2 changed files with 7 additions and 1 deletions

View file

@ -68,13 +68,18 @@ StringView String::substring_view(int start, int length) const
}
Vector<String> String::split(const char separator) const
{
return split_limit(separator, 0);
}
Vector<String> String::split_limit(const char separator, int limit) const
{
if (is_empty())
return {};
Vector<String> v;
ssize_t substart = 0;
for (ssize_t i = 0; i < length(); ++i) {
for (ssize_t i = 0; i < length() && (v.size() + 1) != limit; ++i) {
char ch = characters()[i];
if (ch == separator) {
ssize_t sublen = i - substart;