mirror of
https://github.com/exyte/Macaw.git
synced 2024-09-11 05:05:23 +03:00
Parse numbers avoiding regex matching
This commit is contained in:
parent
e63e6f2407
commit
1101d4fddc
@ -1659,31 +1659,28 @@ open class SVGParser {
|
||||
}
|
||||
|
||||
fileprivate func doubleFromString(_ string: String) -> Double? {
|
||||
if let doubleValue = Double(string) {
|
||||
return doubleValue
|
||||
}
|
||||
if string == "none" {
|
||||
return 0
|
||||
}
|
||||
guard let matcher = SVGParserRegexHelper.getUnitsIdenitifierMatcher() else {
|
||||
|
||||
let scanner = Scanner(string: string)
|
||||
let value = scanner.scannedDouble()
|
||||
let unit = scanner.scannedCharacters(from: .unitCharacters)
|
||||
|
||||
if !scanner.isAtEnd {
|
||||
let junk = scanner.scannedUpToCharacters(from: []) ?? ""
|
||||
print("Found trailing junk \"\(junk)\" in string \"\(string)\".")
|
||||
return .none
|
||||
}
|
||||
let fullRange = NSRange(location: 0, length: string.count)
|
||||
if let match = matcher.firstMatch(in: string, options: .reportCompletion, range: fullRange) {
|
||||
|
||||
let unitString = (string as NSString).substring(with: match.range(at: 1))
|
||||
let numberString = String(string.dropLast(unitString.count))
|
||||
let value = Double(numberString) ?? 0
|
||||
switch unitString {
|
||||
case "px" :
|
||||
switch unit {
|
||||
case nil, "px":
|
||||
return value
|
||||
default:
|
||||
print("SVG parsing error. Unit \(unitString) not supported")
|
||||
print("SVG parsing error. Unit \"\(unit ?? "")\" is not supported")
|
||||
return value
|
||||
}
|
||||
}
|
||||
return .none
|
||||
}
|
||||
|
||||
fileprivate func getDoubleValueFromPercentage(_ element: SWXMLHash.XMLElement, attribute: String) -> Double? {
|
||||
guard let attributeValue = element.allAttributes[attribute]?.text else {
|
||||
@ -2189,3 +2186,43 @@ fileprivate enum SVGKeys {
|
||||
static let color = "color"
|
||||
static let currentColor = "currentColor"
|
||||
}
|
||||
|
||||
fileprivate extension Scanner {
|
||||
/// A version of `scanDouble()`, available for an earlier OS.
|
||||
func scannedDouble() -> Double? {
|
||||
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
|
||||
return scanDouble()
|
||||
} else {
|
||||
var double: Double = 0
|
||||
return scanDouble(&double) ? double : nil
|
||||
}
|
||||
}
|
||||
|
||||
/// A version of `scanCharacters(from:)`, available for an earlier OS.
|
||||
func scannedCharacters(from set: CharacterSet) -> String? {
|
||||
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
|
||||
return scanCharacters(from: set)
|
||||
} else {
|
||||
var string: NSString? = nil
|
||||
return scanCharacters(from: set, into: &string) ? string as String? : nil
|
||||
}
|
||||
}
|
||||
|
||||
/// A version of `scanUpToCharacters(from:)`, available for an earlier OS.
|
||||
func scannedUpToCharacters(from set: CharacterSet) -> String? {
|
||||
if #available(OSX 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
|
||||
return scanUpToCharacters(from: set)
|
||||
} else {
|
||||
var string: NSString? = nil
|
||||
return scanUpToCharacters(from: set, into: &string) ? string as String? : nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate extension CharacterSet {
|
||||
/// Latin alphabet characters.
|
||||
static let latinAlphabet = CharacterSet(charactersIn: "a"..."z")
|
||||
.union(CharacterSet(charactersIn: "A"..."Z"))
|
||||
|
||||
static let unitCharacters = CharacterSet.latinAlphabet
|
||||
}
|
||||
|
@ -6,13 +6,11 @@ class SVGParserRegexHelper {
|
||||
fileprivate static let transformPattern = "\\-?\\d+\\.?\\d*e?\\-?\\d*"
|
||||
fileprivate static let textElementPattern = "<text.*?>((?s:.*))<\\/text>"
|
||||
fileprivate static let maskIdenitifierPattern = "url\\(#((?s:.*))\\)"
|
||||
fileprivate static let unitsIdenitifierPattern = "([a-zA-Z]+)$"
|
||||
|
||||
fileprivate static var transformMatcher: NSRegularExpression?
|
||||
fileprivate static var transformAttributeMatcher: NSRegularExpression?
|
||||
fileprivate static var textElementMatcher: NSRegularExpression?
|
||||
fileprivate static var maskIdenitifierMatcher: NSRegularExpression?
|
||||
fileprivate static var unitsMatcher: NSRegularExpression?
|
||||
|
||||
class func getTransformAttributeMatcher() -> NSRegularExpression? {
|
||||
if self.transformAttributeMatcher == nil {
|
||||
@ -57,16 +55,4 @@ class SVGParserRegexHelper {
|
||||
}
|
||||
return self.maskIdenitifierMatcher
|
||||
}
|
||||
|
||||
class func getUnitsIdenitifierMatcher() -> NSRegularExpression? {
|
||||
if unitsMatcher == nil {
|
||||
do {
|
||||
unitsMatcher = try NSRegularExpression(pattern: unitsIdenitifierPattern, options: .caseInsensitive)
|
||||
} catch {
|
||||
|
||||
}
|
||||
}
|
||||
return unitsMatcher
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user