Skip to content

Commit 81a76e1

Browse files
committed
0.0.32
1 parent 3097cf4 commit 81a76e1

File tree

7 files changed

+81
-7
lines changed

7 files changed

+81
-7
lines changed

Changes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ Revision history for String-Utils
22

33
{{$NEXT}}
44

5+
0.0.32 2024-12-16T17:13:30+01:00
6+
- Add support for "abbrev", inspired by Text::Abbrev by
7+
Kamila Borowska
8+
59
0.0.31 2024-12-03T22:36:38+01:00
610
- Make "word-at" also return the ordinal number of the word in
711
string, and return Empty if not found (instead of Nil)

META6.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@
3030
],
3131
"test-depends": [
3232
],
33-
"version": "0.0.31"
33+
"version": "0.0.32"
3434
}

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ dd expand-tab("a\tbb\tccc",4); # "a bb ccc"
7777

7878
say word-at("foo bar baz", 5); # (4 3)
7979

80+
say abbrev(<yes no>).keys.sort; # (n no y ye yes)
81+
8082
use String::Utils <before after>; # only import "before" and "after"
8183
```
8284

@@ -448,6 +450,19 @@ Returns a `List` with the start position, the number of characters of the word,
448450

449451
Returns `Empty` if no word could be found at the given position, or the position was out of range.
450452

453+
abbrev
454+
------
455+
456+
```raku
457+
say abbrev(<yes no>).keys.sort; # (n no y ye yes)
458+
459+
say abbrev(<foo bar baz>).keys.sort; # (bar baz f fo foo)
460+
```
461+
462+
Takes 0 or more strings as arguments, and returns a `Map` with all shortest possible unambigious versions of the given strings as keys, and their associated original string as the value.
463+
464+
Inspired by Text::Abbrev module by KamilaBorowska.
465+
451466
AUTHOR
452467
======
453468

doc/String-Utils.rakudoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ dd expand-tab("a\tbb\tccc",4); # "a bb ccc"
7676

7777
say word-at("foo bar baz", 5); # (4 3)
7878

79+
say abbrev(<yes no>).keys.sort; # (n no y ye yes)
80+
7981
use String::Utils <before after>; # only import "before" and "after"
8082

8183
=end code
@@ -538,6 +540,22 @@ at the given position, or directly before it (using C<.words> semantics).
538540
Returns C<Empty> if no word could be found at the given position, or the
539541
position was out of range.
540542

543+
=head2 abbrev
544+
545+
=begin code :lang<raku>
546+
547+
say abbrev(<yes no>).keys.sort; # (n no y ye yes)
548+
549+
say abbrev(<foo bar baz>).keys.sort; # (bar baz f fo foo)
550+
551+
=end code
552+
553+
Takes 0 or more strings as arguments, and returns a C<Map> with all
554+
shortest possible unambigious versions of the given strings as keys,
555+
and their associated original string as the value.
556+
557+
Inspired by Text::Abbrev module by KamilaBorowska.
558+
541559
=head1 AUTHOR
542560

543561
Elizabeth Mattijsen <[email protected]>

lib/String/Utils.rakumod

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,38 @@ my sub word-at(str $string, int $cursor) {
570570
}
571571
}
572572

573+
my proto sub abbrev(|) {*}
574+
my multi sub abbrev() { BEGIN Map.new }
575+
my multi sub abbrev(*@words) { abbrev(@words) }
576+
my multi sub abbrev(@words) {
577+
my $result := Map.new;
578+
my $seen := nqp::getattr($result,Map,'$!storage');
579+
for @words -> str $word {
580+
nqp::bindkey($seen,$word,$word);
581+
582+
my int $chars = nqp::chars($word);
583+
nqp::while(
584+
--$chars > 0,
585+
nqp::stmts(
586+
(my str $needle = nqp::substr($word,0,$chars)),
587+
nqp::if(
588+
nqp::existskey($seen,$needle),
589+
nqp::stmts(
590+
nqp::deletekey($seen,$needle),
591+
nqp::while(
592+
--$chars,
593+
nqp::deletekey($seen,nqp::substr($word,0,$chars))
594+
)
595+
),
596+
nqp::bindkey($seen,$needle,$word)
597+
)
598+
)
599+
);
600+
}
601+
602+
$result
603+
}
604+
573605
my sub EXPORT(*@names) {
574606
Map.new: @names
575607
?? @names.map: {

t/01-basic.rakutest

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use Test;
22
use String::Utils;
33

4-
plan 109;
4+
plan 111;
55

66
is after("foobar","foo"), "bar", 'after(foo) ok?';
77
is "foobar".&after("foo"), "bar", '.&after(foo) ok?';
@@ -135,6 +135,11 @@ is-deeply word-at($string, $_), ( 1,3,0), "'foo' at $_" for 1 .. 4;
135135
is-deeply word-at($string, $_), ( 6,3,1), "'bar' at $_" for 6 .. 9;
136136
is-deeply word-at($string, $_), (10,3,2), "'baz' at $_" for 10 .. 13;
137137

138-
is paragraphs($*PROGRAM.lines).elems, 32, 'reading from file lazily';
138+
is-deeply abbrev(<yes no>), <n no no no y yes ye yes yes yes>.Map,
139+
"did yes/no abbreviate correctly";
140+
is-deeply abbrev(<foo bar baz>), <bar bar baz baz f foo fo foo foo foo>.Map,
141+
"did foo/bar/baz abbreviate correctly";
142+
143+
is paragraphs($*PROGRAM.lines).elems, 33, 'reading from file lazily';
139144

140145
# vim: expandtab shiftwidth=4

t/02-selective-importing.rakutest

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use Test;
22

33
my constant @subs = <
4-
after all-same around before between between-included chomp-needle
5-
consists-of expand-tab has-marks is-sha1 is-lowercase is-uppercase
6-
is-whitespace leading-whitespace leaf letters ngram non-word
7-
regexify root stem trailing-whitespace word-at
4+
abbrev after all-same around before between between-included
5+
chomp-needle consists-of expand-tab has-marks is-sha1 is-lowercase
6+
is-uppercase is-whitespace leading-whitespace leaf letters ngram
7+
non-word regexify root stem trailing-whitespace word-at
88
>;
99

1010
plan @subs + 2;

0 commit comments

Comments
 (0)