mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +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:
parent
ccc6e69a29
commit
042895317d
Notes:
sideshowbarker
2024-07-19 13:44:54 +09:00
Author: https://github.com/deoxxa Commit: https://github.com/SerenityOS/serenity/commit/042895317df Pull-request: https://github.com/SerenityOS/serenity/pull/187
2 changed files with 7 additions and 1 deletions
|
@ -109,6 +109,7 @@ public:
|
|||
return m_impl->to_uppercase();
|
||||
}
|
||||
|
||||
Vector<String> split_limit(char separator, int limit) const;
|
||||
Vector<String> split(char separator) const;
|
||||
String substring(int start, int length) const;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue