An advanced Ethereum/EVM mobile wallet
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
alpha-wallet-ios/Trust/Extensions/String.swift

79 lines
2.0 KiB

// 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 [:]
}
var drop0x: String {
if self.count > 2 && self.substring(with: 0..<2) == "0x" {
return String(self.dropFirst(2))
}
return self
}
var add0x: String {
return "0x" + self
}
}
extension String {
func index(from: Int) -> Index {
return self.index(startIndex, offsetBy: from)
}
func substring(from: Int) -> String {
let fromIndex = index(from: from)
return String(self[fromIndex...])
}
func substring(to: Int) -> String {
let toIndex = index(from: to)
return String(self[..<toIndex])
}
func substring(with r: Range<Int>) -> String {
let startIndex = index(from: r.lowerBound)
let endIndex = index(from: r.upperBound)
return String(self[startIndex..<endIndex])
}
}