Skip to content

Commit d3bc38e

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

File tree

9 files changed

+38
-94
lines changed

9 files changed

+38
-94
lines changed

AGString.xcodeproj/project.pbxproj

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@
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 */; };
58+
FFEFD6A5237984DE00B17B4A /* AGMatchLazyListIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEFD6A123796EDE00B17B4A /* AGMatchLazyListIterator.swift */; };
59+
FFEFD6A6237984DE00B17B4A /* AGMatchLazyListIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEFD6A123796EDE00B17B4A /* AGMatchLazyListIterator.swift */; };
60+
FFEFD6A7237984DE00B17B4A /* AGMatchLazyListIterator.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEFD6A123796EDE00B17B4A /* AGMatchLazyListIterator.swift */; };
61+
FFEFD6A8237984E000B17B4A /* AGMatchLazyList.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEFD69F23796E4200B17B4A /* AGMatchLazyList.swift */; };
62+
FFEFD6A9237984E000B17B4A /* AGMatchLazyList.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEFD69F23796E4200B17B4A /* AGMatchLazyList.swift */; };
63+
FFEFD6AA237984E100B17B4A /* AGMatchLazyList.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFEFD69F23796E4200B17B4A /* AGMatchLazyList.swift */; };
6064
/* End PBXBuildFile section */
6165

6266
/* Begin PBXContainerItemProxy section */
@@ -142,10 +146,8 @@
142146
FF4F190B236F126000A0CD30 /* Utility.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utility.swift; sourceTree = "<group>"; };
143147
FF5EFC82236EF301005134D1 /* StringIndex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringIndex.swift; sourceTree = "<group>"; };
144148
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>"; };
146149
FFEFD69F23796E4200B17B4A /* AGMatchLazyList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AGMatchLazyList.swift; sourceTree = "<group>"; };
147150
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>"; };
149151
/* End PBXFileReference section */
150152

151153
/* Begin PBXFrameworksBuildPhase section */
@@ -315,10 +317,8 @@
315317
BDDA016123703F820001AF7C /* AGMatchListIterator.swift */,
316318
FF6C86D12372D77B004C83A0 /* String+NSRange.swift */,
317319
FF2593D9237827DE003C3B1F /* AGMatchList.swift */,
318-
FFEFD69D23796B2100B17B4A /* AGMatchListProtocol.swift */,
319320
FFEFD69F23796E4200B17B4A /* AGMatchLazyList.swift */,
320321
FFEFD6A123796EDE00B17B4A /* AGMatchLazyListIterator.swift */,
321-
FFEFD6A32379788000B17B4A /* AGMatchListIteratorProtocol.swift */,
322322
);
323323
path = Regex;
324324
sourceTree = "<group>";
@@ -716,8 +716,6 @@
716716
FFEFD6A223796EDE00B17B4A /* AGMatchLazyListIterator.swift in Sources */,
717717
3D9C42A222745900000A6585 /* AGString.swift in Sources */,
718718
FF2593DA237827DE003C3B1F /* AGMatchList.swift in Sources */,
719-
FFEFD6A42379788000B17B4A /* AGMatchListIteratorProtocol.swift in Sources */,
720-
FFEFD69E23796B2100B17B4A /* AGMatchListProtocol.swift in Sources */,
721719
FF6C86D22372D77B004C83A0 /* String+NSRange.swift in Sources */,
722720
BDB3A5A2236EF1B30041D167 /* AGRegex.swift in Sources */,
723721
FFEFD6A023796E4200B17B4A /* AGMatchLazyList.swift in Sources */,
@@ -729,13 +727,15 @@
729727
buildActionMask = 2147483647;
730728
files = (
731729
2FBE39ED2377C7B700D65251 /* AGRegex.swift in Sources */,
730+
FFEFD6A5237984DE00B17B4A /* AGMatchLazyListIterator.swift in Sources */,
732731
2FBE39EE2377C7B700D65251 /* AGMatch.swift in Sources */,
733732
2FBE39EF2377C7B700D65251 /* AGMatchListIterator.swift in Sources */,
734733
2FBE39F02377C7B700D65251 /* String+NSRange.swift in Sources */,
735734
2FBE39F12377C7B700D65251 /* StringIndex.swift in Sources */,
736735
FF2593DB237827E5003C3B1F /* AGMatchList.swift in Sources */,
737736
2FBE39F22377C7B700D65251 /* Utility.swift in Sources */,
738737
3D9C42B1227459C1000A6585 /* AGString.swift in Sources */,
738+
FFEFD6A8237984E000B17B4A /* AGMatchLazyList.swift in Sources */,
739739
);
740740
runOnlyForDeploymentPostprocessing = 0;
741741
};
@@ -744,13 +744,15 @@
744744
buildActionMask = 2147483647;
745745
files = (
746746
2FBE39F32377C7C000D65251 /* AGRegex.swift in Sources */,
747+
FFEFD6A6237984DE00B17B4A /* AGMatchLazyListIterator.swift in Sources */,
747748
2FBE39F42377C7C000D65251 /* AGMatch.swift in Sources */,
748749
2FBE39F52377C7C000D65251 /* AGMatchListIterator.swift in Sources */,
749750
2FBE39F62377C7C000D65251 /* String+NSRange.swift in Sources */,
750751
2FBE39F72377C7C000D65251 /* StringIndex.swift in Sources */,
751752
FF2593DC237827E5003C3B1F /* AGMatchList.swift in Sources */,
752753
2FBE39F82377C7C000D65251 /* Utility.swift in Sources */,
753754
3D9C42BF227459F6000A6585 /* AGString.swift in Sources */,
755+
FFEFD6A9237984E000B17B4A /* AGMatchLazyList.swift in Sources */,
754756
);
755757
runOnlyForDeploymentPostprocessing = 0;
756758
};
@@ -759,13 +761,15 @@
759761
buildActionMask = 2147483647;
760762
files = (
761763
2FBE39F92377C7C600D65251 /* AGRegex.swift in Sources */,
764+
FFEFD6A7237984DE00B17B4A /* AGMatchLazyListIterator.swift in Sources */,
762765
2FBE39FA2377C7C600D65251 /* AGMatch.swift in Sources */,
763766
2FBE39FB2377C7C600D65251 /* AGMatchListIterator.swift in Sources */,
764767
2FBE39FC2377C7C600D65251 /* String+NSRange.swift in Sources */,
765768
2FBE39FD2377C7C600D65251 /* StringIndex.swift in Sources */,
766769
FF2593DD237827E5003C3B1F /* AGMatchList.swift in Sources */,
767770
2FBE39FE2377C7C600D65251 /* Utility.swift in Sources */,
768771
3D9C42CD22745A28000A6585 /* AGString.swift in Sources */,
772+
FFEFD6AA237984E100B17B4A /* AGMatchLazyList.swift in Sources */,
769773
);
770774
runOnlyForDeploymentPostprocessing = 0;
771775
};

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)