在Rebol中计算行数最快/最有效的方法是什么? [英] What's the fastest/most efficient way to count lines in Rebol?
问题描述
给出一个字符串string
,计算其中行数的最快/最有效方法是什么?对于任何口味的Rebol都将接受最佳答案.我一直在假设parse [some [thru]]
组合是遍历字符串的最快方法,但后来我不确定,因此转向SO:
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 under the assumption that the parse [some [thru]]
combination was the fastest way to traverse a string, but then I don't know that for certain, hence turning to SO:
count-lines: func [string [string!] /local count][
parse/all string [
(count: 1) some [thru newline (count: count + 1)]
]
count
]
或者:
count-lines: func [string [string!] /local count][
count: 0
until [
count: count + 1
not string: find/tail string newline
]
count
]
柜台又如何呢?重复的效率如何?
And how about counters? How efficient is repeat?
count-lines: func [string [string!]][
repeat count length? string [
unless string: find/tail string newline [
break/return count
]
]
]
更新:行数遵循文本编辑器"原则:
Update: line count goes by the Text Editor principle:
一个空文档的行数仍为1.所以:
An empty document still has a line count of one. So:
>> count-lines ""
== 1
>> count-lines "^/"
== 2
推荐答案
增强的PARSE版本,由BrianH建议:
Enhanced PARSE version, as suggested by BrianH:
i: 1 ; add one as TextMate
parse text [any [thru newline (++ i)]]
print i
这篇关于在Rebol中计算行数最快/最有效的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!