Skip to content

Commit

Permalink
Merge pull request #69 from sharplet/reranged
Browse files Browse the repository at this point in the history
Replace usage of String.UTF16View with built in range conversions
  • Loading branch information
sharplet authored Apr 21, 2019
2 parents 946c43d + fde1875 commit a819aed
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 111 deletions.
1 change: 0 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ disabled_rules:
excluded:
- .build
- Carthage
- Source/Regex/Memo.swift
- Tests/*/XCTestManifests.swift
identifier_name:
excluded:
Expand Down
10 changes: 0 additions & 10 deletions Regex.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
4A698B931E130A6C006951B2 /* OptionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A698B871E130A60006951B2 /* OptionsTests.swift */; };
4A698B961E130A6D006951B2 /* OptionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A698B871E130A60006951B2 /* OptionsTests.swift */; };
4A698B991E130A6E006951B2 /* OptionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A698B871E130A60006951B2 /* OptionsTests.swift */; };
4AC489011F34E7AC00A84B71 /* Memo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AC489001F34E7AC00A84B71 /* Memo.swift */; };
4AC489021F34E7CC00A84B71 /* Memo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AC489001F34E7AC00A84B71 /* Memo.swift */; };
4AC489031F34E7CD00A84B71 /* Memo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AC489001F34E7AC00A84B71 /* Memo.swift */; };
4AC489041F34E7CD00A84B71 /* Memo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AC489001F34E7AC00A84B71 /* Memo.swift */; };
904DE097222C8DCE00F491FA /* StringReplacementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A698B891E130A60006951B2 /* StringReplacementTests.swift */; };
904DE098222C8DCF00F491FA /* StringReplacementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A698B891E130A60006951B2 /* StringReplacementTests.swift */; };
904DE099222C8DCF00F491FA /* StringReplacementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A698B891E130A60006951B2 /* StringReplacementTests.swift */; };
Expand Down Expand Up @@ -100,7 +96,6 @@
4A8249721EF0444A00002E42 /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Quick.framework; path = Carthage/Build/Mac/Quick.framework; sourceTree = "<group>"; };
4A8249751EF0445600002E42 /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Nimble.framework; path = Carthage/Build/tvOS/Nimble.framework; sourceTree = "<group>"; };
4A8249761EF0445600002E42 /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Quick.framework; path = Carthage/Build/tvOS/Quick.framework; sourceTree = "<group>"; };
4AC489001F34E7AC00A84B71 /* Memo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Memo.swift; sourceTree = "<group>"; };
CE26D9ED1C34D96A003CFB54 /* ThreadLocal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThreadLocal.swift; sourceTree = "<group>"; };
CE4BE2961C1682CF005E4D4F /* Options.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Options.swift; sourceTree = "<group>"; };
CE6CDF601BE239DE00AE6393 /* Regex.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Regex.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -206,7 +201,6 @@
children = (
CE6CDF851BE2C64D00AE6393 /* Foundation+Ranges.swift */,
CE6CDF831BE2C52600AE6393 /* MatchResult.swift */,
4AC489001F34E7AC00A84B71 /* Memo.swift */,
CE4BE2961C1682CF005E4D4F /* Options.swift */,
CE6CDF811BE23BE800AE6393 /* Regex.swift */,
CEDA44A01C45045B00B78CAF /* String+ReplaceMatching.swift */,
Expand Down Expand Up @@ -561,7 +555,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4AC489011F34E7AC00A84B71 /* Memo.swift in Sources */,
CE4BE2971C1682CF005E4D4F /* Options.swift in Sources */,
CE6CDF841BE2C52600AE6393 /* MatchResult.swift in Sources */,
CEDA44A11C45045B00B78CAF /* String+ReplaceMatching.swift in Sources */,
Expand All @@ -588,7 +581,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4AC489021F34E7CC00A84B71 /* Memo.swift in Sources */,
CE4BE2981C168507005E4D4F /* Options.swift in Sources */,
CEC768801BE99B690066BA1E /* Regex.swift in Sources */,
CEDA44A21C45045B00B78CAF /* String+ReplaceMatching.swift in Sources */,
Expand All @@ -615,7 +607,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4AC489031F34E7CD00A84B71 /* Memo.swift in Sources */,
CED611CB1C2A28CB008D212A /* Regex.swift in Sources */,
CED611CC1C2A28CB008D212A /* MatchResult.swift in Sources */,
CEDA44A31C45045B00B78CAF /* String+ReplaceMatching.swift in Sources */,
Expand All @@ -629,7 +620,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4AC489041F34E7CD00A84B71 /* Memo.swift in Sources */,
CED611ED1C2A2CC4008D212A /* Regex.swift in Sources */,
CED611EE1C2A2CC4008D212A /* MatchResult.swift in Sources */,
CEDA44A41C45045B00B78CAF /* String+ReplaceMatching.swift in Sources */,
Expand Down
44 changes: 12 additions & 32 deletions Source/Regex/MatchResult.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Foundation
import class Foundation.NSTextCheckingResult

/// A `MatchResult` encapsulates the result of a single match in a string,
/// providing access to the matched string, as well as any capture groups within
Expand All @@ -25,7 +25,7 @@ public struct MatchResult {

/// The range of the matched string.
public var range: Range<String.Index> {
return _string.range(from: _result.range)
return _result.range
}

/// The matching string for each capture group in the regular expression
Expand All @@ -50,7 +50,7 @@ public struct MatchResult {
///
/// - seealso: The discussion and example for `MatchResult.captures`.
public var captureRanges: [Range<String.Index>?] {
return _captureRanges.value
return _result.captureRanges
}

// MARK: Internal initialisers
Expand All @@ -59,24 +59,10 @@ public struct MatchResult {
return _result.result
}

private let _captureRanges: Memo<[Range<String.Index>?]>
private let _result: _MatchResult
private let _string: String

internal init(_ string: String, _ result: NSTextCheckingResult) {
self._result = _MatchResult(string, result)
self._string = string
self._captureRanges = Memo { [_result] in
_result.captureRanges.map { utf16range in
utf16range.map { string.range(from: $0) }
}
}
}
}

private extension String {
func range(from utf16Range: Range<UTF16View.Index>) -> Range<Index> {
return utf16Range
}
}

Expand All @@ -90,28 +76,22 @@ private final class _MatchResult {
self.result = result
}

lazy var range: Range<String.UTF16View.Index> = {
self.utf16Range(from: self.result.range)!
lazy var range: Range<String.Index> = {
Range(self.result.range, in: string)!
}()

lazy var captures: [String?] = {
self.captureRanges.map { $0.map(self.substring(from:)) }
self.captureRanges.map { range in
range.map { String(self.string[$0]) }
}
}()

lazy var captureRanges: [Range<String.UTF16View.Index>?] = {
self.result.ranges.dropFirst().map(self.utf16Range(from:))
lazy var captureRanges: [Range<String.Index>?] = {
self.result.ranges.dropFirst().map { Range($0, in: self.string) }
}()

lazy var matchedString: String = {
let range = self.utf16Range(from: self.result.range)!
return self.substring(from: range)
let range = Range(self.result.range, in: self.string)!
return String(self.string[range])
}()

private func utf16Range(from range: NSRange) -> Range<String.UTF16View.Index>? {
return Range(range, in: string)
}

private func substring(from range: Range<String.UTF16View.Index>) -> String {
return String(describing: string[range])
}
}
68 changes: 0 additions & 68 deletions Source/Regex/Memo.swift

This file was deleted.

0 comments on commit a819aed

Please sign in to comment.