@@ -704,15 +704,15 @@ private class Pattern {
704
704
/// \p Str has to point in the beginning of the definition (right after the
705
705
/// opening sequence). Returns the offset of the closing sequence within Str,
706
706
/// or npos if it was not found.
707
- private func findRegexVarEnd( _ regVar : String ) -> String . Index ? {
707
+ private func findRegexVarEnd( _ regVar : String , brackets : ( open : Character , close : Character ) , terminator : String ) -> String . Index ? {
708
708
var string = regVar
709
709
// Offset keeps track of the current offset within the input Str
710
710
var offset = regVar. startIndex
711
711
// [...] Nesting depth
712
712
var bracketDepth = 0
713
713
714
714
while let firstChar = string. characters. first {
715
- if string. hasPrefix ( " ]] " ) && bracketDepth == 0 {
715
+ if string. hasPrefix ( terminator ) && bracketDepth == 0 {
716
716
return offset
717
717
}
718
718
if firstChar == " \\ " {
@@ -721,11 +721,11 @@ private class Pattern {
721
721
offset = regVar. index ( offset, offsetBy: 2 )
722
722
} else {
723
723
switch firstChar {
724
- case " [ " :
724
+ case brackets . open :
725
725
bracketDepth += 1
726
- case " ] " :
726
+ case brackets . close :
727
727
if bracketDepth == 0 {
728
- diagnose ( . error, . string( regVar) , " missing closing \" ] \" for regex variable " )
728
+ diagnose ( . error, . string( regVar) , " missing closing \" \( brackets . close ) \" for regex variable " )
729
729
return nil
730
730
}
731
731
bracketDepth -= 1
@@ -808,7 +808,8 @@ private class Pattern {
808
808
// RegEx matches.
809
809
if patternStr. range ( of: " {{ " ) ? . lowerBound == patternStr. startIndex {
810
810
// This is the start of a regex match. Scan for the }}.
811
- guard let End = patternStr. range ( of: " }} " ) else {
811
+ patternStr = patternStr. substring ( from: patternStr. index ( patternStr. startIndex, offsetBy: 2 ) )
812
+ guard let end = self . findRegexVarEnd ( patternStr, brackets: ( open: " { " , close: " } " ) , terminator: " }} " ) else {
812
813
let loc = CheckLoc . inBuffer ( pattern. baseAddress!, buf)
813
814
diagnose ( . error, loc, " found start of regex string with no end '}}' " )
814
815
return true
@@ -821,22 +822,15 @@ private class Pattern {
821
822
regExPattern += " ( "
822
823
curParen += 1
823
824
824
- let substr = patternStr. substring (
825
- with: Range < String . Index > (
826
- uncheckedBounds: (
827
- patternStr. index ( patternStr. startIndex, offsetBy: 2 ) ,
828
- End . lowerBound
829
- )
830
- )
831
- )
825
+ let substr = patternStr. substring ( to: end)
832
826
let ( res, paren) = self . addRegExToRegEx ( substr, curParen)
833
827
curParen = paren
834
828
if res {
835
829
return true
836
830
}
837
831
regExPattern += " ) "
838
832
839
- patternStr = patternStr. substring ( from: patternStr. index ( End . lowerBound , offsetBy: 2 ) )
833
+ patternStr = patternStr. substring ( from: patternStr. index ( end , offsetBy: 2 ) )
840
834
continue
841
835
}
842
836
@@ -849,7 +843,7 @@ private class Pattern {
849
843
// Find the closing bracket pair ending the match. End is going to be an
850
844
// offset relative to the beginning of the match string.
851
845
let regVar = patternStr. substring ( from: patternStr. index ( patternStr. startIndex, offsetBy: 2 ) )
852
- guard let end = self . findRegexVarEnd ( regVar) else {
846
+ guard let end = self . findRegexVarEnd ( regVar, brackets : ( open : " [ " , close : " ] " ) , terminator : " ]] " ) else {
853
847
let loc = CheckLoc . inBuffer ( pattern. baseAddress!, buf)
854
848
diagnose ( . error, loc, " invalid named regex reference, no ]] found " )
855
849
return true
0 commit comments