develooper Front page | perl.perl6.users | Postings from February 2019

Re: "index" source code?

Thread Previous | Thread Next
From:
Brad Gilbert
Date:
February 3, 2019 15:20
Subject:
Re: "index" source code?
Message ID:
CAD2L-T12Ts5jK=w9yxNTb9OYYiKLUXKWFnsQDUd-vfn+_hPjAA@mail.gmail.com
The Rakudo Perl6 compilier is written in Perl6 (and a subset of Perl6 named NQP)

The `index` subroutines are at
https://github.com/rakudo/rakudo/blob/64c88f919841c58f5a6dffd3581770e06a8fd6a5/src/core/Cool.pm6#L276-L282

    proto sub index($, $, $?, *%) {*}
    multi sub index(Cool $s, Cool $needle --> Int:D) {
        $s.index($needle)
    }
    multi sub index(Cool $s, Cool $needle, Cool $pos --> Int:D) {
        $s.index($needle,$pos)
    }

They basically only call the `index` and `rindex` methods

To keep from showing you all of the methods, they all coerce the value
to Str then call `index` on that.

The location of the Str.index methods is
https://github.com/rakudo/rakudo/blob/3d581c8d23e0b47fd09616a1165f84568531a4aa/src/core/Str.pm6#L255-L284

    proto method index(|) {*}
    multi method index(Str:D: Cool:D $needle --> Int:D) {
        self.index: $needle.Str
    }
    multi method index(Str:D: Str:D $needle --> Int:D) {
        nqp::if(
          nqp::islt_i((my int $i =
            nqp::index($!value,nqp::getattr($needle,Str,'$!value'))),
            0
          ),
          Nil,
          nqp::p6box_i($i)
        )
    }
    multi method index(Str:D: Cool:D $needle, Cool:D $pos --> Int:D) {
        self.index: $needle.Str, $pos.Int
    }
    multi method index(Str:D: Str:D $needle, Int:D $pos --> Int:D) {
        nqp::if(
          nqp::isbig_I(nqp::decont($pos)) || nqp::islt_i($pos,0),
          self!INDEX-OOR($pos),
          nqp::if(
            nqp::islt_i((my int $i = nqp::index(
              $!value,nqp::getattr($needle,Str,'$!value'),$pos
            )),0),
            Nil,
            nqp::p6box_i($i)
          )
        )
    }

Note that they heavily use NQP opcodes, so you shouldn't try and copy them.

On Sun, Feb 3, 2019 at 12:35 AM ToddAndMargo via perl6-users
<perl6-users@perl.org> wrote:
>
> Hi All,
>
> Was the subroutine "index" written Perl6?  If so,
> where can I view the source code?
>
> Many thanks,
> -T

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About