to String() - > string- Module
- std.
url - Receiver
- URL
- Minimum arity
- 0
- Maximum arity
- 0
Declaration and parameters
LinkAvailability
Included by default: Pending
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.
Source evidence
- compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1084 - compiler/crates/topaz_resolve/src/stdlib.rs at
b48527d0a174· 59 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1084 - compiler/crates/topaz_self_frontend/topaz/checker/builtin_signatures.tpz at
b48527d0a174· 453 - compiler/crates/topaz_self_frontend/topaz/checker/members_capabilities.tpz at
b48527d0a174· 1296 - compiler/crates/topaz_value/src/value/members.rs at
b48527d0a174· 284
Receiver
URL
Parameter constraints
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.
Related rule: Parse and serialize absolute URLsURL.scheme returns the lowercase scheme without its delimiter. URL.host returns Some containing the serialized host without user information or a port. IPv6 hosts include their square brackets.
A URL whose host is absent returns None() from URL.host. A present empty host returns Some containing an empty string. A local file URL can have that empty host.
URL.path returns the serialized path without the query or fragment. A hierarchical path retains its leading slash. An opaque path has no implied slash. Percent escapes stay encoded so an escaped slash remains path data.
URL.fragment returns None() when there is no fragment delimiter. An explicit empty fragment returns Some containing an empty string. A present fragment excludes its delimiter and retains percent encoding.
Scheme, host, path, and fragment inspection cannot fail after construction. Each receiver is evaluated once. The returned strings are immutable and inspection has no external effects.
Read URL.query for decoded query fields. URL.path and URL.fragment do not apply query decoding. In particular, a plus sign in those components remains a plus sign.
Related rule: Read serialized URL components and their absenceResult
The declared result type is string.
Effects
Failures
Result and effects
LinkSerializes all URL components into stable URL text.
Resolved contracts
LinkRelated entries
LinkParse and serialize absolute URLs
LinkParse 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.
Why
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.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1722 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1768 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-parseatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi
Normalize the scheme, host, port, and path
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
https://example.com/a/c?x=1#part
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1722 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1768 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-parseatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi
Handle invalid absolute URL text as data
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
truetruetruetruetrue
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1722 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1768 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-parseatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi
Check both port boundaries and the first invalid port
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
https://example.com:0/https://example.com:65535/invalid port
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1722 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1768 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-parseatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi
Apply URL preprocessing and escape a path space
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
https://example.com/a%20b
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1722 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1768 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-parseatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi
Preserve serialized escapes and empty components through parsing
Linkimport 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/?#
Expected result
Expected result
Execution completes. Exit code 0.
https://example.com/a%2fb?x=%2b#p%2fqhttps://example.com/a%2fb?x=%2b#p%2fqhttps://example.com/?#https://example.com/?#
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1722 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1768 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-parseatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi
Evaluate the input once before reporting a parse error
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
inputinvalid URL
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1722 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1768 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-parseatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi
Reject a forbidden domain character
Linkimport 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
Expected result
Expected result
Execution completes. Exit code 0.
invalid host
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1722 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1768 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-parseatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi
Read serialized URL components and their absence
LinkRead serialized URL components and their absence
URL.scheme returns the lowercase scheme without its delimiter. URL.host returns Some containing the serialized host without user information or a port. IPv6 hosts include their square brackets.
A URL whose host is absent returns None() from URL.host. A present empty host returns Some containing an empty string. A local file URL can have that empty host.
URL.path returns the serialized path without the query or fragment. A hierarchical path retains its leading slash. An opaque path has no implied slash. Percent escapes stay encoded so an escaped slash remains path data.
URL.fragment returns None() when there is no fragment delimiter. An explicit empty fragment returns Some containing an empty string. A present fragment excludes its delimiter and retains percent encoding.
Scheme, host, path, and fragment inspection cannot fail after construction. Each receiver is evaluated once. The returned strings are immutable and inspection has no external effects.
Read URL.query for decoded query fields. URL.path and URL.fragment do not apply query decoding. In particular, a plus sign in those components remains a plus sign.
Why
Component access must preserve delimiters as data and distinguish absence from an empty component. Encoded path and fragment values can be placed back into a URL without turning an escaped separator into structure.
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1730 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1761 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-componentsatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi
Read encoded URL components without decoding delimiters
Linkimport std.url { URL }import std.iomatch (URL.parse("HTTPS://EXAMPLE.COM:8443/a%2Fb?q=a+b#x%20y")) {case Ok(value) => {io.writeLine(value.scheme())match (value.host()) {case Some(host) => io.writeLine(host)case None() => io.writeLine("no host")}io.writeLine(value.path())match (value.fragment()) {case Some(fragment) => io.writeLine(fragment)case None() => io.writeLine("no fragment")}io.writeLine(value.toString())}case Err(_) => io.writeLine("parse failed")}
https example.com /a%2Fb x%20y https://example.com:8443/a%2Fb?q=a+b#x%20y
Expected result
Expected result
Execution completes. Exit code 0.
httpsexample.com/a%2Fbx%20yhttps://example.com:8443/a%2Fb?q=a+b#x%20y
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1730 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1761 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-componentsatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi
Distinguish an absent fragment from an empty fragment
Linkimport std.url { URL }import std.iofor text in (["mailto:reader@example.com", "mailto:reader@example.com#"]) {match (URL.parse(text)) {case Ok(value) => {io.writeLine(value.scheme())match (value.host()) {case None() => io.writeLine("no host")case Some(host) => io.writeLine("host={host}")}io.writeLine(value.path())match (value.fragment()) {case None() => io.writeLine("no fragment")case Some(fragment) => io.writeLine("fragment={fragment}")}io.writeLine("{value.query().length}")}case Err(_) => io.writeLine("parse failed")}}
mailto no host reader@example.com no fragment 0 mailto no host reader@example.com fragment= 0
Expected result
Expected result
Execution completes. Exit code 0.
mailtono hostreader@example.comno fragment0mailtono hostreader@example.comfragment=0
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1730 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1761 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-componentsatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi
Read IPv6 and file hosts without losing authority data
Linkimport std.url { URL }import std.iofor text in (["https://u:p@[2001:DB8::1]:443/", "file:///tmp/a"]) {match (URL.parse(text)) {case Ok(value) => {match (value.host()) {case Some(host) => io.writeLine("host={host}")case None() => io.writeLine("no host")}io.writeLine(value.toString())}case Err(_) => io.writeLine("parse failed")}}
host=[2001:db8::1] https://u:p@[2001:db8::1]/ host= file:///tmp/a
Expected result
Expected result
Execution completes. Exit code 0.
host=[2001:db8::1]https://u:p@[2001:db8::1]/host=file:///tmp/a
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1730 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1761 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-componentsatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi
Serialize a Unicode domain, path, and fragment
Linkimport std.url { URL }import std.iomatch (URL.parse("https://bücher.example/한#α")) {case Ok(value) => {match (value.host()) {case Some(host) => io.writeLine(host)case None() => io.writeLine("no host")}io.writeLine(value.path())match (value.fragment()) {case Some(fragment) => io.writeLine(fragment)case None() => io.writeLine("no fragment")}io.writeLine(value.toString())}case Err(_) => io.writeLine("parse failed")}
xn--bcher-kva.example /%ED%95%9C %CE%B1 https://xn--bcher-kva.example/%ED%95%9C#%CE%B1
Expected result
Expected result
Execution completes. Exit code 0.
xn--bcher-kva.example/%ED%95%9C%CE%B1https://xn--bcher-kva.example/%ED%95%9C#%CE%B1
Source evidence
- compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1619 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1730 - compiler/crates/topaz_value/src/value/builtins/path_regex_url.rs at
912a5e86e1f9· 1761 - url.bs at
845bafca22fb· URL parsing and serialization and form parsing -
topaz.stdlib.contract.url-componentsatworking-tree - topaz.decision.url-standard.1 at
topaz.decisi