Yes Bill,
Sorry about that. I do see the attachments in my outbox. Raul has already
fixed it, but I'll paste the other 2 files here anyway, for completeness.
=== rxrplc_test.ijs ===
NB.*trial m run one test
NB.-Run one test.
NB.-
NB.-syntax:
NB.+ failed=. trial re;sub;in;want
NB.-Determine if replacing `re` in `in` with `sub` produces `want`.
trial=: monad define"1
're sub in want'=. y
-. want -: (<sub) (re rxmatches in) rxmerge in
)
NB.*trials m run tests from file
NB.-Run tests from file.
NB.-
NB.-syntax:
NB.+ failures=. trials file
NB.-Determine line numbers in `file` for which the test fails.
NB.-`file` is the path to a file that holds groups of 4 lines
NB.-where the lines are, in order, `re`, `sub`, 'in`, and `want`.
NB.-
NB.-See also rxrplc_test.pl.
trials=: monad define
content=: fread y
if. ' ' ~: {. 0#content do. throw. end.
content=: <;._2 content
if. 0 ~: 4| #content do. throw. end.
content=: >(,.4 4)<;._3 content
failures=: 1 + 4 * I. trial content
)
=== rxrplc_test.ijs ===
=== rxrplc_test.pl ===
#!/usr/bin/perl
# This script is an aid in testing J's rxrplc function.
# The assumption is that, in j
# (<sub) (re rxmatches in) rxmerge in
# is the same as, in perl
# $in =~ s/$re/$sub/g;
# It is used to verify that the tests in a file are correct.
# Tests using pcre directly are a bit more difficult, but pcredemo.c can
# be used manually to verify that the same matches are found in the same
places.
use warnings;
use strict;
sub trials ($) {
my($file) = @_;
our $line = 1;
our $count = 0;
our $failures = 0;
undef $/; # read the whole file
if (open(my $fh, '<', $file)) {
print "Testing for '$file'...\n";
our $trials = <$fh>;
our($n,$re,$sub,$in,$want);
while (
($n = ((($re,$sub,$in,$want,$trials) = split("\n", $trials, 5))) + 0) == 5
) {
$in =~ s/$re/$sub/g;
if ($in ne $want) {
print " Unexpected result from test at line $line.\n";
$failures += 1;
}
$count += 1;
$line += 4;
}
if ($n != 0) {
my $lines = $line + $n - 2; # -2: $line is ahead by 1 and so is $n
die "Incomplete test description in '$file' ($lines lines)\n";
}
print " ...failures: ($failures/$count)\n";
} else {
die "Could not open file '$file' $!";
}
}
for (@ARGV) {
trials($_);
}
=== rxrplc_test.pl ===
On Thu, Apr 7, 2022 at 10:18 PM bill lam <[email protected]> wrote:
> Only the rxrplc_test.txt was attached.
>
> On Thu, 7 Apr 2022 at 10:20 AM Rik Renich <[email protected]> wrote:
>
> > There seems to be a bug in rxmatches. I expect ('|$' rxmatches 'is') to
> > have three matches, but the final one is omitted. Likewise for an empty
> > regex. For comparison with perl:
> >
> > cat | perl
> > $_= "is"; s/$/--/g; print "$_\n";
> > $_= "is"; s/|$/--/g; print "$_\n";
> > $_= "is"; s//--/g; print "$_\n";
> >
> > is--
> > --i--s--
> > --i--s--
> >
> > Note that perl matches the end of the string for all 3 cases.
> >
> > jc
> > s=: 'is'
> > (<'--') ('$' rxmatches s) rxmerge s
> > is--
> > (<'--') ('|$' rxmatches s) rxmerge s
> > --i--s
> > (<'--') ('' rxmatches s) rxmerge s
> > --i--s
> > exit''
> >
> > Note that I have used rxmerge to mimic the example given in perl.
> However,
> > the unexpected result comes from rxmatches.
> >
> > As these examples show, rxmatches is not compatible with perl for the
> > second 2 cases. Clearly the second case should match the end of the
> > string, as one clause of the regex is "end of string." The third case
> > seems to be the same bug.
> >
> > If you visit https://www.pcre.org/original/doc/html/pcreapi.html and
> > search
> > for "tricky" you will find:
> >
> > Finding all the matches in a subject is tricky when the pattern can match
> > an empty string. It is possible to emulate Perl's /g behaviour by first
> > trying the match again at the same offset, with the PCRE_NOTEMPTY_ATSTART
> > and PCRE_ANCHORED options, and then if that fails, advancing the starting
> > offset and trying an ordinary match again. There is some code that
> > demonstrates how to do this in the pcredemo sample program. In the most
> > general case, you have to check to see if the newline convention
> recognizes
> > CRLF as a newline, and if so, and the current character is CR followed by
> > LF, advance the starting offset by two characters instead of one.
> >
> > I have tried pcredemo and it provides results consistent with perl.
> >
> > I have provided test scripts in both perl and ijs, along with a minimal
> > test file.
> >
> > Thanks,
> > Rik
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm