This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathString+Helpers.swift
More file actions
167 lines (149 loc) · 6.01 KB
/
Copy pathString+Helpers.swift
File metadata and controls
167 lines (149 loc) · 6.01 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
import Foundation
extension String {
func stringByDecodingXMLCharacters() -> String {
return NSString.wpkit_decodeXMLCharacters(in: self)
}
func stringByEncodingXMLCharacters() -> String {
return NSString.wpkit_encodeXMLCharacters(in: self)
}
func trim() -> String {
return trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
/// Returns `self` if not empty, or `nil` otherwise
///
func nonEmptyString() -> String? {
return isEmpty ? nil : self
}
/// Returns a string without the character at the specified index.
/// This is a non-mutating version of `String.remove(at:)`.
func removing(at index: Index) -> String {
var copy = self
copy.remove(at: index)
return copy
}
/// Returns a count of valid text characters.
/// - Note : This implementation is influenced by `-wordCount` in `NSString+Helpers`.
var characterCount: Int {
var charCount = 0
if isEmpty == false {
let textRange = startIndex..<endIndex
enumerateSubstrings(in: textRange, options: [.byWords, .localized]) { word, _, _, _ in
let wordLength = word?.count ?? 0
charCount += wordLength
}
}
return charCount
}
}
// MARK: - Prefix removal
extension String {
/// Removes the given prefix from the string, if exists.
///
/// Calling this method might invalidate any existing indices for use with this string.
///
/// - Parameters:
/// - prefix: A possible prefix to remove from this string.
///
mutating func removePrefix(_ prefix: String) {
if let prefixRange = range(of: prefix), prefixRange.lowerBound == startIndex {
removeSubrange(prefixRange)
}
}
/// Returns a string with the given prefix removed, if it exists.
///
/// - Parameters:
/// - prefix: A possible prefix to remove from this string.
///
func removingPrefix(_ prefix: String) -> String {
var copy = self
copy.removePrefix(prefix)
return copy
}
/// Removes the prefix from the string that matches the given pattern, if any.
///
/// Calling this method might invalidate any existing indices for use with this string.
///
/// - Parameters:
/// - pattern: The regular expression pattern to search for. Avoid using `^`.
/// - options: The options applied to the regular expression during matching.
///
/// - Throws: an error if it the pattern is not a valid regular expression.
///
mutating func removePrefix(pattern: String, options: NSRegularExpression.Options = []) throws {
let regexp = try NSRegularExpression(pattern: "^\(pattern)", options: options)
let fullRange = NSRange(location: 0, length: (self as NSString).length)
if let match = regexp.firstMatch(in: self, options: [], range: fullRange) {
let matchRange = match.range
self = (self as NSString).replacingCharacters(in: matchRange, with: "")
}
}
/// Returns a string without the prefix that matches the given pattern, if it exists.
///
/// - Parameters:
/// - pattern: The regular expression pattern to search for. Avoid using `^`.
/// - options: The options applied to the regular expression during matching.
///
/// - Throws: an error if it the pattern is not a valid regular expression.
///
func removingPrefix(pattern: String, options: NSRegularExpression.Options = []) throws -> String {
var copy = self
try copy.removePrefix(pattern: pattern, options: options)
return copy
}
}
// MARK: - Suffix removal
extension String {
/// Removes the given suffix from the string, if exists.
///
/// Calling this method might invalidate any existing indices for use with this string.
///
/// - Parameters:
/// - suffix: A possible suffix to remove from this string.
///
mutating func removeSuffix(_ suffix: String) {
if let suffixRange = range(of: suffix, options: [.backwards]), suffixRange.upperBound == endIndex {
removeSubrange(suffixRange)
}
}
/// Returns a string with the given suffix removed, if it exists.
///
/// - Parameters:
/// - suffix: A possible suffix to remove from this string.
///
func removingSuffix(_ suffix: String) -> String {
var copy = self
copy.removeSuffix(suffix)
return copy
}
/// Removes the suffix from the string that matches the given pattern, if any.
///
/// Calling this method might invalidate any existing indices for use with this string.
///
/// - Parameters:
/// - pattern: The regular expression pattern to search for. Avoid using `$`.
/// - options: The options applied to the regular expression during matching.
///
/// - Throws: an error if it the pattern is not a valid regular expression.
///
mutating func removeSuffix(pattern: String, options: NSRegularExpression.Options = []) throws {
let regexp = try NSRegularExpression(pattern: "\(pattern)$", options: options)
let fullRange = NSRange(location: 0, length: (self as NSString).length)
if let match = regexp.firstMatch(in: self, options: [], range: fullRange) {
let matchRange = match.range
self = (self as NSString).replacingCharacters(in: matchRange, with: "")
}
}
/// Returns a string without the suffix that matches the given pattern, if it exists.
///
/// - Parameters:
/// - pattern: The regular expression pattern to search for. Avoid using `$`.
/// - options: The options applied to the regular expression during matching.
///
/// - Throws: an error if it the pattern is not a valid regular expression.
///
func removingSuffix(pattern: String, options: NSRegularExpression.Options = []) throws -> String {
var copy = self
try copy.removeSuffix(pattern: pattern, options: options)
return copy
}
}