extension String subscript index Int Character if index return self ad

  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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
extension String {
subscript(index:Int) -> Character {
if index < 0 {
return self[advance(self.endIndex, index)]
}
return self[advance(self.startIndex, index)]
}
func substring(from:Int, to: Int = -1) -> String {
return self[Range<Int>(start: from, end: to)]
}
func substring(from:Int, length: Int) -> String {
return self[Range<Int>(start: from, end: from + length)]
}
func substring(range:NSRange) -> String {
return self[Range<Int>(start: range.location, end: range.location + range.length)]
}
subscript(range:Range<Int>) -> String {
if range.startIndex >= self.length() {
return String()
}
let start = advance(self.startIndex, min(self.length(), range.startIndex))
if range.endIndex < 0 {
return self[start..<self.endIndex]
}
let end = advance(self.startIndex, min(self.length(), range.endIndex))
return self[start..<end]
}
func allIndexesOf(string: String) -> [Int] {
var res:[Int] = []
var substr = self.substring(0)
while let index = substr.firstIndexOf(string) {
res.append(index + (self.length() - substr.length()))
substr = substr.substring(index + string.length())
}
return res
}
func firstIndexOf(string: String) -> Int? {
if let range = self.rangeOfString(string,
options: NSStringCompareOptions.LiteralSearch,
range: self.startIndex..<self.endIndex,
locale: NSLocale.autoupdatingCurrentLocale())
{
return self.unwrapIndex(range.startIndex)
}
return nil
}
func firstIndexOf(charset: NSCharacterSet) -> Int? {
return self.componentsSeparatedByCharactersInSet(charset).first?.length()
}
func lastIndexOf(charset: NSCharacterSet) -> Int? {
return self.componentsSeparatedByCharactersInSet(charset).last?.length()
}
func contains(string: String) -> Bool {
return self.firstIndexOf(string) != nil
}
func contains(charset: NSCharacterSet) -> Bool {
return count(self.componentsSeparatedByCharactersInSet(charset)) > 1
}
func lastIndexOf(string: String) -> Int? {
if let range = self.rangeOfString(string,
options: NSStringCompareOptions.BackwardsSearch | NSStringCompareOptions.LiteralSearch,
range: self.startIndex..<self.endIndex,
locale: NSLocale.autoupdatingCurrentLocale())
{
return self.unwrapIndex(range.startIndex)
}
return nil
}
func startsWith(string: String) -> Bool {
return self.firstIndexOf(string) == 0
}
func startsWith(charset: NSCharacterSet) -> Bool {
if self.length() == 0 {
return false
}
return self.substring(0, to: 1).stringByTrimmingCharactersInSet(charset).length() == 0
}
func endsWith(charset: NSCharacterSet) -> Bool {
if self.length() == 0 {
return false
}
return self.substring(self.length() - 1, to: -1).stringByTrimmingCharactersInSet(charset).length() == 0
}
func endsWith(string: String) -> Bool {
return self.lastIndexOf(string) == self.length() - string.length()
}
func unwrapIndex(index: String.Index) -> Int {
var cursor = self.startIndex
var res:Int = 0
while cursor != index {
cursor = advance(cursor, 1)
res += 1
}
return res
}
func wrapIndex(index: Int) -> String.Index {
var cursor = self.startIndex
return advance(cursor, index)
}
func length() -> Int {
return count(self)
}
// modified from https://gist.github.com/kyro38/50102a47937e9896e4f4
func levenshteinDistance(toString: String) -> Int {
class Array2D {
var cols:Int, rows:Int
var matrix: [Int]
init(cols:Int, rows:Int) {
self.cols = cols
self.rows = rows
matrix = Array(count:cols*rows, repeatedValue:0)
}
subscript(col:Int, row:Int) -> Int {
get {
return matrix[cols * row + col]
}
set {
matrix[cols*row+col] = newValue
}
}
func colCount() -> Int {
return self.cols
}
func rowCount() -> Int {
return self.rows
}
}
let a = Array(self.utf16)
let b = Array(toString.utf16)
var dist = Array2D(cols: a.count + 1, rows: b.count + 1)
for i in 1...a.count {
dist[i, 0] = i
}
for j in 1...b.count {
dist[0, j] = j
}
for i in 1...a.count {
for j in 1...b.count {
if a[i-1] == b[j-1] {
dist[i, j] = dist[i-1, j-1] // noop
} else {
dist[i, j] = min(
dist[i-1, j] + 1, // deletion
dist[i, j-1] + 1, // insertion
dist[i-1, j-1] + 1 // substitution
)
}
}
}
return dist[a.count, b.count]
}
func matchRegex(pattern: Regex) -> Bool {
let range: NSRange = NSMakeRange(0, count(self))
if pattern.regex != nil {
let matches: [AnyObject] = pattern.regex!.matchesInString(self, options: pattern.matchingOptions, range: range)
return matches.count > 0
}
return false
}
func match(patternString: String) -> Bool {
return self.matchRegex(Regex(pattern: patternString))
}
func replaceRegex(pattern: Regex, template: String) -> String {
if self.matchRegex(pattern) {
let range: NSRange = NSMakeRange(0, count(self))
if pattern.regex != nil {
return pattern.regex!.stringByReplacingMatchesInString(self, options: pattern.matchingOptions, range: range, withTemplate: template)
}
}
return self
}
func replace(pattern: String, template: String) -> String {
return self.replaceRegex(Regex(pattern: pattern), template: template)
}
func rangeFromNSRange(nsRange : NSRange) -> Range<String.Index>? {
let utf16start = self.utf16.startIndex
if let from = String.Index(self.utf16.startIndex + nsRange.location, within: self),
let to = String.Index(self.utf16.startIndex + nsRange.location + nsRange.length, within: self) {
return from ..< to
}
return nil
}
}