Skip to content

Commit cc33816

Browse files
committed
refactor lazy structure to using Lazy List Directly
1 parent 3960781 commit cc33816

File tree

9 files changed

+26
-94
lines changed

9 files changed

+26
-94
lines changed

AGString.xcodeproj/project.pbxproj

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@
5353
FF4F190C236F126000A0CD30 /* Utility.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF4F190B236F126000A0CD30 /* Utility.swift */; };
5454
FF5EFC83236EF301005134D1 /* StringIndex.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF5EFC82236EF301005134D1 /* StringIndex.swift */; };
5555
FF6C86D22372D77B004C83A0 /* String+NSRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF6C86D12372D77B004C83A0 /* String+NSRange.swift */; };
56-
FFEFD69E23796B2100B17B4A /* AGMatchListProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEFD69D23796B2100B17B4A /* AGMatchListProtocol.swift */; };
5756
FFEFD6A023796E4200B17B4A /* AGMatchLazyList.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEFD69F23796E4200B17B4A /* AGMatchLazyList.swift */; };
5857
FFEFD6A223796EDE00B17B4A /* AGMatchLazyListIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEFD6A123796EDE00B17B4A /* AGMatchLazyListIterator.swift */; };
59-
FFEFD6A42379788000B17B4A /* AGMatchListIteratorProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEFD6A32379788000B17B4A /* AGMatchListIteratorProtocol.swift */; };
6058
/* End PBXBuildFile section */
6159

6260
/* Begin PBXContainerItemProxy section */
@@ -142,10 +140,8 @@
142140
FF4F190B236F126000A0CD30 /* Utility.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utility.swift; sourceTree = "<group>"; };
143141
FF5EFC82236EF301005134D1 /* StringIndex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringIndex.swift; sourceTree = "<group>"; };
144142
FF6C86D12372D77B004C83A0 /* String+NSRange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+NSRange.swift"; sourceTree = "<group>"; };
145-
FFEFD69D23796B2100B17B4A /* AGMatchListProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AGMatchListProtocol.swift; sourceTree = "<group>"; };
146143
FFEFD69F23796E4200B17B4A /* AGMatchLazyList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AGMatchLazyList.swift; sourceTree = "<group>"; };
147144
FFEFD6A123796EDE00B17B4A /* AGMatchLazyListIterator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AGMatchLazyListIterator.swift; sourceTree = "<group>"; };
148-
FFEFD6A32379788000B17B4A /* AGMatchListIteratorProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AGMatchListIteratorProtocol.swift; sourceTree = "<group>"; };
149145
/* End PBXFileReference section */
150146

151147
/* Begin PBXFrameworksBuildPhase section */
@@ -315,10 +311,8 @@
315311
BDDA016123703F820001AF7C /* AGMatchListIterator.swift */,
316312
FF6C86D12372D77B004C83A0 /* String+NSRange.swift */,
317313
FF2593D9237827DE003C3B1F /* AGMatchList.swift */,
318-
FFEFD69D23796B2100B17B4A /* AGMatchListProtocol.swift */,
319314
FFEFD69F23796E4200B17B4A /* AGMatchLazyList.swift */,
320315
FFEFD6A123796EDE00B17B4A /* AGMatchLazyListIterator.swift */,
321-
FFEFD6A32379788000B17B4A /* AGMatchListIteratorProtocol.swift */,
322316
);
323317
path = Regex;
324318
sourceTree = "<group>";
@@ -716,8 +710,6 @@
716710
FFEFD6A223796EDE00B17B4A /* AGMatchLazyListIterator.swift in Sources */,
717711
3D9C42A222745900000A6585 /* AGString.swift in Sources */,
718712
FF2593DA237827DE003C3B1F /* AGMatchList.swift in Sources */,
719-
FFEFD6A42379788000B17B4A /* AGMatchListIteratorProtocol.swift in Sources */,
720-
FFEFD69E23796B2100B17B4A /* AGMatchListProtocol.swift in Sources */,
721713
FF6C86D22372D77B004C83A0 /* String+NSRange.swift in Sources */,
722714
BDB3A5A2236EF1B30041D167 /* AGRegex.swift in Sources */,
723715
FFEFD6A023796E4200B17B4A /* AGMatchLazyList.swift in Sources */,

Sources/Regex/AGMatchLazyList.swift

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
import Foundation
1010

