Skip to content

Strings ​

JavaScript's strings, in Rust.

A JavaScript string is a sequence of UTF-16 code units; a Rust str is UTF-8. Vue renders with JavaScript's string methods, so wherever the two would differ the generated code calls a Rust twin that counts and compares as JavaScript does. Each twin is held to vectors recorded from JavaScript. The twins are #[doc(hidden)]: this page describes what they do, and their signatures follow the version of generated code, outside the crate's stable API.

Counting in UTF-16 code units ​

.length, and every index and length a string method takes or returns, counts UTF-16 code units: a character beyond U+FFFF, such as 🦀, is two. So "🦀".length is 2 and "🦀 crab".slice(3) is "crab", where Rust's byte indices would make the first 4 and the second a panic.

vue
<!-- client/components/Initials.vue -->
<script setup lang="ts">
defineProps<{ name: string; id: number }>();
</script>

<template>
  <p :title="name.toUpperCase()">{{ name.slice(0, 1) }}. #{{ String(id).padStart(4, "0") }} {{ name < "N" ? "A–M" : "N–Z" }}</p>
</template>
rust
/// Write the component's server render into `out`.
pub fn render(out: &mut String, props: &Props<'_>) {
    out.reserve(
        26
        + props.name.len()
    );
    out.push_str("<p");
    if props.name.to_uppercase().is_empty() {
        out.push_str(" title");
    } else {
        out.push_str(" title=\"");
        fv::core::escape_into(out, &props.name.to_uppercase());
        out.push('"');
    }
    out.push('>');
    fv::core::escape_into(out, &fv::strings::js_slice(&props.name, 0.0f64, Some(1.0f64)));
    out.push_str(". #");
    fv::core::escape_into(out, &fv::strings::js_pad_start(&fv::core::Js(props.id).to_string(), 4.0f64, "0"));
    out.push(' ');
    fv::core::escape_into(out, if fv::strings::js_cmp(&props.name, "N").is_lt() { "A–M" } else { "N–Z" });
    out.push_str("</p>");
}
let mut out = String::new();
initials::render(&mut out, &initials::Props::new("Ada", 7));
assert_eq!(out, r#"<p title="ADA">A. #0007 A–M</p>"#);

An index is an f64, because it is a JavaScript number: a fraction is truncated, NaN is 0, and where JavaScript counts a negative index from the end, so does the twin. A position or a limit that is undefined, or an optional number that is absent, is the default JavaScript takes for it (the start, or the end for .endsWith() and .lastIndexOf()).

JavaScriptRust
s.lengthjs_length
s.trim(), s.trimStart(), s.trimEnd(), and s.trimLeft(), s.trimRight()js_trim, js_trim_start, js_trim_end: JavaScript's whitespace, which is not Rust's
s.slice(start, end)js_slice
s.substring(start, end)js_substring
s.substr(start, length)js_substr: start counts from the end when negative
s.at(i), s.charAt(i)js_at (None for undefined), js_char_at
s.charCodeAt(i), s.codePointAt(i)js_char_code_at (NaN outside the string), js_code_point_at (None for undefined; inside a pair, the second half alone)
s.indexOf(x), s.lastIndexOf(x)js_index_of, js_last_index_of
s.indexOf(x, i), s.lastIndexOf(x, i)js_index_of_from, js_last_index_of_from: "" is found at i itself, and a NaN position for .lastIndexOf() is the end
s.split(x), s.split(x, n)js_split, js_split_limit: the first n pieces, n taken as ToUint32 takes it (-1 is 4294967295, 2³² is 0)
s.concat(a, b)a format!, each argument written as String() writes it
s.replace(x, y), s.replaceAll(x, y)js_replace, js_replace_all, with the $&, $`, $' and $$ patterns JavaScript reads in y
s.padStart(n, x), s.padEnd(n, x)js_pad_start, js_pad_end
s.repeat(n)js_repeat
s.includes(x), s.startsWith(x), s.endsWith(x)str::contains, starts_with, ends_with: they agree
s.includes(x, i), s.startsWith(x, i), s.endsWith(x, i)js_includes_from, js_starts_with_at, js_ends_with_at: inside a pair only "" starts or ends
s.toUpperCase(), s.toLowerCase()str::to_uppercase, to_lowercase: they agree, ß becoming SS included
rust
use ferrovue::strings::{js_index_of, js_length, js_pad_start, js_slice, js_split};

assert_eq!(js_length("🦀 crab"), 7);
assert_eq!(js_slice("🦀 crab", 3.0, None), "crab");
assert_eq!(js_index_of("🦀 crab", "crab"), 3);
assert_eq!(js_split("a,b,", ","), ["a", "b", ""]);
assert_eq!(js_pad_start("7", 3.0, "0"), "007");

A position inside a surrogate pair gives what JavaScript gives: a search string that holds no half cannot start at the pair's second half, so "🦀x".indexOf("x", 1) is 2 and "🦀x".startsWith("x", 1) is false, while "🦀".indexOf("", 1) is 1.

rust
assert_eq!(ferrovue::strings::js_index_of_from("🦀x", "x", 1.0), 2);
assert!(!ferrovue::strings::js_starts_with_at("🦀x", "x", 1.0));
assert_eq!(ferrovue::strings::js_split_limit("a,b,c", ",", 2.0), ["a", "b"]);
assert_eq!(ferrovue::strings::js_substr("ferrovue", -3.0, None), "vue");
assert_eq!(ferrovue::strings::js_code_point_at("🦀", 0.0), Some(0x1F980));

A pattern is a string, or a regular expression literal of the subset below. A replacement function is refused, as are .toLocaleUpperCase() and .toLocaleLowerCase(), since the server's locale is not the browser's, and .localeCompare() and .toLocaleString(), which format with Intl (dates_and_prices).

Reading every index ​

s[i], .at(i), .charAt(i), .charCodeAt(i) and .codePointAt(i) of a prop, at an index that is not a literal, read through a UnitCursor: one for each place in the template that reads, declared at the top of the render. Each read walks from where that place's last read ended, so a v-for over every index of a prop, forwards or backwards, takes time linear in its length, as in V8. A read in a computed, a function or setup, or of a string the render builds, counts its index from the start of the string.

rust
let units = ferrovue::strings::UnitCursor::new();
let codes: Vec<f64> = (0..4).map(|i| units.char_code_at("é🦀a", f64::from(i))).collect();
assert_eq!(codes, [233.0, 55358.0, 56704.0, 97.0]);
assert_eq!(units.at("é🦀a", -1.0), Some("a"));

Regular expressions ​

.replace(), .replaceAll() and .split() take a regular expression literal, and a literal's .test(s) tests a string, in a subset whose matching is JavaScript's without the u flag, over UTF-16 code units: JsRegex runs it, held once for the program's life, and the vectors it is tested against are recorded from V8.

PartThe subset holds
Charactersliterals, \t, \n, \v, \f, \r, \0, \xHH, \uHHHH, and a \ before a character of the syntax
Classes. (any unit but a line terminator), [abc], [a-z], [^…], \d, \w, \s and \D, \W, \S, also inside a class
Assertions^ and $ (the start and end of the string), \b and \B
Quantifiers*, +, ?, {n}, {n,}, {n,m}, greedy or lazy (*?), a bound above 2147483647 counting as 2147483647 as in V8
Flagsg (every match, for .replace(); .replaceAll() needs it) and i (case folded as JavaScript folds it without u)

A replacement reads $&, $`, $' and $$ as JavaScript reads them; $1 is written as it is, since the subset has no groups. Everything else is refused where it is written (FV0710): groups, alternation (a|b), backreferences, lookarounds, the flags m, s, u, y and d, a regular expression held in a constant, and methods other than .test(). A regular expression whose match may begin or end between the halves of a surrogate pair (. alone, an empty match) gives a string that may hold half of one, which is refused where that matters, as below.

rust
let slug = ferrovue::strings::JsRegex::new("[^a-z0-9]+", "g");
assert_eq!(slug.replace("the rust book", "-"), "the-rust-book");
assert!(ferrovue::strings::JsRegex::new("^\\d+$", "").test("2026"));
assert_eq!(ferrovue::strings::JsRegex::new(r"\s*,\s*", "").split("a , b,c", None), ["a", "b", "c"]);

Halves of surrogate pairs ​

A JavaScript string can hold half of a surrogate pair: "🦀".slice(0, 1), "🦀".charAt(1), "🦀".split(""). A Rust string cannot. The twins write U+FFFD in its place, which is exactly what the page carries anyway: a server sends Vue's string as UTF-8, and UTF-8 writes each half as U+FFFD (res.end, Buffer.from and TextEncoder all do). The length in code units is the same.

rust
assert_eq!(ferrovue::strings::js_slice("🦀x", 1.0, None), "\u{FFFD}x");
assert_eq!(ferrovue::strings::js_char_at("🦀", 0.0), "\u{FFFD}");
assert_eq!(ferrovue::strings::js_split("🦀", ""), ["\u{FFFD}", "\u{FFFD}"]);

A string known to hold ASCII alone has no pairs to cut: the string of a number (String(n), n.toFixed(2), `${n}`), an ASCII literal, and what slicing, trimming, padding, case mapping, joining or split make of them. Its pieces are whole characters, which may be joined, compared and counted as any string is, so a thousands separator can be written in the template:

ts
function grouped(n: number): string {
  const d = String(n);
  return d.split("").reduce((acc, ch, i) => (i > 0 && (d.length - i) % 3 === 0 ? `${acc},${ch}` : `${acc}${ch}`), "");
}

Where the half itself would decide the result, the compiler refuses: two strings that may each hold a half compared or ordered, side by side (a.slice(0, 1) + b.slice(1), a .join(""), a class object's names), one searched for in another (.includes(a.charAt(0))), repeated or padded where halves would join (beside one another, or with only a string that may be empty between them), ordered against anything but a literal below U+D800, read by .charCodeAt() or .codePointAt() (which would read U+FFFD's code), or written by JSON.stringify (which escapes it), by encodeURIComponent() and encodeURI() or into a <RouterLink> (whose encoding throws). Three corners remain, by design:

  • a string holding U+FFFD in the props compares equal to a half, or finds one, where JavaScript would tell them apart;
  • a half handed to a child component or a helper is U+FFFD there, which matters only if that component then compares or joins it as above;
  • Vue itself cannot hydrate such text cleanly: the browser reads U+FFFD where its own render holds the half, and reports a mismatch.

Ordering ​

<, >, <= and >= between two strings order them by UTF-16 code unit, as JavaScript does, with js_cmp. That is not the order of the characters: every character from U+E000 to U+FFFF comes after one beyond U+FFFF, whose first code unit is a surrogate.

rust
use std::cmp::Ordering;

assert_eq!(ferrovue::strings::js_cmp("B", "a"), Ordering::Less); // by code unit, not alphabetically
assert_eq!(ferrovue::strings::js_cmp("~", "🦀"), Ordering::Greater); // U+FF5E, after 0xD83E
assert_eq!("~".cmp("🦀"), Ordering::Less); // Rust's own order

Ordering a string against a number is refused: JavaScript converts the string to a number first.

Numbers from strings, and strings from values ​

JavaScriptRust
Number(s)js_number: JavaScript's whitespace trimmed, "" is 0, 0x, 0o and 0b read, anything else that is not a number NaN
parseInt(s), parseInt(s, 10), parseInt(s, 16)js_parse_int: the leading digits, or NaN
parseFloat(s)js_parse_float: the longest decimal literal at the start, or NaN
JSON.stringify(s) of a stringjs_json_string
JSON.stringify(x) of a numberjs_json_number: null for NaN and the infinities
String(x), `${x}`, x.toString()push_number, Js. See numbers
String(x), `${x}`, s.concat(x) of a value that may be absentundefined, or null for a value typed T | null
String(xs), `${xs}` of a listthe items joined by ,, as xs.join() joins them: an item that is null or undefined as nothing

{{ x }} is not String(x): Vue writes an absent value as nothing and a list as JSON, two spaces deep (toDisplayString), so {{ label }} of an absent label writes nothing where {{ `${label}` }} writes undefined, and {{ tags }} of a list is refused where {{ `${tags}` }} writes a,b. A value that may be either null or undefined, which its Rust Option cannot tell apart, is refused (FV0721): give it a fallback with ??.

Of a string, all three give a Float, since the result may be a fraction or NaN. JSON.stringify also takes a boolean and a list of strings, numbers or booleans, which the generated code writes between [ and ] with the twins above. parseInt with another radix, or of a number, is refused.

rust
assert_eq!(ferrovue::strings::js_number(" 0x1F "), 31.0);
assert_eq!(ferrovue::strings::js_parse_int("  42px", 10), 42.0);
assert_eq!(ferrovue::strings::js_parse_float(" 1.5e3px"), 1500.0);
assert_eq!(ferrovue::strings::js_json_string("say \"hi\""), r#""say \"hi\"""#);
assert_eq!(ferrovue::strings::js_json_number(f64::NAN), "null");

URIs ​

encodeURIComponent(s) and encodeURI(s) write each byte of a character's UTF-8 as %XX, the hexadecimal digits in upper case, except the characters JavaScript keeps: js_encode_uri_component keeps the ASCII letters and digits and -_.!~*'(), and js_encode_uri the characters a URI reserves as well, ;/?:@&=+$,#. Both are held to uri.json, every ASCII character and a few hundred strings drawn at random, recorded from Node and read alike by Chromium and Firefox. Their argument is a string, or anything String() writes (encodeURIComponent(page) of a number). JavaScript throws a URIError on half of a surrogate pair, which ferrovue holds as U+FFFD, so a string that may hold one is refused (FV0706). decodeURI() and decodeURIComponent(), which throw on a malformed escape, are refused too.

rust
assert_eq!(ferrovue::core::js_encode_uri_component("rust & vue/é"), "rust%20%26%20vue%2F%C3%A9");
assert_eq!(ferrovue::core::js_encode_uri("/a b?q=1&r=[2]#top"), "/a%20b?q=1&r=%5B2%5D#top");

Slicing lists ​

A list's .slice(start, end) takes the same range as a string's, counted in items: js_slice_items takes the items js_slice_range gives. .at(i) is the item js_item_at picks, counted from the end when i is negative and undefined outside the list, and list[i] with an integer i is list.get(i), undefined for a negative i too. The other array methods with an arrow function, .filter(), .map(), .some(), .every(), .find(), .findIndex(), .findLast() and .findLastIndex(), become Rust iterator adaptors, as they have no JavaScript corners to reproduce. Given Boolean in place of an arrow function, each takes the item's truthiness, as x => !!x does: .filter(Boolean) drops empty strings, 0, NaN and false, and, over a list or an array literal whose items may be null or undefined, the absent ones too, leaving a list of the values. .indexOf() and .lastIndexOf() compare with ===: NaN is found nowhere, though .includes() finds it, and -0 is found as 0. .concat() chains the items of lists of one item type, numbers whether number or Float, and so does a spread in an array literal, [...a, x, ...b].

Building lists ​

.reduce(f, init) and .reduceRight(f, init) fold the items with js_reduce, from the end for .reduceRight(), keeping the index each item has in the list. The accumulator's type is fixed when compiling, from the initial value and the function's body: an integer start whose body gives a fraction, or may give -0 (acc * -x), accumulates a Float, and a string grows by +, under the rules + follows for halves of surrogate pairs. Without an initial value JavaScript throws on an empty list, so both are refused without one.

.toSorted() without a comparator orders the items by String(x), compared by UTF-16 code unit as js_cmp compares them: js_sorted_by_string sorts [10, 9, 1] to [1, 10, 9], puts NaN after Infinity and leaves -0 and 0, both "0", in their order. With a comparator, the compiler accepts the shapes whose order it can reproduce for every input: a - b and b - a of integers or of integer fields (a.n - b.n), whose sign js_order takes, and ?: over <, >, <=, >=, === and !== of integers or strings, giving literals whose signs make a consistent order (a < b ? -1 : a > b ? 1 : 0). js_sorted sorts by it, and, as V8 and SpiderMonkey do, keeps the items it finds equal in their order. A Float key is refused: once a NaN is among the keys, the comparator is inconsistent and each engine orders the items its own way. So is localeCompare, which depends on the locale. .toReversed() is the items from the end, and [...list].sort(…) and [...list].reverse() are the same as .toSorted(…) and .toReversed(); .sort() and .reverse() of a prop or state would change it in place, and are refused.

.flat() joins the inner lists of a list of lists, one level deep or to the depth it is given, which is a literal number or Infinity since the type of the result depends on it, and .flatMap(f) joins the lists f gives, keeping a value that is not a list as .map() keeps it. Both become Rust's flat_map; nothing they flatten is skipped, as JavaScript skips only the holes of a sparse array, which a prop or an array literal the compiler accepts never has.

Math.max(...xs) and Math.min(...xs) fold the items with js_max and js_min from -Infinity and Infinity, which they give for an empty list, NaN when any item is, and 0 above -0.

rust
use std::cmp::Ordering;

let words = ["a", "b", "c", "d"];
let last_two: Vec<&str> = ferrovue::strings::js_slice_items(words.iter().copied(), -2.0, None).collect();
assert_eq!(last_two, ["c", "d"]);
assert_eq!(ferrovue::strings::js_item_at(words.iter().copied(), -1.0), Some("d"));
assert_eq!(ferrovue::strings::js_item_at(words.iter().copied(), 4.0), None);

let sorted: Vec<i64> = ferrovue::strings::js_sorted_by_string([10, 9, 1].into_iter(), |v| ferrovue::core::Js(*v).to_string()).collect();
assert_eq!(sorted, [1, 10, 9]);
let by_value: Vec<i64> = ferrovue::strings::js_sorted([10, 9, 1].into_iter(), |a, b| ferrovue::strings::js_order(*a as f64, *b as f64)).collect();
assert_eq!(by_value, [1, 9, 10]);
assert_eq!(ferrovue::strings::js_order(f64::NAN, 1.0), Ordering::Equal);
assert_eq!(ferrovue::strings::js_reduce(words.iter().rev(), String::new(), |acc, w| acc + w), "dcba");
assert!([0.0, -0.0].into_iter().fold(f64::INFINITY, ferrovue::core::js_min).is_sign_negative());

Panics ​

Where JavaScript throws, Vue's render fails, and the generated code refuses the render: a negative or infinite .repeat() count (FV0714), and a .repeat(), .padStart() or .padEnd() longer than the longest string V8 makes, 2²⁹ − 24 code units (FV0725). The generated try_render returns the refusal, and render panics with its message (see errors_and_limits). A .repeat() by a negative literal is refused at compile time. A computed that calls one of them is worked out where the render first reads it, once, as Vue works it out, so it is refused only when the render reads it.