diff --git a/Documentation/Evolution/StringProcessingAlgorithms.md b/Documentation/Evolution/StringProcessingAlgorithms.md index b976c562e..8680ff75a 100644 --- a/Documentation/Evolution/StringProcessingAlgorithms.md +++ b/Documentation/Evolution/StringProcessingAlgorithms.md @@ -511,48 +511,48 @@ extension RangeReplaceableCollection where SubSequence == Substring { /// the given regex are replaced by another regex match. /// - Parameters: /// - regex: A regex describing the sequence to replace. - /// - replacement: A closure that receives the full match information, - /// including captures, and returns a replacement collection. /// - subrange: The range in the collection in which to search for `regex`. /// - maxReplacements: A number specifying how many occurrences of the /// sequence matching `regex` to replace. Default is `Int.max`. + /// - replacement: A closure that receives the full match information, + /// including captures, and returns a replacement collection. /// - Returns: A new collection in which all occurrences of subsequence /// matching `regex` are replaced by `replacement`. public func replacing( _ regex: R, - with replacement: (RegexMatch) throws -> Replacement, subrange: Range, - maxReplacements: Int = .max + maxReplacements: Int = .max, + with replacement: (RegexMatch) throws -> Replacement ) rethrows -> Self where Replacement.Element == Element /// Returns a new collection in which all occurrences of a sequence matching /// the given regex are replaced by another collection. /// - Parameters: /// - regex: A regex describing the sequence to replace. - /// - replacement: A closure that receives the full match information, - /// including captures, and returns a replacement collection. /// - maxReplacements: A number specifying how many occurrences of the /// sequence matching `regex` to replace. Default is `Int.max`. + /// - replacement: A closure that receives the full match information, + /// including captures, and returns a replacement collection. /// - Returns: A new collection in which all occurrences of subsequence /// matching `regex` are replaced by `replacement`. public func replacing( _ regex: R, - with replacement: (RegexMatch) throws -> Replacement, - maxReplacements: Int = .max + maxReplacements: Int = .max, + with replacement: (RegexMatch) throws -> Replacement ) rethrows -> Self where Replacement.Element == Element /// Replaces all occurrences of the sequence matching the given regex with /// a given collection. /// - Parameters: /// - regex: A regex describing the sequence to replace. - /// - replacement: A closure that receives the full match information, - /// including captures, and returns a replacement collection. /// - maxReplacements: A number specifying how many occurrences of the /// sequence matching `regex` to replace. Default is `Int.max`. + /// - replacement: A closure that receives the full match information, + /// including captures, and returns a replacement collection. public mutating func replace( _ regex: R, - with replacement: (RegexMatch) throws -> Replacement, - maxReplacements: Int = .max + maxReplacements: Int = .max, + with replacement: (RegexMatch) throws -> Replacement ) rethrows where Replacement.Element == Element } ``` diff --git a/Sources/_StringProcessing/Algorithms/Matching/MatchReplace.swift b/Sources/_StringProcessing/Algorithms/Matching/MatchReplace.swift index 09e021a29..206d68554 100644 --- a/Sources/_StringProcessing/Algorithms/Matching/MatchReplace.swift +++ b/Sources/_StringProcessing/Algorithms/Matching/MatchReplace.swift @@ -118,19 +118,19 @@ extension RangeReplaceableCollection where SubSequence == Substring { /// the given regex are replaced by another regex match. /// - Parameters: /// - regex: A regex describing the sequence to replace. - /// - replacement: A closure that receives the full match information, - /// including captures, and returns a replacement collection. /// - subrange: The range in the collection in which to search for `regex`. /// - maxReplacements: A number specifying how many occurrences of the /// sequence matching `regex` to replace. Default is `Int.max`. + /// - replacement: A closure that receives the full match information, + /// including captures, and returns a replacement collection. /// - Returns: A new collection in which all occurrences of subsequence /// matching `regex` are replaced by `replacement`. @available(SwiftStdlib 5.7, *) public func replacing( _ regex: R, - with replacement: (Regex.Match) throws -> Replacement, subrange: Range, - maxReplacements: Int = .max + maxReplacements: Int = .max, + with replacement: (Regex.Match) throws -> Replacement ) rethrows -> Self where Replacement.Element == Element { precondition(maxReplacements >= 0) @@ -155,43 +155,43 @@ extension RangeReplaceableCollection where SubSequence == Substring { /// the given regex are replaced by another collection. /// - Parameters: /// - regex: A regex describing the sequence to replace. - /// - replacement: A closure that receives the full match information, - /// including captures, and returns a replacement collection. /// - maxReplacements: A number specifying how many occurrences of the /// sequence matching `regex` to replace. Default is `Int.max`. + /// - replacement: A closure that receives the full match information, + /// including captures, and returns a replacement collection. /// - Returns: A new collection in which all occurrences of subsequence /// matching `regex` are replaced by `replacement`. @available(SwiftStdlib 5.7, *) public func replacing( _ regex: R, - with replacement: (Regex.Match) throws -> Replacement, - maxReplacements: Int = .max + maxReplacements: Int = .max, + with replacement: (Regex.Match) throws -> Replacement ) rethrows -> Self where Replacement.Element == Element { try replacing( regex, - with: replacement, subrange: startIndex..( _ regex: R, - with replacement: (Regex.Match) throws -> Replacement, - maxReplacements: Int = .max + maxReplacements: Int = .max, + with replacement: (Regex.Match) throws -> Replacement ) rethrows where Replacement.Element == Element { self = try replacing( regex, - with: replacement, subrange: startIndex..( + _ regex: R, + input: String, + _ replace: (Regex.Match) -> String, + _ tests: (subrange: Range, maxReplacement: Int, result: String)..., + file: StaticString = #file, + line: UInt = #line + ) { + for (subrange, maxReplacement, result) in tests { + XCTAssertEqual(input.replacing(regex, subrange: subrange, maxReplacements: maxReplacement, with: replace), result, file: file, line: line) + } + } + + let int = Capture(OneOrMore(.digit)) { Int($0)! } + + let addition = "9+16, 0+3, 5+5, 99+1" + + replaceTest( + Regex { int; "+"; int }, + input: "9+16, 0+3, 5+5, 99+1", + { match in "\(match.output.1 + match.output.2)" }, + + (subrange: addition.startIndex..