본문으로 건너뛰기
토파즈6.0 · 출시 전
다운로드 →

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    라이브러리 · std.url

    URL.query

    Returns an independent map of form-decoded query names and ordered value arrays.

    query() -> Map<string, Array<string>>
    수신자
    URL
    최소 인자 수
    0
    최대 인자 수
    0

    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

    매개변수 제약

    URL.query returns a new Map from decoded names to arrays of decoded values. A missing query and an empty query both produce an empty map.

    Query decoding follows application/x-www-form-urlencoded parsing. Ampersands separate fields. Empty fields between separators are skipped. The first equals sign separates each name from its value. A field without that sign has an empty value.

    An explicit empty name is retained. A plus sign decodes to an ASCII space. Valid percent escapes decode to bytes and those bytes decode as UTF-8. Malformed percent escapes remain literal text and invalid UTF-8 sequences use the replacement character.

    Repeated names are grouped after decoding. Values retain encounter order. Map keys retain the order in which their decoded names first appear. No sorting, deduplication, or comma splitting occurs.

    The returned Map and its value arrays follow collection value semantics. URL.toString preserves the stored serialized query rather than serializing this map.

    Reading a query evaluates its receiver once and performs no external operation. A constructed URL can always produce its query map. Query decoding does not return a diagnostic or a Result error.

    결과

    The declared result type is Map<string, Array<string>>.

    효과

    실패

    Result and effects

    링크

    Returns an independent map of form-decoded query names and ordered value arrays.

    Resolved contracts

    링크

    Related entries

    링크

    std.url →

    Decode query fields into an independent map

    링크
    규칙topaz.stdlib.contract.url-query.rule

    Decode query fields into an independent map

    URL.query returns a new Map from decoded names to arrays of decoded values. A missing query and an empty query both produce an empty map.

    Query decoding follows application/x-www-form-urlencoded parsing. Ampersands separate fields. Empty fields between separators are skipped. The first equals sign separates each name from its value. A field without that sign has an empty value.

    An explicit empty name is retained. A plus sign decodes to an ASCII space. Valid percent escapes decode to bytes and those bytes decode as UTF-8. Malformed percent escapes remain literal text and invalid UTF-8 sequences use the replacement character.

    Repeated names are grouped after decoding. Values retain encounter order. Map keys retain the order in which their decoded names first appear. No sorting, deduplication, or comma splitting occurs.

    The returned Map and its value arrays follow collection value semantics. URL.toString preserves the stored serialized query rather than serializing this map.

    Reading a query evaluates its receiver once and performs no external operation. A constructed URL can always produce its query map. Query decoding does not return a diagnostic or a Result error.

    이유

    Query syntax has its own escaping rules and permits repeated names. Grouped arrays preserve every value while immutable URLs keep query inspection from silently rewriting a link.

    Decode repeated query fields and preserve their value order

    링크
    main.tpzDecode repeated query fields and preserve their value order플레이그라운드 열기 →
    1. import std.url { URL }
    2. import std.io
    3. match (URL.parse("https://example.com/?q=a+b&q=%ED%95%9C&empty&x=a=b&=blank")) {
    4. case Ok(value) => {
    5. let fields = value.query()
    6. for name in (["q", "empty", "x", ""]) {
    7. match (fields.get(name)) {
    8. case Some(values) => {
    9. for item in (values) {
    10. io.writeLine("{name}={item}")
    11. }
    12. }
    13. case None() => io.writeLine("missing")
    14. }
    15. }
    16. }
    17. case Err(_) => io.writeLine("parse failed")
    18. }
    기대 출력종료 코드 0
    q=a b
    q=한
    empty=
    x=a=b
    =blank
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. q=a b
    2. q=한
    3. empty=
    4. x=a=b
    5. =blank

    Skip empty separators while retaining an explicit empty field

    링크
    main.tpzSkip empty separators while retaining an explicit empty field플레이그라운드 열기 →
    1. import std.url { URL }
    2. import std.io
    3. for text in (["https://example.com/", "https://example.com/?", "https://example.com/?&&a=1&&", "https://example.com/?="]) {
    4. match (URL.parse(text)) {
    5. case Ok(value) => {
    6. let fields = value.query()
    7. let hasEmptyName = fields.containsKey("")
    8. io.writeLine("{fields.length}:{hasEmptyName}")
    9. }
    10. case Err(_) => io.writeLine("parse failed")
    11. }
    12. }
    기대 출력종료 코드 0
    0:false
    0:false
    1:false
    1:true
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 0:false
    2. 0:false
    3. 1:false
    4. 1:true

    Keep malformed percent escapes as query data

    링크
    main.tpzKeep malformed percent escapes as query data플레이그라운드 열기 →
    1. import std.url { URL }
    2. import std.io
    3. for text in (["https://example.com/?x=%", "https://example.com/?x=%GG"]) {
    4. match (URL.parse(text)) {
    5. case Ok(value) => {
    6. match (value.query().get("x")) {
    7. case Some(values) => io.writeLine(values[0])
    8. case None() => io.writeLine("missing")
    9. }
    10. }
    11. case Err(_) => io.writeLine("parse failed")
    12. }
    13. }
    기대 출력종료 코드 0
    %
    %GG
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. %
    2. %GG

    Replace an invalid decoded UTF-8 byte in a query field

    링크
    main.tpzReplace an invalid decoded UTF-8 byte in a query field플레이그라운드 열기 →
    1. import std.url { URL }
    2. import std.io
    3. match (URL.parse("https://example.com/?x=%ff")) {
    4. case Ok(value) => {
    5. match (value.query().get("x")) {
    6. case Some(values) => io.writeLine(values[0])
    7. case None() => io.writeLine("missing")
    8. }
    9. io.writeLine(value.toString())
    10. }
    11. case Err(_) => io.writeLine("parse failed")
    12. }
    기대 출력종료 코드 0
    �
    https://example.com/?x=%ff
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. �
    2. https://example.com/?x=%ff

    Mutate a query snapshot without changing its URL

    링크
    main.tpzMutate a query snapshot without changing its URL플레이그라운드 열기 →
    1. import std.url { URL }
    2. import std.io
    3. match (URL.parse("https://example.com/?k=first")) {
    4. case Ok(value) => {
    5. let mut snapshot = value.query()
    6. snapshot.insert("k", ["changed", "again"])
    7. snapshot.insert("extra", ["local"])
    8. let reread = value.query()
    9. io.writeLine("{reread.length}")
    10. match (reread.get("k")) {
    11. case Some(values) => {
    12. for item in (values) {
    13. io.writeLine(item)
    14. }
    15. }
    16. case None() => io.writeLine("missing")
    17. }
    18. io.writeLine(value.toString())
    19. }
    20. case Err(_) => io.writeLine("parse failed")
    21. }
    기대 출력종료 코드 0
    1
    first
    https://example.com/?k=first
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 1
    2. first
    3. https://example.com/?k=first

    메뉴

    다운로드 →