std/uri/repaired

std/uri/src/repaired.trb

repaired, the repairs the WHATWG URL parser makes before it parses (docs/design/URI.md section 6). It is a function and not a mode of Uri.tryFrom, so what a reader sees at the call is the address-bar case.

fn repaired

fn repaired(text: String): String

The repairs a browser makes to what a person typed, before it reads it as a URL: leading and trailing control characters and spaces (U+0000 to U+0020) are dropped, every tab, line feed and carriage return anywhere in the text is removed, and a \ is read as / in the part before the first ? or # - when the scheme is one of the special ones (http, https, ws, wss, ftp, file, in any case) or the text has no scheme. A \ in the query, in the fragment or after any other scheme (git+ssh:, mailto:, urn:) stays, and so does any other control character inside the text: Uri.tryFrom percent-encodes it.

Nothing fails and nothing is checked: the result is as much of a URI as the text was, and Uri.tryFrom decides whether that is one. The function is idempotent, repaired(repaired(text)) == repaired(text).

Examples

const typed = "  http:\\\\example.com\\a b\n"
print repaired(typed)
print(Uri.tryFrom(repaired(typed))?)