forked from jpedrosa/sua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.swift
163 lines (139 loc) · 4.33 KB
/
string.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
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
public extension String {
/**
It takes an array of char codes from which a new String will
be generated.
The start and end parameters can be omitted.
The end parameter is inclusive. If the end parameter is negative, the end
will be interpreted to be the value of the count of the array minus 1.
**Note**: if the range includes a null value (0), since the String
will be generated using a null-delimited C String function, the
String may be truncated at the null value instead.
E.g.:
var a: [CChar] = [72, 101, 108, 108, 111]
print(String.fromCharCodes(a) ?? "") // Prints Hello
print(String.fromCharCodes(a, start: 1, end: 3) ?? "") // Prints ell
*/
public static func fromCharCodes(charCodes: [CChar], start: Int = 0,
end: Int = -1) -> String? {
let lasti = charCodes.count - 1
let ei = end < 0 ? lasti : end
if ei < start {
return nil
} else {
var a: [CChar]
if ei < lasti {
a = [CChar](charCodes[start...ei + 1])
a[a.count - 1] = 0
} else {
a = [CChar](charCodes[start...lasti])
if a[a.count - 1] != 0 {
a.append(0)
}
}
return String(cString: &a)
}
}
public static func fromCharCodes(charCodes: [UInt8], start: Int = 0,
end: Int = -1) -> String? {
let lasti = charCodes.count - 1
let ei = end < 0 ? lasti : end
if ei < start {
return nil
} else {
var a: [UInt8]
if ei < lasti {
a = [UInt8](charCodes[start...ei + 1])
a[a.count - 1] = 0
} else {
a = [UInt8](charCodes[start...lasti])
if a[a.count - 1] != 0 {
a.append(0)
}
}
let ap = UnsafePointer<CChar>(a)
return String(cString: ap)
}
}
/**
Splits a string once or into 2 halves by returning a tuple with the left
and right values.
If the string pattern is not found at all, the left value will always be
nil.
E.g.:
print("abcdef".splitOnce("cd")) // Prints (Optional("ab"), Optional("ef"))
print("abcdef".splitOnce("xy")) // Prints (nil, nil)
print("abcdef".splitOnce("ab")) // Prints (Optional(""), Optional("cdef"))
print("abcdef".splitOnce("ef")) // (Optional("abcd"), nil)
*/
public func splitOnce(string: String) -> (left: String?, right: String?) {
var left: String?
var right: String?
if !string.isEmpty {
let sfc = string.utf16[0]
let jlen = string.utf16.count
let len = utf16.count
let validLen = len - jlen + 1
for i in 0..<validLen {
if utf16[i] == sfc {
var ok = true
var j = 1
while j < jlen {
if utf16[i + j] !=
string.utf16[j] {
ok = false
break
}
j += 1
}
if ok {
left = utf16.substring(startIndex: 0, endIndex: i)
if i + j < len {
right = utf16.substring(startIndex: i + j, endIndex: len)
}
break
}
}
}
}
return (left, right)
}
// Just a very handy method for returning the String bytes.
public var bytes: [UInt8] { return [UInt8](utf8) }
}
public extension String.UTF16View {
// Handy method for obtaining a string out of UTF16 indices.
public func substring(startIndex: Int, endIndex: Int)
-> String? {
return String(self[
self.index(self.startIndex, offsetBy: startIndex
)..<self.index(self.startIndex, offsetBy: endIndex)])
}
// Handy method for obtaining a UTF16 code unit to compare with.
public subscript(index: Int) -> UInt16 {
return self[self.index(startIndex, offsetBy: index)]
}
public func endsWith(_ string: String) -> Bool {
let a = string.utf16
let alen = a.count
let len = self.count
if len >= alen && alen > 0 {
let j = len - alen
for i in 0..<alen {
if a[i] != self[j + i] {
return false
}
}
return true
}
return false
}
}
public extension String.CharacterView {
// Handy method for obtaining a string out of character indices.
public func substring(startIndex: Int, endIndex: Int)
-> String {
return String(self[
self.index(self.startIndex, offsetBy: startIndex
)..<self.index(self.startIndex, offsetBy: endIndex)])
}
}