Skip to content

Commit e94ecea

Browse files
authored
Merge pull request swiftlang#582 from Azoy/last-fixes-57
[5.7] Fix API naming mismatches from proposals
2 parents 8087a3d + df5c9ad commit e94ecea

File tree

4 files changed

+62
-33
lines changed

4 files changed

+62
-33
lines changed

Sources/RegexBuilder/Anchor.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ extension Anchor {
118118
///
119119
/// This anchor is equivalent to `^` in regex syntax when the `m` option
120120
/// has been enabled or `anchorsMatchLineEndings(true)` has been called.
121+
///
122+
/// For example, the following regexes are all equivalent:
123+
///
124+
/// - `Regex { Anchor.startOfLine }`
125+
/// - `/(?m)^/` or `/(?m:^)/`
126+
/// - `/^/.anchorsMatchLineEndings(true)`
121127
public static var startOfLine: Anchor {
122128
Anchor(kind: .startOfLine)
123129
}
@@ -127,6 +133,12 @@ extension Anchor {
127133
///
128134
/// This anchor is equivalent to `$` in regex syntax when the `m` option
129135
/// has been enabled or `anchorsMatchLineEndings(true)` has been called.
136+
///
137+
/// For example, the following regexes are all equivalent:
138+
///
139+
/// - `Regex { Anchor.endOfLine }`
140+
/// - `/(?m)$/` or `/(?m:$)/`
141+
/// - `/$/.anchorsMatchLineEndings(true)`
130142
public static var endOfLine: Anchor {
131143
Anchor(kind: .endOfLine)
132144
}

Sources/_StringProcessing/Regex/Options.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@_implementationOnly import _RegexParser
1313

1414
@available(SwiftStdlib 5.7, *)
15-
extension RegexComponent {
15+
extension Regex {
1616
/// Returns a regular expression that ignores case when matching.
1717
///
1818
/// - Parameter ignoresCase: A Boolean value indicating whether to ignore case.
@@ -65,7 +65,7 @@ extension RegexComponent {
6565
/// - Parameter wordBoundaryKind: The algorithm to use for determining word boundaries.
6666
/// - Returns: The modified regular expression.
6767
public func wordBoundaryKind(_ wordBoundaryKind: RegexWordBoundaryKind) -> Regex<RegexOutput> {
68-
wrapInOption(.unicodeWordBoundaries, addingIf: wordBoundaryKind == .unicodeLevel2)
68+
wrapInOption(.unicodeWordBoundaries, addingIf: wordBoundaryKind == .default)
6969
}
7070

7171
/// Returns a regular expression where the start and end of input
@@ -83,8 +83,8 @@ extension RegexComponent {
8383
///
8484
/// This method corresponds to applying the `m` option in regex syntax. For
8585
/// this behavior in the `RegexBuilder` syntax, see
86-
/// ``Anchor.startOfLine``, ``Anchor.endOfLine``, ``Anchor.startOfInput``,
87-
/// and ``Anchor.endOfInput``.
86+
/// ``Anchor.startOfLine``, ``Anchor.endOfLine``, ``Anchor.startOfSubject``,
87+
/// and ``Anchor.endOfSubject``.
8888
///
8989
/// - Parameter matchLineEndings: A Boolean value indicating whether `^` and
9090
/// `$` should match the start and end of lines, respectively.
@@ -205,7 +205,7 @@ public struct RegexWordBoundaryKind: Hashable {
205205
/// that match `/\w\W/` or `/\W\w/`, or between the start or end of the input
206206
/// and a `\w` character. Word boundaries therefore depend on the option-
207207
/// defined behavior of `\w`.
208-
public static var unicodeLevel1: Self {
208+
public static var simple: Self {
209209
.init(base: .unicodeLevel1)
210210
}
211211

@@ -215,7 +215,7 @@ public struct RegexWordBoundaryKind: Hashable {
215215
/// Default word boundaries use a Unicode algorithm that handles some cases
216216
/// better than simple word boundaries, such as words with internal
217217
/// punctuation, changes in script, and Emoji.
218-
public static var unicodeLevel2: Self {
218+
public static var `default`: Self {
219219
.init(base: .unicodeLevel2)
220220
}
221221
}

Tests/RegexBuilderTests/RegexDSLTests.swift

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,10 @@ class RegexDSLTests: XCTestCase {
467467
("abcabc", "abcabc"),
468468
("abcABCaBc", "abcABCaBc"),
469469
matchType: Substring.self, ==) {
470-
OneOrMore {
471-
"abc"
470+
Regex {
471+
OneOrMore {
472+
"abc"
473+
}
472474
}.ignoresCase(true)
473475
}
474476

@@ -480,8 +482,10 @@ class RegexDSLTests: XCTestCase {
480482
("abcabc", "abcabc"),
481483
("abcABCaBc", "abcABCaBc"),
482484
matchType: Substring.self, ==) {
483-
OneOrMore {
484-
"abc"
485+
Regex {
486+
OneOrMore {
487+
"abc"
488+
}
485489
}
486490
.ignoresCase(true)
487491
.ignoresCase(false)
@@ -497,9 +501,13 @@ class RegexDSLTests: XCTestCase {
497501
("abcabc", "abcabc"),
498502
("abcdeABCdeaBcde", "abcdeABCdeaBcde"),
499503
matchType: Substring.self, ==) {
500-
OneOrMore {
501-
"abc".ignoresCase(true)
502-
Optionally("de")
504+
Regex {
505+
OneOrMore {
506+
Regex {
507+
"abc"
508+
}.ignoresCase(true)
509+
Optionally("de")
510+
}
503511
}
504512
.ignoresCase(false)
505513
}
@@ -536,11 +544,13 @@ class RegexDSLTests: XCTestCase {
536544
"stop"
537545
" "
538546

539-
Capture {
540-
OneOrMore(.word)
541-
Anchor.wordBoundary
542-
}
543-
.wordBoundaryKind(.unicodeLevel1)
547+
Regex {
548+
Capture {
549+
OneOrMore(.word)
550+
Anchor.wordBoundary
551+
}
552+
}.wordBoundaryKind(.simple)
553+
544554
OneOrMore(.any, .reluctant)
545555
"stop"
546556
}
@@ -550,15 +560,17 @@ class RegexDSLTests: XCTestCase {
550560
matchType: (Substring, Substring, Substring).self, ==) {
551561
Capture {
552562
// Reluctant behavior due to option
553-
OneOrMore(.anyOf("abcd"))
554-
.repetitionBehavior(.reluctant)
563+
Regex {
564+
OneOrMore(.anyOf("abcd"))
565+
}.repetitionBehavior(.reluctant)
555566
}
556567
ZeroOrMore("a"..."z")
557568

558569
Capture {
559570
// Eager behavior due to explicit parameter, despite option
560-
OneOrMore(.digit, .eager)
561-
.repetitionBehavior(.reluctant)
571+
Regex {
572+
OneOrMore(.digit, .eager)
573+
}.repetitionBehavior(.reluctant)
562574
}
563575
ZeroOrMore(.digit)
564576
}
@@ -567,10 +579,11 @@ class RegexDSLTests: XCTestCase {
567579
("abcdefg", ("abcdefg", "abcdefg")),
568580
("abcdéfg", ("abcdéfg", "abcd")),
569581
matchType: (Substring, Substring).self, ==) {
570-
Capture {
571-
OneOrMore(.word)
572-
}
573-
.asciiOnlyWordCharacters()
582+
Regex {
583+
Capture {
584+
OneOrMore(.word)
585+
}
586+
}.asciiOnlyWordCharacters()
574587

575588
ZeroOrMore(.any)
576589
}
@@ -601,8 +614,10 @@ class RegexDSLTests: XCTestCase {
601614
("abc1def2", ("abc1def2", "1")),
602615
matchType: (Substring, Substring).self, ==)
603616
{
604-
OneOrMore(.reluctant) {
605-
One(.word)
617+
Regex {
618+
OneOrMore(.reluctant) {
619+
One(.word)
620+
}
606621
}.repetitionBehavior(.possessive)
607622
Capture(.digit)
608623
ZeroOrMore(.any)
@@ -654,8 +669,9 @@ class RegexDSLTests: XCTestCase {
654669
{
655670
Regex {
656671
Capture {
657-
OneOrMore("a")
658-
.repetitionBehavior(.eager)
672+
Regex {
673+
OneOrMore("a")
674+
}.repetitionBehavior(.eager)
659675
}
660676
OneOrMore("a")
661677
}.repetitionBehavior(.possessive)
@@ -667,8 +683,9 @@ class RegexDSLTests: XCTestCase {
667683
{
668684
Regex {
669685
Capture {
670-
OneOrMore("a")
671-
.repetitionBehavior(.reluctant)
686+
Regex {
687+
OneOrMore("a")
688+
}.repetitionBehavior(.reluctant)
672689
}
673690
OneOrMore("a")
674691
}.repetitionBehavior(.possessive)

Tests/RegexTests/UTS18Tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ extension UTS18Tests {
222222
// - Nonspacing marks are never divided from their base characters, and
223223
// otherwise ignored in locating boundaries.
224224
func testSimpleWordBoundaries() {
225-
let simpleWordRegex = regex(#".+?\b"#).wordBoundaryKind(.unicodeLevel1)
225+
let simpleWordRegex = regex(#".+?\b"#).wordBoundaryKind(.simple)
226226
expectFirstMatch(input, simpleWordRegex, input[pos: ..<11])
227227
expectFirstMatch("don't", simpleWordRegex, "don")
228228
expectFirstMatch("Cafe\u{301}", simpleWordRegex, "Café")

0 commit comments

Comments
 (0)