shuf.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
  3. * Copyright (c) 2022, Eli Youngs <eli.m.youngs@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Random.h>
  8. #include <AK/String.h>
  9. #include <AK/Vector.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibCore/Stream.h>
  12. #include <LibCore/System.h>
  13. #include <LibMain/Main.h>
  14. #include <stdlib.h>
  15. ErrorOr<int> serenity_main(Main::Arguments arguments)
  16. {
  17. TRY(Core::System::pledge("stdio rpath"));
  18. Core::ArgsParser args_parser;
  19. StringView path;
  20. Optional<size_t> head_count;
  21. bool is_zero_terminated = false;
  22. args_parser.add_positional_argument(path, "File", "file", Core::ArgsParser::Required::No);
  23. args_parser.add_option(head_count, "Output at most \"count\" lines", "head-count", 'n', "count");
  24. args_parser.add_option(is_zero_terminated, "Split input on \\0, not newline", "zero-terminated", 'z');
  25. args_parser.parse(arguments);
  26. auto file = TRY(Core::Stream::File::open_file_or_standard_stream(path, Core::Stream::OpenMode::Read));
  27. ByteBuffer buffer = TRY(file->read_all());
  28. u8 input_delimiter = is_zero_terminated ? '\0' : '\n';
  29. Vector<Bytes> lines;
  30. auto bytes = buffer.span();
  31. size_t line_start = 0;
  32. size_t line_length = 0;
  33. for (size_t i = 0; i < bytes.size(); ++i) {
  34. if (bytes[i] == input_delimiter) {
  35. lines.append(bytes.slice(line_start, line_length));
  36. line_start = i + 1;
  37. line_length = 0;
  38. } else {
  39. ++line_length;
  40. }
  41. }
  42. if (line_length > 0) {
  43. lines.append(bytes.slice(line_start));
  44. }
  45. if (lines.is_empty())
  46. return 0;
  47. // Fisher-Yates shuffle
  48. Bytes tmp;
  49. for (size_t i = lines.size() - 1; i >= 1; --i) {
  50. size_t j = get_random_uniform(i + 1);
  51. // Swap i and j
  52. if (i == j)
  53. continue;
  54. tmp = lines[j];
  55. lines[j] = lines[i];
  56. lines[i] = tmp;
  57. }
  58. Array<u8, 1> output_delimiter = { '\n' };
  59. for (size_t i = 0; i < min(head_count.value_or(lines.size()), lines.size()); ++i) {
  60. TRY(Core::System::write(STDOUT_FILENO, lines.at(i)));
  61. TRY(Core::System::write(STDOUT_FILENO, output_delimiter));
  62. }
  63. return 0;
  64. }