Thanks for the input!
@Marc56us
Sorry, your version won't work. The number 0 would be treated as any other character that is not a number (and <= 9 would be required to catch the 9)
@Oso
I've timed all variants with a loop of 100.000 runs per function (ofc without the debugger), results are:
Code: Select all
Result (function 1): 6481 [92.197 ms]
Result (function 2): 6481 [106.495 ms]
Result (function 3): 6481 [133.019 ms]
Result (function 4): 6481 [42.623 ms]
Result (function 5): 6481 [45.636 ms]
So going to the end of the string and then reverse it (via pointers) is atm the fastest method.
The slight variant 5 (not shuffling the output string but return the reverse string at the end) is minimal slower.
That might change if the test string would be significant longer though...
The only way to speed up function 4/5 would maybe by advancing to the last character of the string (via pointer) without a loop (if that's even possible) before going over them in reverse order
Code: Select all
Define.i hRegex
hRegex = CreateRegularExpression(#PB_Any, "\d+$", #PB_RegularExpression_MultiLine)
Procedure.s GetTrailingNumbersFromString1(hRegex, string.s)
Protected.s result
If ExamineRegularExpression(hRegex, string)
While NextRegularExpressionMatch(hRegex)
result = RegularExpressionMatchString(hRegex)
Wend
EndIf
ProcedureReturn result
EndProcedure
Procedure.s GetTrailingNumbersFromString2(string.s)
Protected.i i, num
Protected.s char, result
For i = Len(string) To 1 Step -1
char = Mid(string, i, 1)
Select char
Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
result + char
Default
Break
EndSelect
Next
ProcedureReturn ReverseString(result)
EndProcedure
Procedure.s GetTrailingNumbersFromString3(*StrPtr.Character)
Protected.s result
While *StrPtr\c
Select *StrPtr\c
Case 48 To 57 ; 0 - 9 as ascii
result + Chr(*StrPtr\c)
Default
result = ""
EndSelect
*StrPtr + 2
Wend
ProcedureReturn result
EndProcedure
Procedure.s GetTrailingNumbersFromString4(*StrPtr.Character)
Protected.s result
Protected *LastPtr.Character = *StrPtr
While *LastPtr\c ; Start last ptr at the beginning, looking for zero byte
*LastPtr + 2 ; Next char.
Wend
*LastPtr - 2 ; Adjust because we're now on the zero byte
While *LastPtr >= *StrPtr ; Loop again but from the last character, in reverse, until start
Select *LastPtr\c
Case 48 To 57
result = Chr(*LastPtr\c) + result ; Add the character to the start (not the end)
Default
Break ; Drop out the loop, as we've found a non-numeric
EndSelect
*LastPtr - 2 ; Prev. character
Wend
ProcedureReturn result
EndProcedure
Procedure.s GetTrailingNumbersFromString5(*StrPtr.Character)
Protected.s result
Protected *LastPtr.Character = *StrPtr
While *LastPtr\c ; Start last ptr at the beginning, looking for zero byte
*LastPtr + 2 ; Next char.
Wend
*LastPtr - 2 ; Adjust because we're now on the zero byte
While *LastPtr >= *StrPtr ; Loop again but from the last character, in reverse, until start
Select *LastPtr\c
Case 48 To 57
result + Chr(*LastPtr\c) ; Add the character to the start (not the end)
Default
Break ; Drop out the loop, as we've found a non-numeric
EndSelect
*LastPtr - 2 ; Prev. character
Wend
ProcedureReturn ReverseString(result)
EndProcedure