Swift: a quick way to parse HTML

I have a large source code file in which I need to parse specific text. I want to do it as quickly as possible. What would be the fastest way to do this in Swift? Are these all the options I could think of?

  • Using a third-party library of string functions - I tried this. It works well, but I think it is much slower compared to other lower-level methods in general, unless there are some quick ones specifically for Swift.

  • Using a third-party HTML parser. I have studied a few, but I'm not sure that they will suit my needs. Before I start with this, I just want to know if they are usually faster, if there are any noticeable fast ones, and if I can configure them to get exactly what I want from the source code.

  • Using String or NSString. From what I understand, using String vs NSString should not give any speed difference. I really like this approach and it is lower than some others, so should I expect pretty high performance?

  • Using regular expressions. I was told that since they are lower, they should ideally be the fastest. I used to use regular expressions, but not in ios. Is parsing easy with NSRegularExpression, and faster?

Thanks!

+6
source share
1 answer

Came to this link exploring your question: http://benedictcohen.co.uk/blog/archives/74

The authors explain an older approach to what @CodaFi suggested, but at the end there is a corresponding update that you should check:

The easiest way to parse HTML is to treat it as XML and use NSXMLParser. iOS comes with LibTidy, which is capable of capturing a lot of markup. Use LibTidy to create pure XML and pass this XML to NSXMLParser. Use only the approach described above if it is not possible to use NSXMLParser.

So maybe option 4 or 5 is for you?

0
source

Source: https://habr.com/ru/post/978655/


All Articles