Recursion Function to remove BB Code code tags, described in last two posts




Code:
'140 '
Public Function LongWayOfDoingIt2ReCurseCyClops(ByRef strBBCode As String, ByVal BkStt As Long) As String ' I do not actually need any As ______ as nothing is returned, but you never know a string to retuen might be useful later
150  Dim vTemp As Variant ' For Debugs
160 '
170  Rem 1) Main Loop for finding BB code pair String=====================================
180  Dim StpBk As Long
190      For StpBk = BkStt To 1 Step -1 ' Looking for the first ] likely at end of end tag.
200      Let vTemp = Mid(strBBCode, StpBk, 1)
210          If Mid(strBBCode, StpBk, 1) = "]" Then ' we note Position and need to look for a [/ On a recursion call this conditiuon is always met
220          Dim BcrdEEnd As Long: Let BcrdEEnd = StpBk '     Position of possible End Code Tag End
230          Dim posBBCodeTagSrch As Long ' For search through possible valid BBCode Section'_-Loop for Pos tag pair section--
240                For posBBCodeTagSrch = StpBk To 2 Step -1
250                  If Mid(strBBCode, posBBCodeTagSrch, 1) = "]" Then Let BcrdEEnd = posBBCodeTagSrch: GoTo NxtCESPosPos ' If we hit another ] then change the stop tag stop character position and start again to see if is a valid BB code section
260                  If Mid(strBBCode, posBBCodeTagSrch, 2) = "[/" Then     ' Found a stop tag "[/" at posBBCodeTagSrch, so mark..
270                  Dim BcrdESt As Long: Let BcrdESt = posBBCodeTagSrch    ' ..Position of End Code Tag Start and ..
280                   Exit For ' we have an end code tag from a possibly code tag pair section
290                  Else 'No luck finding a "[/" start bit of stop tag yet so keep looking Matey
300                  End If
310 NxtCESPosPos:  Next posBBCodeTagSrch ' trying to find a start [/ pair further back keep looking Matey
320                If posBBCodeTagSrch = 3 Then Exit Function 'case we never found the start of an End Code Tag
330                                          ' Loop likely at end of start tag ]
340                    For posBBCodeTagSrch = BcrdESt - 1 To 1 Step -1 ' Looping furhter back to find a "]" likely of a start code tag but possibly a nested stop tag
350                      If Mid(strBBCode, posBBCodeTagSrch, 1) = "]" Then ' possible start from back of "]" likely of a start code tag but possibly a nested stop tag found so....
360                      If Mid(strBBCode, posBBCodeTagSrch - 1, 1) = "]" Then GoTo NxtCSSPosPos ' we have a second "]" one back, looking back so this will be caught by next back 1
370                      Dim BcrdSEnd As Long: Let BcrdSEnd = posBBCodeTagSrch  ' Position of possible Start Code Tag End
380                      Dim LTagStLoop As Long ' _- Most Innerloop to find start of start Code Tag
390                          For LTagStLoop = posBBCodeTagSrch - 1 To 1 Step -1 ' _- Most inner loop [ likely start of start tag_
400                              If Mid(strBBCode, LTagStLoop, 1) = "[" Then
410                              Dim BcrdSSt: Let BcrdSSt = LTagStLoop
420                                  Rem If [/ Then Oh dear time for a "recursion wonk"
430                                  If Mid(strBBCode, LTagStLoop, 2) = "[/" Then 'Seems like our last attempt at finding a start tag caught a nested end tag
440  'Time to freeze this Function with current end tag info.
450                                   Call LongWayOfDoingIt2ReCurseCyClops(strBBCode, BcrdSEnd) ' Position of possible Start Code Tag End is actually another end tag end, so give this ] position as the start position and run another copy of the Function. At the start of which the first check should catch the ] and redo the finf´ding of the last tag complete so is a bit ineffecient
460  'Time to unfreeze this Function, and continue where left off having now possibly modified the main string for one or more nested tag pairs, as well as probably catching all but the possible pair that I froze on
470                                   Let posBBCodeTagSrch = LTagStLoop ' When we went off to the copy of the function our LTagStLoop was at the start of what turned out to be a nested end code tag. That is as good a place as any to strt going further back now to try to find the match for the end tag before the nested one. We will be going throught the lkast nested one, but never mind
480                                   GoTo NxtCSSPosPos ' unfreeze search for a matching start tag
490                                  Else ' As soon as we come here we are likely
500                                  End If ' ready to do the string modification for a found matching pair
510                               Let StpBk = BcrdSSt ' - 1 will be done at Next StpBk 'End of Code Tag section search ' I am commiting the cardinal sin of changing the Loop bound variable caount in the main outer step back in character Loop
520                               GoTo FkOffCrd ' We leave this For the start Code Tag Start Search and go to Fuk Off Crd:
530                              Else
540                              End If
550                          Next LTagStLoop ' _- End Most inner loop [ likely start of start tag_
560                          If LTagStLoop = 1 Then GoTo TheEnd 'I think here we must always be at LTagStLoop = 1 - Case I think we failed to find a start start [ for the complete possible Code Tag section search
570                      Else ' Not found a "]" likely of a start code tag but possibly a nested stop tag found
580                      End If
590 NxtCSSPosPos:      Next posBBCodeTagSrch ' End Loop likely end of start tag ]
600                    If posBBCodeTagSrch = 0 Then GoTo TheEnd ' For any outer search failed to find a ] at all or a search for matching start tag that was fruitless
610 FkOffCrd:   Rem 3) "I think we come here with a complete set of 4 positions"
620              If BcrdEEnd <> 0 And BcrdESt <> 0 And BcrdSEnd <> 0 And BcrdSSt <> 0 Then 'Just to check'
630              Dim SttBcrd As String, StpBcrd As String ' Start and stop tags as complete strings including [ and ]
640              Dim LenSttBcrd As Long, LenStpBcrd As Long ' Some extra variables for clarity
650               Let LenSttBcrd = BcrdSEnd - BcrdSSt + 1: Let LenStpBcrd = BcrdEEnd - BcrdESt + 1 ' As taypical to get length of anything when start and stop positions are known.. -- (stoppos-startpos+1)
660               Let SttBcrd = Mid(strBBCode, BcrdSSt, LenSttBcrd) ' In String  ,  part from [  ,  for length (stoppos-startpos+1) '  like |Color=Red|  or |b|
670               Let StpBcrd = Mid(strBBCode, BcrdESt, LenStpBcrd) ' like |/Color| |/b|
680              Dim SttWrd As String, StpWrd As String
690               Let StpWrd = Mid(StpBcrd, 3, Len(StpBcrd) - 3) ' like Color from |/Color| - Whole Word, from 3rd Chr , for Length as  WholeWord-3
700               Let SttWrd = Mid(SttBcrd, 2, Len(StpWrd))      ' like Color from |Color=Red|
710                  If StrComp(StpWrd, SttWrd, vbTextCompare) = 0 And Mid(StpBcrd, 2, 1) = "/" Then ' If UCase(SttWrd) = UCase(StpWrd) And Mid(StpBcrd, 2, 1) = "/" Then 'Ucase allows for differences in cases
720                   Mid(strBBCode, BcrdSSt, LenSttBcrd) = String(LenSttBcrd, "|") ' Mid can be used to put in strBBCode   ,   starting from start of tag   ,   for a length space within of the tag    =   any symbol as many times as that length space
730                   Mid(strBBCode, BcrdESt, LenStpBcrd) = String(LenStpBcrd, "|")
740                  Else ' ' No manipulationn of string for non matching tag word
750                  End If
760              Else 'No manipulation of string for not all 4 [ and ] found. Not sure if I ever come here..
770              End If
780          Else ' Case no "]" found in main strBBCode
790          End If
800      Next StpBk ' Stepping further back in Main Loop for finding first "]"============
810 TheEnd:
820  ' Let LongWayOfDoingIt2ReCurseCyClops = "Return from Function after running" ' Option if I wanted to give a value to be returned from Function. By virtue of the ByRef at signature line I effectively continually apply changes to the variable strBBCode
End Function