For an iOS app which helps me rolling back vandalism on Stack Exchange, I have a piece of Swift code which downloads a revision page (example) and tries to find the 'spacer' fragment just above a certain revision. It might not be clear what I'm talking about, so here's a picture from Firefox + developer tools:
I have the HTML content of this page, and the GUID of the revision (8f9ab85f-1401-41e9-8f75-8a07b10bad32
) from the Stack Exchange API. I'm looking for that element just above the revision header, since those are the only HTML elements with IDs on the page. I need that spacer-9617187a-fe48-4212-9a1a-f3a366e62736
so I can link directly to https://codereview.stackexchange.com/posts/189958/revisions#spacer-9617187a-fe48-4212-9a1a-f3a366e62736
For that, I've written a few lines of Swift code. The problem is that string handling in Swift confuses the **** out of me. Most of the language feels rather good, but I'd rather do string manipulation in SQL than in Swift...
Here is what I have so far. It works, but I was wondering if it could break in cases I haven't foreseen, or if it can be made more understandable/manageable by a future me. You see, even Stack Exchange's syntax highlighter has problems understanding it...
The input parameters for this piece of code are html
(a String containing the content of the revisions page, e.g. https://codereview.stackexchange.com/posts/189958/revisions) and revisionGUID
(8F9AB85F-1401-41E9-8F75-8A07B10BAD32
in the example above - the API returns them in upper case). fragment
is eventually used as output parameter. The 43
is the length of spacer-
plus a GUID.
// Find fragment just above selected revision
let range = html.range(of: #"onclick="StackExchange.revisions.toggle('"# + revisionGUID.lowercased() + #"')""#)!
let index = html.range(of: #"<tr id=""#, options: .backwards, range: html.startIndex..<range.lowerBound)!.upperBound
let fragment = String(html[index..<html.index(index, offsetBy: 43)])