mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 09:30:24 +00:00
Userland: Add support for -c/--complement flag to tr
This commit is contained in:
parent
7f2d3df906
commit
89ba022ede
Notes:
sideshowbarker
2024-07-18 07:05:58 +09:00
Author: https://github.com/boricj Commit: https://github.com/SerenityOS/serenity/commit/89ba022eded Pull-request: https://github.com/SerenityOS/serenity/pull/9344 Reviewed-by: https://github.com/alimpfard
1 changed files with 17 additions and 1 deletions
|
@ -4,17 +4,20 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
bool complement_flag = false;
|
||||
bool delete_flag = 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", nullptr, 'd');
|
||||
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);
|
||||
|
@ -25,7 +28,20 @@ int main(int argc, char** argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
auto from_str = AK::StringView(from_chars);
|
||||
String from_complement;
|
||||
StringView from_str;
|
||||
if (complement_flag) {
|
||||
auto original_set = StringView(from_chars);
|
||||
StringBuilder complement_set;
|
||||
for (int i = 0; i < 256; i++) {
|
||||
if (!original_set.contains(i))
|
||||
complement_set.append(static_cast<int>(i));
|
||||
}
|
||||
from_complement = complement_set.to_string();
|
||||
from_str = from_complement;
|
||||
} else {
|
||||
from_str = from_chars;
|
||||
}
|
||||
|
||||
if (delete_flag) {
|
||||
for (;;) {
|
||||
|
|
Loading…
Reference in a new issue