Skip to content

Commit

Permalink
chore: String extension codestyle change
Browse files Browse the repository at this point in the history
  • Loading branch information
JeneaVranceanu committed Mar 31, 2024
1 parent b455811 commit 848101f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 56 deletions.
4 changes: 2 additions & 2 deletions Sources/Web3Core/EthereumABI/ABITypeParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct ABITypeParser {

static func recursiveParseType(_ string: String) -> (type: ABI.Element.ParameterType?, tail: String?) {
let matcher = try! NSRegularExpression(pattern: ABI.TypeParsingExpressions.typeEatingRegex, options: NSRegularExpression.Options.dotMatchesLineSeparators)
let match = matcher.matches(in: string, options: NSRegularExpression.MatchingOptions.anchored, range: string.fullNSRange)
let match = matcher.matches(in: string, options: NSRegularExpression.MatchingOptions.anchored, range: string.fullNSRange())
guard match.count == 1 else {
return (nil, nil)
}
Expand Down Expand Up @@ -85,7 +85,7 @@ public struct ABITypeParser {

static func recursiveParseArray(baseType: ABI.Element.ParameterType, string: String) -> (type: ABI.Element.ParameterType?, tail: String?) {
let matcher = try! NSRegularExpression(pattern: ABI.TypeParsingExpressions.arrayEatingRegex, options: NSRegularExpression.Options.dotMatchesLineSeparators)
let match = matcher.matches(in: string, options: NSRegularExpression.MatchingOptions.anchored, range: string.fullNSRange)
let match = matcher.matches(in: string, options: NSRegularExpression.MatchingOptions.anchored, range: string.fullNSRange())
guard match.count == 1 else {return (nil, nil)}
var tail: String = ""
var type: ABI.Element.ParameterType?
Expand Down
2 changes: 1 addition & 1 deletion Sources/Web3Core/KeystoreManager/IBAN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public struct IBAN {
public static func isValidIBANaddress(_ iban: String, noValidityCheck: Bool = false) -> Bool {
let regex = "^XE[0-9]{2}(ETH[0-9A-Z]{13}|[0-9A-Z]{30,31})$"
let matcher = try! NSRegularExpression(pattern: regex, options: NSRegularExpression.Options.dotMatchesLineSeparators)
let match = matcher.matches(in: iban, options: NSRegularExpression.MatchingOptions.anchored, range: iban.fullNSRange)
let match = matcher.matches(in: iban, options: NSRegularExpression.MatchingOptions.anchored, range: iban.fullNSRange())
guard match.count == 1 else {
return false
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Web3Core/Utility/NSRegularExpression+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public extension NSRegularExpression {
return groupnames
}

let m = reg.matches(in: pattern, options: .withTransparentBounds, range: pattern.fullNSRange)
let m = reg.matches(in: pattern, options: .withTransparentBounds, range: pattern.fullNSRange())
for (nameIndex, g) in m.enumerated() {
let r = pattern.range(from: g.range(at: 0))
let gstring = String(pattern[r!])
let gmatch = greg.matches(in: gstring, options: [], range: gstring.fullNSRange)
let gmatch = greg.matches(in: gstring, options: [], range: gstring.fullNSRange())
if gmatch.count > 0 {
let r2 = gstring.range(from: gmatch[0].range(at: 1))!
groupnames[String(gstring[r2])] = (g, gmatch[0], nameIndex)
Expand All @@ -34,7 +34,7 @@ public extension NSRegularExpression {
}

func captureGroups(string: String, options: NSRegularExpression.MatchingOptions = []) -> [String: String] {
captureGroups(string: string, options: options, range: string.fullNSRange)
captureGroups(string: string, options: options, range: string.fullNSRange())
}

func captureGroups(string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange) -> [String: String] {
Expand Down
77 changes: 31 additions & 46 deletions Sources/Web3Core/Utility/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,9 @@
import Foundation

extension String {
var fullRange: Range<Index> {
return startIndex..<endIndex
}

public var fullNSRange: NSRange {
return NSRange(fullRange, in: self)
}

func index(of char: Character) -> Index? {
guard let range = range(of: String(char)) else {
return nil
}
return range.lowerBound
}

func split(intoChunksOf chunkSize: Int) -> [String] {
var output = [String]()
let splittedString = self
.map { $0 }
.split(intoChunksOf: chunkSize)
splittedString.forEach {
output.append($0.map { String($0) }.joined(separator: ""))
}
return output
public func fullNSRange() -> NSRange {
return NSRange(fullRange(), in: self)
}

public subscript (bounds: CountableClosedRange<Int>) -> String {
Expand Down Expand Up @@ -59,12 +38,6 @@ extension String {
}
}

func interpretAsBinaryData() -> Data? {
let padded = self.padding(toLength: ((self.count + 7) / 8) * 8, withPad: "0", startingAt: 0)
let byteArray = padded.split(intoChunksOf: 8).map { UInt8(strtoul($0, nil, 2)) }
return Data(byteArray)
}

public func hasHexPrefix() -> Bool {
return self.hasPrefix("0x")
}
Expand All @@ -84,6 +57,34 @@ extension String {
return self
}

internal func fullRange() -> Range<Index> {
return startIndex..<endIndex
}

func index(of char: Character) -> Index? {
guard let range = range(of: String(char)) else {
return nil
}
return range.lowerBound
}

func split(intoChunksOf chunkSize: Int) -> [String] {
var output = [String]()
let splittedString = self
.map { $0 }
.split(intoChunksOf: chunkSize)
splittedString.forEach {
output.append($0.map { String($0) }.joined(separator: ""))
}
return output
}

func interpretAsBinaryData() -> Data? {
let padded = self.padding(toLength: ((self.count + 7) / 8) * 8, withPad: "0", startingAt: 0)
let byteArray = padded.split(intoChunksOf: 8).map { UInt8(strtoul($0, nil, 2)) }
return Data(byteArray)
}

/// Strips leading zeroes from a hex string.
/// ONLY hex string format is supported.
/// - Returns: string with stripped leading zeroes (and 0x prefix) or unchanged string.
Expand Down Expand Up @@ -124,16 +125,9 @@ extension String {
return from ..< to
}

var asciiValue: Int {
get {
let s = self.unicodeScalars
return Int(s[s.startIndex].value)
}
}

/// Strips whitespaces and newlines on both ends.
func trim() -> String {
trimmingCharacters(in: .whitespacesAndNewlines)
trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
}

public var isHex: Bool {
Expand Down Expand Up @@ -170,12 +164,3 @@ extension String {
return result
}
}

extension Character {
var asciiValue: Int {
get {
let s = String(self).unicodeScalars
return Int(s[s.startIndex].value)
}
}
}
8 changes: 4 additions & 4 deletions Sources/web3swift/Utils/EIP/EIP681.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ extension Web3 {
guard let encoding = striped[1].removingPercentEncoding else { return nil }
// guard let url = URL.init(string: encoding) else { return nil }
let matcher = try! NSRegularExpression(pattern: addressRegex, options: NSRegularExpression.Options.dotMatchesLineSeparators)
let match = matcher.matches(in: encoding, options: NSRegularExpression.MatchingOptions.anchored, range: encoding.fullNSRange)
let match = matcher.matches(in: encoding, options: NSRegularExpression.MatchingOptions.anchored, range: encoding.fullNSRange())
guard match.count == 1 else { return nil }
guard match[0].numberOfRanges == 5 else { return nil }
var addressString: String?
Expand Down Expand Up @@ -471,15 +471,15 @@ extension Web3 {
// TODO: try replacing this manual parsing with JSONDecoder and RawRepresentable

let squareBracketRegex = try! NSRegularExpression(pattern: "(\\[*)")
let match = squareBracketRegex.firstMatch(in: rawValue, range: rawValue.fullNSRange)
let match = squareBracketRegex.firstMatch(in: rawValue, range: rawValue.fullNSRange())

guard let bracketsCount = match?.range.upperBound,
bracketsCount > 0 else {
return nil
}

let splitRegex = try! NSRegularExpression(pattern: "(\\]){\(bracketsCount)},(\\[){\(bracketsCount)}")
var indices: [Int] = splitRegex.matches(in: rawValue, range: rawValue.fullNSRange)
var indices: [Int] = splitRegex.matches(in: rawValue, range: rawValue.fullNSRange())
.map { $0.range.lowerBound + bracketsCount }
if !indices.isEmpty {
indices.append(rawValue.count)
Expand Down Expand Up @@ -517,7 +517,7 @@ extension Web3 {

let elementsBoundary = try! NSRegularExpression(pattern: "\",\"")
var indices = Array(elementsBoundary
.matches(in: rawValue, range: rawValue.fullNSRange)
.matches(in: rawValue, range: rawValue.fullNSRange())
.map { result in
result.range.lowerBound + 1
})
Expand Down

0 comments on commit 848101f

Please sign in to comment.