parse(text: string) - > Result<URL, string>- Модуль
- std.
url → - Получатель
- Неприменимо
- Минимальное число аргументов
- 1
- Максимальное число аргументов
- 1
Declaration and parameters
СсылкаДоступность
Включено по умолчанию · Не определено
Import std.url in language mode 5.21.
The current compiler selects std/url.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
Получатель
Ограничения параметров
URL.parse takes a string and applies the WHATWG URL parser with UTF-8 input and no base URL. A successful parse returns Ok containing an immutable URL. Relative references need a base and therefore return Err.
A parser failure returns Err containing a nonempty string. Missing or invalid schemes, invalid hosts, and ports outside 0 through 65535 fail. Error wording is explanatory text and has no fixed spelling.
URL preprocessing removes leading and trailing code points from U+0000 through U+0020. It then removes U+0009, U+000A, and U+000D throughout the input. Other characters are processed by the URL parser. A validation warning that permits parsing does not become an Err.
Schemes use lowercase. Special-scheme domain hosts use ASCII domain serialization. Default ports disappear and hierarchical dot segments are removed. Opaque paths retain their distinct path interpretation.
The default ports are 21 for ftp, 80 for http and ws, and 443 for https and wss. Other valid ports survive serialization as decimal integers. The file scheme has no port.
URL.toString serializes every stored component. It preserves user information, nondefault ports, query order, and explicit empty query and fragment delimiters. Existing percent escapes retain their spelling. Newly escaped characters use percent-encoded UTF-8 bytes with uppercase hexadecimal digits.
Parsing the serialization produces the same serialization and component values. Reading a URL does not modify it. Parsing evaluates its argument once before validation and performs no network or filesystem operation.
Связанное правило · Parse and serialize absolute URLs →Результат
The declared result type is Result<URL, string>.
Эффекты
Ошибки
Result and effects
СсылкаParses an absolute URL with the URL Standard model and returns a URL or an explanatory Err.
Resolved contracts
СсылкаRelated entries
СсылкаParse and serialize absolute URLs
СсылкаParse and serialize absolute URLs
URL.parse takes a string and applies the WHATWG URL parser with UTF-8 input and no base URL. A successful parse returns Ok containing an immutable URL. Relative references need a base and therefore return Err.
A parser failure returns Err containing a nonempty string. Missing or invalid schemes, invalid hosts, and ports outside 0 through 65535 fail. Error wording is explanatory text and has no fixed spelling.
URL preprocessing removes leading and trailing code points from U+0000 through U+0020. It then removes U+0009, U+000A, and U+000D throughout the input. Other characters are processed by the URL parser. A validation warning that permits parsing does not become an Err.
Schemes use lowercase. Special-scheme domain hosts use ASCII domain serialization. Default ports disappear and hierarchical dot segments are removed. Opaque paths retain their distinct path interpretation.
The default ports are 21 for ftp, 80 for http and ws, and 443 for https and wss. Other valid ports survive serialization as decimal integers. The file scheme has no port.
URL.toString serializes every stored component. It preserves user information, nondefault ports, query order, and explicit empty query and fragment delimiters. Existing percent escapes retain their spelling. Newly escaped characters use percent-encoded UTF-8 bytes with uppercase hexadecimal digits.
Parsing the serialization produces the same serialization and component values. Reading a URL does not modify it. Parsing evaluates its argument once before validation and performs no network or filesystem operation.
Причина
A single established parser gives URL text one interpretation. Parsing without a hidden base makes acceptance independent of the working directory and browser location. Stable serialization keeps resource identifiers reusable in text and typeset links.
Normalize the scheme, host, port, and path
Ссылкаimport std.url { URL }import std.iomatch (URL.parse("HTTPS://Example.COM:443/a/./b/../c?x=1#part")) {case Ok(value) => io.writeLine(value.toString())case Err(_) => io.writeLine("parse failed")}
https://example.com/a/c?x=1#part
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
https://example.com/a/c?x=1#part
Handle invalid absolute URL text as data
Ссылкаimport std.url { URL }import std.iofor text in (["", "../guide", "1bad:x", "https://", "https://example.com:wrong"]) {match (URL.parse(text)) {case Err(message) => io.writeLine("{message != ""}")case Ok(_) => io.writeLine("accepted")}}
true
true
true
true
true
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
truetruetruetruetrue
Check both port boundaries and the first invalid port
Ссылкаimport std.url { URL }import std.iofor text in (["https://example.com:0", "https://example.com:65535", "https://example.com:65536"]) {match (URL.parse(text)) {case Ok(value) => io.writeLine(value.toString())case Err(_) => io.writeLine("invalid port")}}
https://example.com:0/
https://example.com:65535/
invalid port
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
https://example.com:0/https://example.com:65535/invalid port
Apply URL preprocessing and escape a path space
Ссылкаimport std.url { URL }import std.iomatch (URL.parse(" \thttps://EXAMPLE.com/a b\n")) {case Ok(value) => io.writeLine(value.toString())case Err(_) => io.writeLine("parse failed")}
https://example.com/a%20b
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
https://example.com/a%20b
Preserve serialized escapes and empty components through parsing
Ссылкаimport std.url { URL }import std.iofor text in (["https://example.com/a%2fb?x=%2b#p%2fq", "https://example.com/?#"]) {match (URL.parse(text)) {case Ok(value) => {io.writeLine(value.toString())match (URL.parse(value.toString())) {case Ok(again) => io.writeLine(again.toString())case Err(_) => io.writeLine("second parse failed")}}case Err(_) => io.writeLine("first parse failed")}}
https://example.com/a%2fb?x=%2b#p%2fq
https://example.com/a%2fb?x=%2b#p%2fq
https://example.com/?#
https://example.com/?#
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
https://example.com/a%2fb?x=%2b#p%2fqhttps://example.com/a%2fb?x=%2b#p%2fqhttps://example.com/?#https://example.com/?#
Evaluate the input once before reporting a parse error
Ссылкаimport std.url { URL }import std.iofunction inputText() -> string {io.writeLine("input")return "../chapter"}match (URL.parse(inputText())) {case Err(_) => io.writeLine("invalid URL")case Ok(_) => io.writeLine("accepted")}
input
invalid URL
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
inputinvalid URL
Reject a forbidden domain character
Ссылкаimport std.url { URL }import std.iomatch (URL.parse("https://exa[mple.com/")) {case Err(_) => io.writeLine("invalid host")case Ok(_) => io.writeLine("accepted")}
invalid host
Ожидаемый результат
Ожидаемый результат
Выполнение завершается. Код завершения 0.
invalid host