@@ -438,12 +438,26 @@ Public Class frmMain
438438 End If
439439 'The line contains GOTO, GOSUB or THEN, go ahead
440440
441- match = Regex.Match(line, "(GOTO|GOSUB|THEN) *[0-9]+$" , RegexOptions.IgnoreCase)
442- If match.Value IsNot "" Then
441+ 'On a line there can be multiple GOSUB statements. Replace each of them.
442+ Dim matches As MatchCollection = Regex.Matches(line, "GOSUB *[0-9]+ *(:|$)" , RegexOptions.IgnoreCase) 'find all occurencies
443+ If matches.Count > 0 Then
444+ Dim editLine As String = "" 'construct a temporary copy of the line with new GOSUB lines replaced
445+ Dim curIndex As Integer = 0 'keep the index of current line position
446+ For Each m In matches
447+ Dim line_no As Match = Regex.Match(m.Value, "[0-9]+" ) 'get the original line number reference
448+ editLine &= line.Substring(curIndex, m.Index + line_no.Index - curIndex) 'copy the line until the match including GOSUB
449+ curIndex = m.Index + line_no.Index + line_no.Length 'position the index after the old line number
450+ editLine &= matchMap(line_no.Value.Trim()) 'add the new line number
451+ Next
452+ editLine &= line.Substring(curIndex, line.Length - curIndex) 'copy the rest of the line untill the end
453+ line = editLine
454+ End If
455+
456+ match = Regex.Match(line, "(GOTO|THEN) *[0-9]+$" , RegexOptions.IgnoreCase)
457+ If match.Success Then
443458 'The line contains GOTO, GOSUB or THEN followed by constant line number at the end of line.
444459 Dim line_no As Match = Regex.Match(match.Value, "[0-9]+$" ) 'get the original line number reference
445460 line = Regex.Replace(line, "GOTO *[0-9]+$" , "GOTO " & matchMap(line_no.Value.Trim()), RegexOptions.IgnoreCase)
446- line = Regex.Replace(line, "GOSUB *[0-9]+$" , "GOSUB " & matchMap(line_no.Value.Trim()), RegexOptions.IgnoreCase)
447461 line = Regex.Replace(line, "THEN *[0-9]+$" , "THEN " & matchMap(line_no.Value.Trim()), RegexOptions.IgnoreCase)
448462 changedLines &= line & vbCrLf
449463 Continue For
0 commit comments