diff --git a/embedding/commentfilter/visual_basic_filter.go b/embedding/commentfilter/visual_basic_filter.go index cd6adcb..5478692 100644 --- a/embedding/commentfilter/visual_basic_filter.go +++ b/embedding/commentfilter/visual_basic_filter.go @@ -60,7 +60,7 @@ func filterVisualBasicLine(line string, mode Mode) (string, bool) { var result strings.Builder position := 0 for position < len(line) { - if quoteEnd := quotedSegmentEnd(line, position, "\""); quoteEnd > position { + if quoteEnd := visualBasicQuotedSegmentEnd(line, position); quoteEnd > position { result.WriteString(line[position:quoteEnd]) position = quoteEnd @@ -87,6 +87,30 @@ func filterVisualBasicLine(line string, mode Mode) (string, bool) { return result.String(), false } +// visualBasicQuotedSegmentEnd returns the end offset of a Visual Basic quoted string. +func visualBasicQuotedSegmentEnd(line string, position int) int { + if position >= len(line) || line[position] != '"' { + return position + } + cursor := position + 1 + for cursor < len(line) { + if line[cursor] != '"' { + cursor++ + + continue + } + if cursor+1 < len(line) && line[cursor+1] == '"' { + cursor += 2 + + continue + } + + return cursor + 1 + } + + return len(line) +} + // remCommentAt reports whether a Visual Basic REM comment starts at position. func remCommentAt(line string, position int) bool { if len(line[position:]) < len(rem) || diff --git a/embedding/commentfilter/visual_basic_filter_test.go b/embedding/commentfilter/visual_basic_filter_test.go index 264d3f1..a331ddf 100644 --- a/embedding/commentfilter/visual_basic_filter_test.go +++ b/embedding/commentfilter/visual_basic_filter_test.go @@ -30,6 +30,9 @@ var _ = Describe("Visual Basic", func() { "' file comment", "REM module comment", "Dim text = \"REM not a comment\"", + "Dim quotedRem = \"\"\"REM not a comment\"", + "Dim quotedApostrophe = \"\"\"' not a comment\"", + "Dim escapedQuote = \"Say \"\"REM\"\" and keep going\" ' inline", "Dim value = 1 ' inline", "Dim ready = True : Rem after statement separator", "Dim reminder = 1", @@ -37,6 +40,9 @@ var _ = Describe("Visual Basic", func() { expected := []string{ "Dim text = \"REM not a comment\"", + "Dim quotedRem = \"\"\"REM not a comment\"", + "Dim quotedApostrophe = \"\"\"' not a comment\"", + "Dim escapedQuote = \"Say \"\"REM\"\" and keep going\" ", "Dim value = 1 ", "Dim ready = True : ", "Dim reminder = 1",