iri-rs-core 3.3.7

Core types, parser, resolver and normalizer for URIs/IRIs (RFC 3986/3987). Borrowed and owned, allocation-conscious, SIMD/SWAR-accelerated.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! Relative reference resolution — oxiri port. Writes directly into output buffer.
use memchr::{memchr, memchr_iter, memrchr};

use crate::parse::Positions;

/// Resolve `relative` against `base`, appending result to `output_buffer`.
///
/// `output_buffer` is expected to be empty: the returned [`Positions`] are
/// offsets from its start.
pub fn resolve(base: (&str, Positions), relative: (&str, Positions), output_buffer: &mut String) -> Positions {
    let (base_iri, base_p) = base;
    let (rel_iri, rel_p) = relative;

    // RFC 3986 §5.2.2, absolute-reference branch: `T.path =
    // remove_dot_segments(R.path)`, so the reference cannot simply be copied
    // verbatim when its path carries dot segments.
    if rel_p.scheme_end != 0 {
        output_buffer.reserve_exact(rel_iri.len());
        if !path_has_dot_segments(&rel_iri[rel_p.authority_end..rel_p.path_end]) {
            output_buffer.push_str(rel_iri);
            return rel_p;
        }
        return rebuild_without_dot_segments(rel_iri, rel_p, rel_p.scheme_end, rel_p.authority_end, output_buffer);
    }
    // RFC 3986 §5.2.2, network-path branch: the base contributes only its
    // scheme, but `T.path = remove_dot_segments(R.path)` applies just the same.
    if rel_p.authority_end > 0 {
        output_buffer.reserve_exact(base_p.scheme_end + rel_iri.len());
        output_buffer.push_str(&base_iri[..base_p.scheme_end]);
        if !path_has_dot_segments(&rel_iri[rel_p.authority_end..rel_p.path_end]) {
            output_buffer.push_str(rel_iri);
            return Positions {
                scheme_end: base_p.scheme_end,
                authority_end: base_p.scheme_end + rel_p.authority_end,
                path_end: base_p.scheme_end + rel_p.path_end,
                query_end: base_p.scheme_end + rel_p.query_end,
            };
        }
        return rebuild_without_dot_segments(rel_iri, rel_p, base_p.scheme_end, base_p.scheme_end + rel_p.authority_end, output_buffer);
    }
    if rel_p.path_end > 0 {
        if rel_iri.starts_with('/') {
            output_buffer.reserve_exact(base_p.authority_end + rel_iri.len());
            output_buffer.push_str(&base_iri[..base_p.authority_end]);
            write_path_without_dot_segments_to(&rel_iri[..rel_p.path_end], output_buffer, base_p.authority_end, false);
        } else if base_p.authority_end > base_p.scheme_end && base_p.authority_end == base_p.path_end {
            output_buffer.reserve_exact(base_p.authority_end + 1 + (rel_iri.len() - rel_p.authority_end));
            output_buffer.push_str(&base_iri[..base_p.authority_end]);
            write_path_without_dot_segments_to(&rel_iri[rel_p.authority_end..rel_p.path_end], output_buffer, base_p.authority_end, true);
        } else if let Some(last_slash) = memrchr(b'/', &base_iri.as_bytes()[base_p.authority_end..base_p.path_end]) {
            output_buffer.reserve_exact(base_p.authority_end + last_slash + (rel_iri.len() - rel_p.authority_end) + 1);
            output_buffer.push_str(&base_iri[..base_p.authority_end]);
            if base_p.authority_end > 0 {
                write_path_without_dot_segments_to(&base_iri[base_p.authority_end..][..last_slash + 1], output_buffer, base_p.authority_end, false);
                let with_prefix_slash = if output_buffer.ends_with('/') {
                    output_buffer.pop();
                    true
                } else {
                    false
                };
                write_path_without_dot_segments_to(
                    &rel_iri[rel_p.authority_end..rel_p.path_end],
                    output_buffer,
                    base_p.authority_end,
                    with_prefix_slash,
                );
            } else {
                output_buffer.push_str(&base_iri[base_p.authority_end..][..last_slash + 1]);
                output_buffer.push_str(&rel_iri[rel_p.authority_end..rel_p.path_end]);
            }
        } else {
            output_buffer.reserve_exact(base_p.authority_end + (rel_iri.len() - rel_p.authority_end));
            output_buffer.push_str(&base_iri[..base_p.authority_end]);
            write_path_without_dot_segments_to(&rel_iri[rel_p.authority_end..rel_p.path_end], output_buffer, base_p.authority_end, false);
        }
        let path_end = output_buffer.len();
        output_buffer.push_str(&rel_iri[rel_p.path_end..]);
        // The only other branch that runs `remove_dot_segments` over the path,
        // and so the only other one that can leave it starting with `//`.
        return guard_ambiguous_path(
            output_buffer,
            Positions {
                scheme_end: base_p.scheme_end,
                authority_end: base_p.authority_end,
                path_end,
                query_end: path_end + (rel_p.query_end - rel_p.path_end),
            },
        );
    }
    if rel_p.query_end > 0 {
        output_buffer.reserve_exact(base_p.path_end + rel_iri.len());
        output_buffer.push_str(&base_iri[..base_p.path_end]);
        output_buffer.push_str(rel_iri);
        return Positions {
            scheme_end: base_p.scheme_end,
            authority_end: base_p.authority_end,
            path_end: base_p.path_end,
            query_end: base_p.path_end + rel_p.query_end,
        };
    }
    output_buffer.reserve_exact(base_p.query_end + rel_iri.len());
    output_buffer.push_str(&base_iri[..base_p.query_end]);
    output_buffer.push_str(rel_iri);
    base_p
}

