diff options
author | Jean-Baptiste Boric <jblbeurope@gmail.com> | 2021-08-11 18:26:24 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-12 00:41:13 +0200 |
commit | 7f2d3df906dac2150361c0ac2b801b3ce8ceeca6 (patch) | |
tree | 1e976fb3292559e065363bc71f743a7a85d0b2f7 /Userland/Utilities | |
parent | 7a9d05c24c69403ddb8a7d365ea61131b6200e02 (diff) | |
download | serenity-7f2d3df906dac2150361c0ac2b801b3ce8ceeca6.zip |
Userland: Add support for multiple character translations to tr
Diffstat (limited to 'Userland/Utilities')
-rw-r--r-- | Userland/Utilities/tr.cpp | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/Userland/Utilities/tr.cpp b/Userland/Utilities/tr.cpp index 4d82a26885..b05965af9c 100644 --- a/Userland/Utilities/tr.cpp +++ b/Userland/Utilities/tr.cpp @@ -16,8 +16,8 @@ int main(int argc, char** argv) Core::ArgsParser args_parser; args_parser.add_option(delete_flag, "Delete characters instead of replacing", nullptr, 'd'); - args_parser.add_positional_argument(from_chars, "Character to translate from", "from"); - args_parser.add_positional_argument(to_chars, "Character to translate to", "to", Core::ArgsParser::Required::No); + args_parser.add_positional_argument(from_chars, "Set of characters to translate from", "from"); + args_parser.add_positional_argument(to_chars, "Set of characters to translate to", "to", Core::ArgsParser::Required::No); args_parser.parse(argc, argv); if (!to_chars && !delete_flag) { @@ -25,9 +25,9 @@ int main(int argc, char** argv) return 1; } - if (delete_flag) { - auto from_str = AK::StringView(from_chars); + auto from_str = AK::StringView(from_chars); + if (delete_flag) { for (;;) { char ch = fgetc(stdin); if (feof(stdin)) @@ -36,16 +36,15 @@ int main(int argc, char** argv) putchar(ch); } } else { - // TODO: Support multiple characters to translate from and to - auto from = from_chars[0]; - auto to = to_chars[0]; + auto to_str = AK::StringView(to_chars); for (;;) { char ch = fgetc(stdin); if (feof(stdin)) break; - if (ch == from) - putchar(to); + auto match = from_str.find_last(ch); + if (match.has_value()) + putchar(to_str[min(match.value(), to_str.length() - 1)]); else putchar(ch); } |