AK: Add implicit String -> StringView conversion

And tidy up existing view() users.
This commit is contained in:
Robin Burchell 2019-06-02 12:19:21 +02:00 committed by Andreas Kling
parent decf1afbaa
commit b55b6cd7fc
6 changed files with 13 additions and 6 deletions

View file

@ -111,7 +111,7 @@ Vector<StringView> String::split_view(const char separator) const
if (taillen != 0)
v.append(substring_view(substart, taillen));
if (characters()[length() - 1] == separator)
v.append(empty().view());
v.append(empty());
return v;
}

View file

@ -3,6 +3,12 @@
namespace AK {
StringView::StringView(const AK::String& string)
: m_characters(string.characters())
, m_length(string.length())
{
}
Vector<StringView> StringView::split_view(const char separator) const
{
if (is_empty())
@ -23,7 +29,7 @@ Vector<StringView> StringView::split_view(const char separator) const
if (taillen != 0)
v.append(substring_view(substart, taillen));
if (characters()[length() - 1] == separator)
v.append(String::empty().view());
v.append(String::empty());
return v;
}

View file

@ -27,6 +27,7 @@ public:
++m_length;
}
}
StringView(const AK::String& string);
bool is_empty() const { return m_length == 0; }
const char* characters() const { return m_characters; }

View file

@ -312,7 +312,7 @@ int Process::do_exec(String path, Vector<String> arguments, Vector<String> envir
if (parts.is_empty())
return -ENOENT;
auto result = VFS::the().open(path.view(), 0, 0, current_directory());
auto result = VFS::the().open(path, 0, 0, current_directory());
if (result.is_error())
return result.error();
auto descriptor = result.value();

View file

@ -101,7 +101,7 @@ int main(int argc, char**argv)
auto hostname = String(client_buffer, nrecv, Chomp);
dbgprintf("LookupServer: Got request for '%s' (using IP %s)\n",
hostname.characters(),
DNS_IP.view().characters());
DNS_IP.characters());
Vector<IPv4Address> addresses;
@ -194,7 +194,7 @@ Vector<IPv4Address> lookup(const String& hostname, bool& did_timeout, const Stri
dst_addr.sin_family = AF_INET;
dst_addr.sin_port = htons(53);
rc = inet_pton(AF_INET, DNS_IP.view().characters(), &dst_addr.sin_addr);
rc = inet_pton(AF_INET, DNS_IP.characters(), &dst_addr.sin_addr);
int nsent = sendto(fd, buffer.pointer(), buffer.size(), 0,(const struct sockaddr *)&dst_addr, sizeof(dst_addr));
if (nsent < 0) {

View file

@ -41,7 +41,7 @@ static void write_var(const String& name, const String& value)
fprintf(stderr, "open: %s", f.error_string());
exit(1);
}
f.write(value.view());
f.write(value);
if (f.error() < 0) {
fprintf(stderr, "write: %s", f.error_string());
exit(1);