This repository was archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathStringExtensions.swift
More file actions
161 lines (125 loc) · 4.66 KB
/
StringExtensions.swift
File metadata and controls
161 lines (125 loc) · 4.66 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
//
// StringExtensions.swift
// Rocket.Chat
//
// Created by Rafael K. Streit on 7/6/16.
// Copyright © 2016 Rocket.Chat. All rights reserved.
//
import Foundation
extension String {
var isValidEmail: Bool {
let emailFormat = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let emailPredicate = NSPredicate(format: "SELF MATCHES %@", emailFormat)
return emailPredicate.evaluate(with: self)
}
func base64Encoded() -> String? {
if let data = self.data(using: .utf8) {
return data.base64EncodedString()
}
return nil
}
func base64Decoded() -> String? {
if let data = Data(base64Encoded: self, options: .ignoreUnknownCharacters) {
return String(data: data, encoding: .utf8)
}
return nil
}
func md5() -> String {
if let stringData = self.data(using: String.Encoding.utf8) {
return hexStringFromData(input: md5Digest(stringData: stringData) as NSData)
}
return ""
}
fileprivate func md5Digest(stringData: Data) -> Data {
var digestData = Data(count: Int(CC_MD5_DIGEST_LENGTH))
_ = digestData.withUnsafeMutableBytes { digestBytes in
stringData.withUnsafeBytes { stringBytes in
CC_MD5(stringBytes, CC_LONG(stringData.count), digestBytes)
}
}
return digestData
}
func sha256() -> String {
if let stringData = self.data(using: String.Encoding.utf8) {
return hexStringFromData(input: digest(input: stringData as NSData))
}
return ""
}
private func digest(input: NSData) -> NSData {
let digestLength = Int(CC_SHA256_DIGEST_LENGTH)
var hash = [UInt8](repeating: 0, count: digestLength)
CC_SHA256(input.bytes, UInt32(input.length), &hash)
return NSData(bytes: hash, length: digestLength)
}
private func hexStringFromData(input: NSData) -> String {
var bytes = [UInt8](repeating: 0, count: input.length)
input.getBytes(&bytes, length: input.length)
var hexString = ""
for byte in bytes {
hexString += String(format: "%02x", UInt8(byte))
}
return hexString
}
func ranges(of string: String) -> [Range<Index>] {
var ranges = [Range<Index>]()
let pCount = string.count
let strCount = self.count
if strCount < pCount { return [] }
for idx in 0...(strCount-pCount) {
let from = index(self.startIndex, offsetBy: idx)
if let toIdx = index(from, offsetBy: pCount, limitedBy: self.endIndex) {
if string == self[from..<toIdx] {
ranges.append(from..<toIdx)
}
}
}
return ranges
}
/**
This method prevents the app from keeping the URL that may
come with (or without) a slash in the end. All the URLs
coming/mapped from the API should not have a slash in the end.
- returns: the same string, but without the last char if it exists.
*/
func removingLastSlashIfNeeded() -> String {
if last == "/" {
return String(self.dropLast())
}
return self
}
func removingWhitespaces() -> String {
return components(separatedBy: .whitespacesAndNewlines).joined()
}
func removingNewLines() -> String {
return components(separatedBy: .newlines).joined()
}
var removingPercentEncoding: String? {
return NSString(string: self).removingPercentEncoding
}
func replacingFirstOccurrence(of string: String, with replacement: String) -> String {
guard let range = self.range(of: string) else { return self }
return replacingCharacters(in: range, with: replacement)
}
func commandAndParams() -> (command: String, params: String)? {
guard self.first == "/" && self.count > 1 else { return nil }
let components = self.components(separatedBy: " ")
let command = String(components[0].dropFirst())
let params = components.dropFirst().joined(separator: " ")
return (command: command, params: params)
}
func reaction() -> String? {
guard self.first == "+" && self.count > 1 else { return nil }
let emoji = String(self.dropFirst())
guard emoji.first == ":" && emoji.last == ":" else { return nil }
return emoji
}
var boolValue: Bool {
return NSString(string: self).boolValue
}
func capitalizingFirstLetter() -> String {
return prefix(1).uppercased() + self.lowercased().dropFirst()
}
mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}