11-
public struct AGMatchLazyList: AGMatchListProtocol {
11+
public struct AGMatchLazyList: Equatable, Sequence {
1212
public typealias Iterator = AGMatchLazyListIterator
13+
public typealias Element = AGMatch
1314

1415
public let baseString: String
1516
private let regex: NSRegularExpression
@@ -19,37 +20,6 @@ public struct AGMatchLazyList: AGMatchListProtocol {
1920
self.regex = regex
2021
}
2122

22-
public var first: AGMatch? {
23-
let matched = regex.firstMatch(in:
24-
baseString,
25-
options: [],
26-
range: NSRange(location: 0, length: baseString.count))
27-
28-
return matched.map {
29-
var group: [String] = []
30-
31-
for index in 0 ..< $0.numberOfRanges {
32-
group.append(baseString[$0.range(at: index)])
33-
}
34-
35-
return AGMatch(start: $0.range.lowerBound,
36-
end: $0.range.upperBound,
37-
base: baseString,
38-
groups: group)
39-
}
40-
}
41-
42-
public var last: AGMatch? {
43-
var iterator = self.makeIterator()
44-
var result: AGMatch?
45-
46-
while let temp = iterator.next() {
47-
result = temp
48-
}
49-
50-
return result
51-
}
52-
5323
public __consuming func makeIterator() -> AGMatchLazyList.Iterator {
5424
return AGMatchLazyListIterator(withBase: baseString,
5525
regex: self.regex)

Sources/Regex/AGMatchLazyListIterator.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
import Foundation
1010

11-
public struct AGMatchLazyListIterator: AGMatchListIteratorProtocol {
12-
11+
public struct AGMatchLazyListIterator: IteratorProtocol {
12+
public typealias Element = AGMatch
13+
1314
private let baseString: String
1415
private let regex: NSRegularExpression
1516
private var offset: Int = 0

Sources/Regex/AGMatchList.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
import Foundation
1010

11-
public struct AGMatchList: AGMatchListProtocol {
11+
public struct AGMatchList: Equatable, Sequence {
1212
public typealias Iterator = AGMatchListIterator
13+
public typealias Element = AGMatch
1314

1415
public let baseString: String
1516
private let list: [Element]

Sources/Regex/AGMatchListIterator.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
import Foundation
1010

11-
public struct AGMatchListIterator: AGMatchListIteratorProtocol {
12-
11+
public struct AGMatchListIterator: IteratorProtocol {
12+
public typealias Element = AGMatch
13+
1314
var iterator: IndexingIterator<[AGMatch]>
1415
public mutating func next() -> AGMatch? {
1516
return iterator.next()

Sources/Regex/AGMatchListIteratorProtocol.swift

Lines changed: 0 additions & 13 deletions
This file was deleted.

Sources/Regex/AGMatchListProtocol.swift

Lines changed: 0 additions & 16 deletions
This file was deleted.

Sources/Regex/AGRegex.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ public class AGRegex {
2020
extension AGRegex {
2121

2222
public func matchAll(_ str: String,
23-
options: NSRegularExpression.MatchingOptions = [] ) -> AGMatchList {
24-
23+
options: NSRegularExpression.MatchingOptions = [])
24+
-> AGMatchList {
2525
let matched = regex.matches(
2626
in: str,
2727
options: options,
2828
range: NSRange(location: 0, length: str.count))
29+
let mapped: [AGMatch] = matched.map {
30+
var group: [String] = []
2931

3032
for index in 0 ..< $0.numberOfRanges {
3133
group.append(str[$0.range(at: index)])
@@ -37,10 +39,6 @@ extension AGRegex {
3739
groups: group)
3840
}
3941

40-
return AGMatch(start: $0.range.lowerBound, end: $0.range.upperBound,
41-
base: str, groups: group)
42-
}
43-
4442
return AGMatchList(base: str, matching: mapped)
4543
}
4644

@@ -61,7 +59,9 @@ extension AGRegex {
6159
return result
6260
}
6361

64-
public func getIterator(_ str: String) -> AGMatchLazyListIterator {
65-
return AGMatchLazyList(withBase: str, regex: self.regex).makeIterator()
62+
public func matchAllLazliy (_ str: String,
63+
option: NSRegularExpression.MatchingOptions = [])
64+
-> AGMatchLazyList {
65+
return AGMatchLazyList(withBase: str, regex: self.regex)
6666
}
6767
}

Tests/AGRegexTest.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class AGRegexTest: XCTestCase {
6161
XCTAssertEqual(actual, expect)
6262
}
6363

64-
func testFindIter() {
64+
func testLazyMatch() {
6565
let r = try! NSRegularExpression(pattern: "([A-Z]+)([0-9]+)", options: [])
6666
let regex = AGRegex(r)
6767
let str = "ABC12DEF3G56HIJ7"
@@ -73,18 +73,14 @@ class AGRegexTest: XCTestCase {
7373
"56 * G",
7474
"7 * HIJ"
7575
]
76-
77-
var testCount = 0
78-
var iterator = regex.getIterator(str)
79-
var index = 0
80-
while let m = iterator.next() {
76+
77+
var actuals: [String] = []
78+
79+
for m in regex.matchAllLazliy(str) {
8180
let actual = "\(m.group(2)) * \(m.group(1))"
82-
let expect = expects[index]
83-
XCTAssertEqual(actual, expect)
84-
testCount += 1
85-
index += 1
81+
actuals.append(actual)
8682
}
87-
88-
XCTAssertEqual(testCount, 4)
83+
84+
XCTAssertEqual(actuals, expects)
8985
}
9086
}

0 commit comments

Comments
 (0)