|
|
|
// Copyright SIX DAY LLC. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
extension String {
|
|
|
|
var hex: String {
|
|
|
|
let data = self.data(using: .utf8)!
|
|
|
|
return data.map { String(format: "%02x", $0) }.joined()
|
|
|
|
}
|
|
|
|
|
|
|
|
var hexEncoded: String {
|
|
|
|
let data = self.data(using: .utf8)!
|
|
|
|
return data.hexEncoded
|
|
|
|
}
|
|
|
|
|
|
|
|
var doubleValue: Double {
|
|
|
|
let formatter = NumberFormatter()
|
|
|
|
formatter.locale = Locale.current
|
|
|
|
formatter.decimalSeparator = "."
|
|
|
|
if let result = formatter.number(from: self) {
|
|
|
|
return result.doubleValue
|
|
|
|
} else {
|
|
|
|
formatter.decimalSeparator = ","
|
|
|
|
if let result = formatter.number(from: self) {
|
|
|
|
return result.doubleValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var trimmed: String {
|
|
|
|
return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
|
|
|
|
}
|
|
|
|
|
|
|
|
var asDictionary: [String: Any]? {
|
|
|
|
if let data = self.data(using: .utf8) {
|
|
|
|
do {
|
|
|
|
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
|
|
|
|
} catch {
|
|
|
|
print(error.localizedDescription)
|
|
|
|
return [:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension String {
|
|
|
|
func index(from: Int) -> Index {
|
|
|
|
return self.index(startIndex, offsetBy: from)
|
|
|
|
}
|
|
|
|
|
|
|
|
func substring(from: Int) -> String {
|
|
|
|
let fromIndex = index(from: from)
|
|
|
|
return substring(from: fromIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func substring(to: Int) -> String {
|
|
|
|
let toIndex = index(from: to)
|
|
|
|
return substring(to: toIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func substring(with r: Range<Int>) -> String {
|
|
|
|
let startIndex = index(from: r.lowerBound)
|
|
|
|
let endIndex = index(from: r.upperBound)
|
|
|
|
return substring(with: startIndex..<endIndex)
|
|
|
|
}
|
|
|
|
}
|