bmx-ng / brl.mod

BlitzMax Runtime Libraries, for BlitzMax NG.
12 stars 11 forks source link

[StringBuilder] Add BufferIndex:int(index:int) to TSplitBuffer #331

Open GWRon opened 3 weeks ago

GWRon commented 3 weeks ago

When currently splitting a TStringBuilder content into parts (sb.Split()) you will receive an instance of TSplitBuffer.

For now you can only retrieve the text of the split part as string or 'string[]` (all of them at once).

I would ask for an additional function which either returns the "start and end" as a custom struct (eg SStringBuilderRange or so) - or at least allow retrieving the index in the "buffer" (the TStringBuilder instance) which allows at least with the help of the "next" element in the split buffer + "total length of sb content" to retrieve indexes there. The struct sounds more feasible.

So why doing this ? In my case I have eg this string from some serialized information: "123,1,2|456,1,0|789,3,4|..." For now I would as first step split by the pipe sign sb.split("|") so I retrieve a TSplitBuffer which allows to so buffer.text(0) and eg retrieve "123,1,2" (creating and allocating a new bbstring for it).

Next step for me would to split this string again into entries ("123", "1", "2" by splitting with ","). 3 new strings are allocated.

Having the indices of the "pieces" the TSplitbuffer knows of would at least allow to skip the first new allocated bbstring.

to even get rid of the other ones I would even suggest something different: Extend ToString() or add compagnons to substring. Something like this: SubstringToInt:int(start:int, length:int) SubstringToFloat:Float(start:int, length:int) ... Why? this should allow extracting numbers from (long) string-portions without having to allocate bbstrings. (Add similar to the splitbuffer and instead of "Text(index)" you could also retrieve numbers from it directly method GetAsDouble:Double(index:int))

(if you then also allow a TStringBuilder to be a kind of virtual window into an other TStringBuilder you could do a kind of "nested" split there)

Dunno if there are "more feasible" ways to approach this but this is what my mind has come up with. Am open for suggestions of course. I know I could use some TRegex for this "special case" but I thought of some more "generic" thing.