Compare commits

...

3 Commits

Author SHA1 Message Date
e378d8ee2f
Revert "libos: Make File::read_line optionally strip the ending newline"
All checks were successful
continuous-integration/drone/push Build is passing
This should be done by the caller, as making libos do it will not return a different value if the line was empty or was EOF.

Fortunately, we now have String::trim.
2023-04-22 15:21:04 +02:00
c075aa77b9
init: Allow empty lines in service files 2023-04-22 15:19:37 +02:00
dcc6bbf055
libluna: Add String::trim 2023-04-22 15:19:07 +02:00
5 changed files with 28 additions and 8 deletions

View File

@ -100,9 +100,12 @@ static Result<void> load_service(StringView path)
while (true) while (true)
{ {
auto line = TRY(file->read_line(false)); auto line = TRY(file->read_line());
if (line.is_empty()) break; if (line.is_empty()) break;
line.trim("\n");
if (line.is_empty()) continue;
auto parts = TRY(line.split_once('=')); auto parts = TRY(line.split_once('='));
if (parts.size() < 2 || parts[0].is_empty() || parts[1].is_empty()) if (parts.size() < 2 || parts[0].is_empty() || parts[1].is_empty())
{ {

View File

@ -32,6 +32,8 @@ class String
return view().split_once(delim); return view().split_once(delim);
} }
void trim(StringView delim);
static Result<String> format(const String& fmt, ...); static Result<String> format(const String& fmt, ...);
static Result<String> format(StringView fmt, ...); static Result<String> format(StringView fmt, ...);

View File

@ -80,6 +80,25 @@ Result<Vector<String>> String::split(StringView delim) const
return view().split(delim); return view().split(delim);
} }
void String::trim(StringView delim)
{
isize i = (isize)m_length;
while (i--)
{
char c = chars()[i];
if (!strchr(delim.chars(), c)) break;
}
i++;
if (m_inline) m_inline_storage[i] = '\0';
else
m_string[i] = '\0';
m_length = (usize)i;
}
const char& String::operator[](usize index) const const char& String::operator[](usize index) const
{ {
expect(index < m_length, "index out of range"); expect(index < m_length, "index out of range");

View File

@ -33,7 +33,7 @@ namespace os
Result<void> write(StringView str); Result<void> write(StringView str);
Result<void> write(const Buffer& buf); Result<void> write(const Buffer& buf);
Result<String> read_line(bool keep_newline = true); Result<String> read_line();
Result<void> read(Buffer& buf, usize size); Result<void> read(Buffer& buf, usize size);

View File

@ -108,7 +108,7 @@ namespace os
return {}; return {};
} }
Result<String> File::read_line(bool keep_newline) Result<String> File::read_line()
{ {
Vector<char> data; Vector<char> data;
@ -121,11 +121,7 @@ namespace os
TRY(data.try_append((char)current)); TRY(data.try_append((char)current));
if (current == '\n') if (current == '\n') break;
{
if (!keep_newline) data.try_pop();
break;
}
} }
if (!data.size()) return String {}; if (!data.size()) return String {};