Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

High Speed String Concatenation Using VBScript

0.00/5 (No votes)
13 Apr 2007 1  
It has been said that large scale creation of long strings from small strings is not practical in VBScript - NOT SO

Introduction

Some time ago, I posted about high speed string concatenation in JScript, here is the VBScript equivalent.

This post is not really a full post. Don't jump to give it a -1 rating. This is here as an addendum to a previous article which - in great detail - shows the thinking behind this but uses JScript. To get the background, go here.

Below is the code. The only real difference between this and the JScript is that in VBScript, arrays are not objects. This means that I have had to create an explicit class to create the objects for the linked list that holds the string fragments. Also please note that in VBScript, arrays are of fixed length. If you redim an array, you actually perform a copy operation (the old one is thrown away and you get a new array). This means that the split/join technique discussed in the messages with the JScript post cannot work in 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())

The original nerds-central post is here. For more like this, check out Nerds-Central.

History

  • 13th April, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here