Click here to Skip to main content
Click here to Skip to main content

High Speed String Concatenation Using VBScript

By , 13 Apr 2007
Rate this:
Please Sign up or sign in to vote.

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

alex turner
Web Developer
United Kingdom United Kingdom
I am now a Software Systems Developer - Senior Principal at Micro Focus Plc. I am honoured to work in a team developing new compiler and runtime technology for Micro Focus.
 
My past includes a Ph.D. in computational quantum mechanics, software consultancy and several/various software development and architecture positions.
 
For more - see
 
blog: http://nerds-central.blogspot.com
 
twitter: http://twitter.com/alexturner

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 13 Apr 2007
Article Copyright 2007 by alex turner
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid