Patterns: WIP Patterns2
This commit is contained in:
@@ -1,13 +1,103 @@
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports System.Text.RegularExpressions
|
||||
|
||||
Namespace [PatternModule]
|
||||
Public Class BaseModule
|
||||
Friend ReadOnly BaseFunctions As BaseFunctions
|
||||
Friend ReadOnly Logger As Logger
|
||||
|
||||
Private ReadOnly MyRegex As Regex = New Regex("{#(\w+)#([\:\.\w\s_-]+)}+")
|
||||
|
||||
Private Const MAX_TRY_COUNT = 500
|
||||
|
||||
Public Sub New(pLogConfig As LogConfig)
|
||||
Logger = pLogConfig.GetLogger()
|
||||
BaseFunctions = New BaseFunctions(pLogConfig)
|
||||
End Sub
|
||||
|
||||
Public Sub IncrementCounterOrThrow(ByRef pCounter As Integer)
|
||||
If pCounter >= MAX_TRY_COUNT Then
|
||||
Throw New OverflowException("Max tries exceeded while replacing placeholders!")
|
||||
End If
|
||||
|
||||
pCounter += 1
|
||||
End Sub
|
||||
|
||||
Public Function ReplacePattern(input As String, type As String, replacement As String) As String
|
||||
Dim elements As MatchCollection = MyRegex.Matches(input)
|
||||
|
||||
If IsNothing(replacement) Then
|
||||
Return input
|
||||
End If
|
||||
|
||||
For Each element As Match In elements
|
||||
' if group 1 contains the 'pattern' the replace whole group with 'replacement'
|
||||
' and return it
|
||||
If element.Groups(1).Value = type Then
|
||||
Return Regex.Replace(input, element.Groups(0).Value, replacement)
|
||||
End If
|
||||
Next
|
||||
|
||||
' no replacement made
|
||||
Return input
|
||||
End Function
|
||||
|
||||
Public Function ContainsPatternAndValue(pInput As String, pType As String, pValue As String) As Boolean
|
||||
Dim oElements As MatchCollection = MyRegex.Matches(pInput)
|
||||
|
||||
For Each oElement As Match In oElements
|
||||
' Pattern in pInput
|
||||
Dim t As String = oElement.Groups(1).Value
|
||||
Dim v As String = oElement.Groups(2).Value
|
||||
|
||||
If t = pType And v = pValue Then
|
||||
Return True
|
||||
End If
|
||||
Next
|
||||
|
||||
Return False
|
||||
End Function
|
||||
|
||||
Public Function ContainsPattern(pInput As String, pType As String) As String
|
||||
Dim oElements As MatchCollection = MyRegex.Matches(pInput)
|
||||
|
||||
For Each oElement As Match In oElements
|
||||
Dim t As String = oElement.Groups(1).Value
|
||||
|
||||
If t = pType Then
|
||||
Return True
|
||||
End If
|
||||
Next
|
||||
|
||||
Return False
|
||||
End Function
|
||||
|
||||
Public Function HasPattern(pInput As String, pType As String) As Boolean
|
||||
Dim oMatches = MyRegex.Matches(pInput)
|
||||
|
||||
For Each oMatch As Match In oMatches
|
||||
For Each oGroup As Group In oMatch.Groups
|
||||
If oGroup.Value = pType Then
|
||||
Return True
|
||||
End If
|
||||
Next
|
||||
Next
|
||||
|
||||
Return False
|
||||
End Function
|
||||
|
||||
Public Function GetNextPattern(input As String, type As String) As Pattern
|
||||
Dim elements As MatchCollection = MyRegex.Matches(input)
|
||||
|
||||
For Each element As Match In elements
|
||||
' Pattern in pInput
|
||||
Dim t As String = element.Groups(1).Value
|
||||
Dim v As String = element.Groups(2).Value
|
||||
|
||||
If t = type Then
|
||||
Return New Pattern(t, v)
|
||||
End If
|
||||
Next
|
||||
|
||||
Return Nothing
|
||||
End Function
|
||||
End Class
|
||||
End Namespace
|
||||
|
||||
Reference in New Issue
Block a user