@@ -704,15 +704,15 @@ private class Pattern {
704704 /// \p Str has to point in the beginning of the definition (right after the
705705 /// opening sequence). Returns the offset of the closing sequence within Str,
706706 /// 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 ? {
708708 var string = regVar
709709 // Offset keeps track of the current offset within the input Str
710710 var offset = regVar. startIndex
711711 // [...] Nesting depth
712712 var bracketDepth = 0
713713
714714 while let firstChar = string. characters. first {
715- if string. hasPrefix ( " ]] " ) && bracketDepth == 0 {
715+ if string. hasPrefix ( terminator ) && bracketDepth == 0 {
716716 return offset
717717 }
718718 if firstChar == " \\ " {
@@ -721,11 +721,11 @@ private class Pattern {
721721 offset = regVar. index ( offset, offsetBy: 2 )
722722 } else {
723723 switch firstChar {
724- case " [ " :
724+ case brackets . open :
725725 bracketDepth += 1
726- case " ] " :
726+ case brackets . close :
727727 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 " )
729729 return nil
730730 }
731731 bracketDepth -= 1
@@ -808,7 +808,8 @@ private class Pattern {
808808 // RegEx matches.
809809 if patternStr. range ( of: " {{ " ) ? . lowerBound == patternStr. startIndex {
810810 // 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 {
812813 let loc = CheckLoc . inBuffer ( pattern. baseAddress!, buf)
813814 diagnose ( . error, loc, " found start of regex string with no end '}}' " )
814815 return true
@@ -821,22 +822,15 @@ private class Pattern {
821822 regExPattern += " ( "
822823 curParen += 1
823824
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)
832826 let ( res, paren) = self . addRegExToRegEx ( substr, curParen)
833827 curParen = paren
834828 if res {
835829 return true
836830 }
837831 regExPattern += " ) "
838832
839- patternStr = patternStr. substring ( from: patternStr. index ( End . lowerBound , offsetBy: 2 ) )
833+ patternStr = patternStr. substring ( from: patternStr. index ( end , offsetBy: 2 ) )
840834 continue
841835 }
842836
@@ -849,7 +843,7 @@ private class Pattern {
849843 // Find the closing bracket pair ending the match. End is going to be an
850844 // offset relative to the beginning of the match string.
851845 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 {
853847 let loc = CheckLoc . inBuffer ( pattern. baseAddress!, buf)
854848 diagnose ( . error, loc, " invalid named regex reference, no ]] found " )
855849 return true
0 commit comments