mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:27:43 +00:00
Add String::substring().
This commit is contained in:
parent
9cd0a34b5c
commit
0c1a4e8de3
3 changed files with 33 additions and 14 deletions
|
@ -22,28 +22,38 @@ String String::empty()
|
||||||
return StringImpl::theEmptyStringImpl();
|
return StringImpl::theEmptyStringImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String String::substring(size_t start, size_t length) const
|
||||||
|
{
|
||||||
|
ASSERT(m_impl);
|
||||||
|
ASSERT(start + length <= m_impl->length());
|
||||||
|
// FIXME: This needs some input bounds checking.
|
||||||
|
char* buffer;
|
||||||
|
auto newImpl = StringImpl::createUninitialized(length, buffer);
|
||||||
|
memcpy(buffer, characters() + start, length);
|
||||||
|
buffer[length] = '\0';
|
||||||
|
return newImpl;
|
||||||
|
}
|
||||||
|
|
||||||
Vector<String> String::split(const char separator) const
|
Vector<String> String::split(const char separator) const
|
||||||
{
|
{
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
return { };
|
return { };
|
||||||
|
|
||||||
Vector<String> parts;
|
Vector<String> v;
|
||||||
|
size_t substart = 0;
|
||||||
auto* characters = this->characters();
|
for (size_t i = 0; i < length(); ++i) {
|
||||||
unsigned startOfPart = 0;
|
char ch = characters()[i];
|
||||||
unsigned i = 0;
|
|
||||||
for (i = 0; i < length(); ++i) {
|
|
||||||
char ch = characters[i];
|
|
||||||
if (ch == separator) {
|
if (ch == separator) {
|
||||||
if (i != startOfPart) {
|
size_t sublen = i - substart;
|
||||||
parts.append(String(characters + startOfPart, i - startOfPart));
|
if (sublen != 0)
|
||||||
}
|
v.append(substring(substart, sublen));
|
||||||
startOfPart = i + 1;
|
substart = i + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (startOfPart != length())
|
size_t taillen = length() - substart;
|
||||||
parts.append(String(characters + startOfPart, i - startOfPart));
|
if (taillen != 0)
|
||||||
return parts;
|
v.append(substring(substart, taillen));
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer String::toByteBuffer() const
|
ByteBuffer String::toByteBuffer() const
|
||||||
|
|
|
@ -59,6 +59,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<String> split(char separator) const;
|
Vector<String> split(char separator) const;
|
||||||
|
String substring(size_t start, size_t length) const;
|
||||||
|
|
||||||
bool isEmpty() const { return length() == 0; }
|
bool isEmpty() const { return length() == 0; }
|
||||||
unsigned length() const { return m_impl ? m_impl->length() : 0; }
|
unsigned length() const { return m_impl ? m_impl->length() : 0; }
|
||||||
|
|
|
@ -14,6 +14,14 @@ static void testWeakPtr();
|
||||||
|
|
||||||
int main(int, char**)
|
int main(int, char**)
|
||||||
{
|
{
|
||||||
|
{
|
||||||
|
String path = "/////abc/def////g/h/i//";
|
||||||
|
auto parts = path.split('/');
|
||||||
|
for (auto& part : parts)
|
||||||
|
printf("<%s>\n", part.characters());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
String empty = "";
|
String empty = "";
|
||||||
|
|
||||||
char* buffer;
|
char* buffer;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue