'split test.bas for qb64 bplus 2018-05-07
' directly below is Steve's Timed test orig tests commented out
' 2019-02-17 modified by B+ with a new Split1000 sub and a SERIOUS String to Split!
'=================================================================== steve;s speed test
'trivial tests just to test accuracy of split
a(0) = ""
d(0) = " "
a(1) = " test test test " 'good no error!
d(1) = " "
a(2) = " test"
d(2) = " "
a(3) = "3d,z6d,z1 10 #d,z5"
d(3) = ",z"
a(4) = "Monday, , Wednesday, THursday, Friday, , Sunday"
d(4) = ", "
'lets get a serious test in here!! test a 10,000 random number string
a(5) = s$
d(5) = " "
a(6) = s$
d(6) = ", "
FOR j
= 1 TO Limit
'repeat the process multiple times so we can time it. SteveSplit2 a(i), d(i), results1(), NoNull
FOR j
= 1 TO Limit
'repeat the process multiple times so we can time it. Split1000 a(i), d(i), results2()
FOR j
= 1 TO Limit
'repeat the process multiple times so we can time it. Lsplit a(i), d(i), results3()
PRINT:
PRINT "Test names:", "SteveSplit2", "Split1000", "Lsplit" PRINT USING "###.#### ###.#### ###.####"; t1#
- t#
, t2#
- t1#
, t3#
- t2#
PRINT:
PRINT "First Items in Results arrays (up to 10):" PRINT:
INPUT "Press enter for next test... "; wate$
'' ================================= My Old Split test Code
'the space delimiter is such a special case perhaps I should develope a single split for that alone?
''2018-08-25 reworked for space delimiters and more variable declares
''2019-02-15 add Luke's version to compare
'ntests = 5
'DIM a(ntests - 1) AS STRING, d(ntests - 1) AS STRING
'a(0) = ""
'd(0) = " "
'a(1) = " test test test " 'good no error!
'd(1) = " "
'a(2) = " test"
'd(2) = " "
'a(3) = "3d,z6d,z1 10 #d,z5"
'd(3) = ",z"
'a(4) = "Monday, , Wednesday, THursday, Friday, , Sunday"
'd(4) = ", "
'REDIM myarr(0) AS STRING '<<<<< REDIM forces the creation of a dynamic/resizable array
'FOR test = 0 TO ntests - 1
' PRINT: PRINT "splitting {"; a(test); "} with delimeter {"; d(test); "}"
' Split1000 a(test), d(test), myarr()
' amax = UBOUND(myarr)
' FOR i = 0 TO amax
' PRINT i; ":"; myarr(i)
' NEXT i
' INPUT "press enter for next test... "; wate$
'NEXT
'' how about a quick file reader test?
'PRINT: INPUT "Press enter for file test, any other + enter quits! "; wate$
'IF LEN(wate$) THEN END
'CLS
''other wise continue
'OPEN "Split test.bas" FOR BINARY AS #1 '<<< this file name!!!
'ftext$ = SPACE$(LOF(1))
'GET #1, , ftext$
'CLOSE #1
'Split ftext$, CHR$(13) + CHR$(10), myarr()
'FOR i = 0 TO UBOUND(myarr)
' PRINT myarr(i)
' IF i MOD 20 = 19 THEN PRINT: INPUT "press enter for more "; wate$
'NEXT
'PRINT "the end"
'END ' end program
'
'notes: REDIM the array(0) to be loaded before calling Split '<<<<<<<<<<<<<<<<<<<<<<< IMPORTANT!!!!
' bplus modifications of Galleon fix of Bulrush Split reply #13
' http://xmaxw.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=1612.0
' this sub further developed and tested here: \test\Strings\Split test.bas
copy = mystr 'make copy since we are messing with mystr when the delimiter is a space
'special case if delim is space, probably want to remove all excess space
copy
= MID$(copy
, 1, p
- 1) + MID$(copy
, p
+ 1) curpos = 1
arrpos = 0
dpos
= INSTR(curpos
, copy
, delim
) arr
(arrpos
) = MID$(copy
, curpos
, dpos
- curpos
) arrpos = arrpos + 1
curpos = dpos + LD
dpos
= INSTR(curpos
, copy
, delim
) arr
(arrpos
) = MID$(copy
, curpos
)
'notes: REDIM the array(0) to be loaded before calling Split '<<<<<<<<<<<<<<<<<<<<<<< IMPORTANT!!!!
' bplus modifications of Galleon fix of Bulrush Split reply #13
' http://xmaxw.[abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]/forum/index.php?topic=1612.0
' this sub further developed and tested here: \test\Strings\Split test.bas
copy = mystr 'make copy since we are messing with mystr when the delimiter is a space
'special case if delim is space, probably want to remove all excess space
copy
= MID$(copy
, 1, p
- 1) + MID$(copy
, p
+ 1) curpos = 1
arrpos = 0
dpos
= INSTR(curpos
, copy
, delim
) arr
(arrpos
) = MID$(copy
, curpos
, dpos
- curpos
) arrpos = arrpos + 1
curpos
= dpos
+ LEN(delim
) dpos
= INSTR(curpos
, copy
, delim
) arr
(arrpos
) = MID$(copy
, curpos
)
' Luke 2019-02-15
'Split in$ into pieces, chopping at every occurrence of delimiter$. Multiple consecutive occurrences
'of delimiter$ are treated as a single instance. The chopped pieces are stored in result$().
'
'delimiter$ must be one character long.
'result$() must have been REDIMmed previously.
SUB Lsplit
(in$
, delimiter$
, result$
()) start = 1
start = start + 1
finish
= INSTR(start
, in$
, delimiter$
) result$
(UBOUND(result$
)) = MID$(in$
, start
, finish
- start
) start = finish + 1
'Combine all elements of in$() into a single string with delimiter$ separating the elements.
result$ = result$ + delimiter$ + in$(i)
join$ = result$
count = 1: oldi = 1
i
= INSTR(oldi
, text$
, delimiter$
) length = i - oldi
storage_array
(count
) = MID$(text$
, oldi
, length
) count = count - 1 'remove null-strings.
oldi = i + l
i = oldi
count = count + 1
storage_array
(count
) = MID$(text$
, oldi
)
SUB SteveSplit
(text$
, delimiter$
, storage_array
() AS STRING) count = count + 1
i
= INSTR(text$
, delimiter$
) storage_array
(count
) = LEFT$(text$
, i
- 1) SteveSplit
MID$(text$
, i
+ LEN(delimiter$
)), delimiter$
, storage_array
() storage_array(count) = text$
count = 0