exyte / Macaw

Powerful and easy-to-use vector graphics Swift library with SVG support
MIT License
6.01k stars 553 forks source link

Drop uses of regular expressions for parsing #701

Closed aapng closed 4 years ago

aapng commented 4 years ago

Regular expression-based parsing is slower than the use of Scanner, because the latter immediately uses the information about the data it has scanned.

Additionally, the current implementation crashes rarely in NSRegularExpression.firstMatch, which seems to be an issue with Apple's implementation. Although rare, it is noticeable.

All regular expressions (SVGParserRegexHelper) are removed. Parsing is built upon Scanner.

Also, re-parsing of XML during <tspan> processing is also dropped. Already existing XMLIndexer information is processed instead. The change does not affect the current behaviour for non-embedded tspans (tspans within another tspan, which are clearly not supported by the implementation at the moment). There are no new failing tests.

With the changes, SVG parsing is more efficient, shorter, a bit more resilient, has less branching and recursion, has shorter left indents. Although personally, the main reason for the minor refactoring is prevention of the rare crashes.

ystrot commented 4 years ago

Thanks @aapng, it's a huge improvement!