/// Rebuilds a reference whose path carries dot segments: everything up to the
/// path is copied verbatim, the path goes through RFC 3986 §5.2.4
/// `remove_dot_segments`, and the query/fragment tail is copied verbatim.
///
/// `scheme_end`/`authority_end` are positions in the *output*; the caller has
/// already written any base prefix (and reserved capacity) before calling.
/// Outlined and `#[cold]` so the verbatim-copy fast path of [`resolve`] stays
/// small — references with dot segments are rare in practice.
#[cold]
#[inline(never)]
fn rebuild_without_dot_segments(rel_iri: &str, rel_p: Positions, scheme_end: usize, authority_end: usize, output_buffer: &mut String) -> Positions {
    output_buffer.push_str(&rel_iri[..rel_p.authority_end]);
    write_path_without_dot_segments_to(&rel_iri[rel_p.authority_end..rel_p.path_end], output_buffer, authority_end, false);
    let path_end = output_buffer.len();
    output_buffer.push_str(&rel_iri[rel_p.path_end..]);
    guard_ambiguous_path(
        output_buffer,
        Positions {
            scheme_end,
            authority_end,
            path_end,
            query_end: path_end + (rel_p.query_end - rel_p.path_end),
        },
    )
}

/// RFC 3986 §3.3: without an authority a path cannot begin with `//`, or
/// reparsing the result would read those bytes as one.
///
/// Only reachable from the two callers that ran `remove_dot_segments`: a
/// reference parsed with no authority never *starts* with `//` to begin with,
/// so nothing else can produce this shape and the verbatim-copy fast paths of
/// [`resolve`] skip the test entirely. When the result does carry an authority
/// the first comparison settles it, both operands already being in registers.
#[inline]
fn guard_ambiguous_path(output_buffer: &mut String, positions: Positions) -> Positions {
    if positions.authority_end == positions.scheme_end && output_buffer.as_bytes()[positions.authority_end..].starts_with(b"//") {
        return disambiguate_path(output_buffer, positions);
    }
    positions
}

/// Prefixes the path with the `/.` dot segment, which
/// [`write_path_without_dot_segments_to`] removes again on any later
/// resolution, leaving the path it denotes unchanged.
#[cold]
#[inline(never)]
fn disambiguate_path(output_buffer: &mut String, positions: Positions) -> Positions {
    output_buffer.insert_str(positions.authority_end, "/.");
    Positions {
        path_end: positions.path_end + 2,
        query_end: positions.query_end + 2,
        ..positions
    }
}

/// Checks whether the segment of `path` starting at `start` is a complete `.`
/// or `..` segment.
///
/// `start` must be the index of the first byte of a segment, i.e. either `0` or
/// one past a `/`.
#[inline]
const fn segment_is_dots(path: &[u8], start: usize) -> bool {
    // `const fn` forbids `matches!` over `Option<&u8>` patterns bound by a
    // slice index, so the bounds are checked explicitly.
    if start >= path.len() || path[start] != b'.' {
        return false;
    }
    if start + 1 == path.len() || path[start + 1] == b'/' {
        return true;
    }
    if path[start + 1] != b'.' {
        return false;
    }
    start + 2 == path.len() || path[start + 2] == b'/'
}

/// Checks whether `path` contains a `.` or `..` segment, i.e. whether RFC 3986
/// §5.2.4 `remove_dot_segments` would change it.
///
/// This guards the hot path of [`resolve`]: most references have a path with no
/// `.` byte at all, and a single SIMD scan rejects them outright so the caller
/// can keep its verbatim copy instead of walking the path segment by segment.
#[inline]
fn path_has_dot_segments(path: &str) -> bool {
    let path = path.as_bytes();
    match memchr(b'.', path) {
        None => false,
        Some(first_dot) => path_has_dot_segments_from(path, first_dot),
    }
}

/// Continuation of [`path_has_dot_segments`] once a `.` byte is known to exist
/// at `first_dot`. Kept out of line so only the SIMD reject inlines into
/// [`resolve`].
fn path_has_dot_segments_from(path: &[u8], first_dot: usize) -> bool {
    // A dot segment's first `.` sits either at the start of the path or right
    // after a `/`, so it suffices to probe byte 0 once and then visit each `.`
    // occurrence from `first_dot` on, checking whether it follows a `/`. The
    // resumable `memchr_iter` makes this a single SIMD pass regardless of how
    // many non-segment dots (file extensions, version numbers) the path holds.
    if first_dot == 0 && segment_is_dots(path, 0) {
        return true;
    }
    for index in memchr_iter(b'.', &path[first_dot..]) {
        let dot = first_dot + index;
        if dot > 0 && path[dot - 1] == b'/' && segment_is_dots(path, dot) {
            return true;
        }
    }
    false
}

fn write_path_without_dot_segments_to(mut input: &str, output: &mut String, output_path_start: usize, with_prefix_slash: bool) {
    // Dot-segment-free input — the overwhelmingly common case — needs no
    // segment-by-segment walk: `remove_dot_segments` is the identity on it, so
    // a single copy suffices. Callers passing `with_prefix_slash` guarantee a
    // rootless `input`, and the walk below would emit exactly one leading `/`.
    if !path_has_dot_segments(input) {
        if with_prefix_slash {
            output.push('/');
        }
        output.push_str(input);
        return;
    }
    if with_prefix_slash {
        if input.starts_with("./") {
            input = &input[1..];
        } else if input == "." {
            input = "/";
        } else if input.starts_with("../") {
            input = &input[2..];
            remove_last_segment(output, output_path_start);
        } else if input == ".." {
            input = "/";
            remove_last_segment(output, output_path_start);
        } else {
            output.push('/');
            let slash = memchr(b'/', input.as_bytes()).unwrap_or(input.len());
            output.push_str(&input[..slash]);
            input = &input[slash..];
        }
    }
    while !input.is_empty() {
        if let Some(rest) = input.strip_prefix("../") {
            input = rest;
        } else if let Some(rest) = input.strip_prefix("./") {
            input = rest;
        } else if input.starts_with("/./") {
            input = &input[2..];
        } else if input == "/." {
            input = "/";
        } else if input.starts_with("/../") {
            input = &input[3..];
            remove_last_segment(output, output_path_start);
        } else if input == "/.." {
            input = "/";
            remove_last_segment(output, output_path_start);
        } else if input == "." || input == ".." {
            input = "";
        } else {
            input = if let Some(rest) = input.strip_prefix('/') {
                output.push('/');
                rest
            } else {
                input
            };
            let slash = memchr(b'/', input.as_bytes()).unwrap_or(input.len());
            output.push_str(&input[..slash]);
            input = &input[slash..];
        }
    }
}

fn remove_last_segment(output: &mut String, output_path_start: usize) {
    let last = memrchr(b'/', &output.as_bytes()[output_path_start..]).unwrap_or(0);
    output.truncate(output_path_start + last);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse::{find_iri_positions, find_iri_ref_positions};

    /// RFC 3986 §5.4 base against which the reference vectors are resolved.
    const RFC3986_BASE: &str = "http://a/b/c/d;p?q";

    /// `@base` of the W3C JSON-LD expansion test `#t0062`.
    const T0062_BASE: &str = "http://example.com/some/deep/directory/and/file#with-a-fragment";

    /// Resolves `reference` against `base` and asserts that the [`Positions`]
    /// returned by [`resolve`] describe the produced string, which is the part
    /// of the contract a caller cannot re-derive cheaply.
    fn resolved(base: &str, reference: &str) -> String {
        let base_p = find_iri_positions(base);
        let rel_p = find_iri_ref_positions(reference);
        let mut output = String::new();
        let positions = resolve((base, base_p), (reference, rel_p), &mut output);
        assert_eq!(
            positions,
            find_iri_positions(&output),
            "positions returned for `{reference}` against `{base}` do not describe `{output}`"
        );
        output
    }

    #[test]
    fn rfc3986_normal_references_resolve() {
        for (reference, expected) in [
            ("g:h", "g:h"),
            ("g", "http://a/b/c/g"),
            ("./g", "http://a/b/c/g"),
            ("g/", "http://a/b/c/g/"),
            ("/g", "http://a/g"),
            ("//g", "http://g"),
            ("?y", "http://a/b/c/d;p?y"),
            ("g?y", "http://a/b/c/g?y"),
            ("#s", "http://a/b/c/d;p?q#s"),
            ("g#s", "http://a/b/c/g#s"),
            ("g?y#s", "http://a/b/c/g?y#s"),
            (";x", "http://a/b/c/;x"),
            ("g;x", "http://a/b/c/g;x"),
            ("g;x?y#s", "http://a/b/c/g;x?y#s"),
            ("", "http://a/b/c/d;p?q"),
            (".", "http://a/b/c/"),
            ("./", "http://a/b/c/"),
            ("..", "http://a/b/"),
            ("../", "http://a/b/"),
            ("../g", "http://a/b/g"),
            ("../..", "http://a/"),
            ("../../", "http://a/"),
            ("../../g", "http://a/g"),
        ] {
            assert_eq!(resolved(RFC3986_BASE, reference), expected, "reference `{reference}`");
        }
    }

    #[test]
    fn rfc3986_abnormal_references_resolve() {
        for (reference, expected) in [
            ("../../../g", "http://a/g"),
            ("../../../../g", "http://a/g"),
            ("/./g", "http://a/g"),
            ("/../g", "http://a/g"),
            ("g.", "http://a/b/c/g."),
            (".g", "http://a/b/c/.g"),
            ("g..", "http://a/b/c/g.."),
            ("..g", "http://a/b/c/..g"),
            ("./../g", "http://a/b/g"),
            ("./g/.", "http://a/b/c/g/"),
            ("g/./h", "http://a/b/c/g/h"),
            ("g/../h", "http://a/b/c/h"),
            ("g;x=1/./y", "http://a/b/c/g;x=1/y"),
            ("g;x=1/../y", "http://a/b/c/y"),
            ("g?y/./x", "http://a/b/c/g?y/./x"),
            ("g?y/../x", "http://a/b/c/g?y/../x"),
            ("g#s/./x", "http://a/b/c/g#s/./x"),
            ("g#s/../x", "http://a/b/c/g#s/../x"),
            ("http:g", "http:g"),
        ] {
            assert_eq!(resolved(RFC3986_BASE, reference), expected, "reference `{reference}`");
        }
    }

    #[test]
    fn dot_segments_in_the_base_path_are_removed() {
        for (reference, expected) in [
            ("..", "http://a/bb/"),
            ("../", "http://a/bb/"),
            ("../g", "http://a/bb/g"),
            ("../..", "http://a/"),
            ("../../", "http://a/"),
            ("../../g", "http://a/g"),
        ] {
            assert_eq!(resolved("http://a/bb/ccc/./d;p?q", reference), expected, "reference `{reference}`");
        }
        assert_eq!(resolved("http://a/bb/ccc/../d;p?q", "../../"), "http://a/");
    }

    #[test]
    fn empty_segments_in_the_base_path_are_preserved() {
        for (reference, expected) in [("xyz", "http://ab//de//xyz"), ("./xyz", "http://ab//de//xyz"), ("../xyz", "http://ab//de/xyz")] {
            assert_eq!(resolved("http://ab//de//ghi", reference), expected, "reference `{reference}`");
        }
    }

    /// RFC 3986 §5.2.3: a base with an authority and an empty path merges as if
    /// its path were `/`.
    #[test]
    fn references_resolve_against_an_empty_base_path() {
        for (reference, expected) in [
            (".", "http://a/"),
            ("./", "http://a/"),
            ("..", "http://a/"),
            ("../", "http://a/"),
            ("../g", "http://a/g"),
            ("g", "http://a/g"),
            ("g/", "http://a/g/"),
            ("g/h", "http://a/g/h"),
            ("?q", "http://a?q"),
            ("#f", "http://a#f"),
        ] {
            assert_eq!(resolved("http://a", reference), expected, "reference `{reference}`");
        }
    }

    /// <https://github.com/timothee-haudebourg/iref/issues/14>
    #[test]
    fn rootless_base_path_keeps_its_colons() {
        assert_eq!(resolved("scheme:a:b/", "Foo"), "scheme:a:b/Foo");
    }

    #[test]
    fn longer_base_segments_resolve() {
        for (reference, expected) in [("#s", "http://a/bb/ccc/d;p?q#s"), ("", "http://a/bb/ccc/d;p?q")] {
            assert_eq!(resolved("http://a/bb/ccc/d;p?q", reference), expected, "reference `{reference}`");
        }
    }

    #[test]
    fn resolved_path_starting_with_two_slashes_is_disambiguated() {
        // `remove_dot_segments` leaves the path `//`, and the base has no
        // authority for those slashes to belong to. Without the `/.` the result
        // would read back as `http://` — an empty authority and an empty path.
        assert_eq!(resolved("http:/a/b", "../..//"), "http:/.//");

        let disambiguated = resolved("http:/a/b", "../..//");
        let positions = find_iri_positions(&disambiguated);
        assert_eq!(positions.authority_end, positions.scheme_end, "the result must still have no authority");
        assert_eq!(&disambiguated[positions.authority_end..positions.path_end], "/.//");
    }

    #[test]
    fn disambiguated_path_denotes_the_same_path_on_re_resolution() {
        // The inserted `/.` is a dot segment, so resolving the result again
        // against itself is a fixed point rather than a growing prefix.
        assert_eq!(resolved("http:/.//", ""), "http:/.//");
        assert_eq!(resolved("http:/.//", "."), "http:/.//");
    }

    #[test]
    fn network_path_reference_removes_dot_segments() {
        // The two entries of the W3C `#t0062` expansion test that a verbatim
        // copy of the reference got wrong.
        assert_eq!(resolved(T0062_BASE, "//example.org/../scheme-relative"), "http://example.org/scheme-relative");
        assert_eq!(
            resolved(T0062_BASE, "//example.org/.././useless/../../scheme-relative"),
            "http://example.org/scheme-relative"
        );
    }

    #[test]
    fn network_path_reference_without_dot_segments_is_copied_verbatim() {
        assert_eq!(resolved(T0062_BASE, "//example.org/scheme-relative"), "http://example.org/scheme-relative");
        assert_eq!(resolved(RFC3986_BASE, "//g/a.b/c..d"), "http://g/a.b/c..d");
    }

    #[test]
    fn network_path_reference_keeps_query_and_fragment_after_dot_removal() {
        assert_eq!(resolved(RFC3986_BASE, "//g/a/../b?y=1#s"), "http://g/b?y=1#s");
    }

    #[test]
    fn absolute_reference_removes_dot_segments() {
        assert_eq!(resolved(RFC3986_BASE, "http://a/b/../c"), "http://a/c");
        assert_eq!(resolved(RFC3986_BASE, "http://a/./b/./c"), "http://a/b/c");
        assert_eq!(resolved(RFC3986_BASE, "http://a/b/c/../../.."), "http://a/");
    }

    #[test]
    fn absolute_reference_without_dot_segments_is_copied_verbatim() {
        assert_eq!(resolved(RFC3986_BASE, "https://example.com/a/b?q=.#f."), "https://example.com/a/b?q=.#f.");
        assert_eq!(resolved(RFC3986_BASE, "http://a/b.c/d..e/.f"), "http://a/b.c/d..e/.f");
    }

    #[test]
    fn absolute_reference_keeps_query_and_fragment_after_dot_removal() {
        assert_eq!(resolved(RFC3986_BASE, "http://a/b/../c?y=1#s"), "http://a/c?y=1#s");
    }

    #[test]
    fn absolute_rootless_reference_removes_dot_segments() {
        // RFC 3986 §5.2.4 applied to a rootless path: `foo` is dropped by the
        // following `..`, leaving an absolute path.
        assert_eq!(resolved(RFC3986_BASE, "urn:foo/../bar"), "urn:/bar");
    }

    #[test]
    fn dot_segment_detection_rejects_paths_without_dot_segments() {
        for path in [
            "", "/", "/a/b/c", "/a.b/c.d", "/..a/b", "/a../b", "/a/..b", "/a/b..", "/...", "/a/.../b", "foo", ".foo", "..foo",
        ] {
            assert!(!path_has_dot_segments(path), "path `{path}`");
        }
    }

    #[test]
    fn dot_segment_detection_accepts_paths_with_dot_segments() {
        for path in [
            ".",
            "..",
            "./",
            "../",
            "./a",
            "../a",
            "/.",
            "/..",
            "/./",
            "/../",
            "/a/./b",
            "/a/../b",
            "/a/.",
            "/a/..",
            "/a.b/../c",
        ] {
            assert!(path_has_dot_segments(path), "path `{path}`");
        }
    }
}