Hi,
primo basically nailed the stringBuilder usage, here a little example:
I may add direct pointer to string in stringBuilder as functionality, if there is interest. That would skip the step with assignment to temporary string variable, making it even fasterUses "console", "stringBuilder" ' this will store the numbers: Dim builder as new StringBuilder() ' access the numbers virtually: Dim myNumber() as Number At 0 Dim i As Long ' just to list our arrays elements ' add a few elements, type Ext equals Number, that's why MKE$: builder.Add(MKE$(0.12345)) ' the first element be 0.12345 ' just append a bunch of new elements: builder.Add(MKE$(1,2,3,4,5)) ' and some more builder.Add(MKE$(6.666, 7.777)) ' now let's read what we have: ' put the virtual array onto the string: string builderData = builder.ToString() Redim myNumber(Strptrlen(Strptr(builderData))/SizeOf(Number)) At Strptr(builderData) ' list what we have For i = 1 to UBound(myNumber) PrintL i, myNumber(i) Next WaitKey
Petr
Learn 3D graphics with ThinBASIC, learn TBGL!
Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
If I understood right the stringbuilder allocates more size than the actual length of the string. But sb.Length() then returns the number of chars that are occupied.
Then assuming sb gets a new method named DataPtr() it were like
?Redim myNumber(sb.Length/SizeOf(Number)) At sb.DataPtr
However we would access the single elements of the array myNumber using parenthesis and index. No matter what type myNumber really is, even fixed-size Udt can be used.
SizeOf(...) tells how many bytes we need, the index of myNumber(...) tells which bytes we need.
Let's say we have the array as in the example stored to builder and we want to list the arrays elements without the use of a virtual variable.
Cumbersome we have to calculate:
How about methods to stringbuilder similar to current _.Char() butFor i = 1 To builder.Length/SizeOf(Number) PrintL i, CVE(Memory_Get(builder.DataPtr + (i - 1) * SizeOf(Number), SizeOf(Number))) Next
_.Element(index, SizeOf(datatype)) : return an elements data, means a sequence of bytes
_.Elements(SizeOf (datatype)) : return UBound of elements stored to sb
or even better tell stringbuilder the SizeOf elements or the required datatype and it will remember it:For i = 1 to builder.Elements(SizeOf(Number)) PrintL i, CVE (builder.Element( i, SizeOf(Number))) Next
I don't know if possible for Udt if using _.SetType, but having to give the size once only instead of repeating it every time were greatDim builder As New stringbuilder builder.SetType(Number) ' default Byte/Char ' or builder.ElementSizeSet(SizeOf(Number)) ' default 1, don't want division by 0... builder.Add(MKE$(0.123)) builder.Add(MKE$(1,2,3,4)) builder.Add(MKE$(5.5,6.6,7.7)) For i = 1 To builder.Elements() PrintL i, CVE(builder.Element(i))) ' ...
@ Eros,
how about
Alias MKE$ As MKNumber$
Alias CVE As CVNumber
For better readability and understanding?
Last edited by ReneMiner; 25-03-2017 at 20:32.
I think there are missing some Forum-sections as beta-testing and support
www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000
Hi friends,
I added the DataPtr property, which allows you to directly access the internal buffer of stringBuilder object!
@Eros: Can we please have this in the next release of thinBasic?
Petr
Learn 3D graphics with ThinBASIC, learn TBGL!
Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB
Bookmarks