Skip to content

Commit f034806

Browse files
committed
Added debugging code to catch flop error
1 parent 81c81ef commit f034806

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

lib/Data/Random/String/Matches.pm

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,11 @@ sub _random_from_class {
10321032

10331033
my @chars;
10341034

1035+
# Debugging this regex: qr/!#-'*+\\-\\.\\^_`|~0-9A-Za-z/
1036+
# which gives this error: 'Argument "#" isn't numeric in range (or flop)'
1037+
# DEBUG
1038+
warn "DEBUG: class = '$class', length = ", length($class);
1039+
10351040
# Handle negation
10361041
my $negate = 0;
10371042
if (substr($class, 0, 1) eq '^') {
@@ -1044,9 +1049,13 @@ sub _random_from_class {
10441049
while ($i < length($class)) {
10451050
my $char = substr($class, $i, 1);
10461051

1052+
# DEBUG
1053+
warn "DEBUG: i=$i, char='$char' (ord=", ord($char), ')';
1054+
10471055
if ($char eq '\\') {
10481056
$i++;
10491057
my $next = substr($class, $i, 1);
1058+
warn "DEBUG: Escaped char: $next";
10501059
if ($next eq 'd') {
10511060
push @chars, ('0'..'9');
10521061
} elsif ($next eq 'w') {
@@ -1060,19 +1069,36 @@ sub _random_from_class {
10601069
push @chars, $self->_unicode_property_chars($prop);
10611070
$i = $end;
10621071
} else {
1072+
# Escaped literal character (including \-, \., \^, etc.)
10631073
push @chars, $next;
10641074
}
1065-
} elsif ($i + 2 < length($class) && substr($class, $i+1, 1) eq '-') {
1066-
# Range
1067-
my $end = substr($class, $i+2, 1);
1068-
push @chars, ($char .. $end);
1069-
$i += 2; # Will be incremented again by loop, total +3
1075+
} elsif ($i + 2 < length($class) && substr($class, $i+1, 1) eq '-' && substr($class, $i+2, 1) ne ']') {
1076+
# Range (but not if - is at end or before ])
1077+
my $next_char = substr($class, $i+1, 1); # Potential range
1078+
my $end_char = substr($class, $i+2, 1);
1079+
1080+
warn "DEBUG: Potential range: '$char' - '$end_char'";
1081+
1082+
# Skip if end_char is a backslash (escaped char follows)
1083+
if ($end_char eq '\\') {
1084+
push @chars, $char;
1085+
} elsif (ord($end_char) >= ord($char)) {
1086+
# Valid range
1087+
warn "DEBUG: Valid range from ", ord($char), ' to ', ord($end_char);
1088+
push @chars, ($char .. $end_char);
1089+
$i += 2; # Will be incremented again by loop, total +3
1090+
} else {
1091+
# Invalid range, treat as literals
1092+
push @chars, $char;
1093+
}
10701094
} else {
10711095
push @chars, $char;
10721096
}
10731097
$i++;
10741098
}
10751099

1100+
warn 'DEBUG: Final chars array has ', scalar(@chars), ' elements';
1101+
10761102
if ($negate) {
10771103
my %excluded = map { $_ => 1 } @chars;
10781104
@chars = grep { !$excluded{$_} } map { chr($_) } (33 .. 126);

0 commit comments

Comments
 (0)