给定一个字符串string
,计算其中行数的最快/最有效方法是什么?将接受任何 Rebol 风格的最佳答案。我一直在假设该parse [some [thru]]
组合是遍历字符串的最快方式,但后来我不确定,因此转向 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
]
那么柜台呢?重复效率如何?
count-lines: func [string [string!]][
repeat count length? string [
unless string: find/tail string newline [
break/return count
]
]
]
更新:行数遵循文本编辑器原则:
空文档的行数仍然为 1。所以:
>> count-lines ""
== 1
>> count-lines "^/"
== 2