Skip to content

Commit

Permalink
Implement index(string, @needles)
Browse files Browse the repository at this point in the history
Will use each element's Str to search the string, and return the
lowest index found or Nil.
  • Loading branch information
lizmat committed Feb 23, 2020
1 parent b4cdccc commit 4cec44a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/core.c/Cool.pm6
Expand Up @@ -281,14 +281,21 @@ my class Cool { # declared in BOOTSTRAP
}
multi method index(Cool:D:
Cool:D $needle, :i(:$ignorecase)!, :m(:$ignoremark) --> Int:D) {
self.Str.index($needle.Str, :$ignorecase, :$ignoremark)
self.Str.index(
nqp::istype($needle,List) ?? $needle !! $needle.Str,
:$ignorecase,
:$ignoremark
)
}
multi method index(Cool:D:
Cool:D $needle, :m(:$ignoremark)! --> Int:D) {
self.Str.index($needle.Str, :$ignoremark)
self.Str.index(
nqp::istype($needle,List) ?? $needle !! $needle.Str,
:$ignoremark
)
}
multi method index(Cool:D: Cool:D $needle --> Int:D) {
self.Str.index($needle.Str)
self.Str.index(nqp::istype($needle,List) ?? $needle !! $needle.Str)
}

multi method index(Cool:D:
Expand Down
89 changes: 89 additions & 0 deletions src/core.c/Str.pm6
Expand Up @@ -636,6 +636,62 @@ my class Str does Stringy { # declared in BOOTSTRAP
) ?? $index !! Nil
}

multi method index(Str:D:
@needles, :i(:$ignorecase)!, :m(:$ignoremark)
--> Int:D) {
my int $i;
my int $index = -1;
my int $chars = nqp::chars(self);
if $ignorecase {
if $ignoremark {
#?if moar
$chars = $index = $i
if ($i =
nqp::indexicim(nqp::substr(self,0,$chars),.Str,0)
) > -1
for @needles;
#?endif
#?if !moar
self!die-named('ignorecase and :ignoremark')
#?endif
}
else {
#?if moar
$chars = $index = $i
if ($i =
nqp::indexic(nqp::substr(self,0,$chars),.Str,0)
) > -1
for @needles;
#?endif
#?if !moar
my str $str = nqp::fc(self);
$chars = $index = $i
if ($i =
nqp::index(nqp::substr(self,0,$chars),nqp::fc(.Str))
) > -1
for @needles;
#?endif
}
}
elsif $ignoremark {
#?if moar
$chars = $index = $i
if ($i = nqp::indexim(nqp::substr(self,0,$chars),.Str,0)) > -1
for @needles;
#?endif
#?if !moar
self!die-named('ignoremark')
#?endif
}
else {
$chars = $index = $i
if ($i = nqp::index(nqp::substr(self,0,$chars),.Str,0)) > -1
for @needles;
}

$index == -1 ?? Nil !! $index
}

multi method index(Str:D:
Str:D $needle, :m(:$ignoremark)!
--> Int:D) {
Expand Down Expand Up @@ -668,6 +724,30 @@ my class Str does Stringy { # declared in BOOTSTRAP
),-1
) ?? $index !! Nil
}
multi method index(Str:D:
@needles, :m(:$ignoremark)!
--> Int:D) {
my int $i;
my int $index = -1;
my int $chars = nqp::chars(self);
if $ignoremark {
#?if moar
$chars = $index = $i
if ($i = nqp::indexim(nqp::substr(self,0,$chars),.Str,0)) > -1
for @needles;
#?endif
#?if !moar
self!die-named('ignorecase and :ignoremark')
#?endif
}
else {
$chars = $index = $i
if ($i = nqp::index(nqp::substr(self,0,$chars),.Str)) > -1
for @needles;
}

$index == -1 ?? Nil !! $index
}

multi method index(Str:D: Str:D $needle --> Int:D) {
nqp::isne_i((my $index := nqp::index(self,$needle)),-1)
Expand All @@ -679,6 +759,15 @@ my class Str does Stringy { # declared in BOOTSTRAP
!! nqp::isne_i((my $index := nqp::index(self,$needle,$pos)),-1)
?? $index !! Nil
}
multi method index(Str:D: @needles --> Int:D) {
my int $i;
my int $index = -1;
my int $chars = nqp::chars(self);
$chars = $index = $i
if ($i = nqp::index(nqp::substr(self,0,$chars), .Str)) > -1
for @needles;
$index == -1 ?? Nil !! $index
}

# Cool catchers
multi method index(Str:D: Cool:D $needle --> Int:D) {
Expand Down

0 comments on commit 4cec44a

Please sign in to comment.