Skip to content

Commit 83e70de

Browse files
committed
Add ExportDfa() to export DFA as DataSection block
A PureBasic include file is created for the `DataSection` block.
1 parent c3af864 commit 83e70de

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,6 @@ Runs the Regex engine against the string. The function requires the pointer to t
106106

107107
- `GetLastErrorMessages()`<br>
108108
Returns the error messages of the last `Create()` call as a human-readable string.
109+
110+
- `ExportDfa(*regExEngine, filePath$, labelName$ = "dfaTable")`<br>
111+
Exports the created DFA as a `DataSection` block in a PureBasic include file. On success `#True` is returned, otherwise `#False`.

RegExEngine.pbi

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ DeclareModule RegEx
6969
; string.
7070
Declare$ GetLastErrorMessages()
7171

72+
; Exports the created DFA as a `DataSection` block in a PureBasic include file.
73+
; On success `#True` is returned, otherwise `#False`.
74+
Declare ExportDfa(*regExEngine, filePath$, labelName$ = "dfaTable")
75+
7276
EndDeclareModule
7377

7478
Module RegEx
@@ -635,4 +639,42 @@ Module RegEx
635639
ProcedureReturn lastErrorMessages$
636640
EndProcedure
637641

642+
Procedure ExportDfa(*regExEngine.RegExEngineStruc, filePath$, labelName$ = "dfaTable")
643+
Protected file, sizeOfArray, i, i2
644+
645+
file = CreateFile(#PB_Any, filePath$)
646+
If file = 0
647+
ProcedureReturn #False
648+
EndIf
649+
650+
WriteStringN(file, "DataSection")
651+
WriteString(file, Space(2) + labelName$ + ":")
652+
653+
sizeOfArray = MemorySize(*regExEngine\dfaStatesPool) / SizeOf(DfaStateStruc) - 1
654+
655+
For i = 0 To sizeOfArray
656+
657+
For i2 = 0 To 255
658+
If i2 % 35 = 0
659+
WriteStringN(file, "")
660+
WriteString(file, Space(2) + "Data.i ")
661+
ElseIf i2 <> 0
662+
WriteString(file, ",")
663+
EndIf
664+
WriteString(file, Str(*regExEngine\dfaStatesPool\states[i]\symbols[i2]))
665+
Next
666+
667+
If *regExEngine\dfaStatesPool\states[i]\isFinalState
668+
WriteString(file, ",#True")
669+
Else
670+
WriteString(file, ",#False")
671+
EndIf
672+
Next
673+
674+
WriteStringN(file, "")
675+
WriteStringN(file, "EndDataSection")
676+
CloseFile(file)
677+
ProcedureReturn #True
678+
EndProcedure
679+
638680
EndModule

0 commit comments

Comments
 (0)