mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 05:47:35 +00:00
Kernel: Add KLexicalPath::try_join and use it
This adds KLexicalPath::try_join(). As this cannot be done without allocation, it uses KString and can fail. This patch also uses it at one place. All the other cases of String::formatted("{}/{}", ...) currently rely on the return value being a String, which means they cannot easily be converted to use the new API.
This commit is contained in:
parent
ee342f5ec3
commit
1f792faf34
3 changed files with 35 additions and 1 deletions
|
@ -55,4 +55,32 @@ Vector<StringView> parts(StringView const& path)
|
|||
return path.split_view('/');
|
||||
}
|
||||
|
||||
OwnPtr<KString> try_join(StringView const& first, StringView const& second)
|
||||
{
|
||||
VERIFY(is_canonical(first));
|
||||
VERIFY(is_canonical(second));
|
||||
VERIFY(!is_absolute(second));
|
||||
|
||||
if (first == "/"sv) {
|
||||
char* buffer;
|
||||
auto string = KString::try_create_uninitialized(1 + second.length(), buffer);
|
||||
if (!string)
|
||||
return {};
|
||||
buffer[0] = '/';
|
||||
__builtin_memcpy(buffer + 1, second.characters_without_null_termination(), second.length());
|
||||
buffer[string->length()] = 0;
|
||||
return string;
|
||||
} else {
|
||||
char* buffer;
|
||||
auto string = KString::try_create_uninitialized(first.length() + 1 + second.length(), buffer);
|
||||
if (!string)
|
||||
return string;
|
||||
__builtin_memcpy(buffer, first.characters_without_null_termination(), first.length());
|
||||
buffer[first.length()] = '/';
|
||||
__builtin_memcpy(buffer + first.length() + 1, second.characters_without_null_termination(), second.length());
|
||||
buffer[string->length()] = 0;
|
||||
return string;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue