Skip to content

Implement \R, \v, \h for character/scalar modes #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/_StringProcessing/Unicode/ScalarProps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,19 @@ extension Unicode.Script {
return result
}
}

extension UnicodeScalar {
var isHorizontalWhitespace: Bool {
value == 0x09 || properties.generalCategory == .spaceSeparator
}

var isVerticalWhitespace: Bool {
switch value {
case 0x000A...0x000D /* LF ... CR */: return true
case 0x0085 /* NEXT LINE (NEL) */: return true
case 0x2028 /* LINE SEPARATOR */: return true
case 0x2029 /* PARAGRAPH SEPARATOR */: return true
default: return false
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Character has a isNewline. Can we share implementation and/or SPI for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is this the same list?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same list — we should ultimately have this on UnicodeScalar as well; probably with a name to match Character's.

28 changes: 19 additions & 9 deletions Sources/_StringProcessing/_CharacterClassModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,18 @@ public struct _CharacterClassModel: Hashable {
matched = c.isNumber && (c.isASCII || !options.usesASCIIDigits)
case .hexDigit:
matched = c.isHexDigit && (c.isASCII || !options.usesASCIIDigits)
case .horizontalWhitespace: fatalError("Not implemented")
case .newlineSequence:
matched = c.isNewline && (c.isASCII || !options.usesASCIISpaces)
case .verticalWhitespace: fatalError("Not implemented")
case .horizontalWhitespace:
matched = c.unicodeScalars.first?.isHorizontalWhitespace == true
&& (c.isASCII || !options.usesASCIISpaces)
case .newlineSequence, .verticalWhitespace:
matched = c.unicodeScalars.first?.isVerticalWhitespace == true
&& (c.isASCII || !options.usesASCIISpaces)
case .whitespace:
matched = c.isWhitespace && (c.isASCII || !options.usesASCIISpaces)
case .word:
matched = c.isWordCharacter && (c.isASCII || !options.usesASCIIWord)
case .custom(let set): matched = set.any { $0.matches(c, with: options) }
case .custom(let set):
matched = set.any { $0.matches(c, with: options) }
}
if isInverted {
matched.toggle()
Expand All @@ -206,14 +209,21 @@ public struct _CharacterClassModel: Hashable {
matched = c.properties.numericType != nil && (c.isASCII || !options.usesASCIIDigits)
case .hexDigit:
matched = Character(c).isHexDigit && (c.isASCII || !options.usesASCIIDigits)
case .horizontalWhitespace: fatalError("Not implemented")
case .newlineSequence: fatalError("Not implemented")
case .verticalWhitespace: fatalError("Not implemented")
case .horizontalWhitespace:
matched = c.isHorizontalWhitespace && (c.isASCII || !options.usesASCIISpaces)
case .verticalWhitespace:
matched = c.isVerticalWhitespace && (c.isASCII || !options.usesASCIISpaces)
case .newlineSequence:
matched = c.isVerticalWhitespace && (c.isASCII || !options.usesASCIISpaces)
if c == "\r" && nextIndex != str.endIndex && str.unicodeScalars[nextIndex] == "\n" {
str.unicodeScalars.formIndex(after: &nextIndex)
}
case .whitespace:
matched = c.properties.isWhitespace && (c.isASCII || !options.usesASCIISpaces)
case .word:
matched = (c.properties.isAlphabetic || c == "_") && (c.isASCII || !options.usesASCIIWord)
case .custom: fatalError("Not supported")
case .custom(let set):
matched = set.any { $0.matches(Character(c), with: options) }
}
if isInverted {
matched.toggle()
Expand Down
14 changes: 13 additions & 1 deletion Tests/RegexTests/UTS18Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ extension UTS18Tests {
var lines = lineInput.matches(of: regex(#"\d{2}"#))
XCTAssertEqual(lines.count, 11)
// Test \R - newline sequence
lines = lineInput.matches(of: regex(#"\d{2}\R"#))
lines = lineInput.matches(of: regex(#"\d{2}\R^"#).anchorsMatchLineEndings())
XCTAssertEqual(lines.count, 11)
// Test \v - vertical space
lines = lineInput.matches(of: regex(#"\d{2}\v^"#).anchorsMatchLineEndings())
XCTAssertEqual(lines.count, 11)
// Test anchors as line boundaries
lines = lineInput.matches(of: regex(#"^\d{2}$"#).anchorsMatchLineEndings())
Expand All @@ -277,6 +280,15 @@ extension UTS18Tests {
lines = lineInput.matches(of: regex(#".+"#))
XCTAssertEqual(lines.count, 11)

// Unicode scalar semantics - \R still matches all, including \r\n sequence
lines = lineInput.matches(
of: regex(#"\d{2}\R^"#).matchingSemantics(.unicodeScalar).anchorsMatchLineEndings())
XCTAssertEqual(lines.count, 11)
// Unicode scalar semantics - \v matches all except for \r\n sequence
lines = lineInput.matches(
of: regex(#"\d{2}\v^"#).matchingSemantics(.unicodeScalar).anchorsMatchLineEndings())
XCTAssertEqual(lines.count, 10)

// Does not contain an empty line
XCTAssertFalse(lineInput.contains(regex(#"^$"#)))
// Does contain an empty line (between \n and \r, which are reversed here)
Expand Down