mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:07:36 +00:00
sort: Add the -z
option to split lines on \0
rather than a newline
This commit is contained in:
parent
b7676cc436
commit
e6112446db
2 changed files with 10 additions and 7 deletions
|
@ -19,6 +19,7 @@ Sort each lines of INPUT (or standard input). A quick sort algorithm is used.
|
||||||
* `-n`, `--numeric`: Treat the key field as a number
|
* `-n`, `--numeric`: Treat the key field as a number
|
||||||
* `-t char`, `--sep char`: The separator to split fields by
|
* `-t char`, `--sep char`: The separator to split fields by
|
||||||
* `-r`, `--reverse`: Sort in reverse order
|
* `-r`, `--reverse`: Sort in reverse order
|
||||||
|
* `-z`, `--zero-terminated`: Use `\0` as the line delimiter instead of a newline
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
|
|
@ -56,11 +56,12 @@ struct Options {
|
||||||
bool unique { false };
|
bool unique { false };
|
||||||
bool numeric { false };
|
bool numeric { false };
|
||||||
bool reverse { false };
|
bool reverse { false };
|
||||||
|
bool zero_terminated { false };
|
||||||
StringView separator { "\0", 1 };
|
StringView separator { "\0", 1 };
|
||||||
Vector<DeprecatedString> files;
|
Vector<DeprecatedString> files;
|
||||||
};
|
};
|
||||||
|
|
||||||
static ErrorOr<void> load_file(Options options, StringView filename, Vector<Line>& lines, HashTable<Line>& seen)
|
static ErrorOr<void> load_file(Options const& options, StringView filename, StringView line_delimiter, Vector<Line>& lines, HashTable<Line>& seen)
|
||||||
{
|
{
|
||||||
auto file = TRY(Core::InputBufferedFile::create(
|
auto file = TRY(Core::InputBufferedFile::create(
|
||||||
TRY(Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read))));
|
TRY(Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read))));
|
||||||
|
@ -68,8 +69,7 @@ static ErrorOr<void> load_file(Options options, StringView filename, Vector<Line
|
||||||
// FIXME: Unlimited line length
|
// FIXME: Unlimited line length
|
||||||
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
|
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
|
||||||
while (TRY(file->can_read_line())) {
|
while (TRY(file->can_read_line())) {
|
||||||
DeprecatedString line = TRY(file->read_line(buffer));
|
DeprecatedString line { TRY(file->read_until(buffer, line_delimiter)) };
|
||||||
|
|
||||||
StringView key = line;
|
StringView key = line;
|
||||||
if (options.key_field != 0) {
|
if (options.key_field != 0) {
|
||||||
auto split = (options.separator[0])
|
auto split = (options.separator[0])
|
||||||
|
@ -106,25 +106,27 @@ ErrorOr<int> serenity_main([[maybe_unused]] Main::Arguments arguments)
|
||||||
args_parser.add_option(options.numeric, "treat the key field as a number", "numeric", 'n');
|
args_parser.add_option(options.numeric, "treat the key field as a number", "numeric", 'n');
|
||||||
args_parser.add_option(options.separator, "The separator to split fields by", "sep", 't', "char");
|
args_parser.add_option(options.separator, "The separator to split fields by", "sep", 't', "char");
|
||||||
args_parser.add_option(options.reverse, "Sort in reverse order", "reverse", 'r');
|
args_parser.add_option(options.reverse, "Sort in reverse order", "reverse", 'r');
|
||||||
|
args_parser.add_option(options.zero_terminated, "Use '\\0' as the line delimiter instead of a newline", "zero-terminated", 'z');
|
||||||
args_parser.add_positional_argument(options.files, "Files to sort", "file", Core::ArgsParser::Required::No);
|
args_parser.add_positional_argument(options.files, "Files to sort", "file", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
|
auto line_delimiter = options.zero_terminated ? "\0"sv : "\n"sv;
|
||||||
Vector<Line> lines;
|
Vector<Line> lines;
|
||||||
HashTable<Line> seen;
|
HashTable<Line> seen;
|
||||||
|
|
||||||
if (options.files.size() == 0) {
|
if (options.files.size() == 0) {
|
||||||
TRY(load_file(options, "-"sv, lines, seen));
|
TRY(load_file(options, "-"sv, line_delimiter, lines, seen));
|
||||||
} else {
|
} else {
|
||||||
for (auto& file : options.files) {
|
for (auto& file : options.files) {
|
||||||
TRY(load_file(options, file, lines, seen));
|
TRY(load_file(options, file, line_delimiter, lines, seen));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
quick_sort(lines);
|
quick_sort(lines);
|
||||||
|
|
||||||
auto print_lines = [](auto const& lines) {
|
auto print_lines = [line_delimiter](auto const& lines) {
|
||||||
for (auto& line : lines)
|
for (auto& line : lines)
|
||||||
outln("{}", line.line);
|
out("{}{}", line.line, line_delimiter);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (options.reverse)
|
if (options.reverse)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue