Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tr: treat unused options -C and -U #373

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 12 additions & 30 deletions bin/tr
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ my(
);

sub usage {
warn "usage: $Program [-cdsUC] string1 string2\n";
warn "usage: $Program [-Ccds] string1 string2\n";
exit EX_FAILURE;
}

getopts('cdsUC', \%opt) or usage();
getopts('Ccds', \%opt) or usage();
my $narg = ($opt{'d'} && !$opt{'s'}) ? 1 : 2;
if (scalar(@ARGV) < $narg) {
warn "missing operand\n";
Expand All @@ -51,13 +51,14 @@ for (($string1, $string2) = splice(@ARGV, 0, 2)) {
s/^\[(.*?)\]$/$1/;
}

my $opts = join '', map { $opt{$_} ? $_ : () } ('c', 's', 'd');

# this is in an eval because tr/// needs to
# see its parts at compile time
my $cflag = ($opt{'C'} || $opt{'c'}) ? 'c' : '';
my $dflag = $opt{'d'} ? 'd' : '';
my $sflag = $opt{'s'} ? 's' : '';
eval qq{
while (<>) {
tr[$string1][$string2]$opts;
tr[$string1][$string2]$cflag$dflag$sflag;
print;
}
1;
Expand All @@ -75,7 +76,7 @@ tr - translate or delete characters

=head1 SYNOPSIS

tr [ -cdsUC ] [ I<SEARCHLIST> [ I<REPLACEMENTLIST> ] ]
tr [ -Ccds ] [ I<SEARCHLIST> [ I<REPLACEMENTLIST> ] ]

=head1 DESCRIPTION

Expand All @@ -89,10 +90,14 @@ Here are the options:

=over

=item -c
=item -C

Complement the SEARCHLIST.

=item -c

The same as -C.

=item -d

Delete found but unreplaced characters.
Expand All @@ -101,14 +106,6 @@ Delete found but unreplaced characters.

Squash duplicate replaced characters.

=item -U

Translate to/from UTF-8.

=item -C

Translate to/from 8-bit char (octet).

=back

In either string, the notation C<a-b> means a range of characters from
Expand All @@ -131,10 +128,6 @@ enough. If the REPLACEMENTLIST is empty, the SEARCHLIST is replicated.
This latter is useful for counting characters in a class or for squashing
character sequences in a class.

The first B<-U> or B<-C> flag applies to the left side of the translation.
The second one applies to the right side. If present, these flags
override the current utf8 state.

=head1 EXAMPLES

The following command creates a list of all the words in F<file1>
Expand All @@ -147,14 +140,6 @@ The following command strips the 8th bit from an input file:

tr "\200-\377" "\000-\177"

The following command translates Latin-1 to Unicode:

tr -CU "\0-\xFF" ""

The following command translates Unicode to Latin-1

tr -UC "\0-\x{FF}" ""

=head1 NOTE

This command is implemented using Perl's C<tr> operator.
Expand All @@ -167,9 +152,6 @@ There is no way to catch the file open error on ARGV handle
processing, so the exit status does not reflect file open
failures.

Not all systems have Unicode support yet, in which case the
B<-U> or B<-C> flags would cause a fatal error.

=head1 AUTHOR

Tom Christiansen, I<tchrist@perl.com>.
Expand Down