【发布时间】:2013-01-23 19:56:12
【问题描述】:
给定一个字符串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
【问题讨论】:
-
此外,我们欢迎任何帮助测试速度/效率声明的真实性。
-
Rebol 程序员会认为空字符串只有 1 行。这种对零的恐惧是怎么回事? :-P
-
任何性能考虑的另一件事是输入的性质。我会研究您尝试使用不同输入的任何技术...一些示例:空字符串,“所有换行符的长字符串”,“零换行符的长字符串"...
标签: string performance newline rebol