forked from swiftlang/swift-experimental-string-processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebug.swift
138 lines (126 loc) · 3.49 KB
/
Debug.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import Foundation
protocol Debug {
func debug()
}
extension Debug {
var maxStringLengthForPrint: Int { 1000 }
var maxMatchCountForPrint: Int { 100 }
}
extension Benchmark {
func debug() {
switch type {
case .whole:
let result = target.wholeMatch(of: regex)
if let match = result {
if match.0.count > maxStringLengthForPrint {
print("- Match: len = \(match.0.count)")
} else {
print("- Match: \(match.0)")
}
} else {
print("- Warning: No match found")
}
case .allMatches:
let results = target.matches(of: regex)
if results.isEmpty {
print("- Warning: No matches")
return
}
print("- Total matches: \(results.count)")
if results.count > maxMatchCountForPrint {
print("# Too many matches, not printing")
let avgLen = results.map({result in String(target[result.range]).count})
.reduce(0.0, {$0 + Double($1)}) / Double(results.count)
print("Average match length = \(avgLen)")
print("First match = \(String(target[results[0].range]))")
return
}
for match in results {
if match.0.count > maxStringLengthForPrint {
print("- Match: len = \(match.0.count)")
} else {
print("- Match: \(match.0)")
}
}
case .first:
let result = target.firstMatch(of: regex)
if let match = result {
if match.0.count > maxStringLengthForPrint {
print("- Match: len = \(match.0.count)")
} else {
print("- Match: \(match.0)")
}
} else {
print("- Warning: No match found")
return
}
}
}
}
extension NSBenchmark {
func debug() {
switch type {
case .allMatches:
let results = regex.matches(in: target, range: range)
if results.isEmpty {
print("- Warning: No matches")
return
}
print("- Total matches: \(results.count)")
if results.count > maxMatchCountForPrint {
print("# Too many matches, not printing")
return
}
for m in results {
if m.range.length > maxStringLengthForPrint {
print("- Match: len = \(m.range.length)")
} else {
print("- Match: \(target[Range(m.range, in: target)!])")
}
}
case .first:
let result = regex.firstMatch(in: target, range: range)
if let match = result {
if match.range.length > maxStringLengthForPrint {
print("- Match: len = \(match.range.length)")
} else {
print("- Match: \(target[Range(match.range, in: target)!])")
}
} else {
print("- Warning: No match found")
return
}
}
}
}
extension InputListBenchmark {
func debug() {
var matched = 0
var failed = 0
for target in targets {
if target.wholeMatch(of: regex) != nil {
matched += 1
} else {
failed += 1
}
}
print("- Matched \(matched) elements of the input set")
print("- Failed to match \(failed) elements of the input set")
}
}
extension InputListNSBenchmark {
func debug() {
var matched = 0
var failed = 0
for target in targets {
let range = range(in: target)
if regex.firstMatch(in: target, range: range) != nil {
matched += 1
} else {
failed += 1
}
}
print("- Matched \(matched) elements of the input set")
print("- Failed to match \(failed) elements of the input set")
}
}