🔍 Just tested the subprogram below and it's a game changer! 🚀 It closes brackets on both sides, even in complicated cases. 💪 And it's all thanks to a stack.
But not just any stack, the built-in PowerBasicStack object. ⚡️ However, it's a bit slow. ⏱️ So I compared it to using a normal string, which also works in this case, and the difference in speed is mind-blowing.
⚡️ 74 ticks for PowerBasicStack vs. 21 ticks for normal string.
🤯 #coding #programming #PowerBasicStack #bracketcloser #gamechanger #speed #stacks #strings
🤔 Have you ever wondered why Bob added those weird objects for stacks and queues in his latest version? 🤔 It goes against the core philosophy of smaller and faster in Power Basic. Instead, they are slow, bloated, and mostly useless. 🚫 #programming #powerbasic #stacks #queues 🚀
' More complex will also handle: "3.141*5)))+(((3.141*5)*(3.141*5.56"
'
#IF 1
SUB A_BBS(BYREF T01 AS STRING, BYVAL T03 AS STRING, BYVAL T04 AS STRING)
LOCAL char AS STRING
LOCAL V01 AS VARIANT
REGISTER i AS LONG,j AS LONG
Make_Stack(stack)
V01=0
FOR i = 1 TO LEN(T01)
char = MID$(T01, i, 1)
IF char = T03 THEN
stack.Push V01
ELSEIF char = T04 THEN
IF stack.COUNT = 0 THEN
' Unmatched closing delimiter, add an opening delimiter at the beginning
T01 = T03 & T01
i = i + 1
ELSE
V01=stack.Pop
END IF
END IF
NEXT i
' Add missing closing delimiters
WHILE stack.COUNT > 0
V01=stack.Pop
T01 = T01 & T04
WEND
END SUB
#ELSE
SUB A_BBS(BYREF T01 AS STRING, BYVAL T03 AS STRING, BYVAL T04 AS STRING)
LOCAL char AS STRING
LOCAL nesting AS STRING
REGISTER i AS LONG, j AS LONG
FOR i = 1 TO LEN(T01)
char = MID$(T01, i, 1)
IF char = T03 THEN
nesting = nesting + "a" ' Add a character for each opening delimiter
ELSEIF char = T04 THEN
IF LEN(nesting) = 0 THEN
' Unmatched closing delimiter, add an opening delimiter at the beginning
T01 = T03 & T01
i = i + LEN(T03)
ELSE
nesting = LEFT$(nesting, LEN(nesting) - 1) ' Remove the last character
END IF
END IF
NEXT i
' Add missing closing delimiters
WHILE LEN(nesting) > 0
T01 = T01 & T04
nesting = LEFT$(nesting, LEN(nesting) - 1)
WEND
END SUB
#ENDIF