mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:04:59 +00:00
Utilities/tr: Implement squeezing repeated characters
This commit is contained in:
parent
1f2ac0ab41
commit
7ce693840b
1 changed files with 10 additions and 3 deletions
|
@ -92,12 +92,14 @@ int main(int argc, char** argv)
|
|||
{
|
||||
bool complement_flag = false;
|
||||
bool delete_flag = false;
|
||||
bool squeeze_repeats = false;
|
||||
const char* from_chars = nullptr;
|
||||
const char* to_chars = nullptr;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(complement_flag, "Take the complement of the first set", "complement", 'c');
|
||||
args_parser.add_option(delete_flag, "Delete characters instead of replacing", "delete", 'd');
|
||||
args_parser.add_option(squeeze_repeats, "Omit repeated characters listed in the 'to' set from the output", "squeeze-repeats", 's');
|
||||
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);
|
||||
|
@ -127,6 +129,7 @@ int main(int argc, char** argv)
|
|||
}
|
||||
} else {
|
||||
auto to_str = build_set(to_chars);
|
||||
Optional<char> last_char;
|
||||
|
||||
for (;;) {
|
||||
char ch = fgetc(stdin);
|
||||
|
@ -134,9 +137,13 @@ int main(int argc, char** argv)
|
|||
break;
|
||||
auto match = from_str.find_last(ch);
|
||||
if (match.has_value())
|
||||
putchar(to_str[min(match.value(), to_str.length() - 1)]);
|
||||
else
|
||||
putchar(ch);
|
||||
ch = to_str[min(match.value(), to_str.length() - 1)];
|
||||
|
||||
if (squeeze_repeats && last_char.has_value() && last_char.value() == ch)
|
||||
continue;
|
||||
|
||||
last_char = ch;
|
||||
putchar(ch);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue