libos: Make File::read_line optionally strip the ending newline

This commit is contained in:
apio 2023-04-22 13:55:07 +02:00
parent e654ed6415
commit 63a2df112d
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 7 additions and 3 deletions

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