summaryrefslogtreecommitdiff
path: root/Userland/Utilities
diff options
context:
space:
mode:
authorEli Youngs <eli.m.youngs@gmail.com>2022-11-24 10:43:35 -0800
committerAndreas Kling <kling@serenityos.org>2022-11-25 22:59:27 +0100
commitc92f450ff099c54a8598da21041999c459423807 (patch)
tree0ab311221babe825d190b8837a450b168918af0c /Userland/Utilities
parentb2fd87950a6265ad0ca30e37be3bb24e7aa0d5d1 (diff)
downloadserenity-c92f450ff099c54a8598da21041999c459423807.zip
shuf: Support splitting on null bytes with -z
Diffstat (limited to 'Userland/Utilities')
-rw-r--r--Userland/Utilities/shuf.cpp5
1 files changed, 4 insertions, 1 deletions
diff --git a/Userland/Utilities/shuf.cpp b/Userland/Utilities/shuf.cpp
index 297b2c5350..c682ca1c92 100644
--- a/Userland/Utilities/shuf.cpp
+++ b/Userland/Utilities/shuf.cpp
@@ -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;