1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:47:37 +00:00

shuf: Support splitting on null bytes with -z

This commit is contained in:
Eli Youngs 2022-11-24 10:43:35 -08:00 committed by Andreas Kling
parent b2fd87950a
commit c92f450ff0

View file

@ -20,21 +20,24 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Core::ArgsParser args_parser;
StringView path;
bool is_zero_terminated = false;
args_parser.add_positional_argument(path, "File", "file", Core::ArgsParser::Required::No);
args_parser.add_option(is_zero_terminated, "Split input on \\0, not newline", "zero-terminated", 'z');
args_parser.parse(arguments);
auto file = TRY(Core::Stream::File::open_file_or_standard_stream(path, Core::Stream::OpenMode::Read));
ByteBuffer buffer = TRY(file->read_all());
u8 input_delimiter = is_zero_terminated ? '\0' : '\n';
Vector<Bytes> lines;
auto bytes = buffer.span();
size_t line_start = 0;
size_t line_length = 0;
for (size_t i = 0; i < bytes.size(); ++i) {
if (bytes[i] == '\n') {
if (bytes[i] == input_delimiter) {
lines.append(bytes.slice(line_start, line_length));
line_start = i + 1;
line_length = 0;