What's the fastest/most efficient way to count lines in Rebol?

前端 未结 9 873
星月不相逢
星月不相逢 2021-01-02 07:33

Given a string string, what is the fastest/most-efficient way to count lines therein? Will accept best answers for any flavour of Rebol. I\'ve been working unde

相关标签:
9条回答
  • 2021-01-02 08:16

    Here's the best simple non-parse version I can think of:

    count-lines: function [text [string!]] [
        i: 1
        find-all text newline [++ i]
        i
    ]
    

    It uses function and ++ from more recent versions of Rebol, and find-all from either R3 or R2/Forward. You could look at the source of find-all and inline what you find and optimize, but situations like this are exactly what we wrote find-all for, so why not use it?

    0 讨论(0)
  • 2021-01-02 08:20

    Why no one came with the simplest solution I wonder :)

    t: "abc^/de^/f^/ghi"
    i: 0 until [i: i + 1 not t: find/tail t newline] i
    == 4
    

    Not sure about the performance but I think it's quite fast, as UNTIL and FIND are natives. WHILE could be used as well.

    i: 1 while [t: find/tail t newline] [i: i + 1] i
    == 4
    

    Just need to check for empty string. And if it would be a function, argument series needs to be HEADed.

    0 讨论(0)
  • 2021-01-02 08:23

    Do not know about performance, and the last line rule (r3).

    >> length? parse "1^/2^/3" "^/"
    == 3
    
    0 讨论(0)
提交回复
热议问题