使用 VBScript 进行高速字符串连接






2.94/5 (6投票s)
有人说,从小的字符串大规模创建长字符串在 VBScript 中是不切实际的——事实并非如此。
引言
之前我发布过一篇关于 JScript 中高速字符串连接的文章,这里是 VBScript 的等效版本。
这篇文章实际上不是一篇完整的文章。请不要急于给它评 -1 分。这是作为一篇先前文章的补充,该文章详细介绍了背后的思考过程,但使用了 JScript。要了解背景,请点击这里。
以下是代码。此代码与 JScript 的真正区别在于,在 VBScript 中,数组不是对象。这意味着我必须创建一个显式类来创建用于保存字符串片段的链表对象。另外请注意,在 VBScript 中,数组的长度是固定的。如果您重新定义数组,实际上会执行复制操作(旧数组将被丢弃,您将获得一个新数组)。这意味着在 JScript 文章的留言中讨论的分割/连接技术在 VBScript 中无法工作。
class XLink
public datum
public nextXLink
private sub Class_Initialize
set nextXLink=nothing
datum=""
end sub
end class
class FStringCat
private sp
private ep
private l
private accum
private sub Class_Initialize
l=0
accum=""
set sp=nothing
end sub
public sub push(what)
accum=accum & what
if len(accum)>2800 then
if(sp is nothing) then
set ep=new XLink
set sp=ep
else
dim oep
set oep=ep
set ep=new XLink
set oep.nextXLink=ep
end if
ep.datum=accum
accum=""
l=l+1
end if
end sub
public function toString()
if l=0 then
toString=accum
exit function
end if
ep.datum=ep.datum & accum
while l>1
dim ptr
set ptr=sp
dim nsp
set nsp=new XLink
dim nep
set nep=nsp
dim nl
nl=0
while not (ptr is nothing)
if nep.datum="" then
nep.datum=ptr.datum
nl=nl+1
else
if ptr.datum<>"" then nep.datum=nep.datum & ptr.datum
set nep.nextXLink=new XLink
set nep=nep.nextXLink
end if
set ptr=ptr.nextXLink
wend
set sp=nsp
set ep=nep
l=nl
wend
toString=sp.datum
end function
end class
' Some example code
WScript.echo("Starting")
dim ts
set ts=new FStringCat
WScript.echo("Created FStringCat")
dim i
for i=1 to 1000
ts.push("Hi there: " & i & vbcrlf)
next
WScript.echo("Results")
WScript.echo(ts.toString())
原始 nerds-central 文章位于 这里。如需更多类似内容,请查看 Nerds-Central。
历史
- 2007年4月13日:初始发布