Skip to content

Commit 3097cf4

Browse files
committed
0.0.31
1 parent be4b0a0 commit 3097cf4

File tree

6 files changed

+47
-38
lines changed

6 files changed

+47
-38
lines changed

Changes

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ Revision history for String-Utils
22

33
{{$NEXT}}
44

5+
0.0.31 2024-12-03T22:36:38+01:00
6+
- Make "word-at" also return the ordinal number of the word in
7+
string, and return Empty if not found (instead of Nil)
8+
59
0.0.30 2024-12-03T20:42:23+01:00
610
- Add support for "word-at"
711

META6.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@
3030
],
3131
"test-depends": [
3232
],
33-
"version": "0.0.30"
33+
"version": "0.0.31"
3434
}

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,12 @@ word-at
441441
-------
442442

443443
```raku
444-
say word-at("foo bar baz", 5); # (4 3)
444+
say word-at("foo bar baz", 5); # (4 3 1)
445445
```
446446

447-
Returns a `List` with the start position and its length of the word found in the given string at the given position, or directly before it. Return `Nil` if no word could be found at the given position, or the position was out of range.
447+
Returns a `List` with the start position, the number of characters of the word, and the ordinal number of the word found in the given string at the given position, or directly before it (using `.words` semantics).
448+
449+
Returns `Empty` if no word could be found at the given position, or the position was out of range.
448450

449451
AUTHOR
450452
======

doc/String-Utils.rakudoc

+6-4
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,15 @@ be replaced by spaces.
527527

528528
=begin code :lang<raku>
529529

530-
say word-at("foo bar baz", 5); # (4 3)
530+
say word-at("foo bar baz", 5); # (4 3 1)
531531

532532
=end code
533533

534-
Returns a C<List> with the start position and its length of the word
535-
found in the given string at the given position, or directly before it.
536-
Return C<Nil> if no word could be found at the given position, or the
534+
Returns a C<List> with the start position, the number of characters of
535+
the word, and the ordinal number of the word found in the given string
536+
at the given position, or directly before it (using C<.words> semantics).
537+
538+
Returns C<Empty> if no word could be found at the given position, or the
537539
position was out of range.
538540

539541
=head1 AUTHOR

lib/String/Utils.rakumod

+19-24
Original file line numberDiff line numberDiff line change
@@ -544,34 +544,29 @@ my sub word-at(str $string, int $cursor) {
544544

545545
# something to look at
546546
if $cursor >= 0 && nqp::chars($string) -> int $length {
547-
if $cursor {
548-
my int $last;
549-
my int $pos;
550-
nqp::while(
551-
$last < $length && ($pos = nqp::findcclass(
552-
nqp::const::CCLASS_WHITESPACE,
553-
$string,
554-
$last,
555-
$length - $last
556-
)) < $cursor,
557-
($last = $pos + 1)
558-
);
559-
$last >= $length
560-
?? Nil
561-
!! ($last, $pos - $last)
562-
}
563-
564-
# cursor at start, so the first word will do
565-
else {
566-
(0, nqp::findcclass(
567-
nqp::const::CCLASS_WHITESPACE,$string,0,$length
568-
));
569-
}
547+
my int $last;
548+
my int $pos;
549+
my int $index;
550+
nqp::while(
551+
$last < $length && ($pos = nqp::findcclass(
552+
nqp::const::CCLASS_WHITESPACE,
553+
$string,
554+
$last,
555+
$length - $last
556+
)) < $cursor,
557+
nqp::stmts(
558+
nqp::if($pos > $last, ++$index),
559+
($last = $pos + 1)
560+
)
561+
);
562+
$last >= $length || $pos == $last
563+
?? Empty
564+
!! ($last, $pos - $last, $index)
570565
}
571566

572567
# nothing to look at
573568
else {
574-
Nil
569+
Empty
575570
}
576571
}
577572

t/01-basic.rakutest

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use Test;
22
use String::Utils;
33

4-
plan 93;
4+
plan 109;
55

66
is after("foobar","foo"), "bar", 'after(foo) ok?';
77
is "foobar".&after("foo"), "bar", '.&after(foo) ok?';
@@ -117,18 +117,24 @@ my $text = "\na\n\nb\n\n\nc";
117117
is-deeply paragraphs($text).map(*.value).join("\n"), $text,
118118
'can we round-trip with .join("\n")?';
119119

120-
is paragraphs($*PROGRAM.lines).elems, 31, 'reading from file lazily';
121-
122120
my str $base = "a\tbb\tccc\tdddd\teeeee";
123121
is-deeply expand-tab($base,0), "abbcccddddeeeee", '0 removes tabs';
124122
is-deeply expand-tab($base,1), "a bb ccc dddd eeeee", '1 replaces tab w/space';
125123
is-deeply expand-tab($base,4), "a bb ccc dddd eeeee", '4 is ok';
126124
is-deeply expand-tab("foo",4), "foo", 'no tabs ok';
127125

128126
my str $string = "foo bar baz";
129-
is-deeply word-at($string, $_), Nil, "no word at $_" for -1, 12;
130-
is-deeply word-at($string, $_), (0,3), "'foo' at $_" for 0 .. 3;
131-
is-deeply word-at($string, $_), (4,3), "'bar' at $_" for 4 .. 7;
132-
is-deeply word-at($string, $_), (8,3), "'baz' at $_" for 8 .. 11;
127+
is-deeply word-at($string, $_), Empty, "no word at $_" for -1, 12;
128+
is-deeply word-at($string, $_), (0,3,0), "'foo' at $_" for 0 .. 3;
129+
is-deeply word-at($string, $_), (4,3,1), "'bar' at $_" for 4 .. 7;
130+
is-deeply word-at($string, $_), (8,3,2), "'baz' at $_" for 8 .. 11;
131+
132+
$string = " foo bar baz";
133+
is-deeply word-at($string, $_), Empty, "no word at $_" for -1, 0, 5, 14;
134+
is-deeply word-at($string, $_), ( 1,3,0), "'foo' at $_" for 1 .. 4;
135+
is-deeply word-at($string, $_), ( 6,3,1), "'bar' at $_" for 6 .. 9;
136+
is-deeply word-at($string, $_), (10,3,2), "'baz' at $_" for 10 .. 13;
137+
138+
is paragraphs($*PROGRAM.lines).elems, 32, 'reading from file lazily';
133139

134140
# vim: expandtab shiftwidth=4

0 commit comments

Comments
 (0)