forked from carabina/Swift-String-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.swift
More file actions
296 lines (230 loc) · 9.61 KB
/
Copy pathStringExtensions.swift
File metadata and controls
296 lines (230 loc) · 9.61 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// String Tools.swift
// Swift String Tools
//
// Created by Jamal Kharrat on 8/11/14.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
extension String {
//MARK: Helper methods
/**
Returns the length of the string.
- returns: Int length of the string.
*/
var length: Int {
return self.characters.count
}
var objcLength: Int {
return self.utf16.count
}
//MARK: - Linguistics
/**
Returns the langauge of a String
NOTE: String has to be at least 4 characters, otherwise the method will return nil.
- returns: String! Returns a string representing the langague of the string (e.g. en, fr, or und for undefined).
*/
func detectLanguage() -> String? {
if self.length > 4 {
let tagger = NSLinguisticTagger(tagSchemes:[NSLinguisticTagSchemeLanguage], options: 0)
tagger.string = self
return tagger.tagAtIndex(0, scheme: NSLinguisticTagSchemeLanguage, tokenRange: nil, sentenceRange: nil)
}
return nil
}
/**
Returns the script of a String
- returns: String! returns a string representing the script of the String (e.g. Latn, Hans).
*/
func detectScript() -> String? {
if self.length > 1 {
let tagger = NSLinguisticTagger(tagSchemes:[NSLinguisticTagSchemeScript], options: 0)
tagger.string = self
return tagger.tagAtIndex(0, scheme: NSLinguisticTagSchemeScript, tokenRange: nil, sentenceRange: nil)
}
return nil
}
/**
Check the text direction of a given String.
NOTE: String has to be at least 4 characters, otherwise the method will return false.
- returns: Bool The Bool will return true if the string was writting in a right to left langague (e.g. Arabic, Hebrew)
*/
var isRightToLeft : Bool {
let language = self.detectLanguage()
return (language == "ar" || language == "he")
}
//MARK: - Usablity & Social
/**
Check that a String is only made of white spaces, and new line characters.
- returns: Bool
*/
func isOnlyEmptySpacesAndNewLineCharacters() -> Bool {
return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).length == 0
}
/**
Checks if a string is an email address using NSDataDetector.
- returns: Bool
*/
var isEmail: Bool {
let dataDetector = try? NSDataDetector(types: NSTextCheckingType.Link.rawValue)
let firstMatch = dataDetector?.firstMatchInString(self, options: NSMatchingOptions.ReportCompletion, range: NSMakeRange(0, length))
return (firstMatch?.range.location != NSNotFound && firstMatch?.URL?.scheme == "mailto")
}
/**
Check that a String is 'tweetable' can be used in a tweet.
- returns: Bool
*/
func isTweetable() -> Bool {
let tweetLength = 140,
// Each link takes 23 characters in a tweet (assuming all links are https).
linksLength = self.getLinks().count * 23,
remaining = tweetLength - linksLength
if linksLength != 0 {
return remaining < 0
} else {
return !(self.utf16.count > tweetLength || self.utf16.count == 0 || self.isOnlyEmptySpacesAndNewLineCharacters())
}
}
/**
Gets an array of Strings for all links found in a String
- returns: [String]
*/
func getLinks() -> [String] {
let detector = try? NSDataDetector(types: NSTextCheckingType.Link.rawValue)
let links = detector?.matchesInString(self, options: NSMatchingOptions.ReportCompletion, range: NSMakeRange(0, length)).map {$0 }
return links!.filter { link in
return link.URL != nil
}.map { link -> String in
return link.URL!.absoluteString
}
}
/**
Gets an array of URLs for all links found in a String
- returns: [NSURL]
*/
func getURLs() -> [NSURL] {
let detector = try? NSDataDetector(types: NSTextCheckingType.Link.rawValue)
let links = detector?.matchesInString(self, options: NSMatchingOptions.ReportCompletion, range: NSMakeRange(0, length)).map {$0 }
return links!.filter { link in
return link.URL != nil
}.map { link -> NSURL in
return link.URL!
}
}
/**
Gets an array of dates for all dates found in a String
- returns: [NSDate]
*/
func getDates() -> [NSDate] {
let error: NSErrorPointer = NSErrorPointer()
let detector: NSDataDetector?
do {
detector = try NSDataDetector(types: NSTextCheckingType.Date.rawValue)
} catch let error1 as NSError {
error.memory = error1
detector = nil
}
let dates = detector?.matchesInString(self, options: NSMatchingOptions.WithTransparentBounds, range: NSMakeRange(0, self.utf16.count)) .map {$0 }
return dates!.filter { date in
return date.date != nil
}.map { link -> NSDate in
return link.date!
}
}
/**
Gets an array of strings (hashtags #acme) for all links found in a String
- returns: [String]
*/
func getHashtags() -> [String]? {
let hashtagDetector = try? NSRegularExpression(pattern: "#(\\w+)", options: NSRegularExpressionOptions.CaseInsensitive)
let results = hashtagDetector?.matchesInString(self, options: NSMatchingOptions.WithoutAnchoringBounds, range: NSMakeRange(0, self.utf16.count)).map { $0 }
return results?.map({
(self as NSString).substringWithRange($0.rangeAtIndex(1))
})
}
/**
Gets an array of distinct strings (hashtags #acme) for all hashtags found in a String
- returns: [String]
*/
func getUniqueHashtags() -> [String]? {
return Array(Set(getHashtags()!))
}
/**
Gets an array of strings (mentions @apple) for all mentions found in a String
- returns: [String]
*/
func getMentions() -> [String]? {
let hashtagDetector = try? NSRegularExpression(pattern: "@(\\w+)", options: NSRegularExpressionOptions.CaseInsensitive)
let results = hashtagDetector?.matchesInString(self, options: NSMatchingOptions.WithoutAnchoringBounds, range: NSMakeRange(0, self.utf16.count)).map { $0 }
return results?.map({
(self as NSString).substringWithRange($0.rangeAtIndex(1))
})
}
/**
Check if a String contains a Date in it.
- returns: Bool with true value if it does
*/
func getUniqueMentions() -> [String]? {
return Array(Set(getMentions()!))
}
/**
Check if a String contains a link in it.
- returns: Bool with true value if it does
*/
func containsLink() -> Bool {
return self.getLinks().count > 0
}
/**
Check if a String contains a date in it.
- returns: Bool with true value if it does
*/
func containsDate() -> Bool {
return self.getDates().count > 0
}
/**
- returns: Base64 encoded string
*/
func encodeToBase64Encoding() -> String {
let utf8str = self.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
return utf8str.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
}
/**
- returns: Decoded Base64 string
*/
func decodeFromBase64Encoding() -> String {
let base64data = NSData(base64EncodedString: self, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
return NSString(data: base64data!, encoding: NSUTF8StringEncoding)! as String
}
// MARK: Subscript Methods
subscript (i: Int) -> String {
return String(Array(self.characters)[i])
}
subscript (r: Range<Int>) -> String {
var start = startIndex.advancedBy(r.startIndex),
end = startIndex.advancedBy(r.endIndex)
return substringWithRange(Range(start: start, end: end))
}
subscript (range: NSRange) -> String {
let end = range.location + range.length
return self[Range(start: range.location, end: end)]
}
subscript (substring: String) -> Range<String.Index>? {
return rangeOfString(substring, options: NSStringCompareOptions.LiteralSearch, range: Range(start: startIndex, end: endIndex), locale: NSLocale.currentLocale())
}
}