docs

您正在閱讀 v0.1.0 的文件。Nabi 0.1.0 已停止支援。 最新版本:v0.1.3 →

本頁尚未翻譯,以下為英文原文。

#Strings and f-strings

本頁內容

Strings show up in almost every program. This chapter covers str and StrBuf, the commonly used methods, the different kinds of literals, and the format specifiers of the f-strings you have been using all along.

#str: immutable UTF-8

A str is a sequence of UTF-8 bytes and never changes once created. That is why len() returns the number of bytes, not characters. To count characters (code points), use char_count().

nabi
let s = "hello, 세계";
println(f"{s.len()} bytes, {s.char_count()} chars");
println(s[0..5]);

for c in "가나".chars() {
    println(f"{c} = U+{c as u32:X}");
}
13 bytes, 9 chars
hello
가 = U+AC00
나 = U+B098

Here the Korean text is deliberate: each Hangul syllable takes 3 bytes in UTF-8, so "hello, 세계" is 13 bytes but only 9 characters.

A slice s[a..b] also cuts at byte positions. Since a Hangul character is 3 bytes, you must not cut anywhere other than a character boundary; a cut inside a character panics and names the byte:

nabi
let s = "hello, 세계";
println(s[0..8]);
text
thread 'main' panicked at main.nabi:2:9:
string slice is not on a char boundary: byte 8 is inside `세` (bytes 7..10)
string offsets count bytes; to work by character, use `chars()`, `char_indices()` or `char_slice(a, b)`
to cut at a boundary, use `floor_char_boundary(i)`; `get(a..b)` gives `none` instead of a panic

To work character by character, use chars() or char_indices(). When an offset may land inside a character, get(a..b) returns none instead of panicking, floor_char_boundary(i) moves the offset back to the start of its character, and char_slice(a, b) counts characters instead of bytes:

nabi
let s = "hello, 세계";
println(f"{s.get(0..8) ?? "none"} {s[0:s.floor_char_boundary(8)]} {s.char_slice(7, 8)}");
none hello,  세

A single character is a char, written in single quotes. It holds one Unicode scalar value and has methods for classification and conversion.

nabi
let c = 'A';
println(f"{c} {c.is_ascii_uppercase()} {c.to_ascii_lowercase()}");
println(f"{'7'.is_ascii_digit()} {'세'.len_utf8()} {'\u{1F600}'}");
println(f"{'ß'.to_uppercase()} {'ß'.to_simple_uppercase()} {'7'.to_digit(10) ?? 0} {'x'.to_digit(10) is none}");
A true a
true 3 😀
SS ß 7 true

Case mapping can turn one character into several, so to_uppercase, to_lowercase and to_titlecase return a str ('ß' becomes "SS"). When you need exactly one char back, use to_simple_uppercase and the other to_simple_* forms, which leave a character without a one-character mapping as it is. to_digit returns none for a character that is not a digit of the radix.

#Common methods

Methods on str do not modify the original; they return a new value. Operations that may find nothing return T | none, so they pair well with ?? and if let from chapter 5.

nabi
let line = "  name = nabi  ";
let t = line.trim();
println(f"[{t}]");
println(f"{t.starts_with("name")} {t.contains("=")} {t.find("nabi") ?? 99}");

if let (k, v) = t.split_once(" = ") {
    println(f"key={k} value={v}");
}

let parts = "a,b,,c".split(",").to_list();
println(f"{parts:?}");
println(f"{"ab".repeat(3)} {t.to_uppercase()} {t.replace("nabi", "NABI")}");
println(f"{"stressed".reversed()} {"banana".count("an")} {"a-b-c".replacen("-", "+", 1)} {"hello nabi".to_titlecase()}");
println(42.to_str() + "px");

let n: i32 = try! "42".parse();
println(f"{n + 1}");
[name = nabi]
true true 7
key=name value=nabi
["a", "b", "", "c"]
ababab NAME = NABI name = NABI
desserts 2 a+b-c Hello Nabi
42px
43

reversed reverses the characters, count counts non-overlapping matches, and replacen replaces only the first n. to_str() works on every Display value, as Python's str(x) does. parse is a function that throws an error on failure, so the example adds try!. Errors are covered in the next chapter. Other methods include lines(), split_whitespace(), strip_prefix(), and trim_start() (str API).

#Unicode text: normalization and graphemes

The same text can be spelled with different code points: "é" is one code point or an e followed by a combining accent. == compares bytes, so normalize text you compare or store with normalize(Nfc). For comparing without case, casefold() (or eq_ignore_case) folds "ß" to "ss" as to_lowercase does not. What a reader sees as one character can be several code points; graphemes() yields those clusters, and words() the words.

nabi
let a = "caf\u{e9}";
let b = "cafe\u{301}";
println(f"{a == b} {a == b.normalize(Nfc)} {"Straße".eq_ignore_case("STRASSE")}");

let s = "e\u{301}🇰🇷👩\u{200D}💻";
println(f"{s.char_count()} chars, {s.grapheme_count()} graphemes");
println(f"{"can't stop, 3.14!".words().to_list():?}");
false true true
7 chars, 3 graphemes
["can't", "stop", "3.14"]

grapheme_slice(0, n) keeps the first n graphemes, so a shortened title never cuts an accent or a flag in half. The forms, folding and segmentation rules are in the str reference.

#StrBuf: building piece by piece

Because str is immutable, concatenating with + in a loop creates a new string every time. When building a string piece by piece, write into a StrBuf buffer and take the result out with to_str() when done.

nabi
let b = StrBuf::new();
for i in 1..=5 {
    if !b.is_empty() { b.push_str(", "); }
    b.push_str(f"{i * i}");
}
b.push('!');
let s = b.to_str();
println(f"{s} ({s.len()} bytes)");
1, 4, 9, 16, 25! (16 bytes)

For joining two strings once, + is enough; it also appends a char (s + '!'), as s += '!' does. Unlike C or Python, however, adjacent string literals ("abc" "def") are not joined automatically. Writing that is a compile error; if you want to join them, write + (1.7.4).

nabi
let greeting = "hello, " + "world" + '!';
println(greeting);
hello, world!

#Literals: escapes, raw strings, line continuation

Ordinary strings process escapes such as \n, \t, \", \\, and \u{..}. For paths or regular expressions full of backslashes, raw strings r"..." are more convenient. If you need a " inside, surround the string with the same number of # on each side.

nabi
println("tab:\t| quote:\" | snow: \u{2603}");
println(r"C:\path\to\file\n");
println(r#"she said "hi""#);
println("one long \
         line");
tab:	| quote:" | snow: ☃
C:\path\to\file\n
she said "hi"
one long line

As in the last example, a \ at the end of a line removes both the newline and the leading whitespace of the next line. This rule does not apply in raw strings either (1.7.2).

#Byte strings b"..."

Protocols and file formats speak bytes, not text. A byte string b"..." is a fixed array [u8; N] of its bytes. Write ASCII directly and anything else as \xHH (any value \x00-\xFF). Where a Slice<u8> is expected, the same literal is a view of that array, so it can be passed straight to functions that take bytes.

nabi
let magic = b"\x7FELF";                  // [u8; 4]
println(f"{magic.len()} {magic:?}");

fn starts_with(data: Slice<u8>, prefix: Slice<u8>) -> bool {
    data.len() >= prefix.len() && data[:prefix.len()] == prefix
}
println(f"{starts_with(b"\x7FELF\x02\x01", b"\x7FELF")}");

let req: Slice<u8> = b"GET / HTTP/1.1\r\n";
println(f"{req[:3]:?} {try! str::from_utf8(req[:3])}");
4 [127, 69, 76, 70]
true
[71, 69, 84] GET

A non-ASCII character written directly in a byte string, or an escape above \xFF, is a compile error; line continuation is not supported.

A single byte is written b'0': a u8 with the same rules (b'\n', b'\xFF'), handy where a parser compares bytes.

nabi
let digits = "a1b22".as_bytes().filter(|c| b'0' <= c <= b'9').count();
println(f"{b'0'} {digits}");
48 3

#Embedding files: include_str! / include_bytes!

To ship a template, a test fixture or an icon inside the executable, embed the file at compile time. include_str!("path") is the file's contents as a str, and include_bytes!("path") its bytes as a [u8; N], like a byte string. The path is relative to the source file that contains the call, and the result is a constant, so it can initialize a const or static.

text
app/
└── src/
    ├── main.nabi
    └── page.html        // <h1>{title}</h1>
app/src/main.nabi
// app/src/main.nabi
const PAGE: str = include_str!("page.html");
static LOGO: [u8; 1406] = include_bytes!("logo.png");

print(PAGE.replace("{title}", "Hello"));

A missing file is a compile error, and so is a file that is not valid UTF-8 for include_str!. Editing the included file rebuilds what uses it.

#f-string holes

Any expression can go inside the { } of an f"...". The rules are the same as Python 3.12 (PEP 701): you can reuse the outer quote character inside a hole, and f-strings can be nested. To write a literal brace, use {{ and }}.

nabi
let d = {"lang": "nabi"};
let words = ["a", "b"];
println(f"{d["lang"]} has {words.len()} words: {words[0] + words[1]}");
println(f"nested: {f"[{1 + 1}]"}");
println(f"{{literal braces}}");
nabi has 2 words: ab
nested: [2]
{literal braces}

Inside a hole you do not use escapes like \"; just write the quotes as they are. For text or holes that span multiple lines, use f"""...""".

#Format specifiers

Put a : after the expression in a hole to specify a format. The syntax is [fill][align][sign][#][0][width][grouping][.precision][type].

nabi
let pi = 3.14159;
println(f"[{pi:.2}] [{pi:8.3}] [{pi:e}]");
println(f"[{42:>6}] [{42:<6}] [{42:^6}] [{42:*^6}]");
println(f"[{"ab":5}] [{"ab":>5}] [{7:03}] [{5:+}]");
println(f"{255:x} {255:#X} {5:b} {-1i8:x}");
println(f"{"hi":?} {[1, 2]:?}");
[3.14] [   3.142] [3.14159e0]
[    42] [42    ] [  42  ] [**42**]
[ab   ] [   ab] [007] [+5]
ff 0xFF 101 ff
"hi" [1, 2]

{x=} prints the expression's text, then =, then its value ({x = } keeps the spaces), as Python's does. , or _ after the width groups an integer's digits in threes ({1234567:,} is 1,234,567), and the type % multiplies by 100 and adds a percent sign ({0.256:.1%} is 25.6%).

nabi
let n = 1234567;
let rate = 0.256;
println(f"{n=} {n:,} {n:_} {rate:.1%} {n + 1 = }");
n=1234567 1,234,567 1_234_567 25.6% n + 1 = 1234568

Without an explicit alignment, numbers are right-aligned and everything else is left-aligned. ? selects the Debug format, which shows strings with their quotes and escapes. Specifiers are checked against the hole's type at compile time, so a mistake like {s:x} (with s: str) is an immediate error.

The g type is the general format of C and Python: the precision counts significant digits (6 by default), and large or small numbers switch to an exponent. {1234.5678:.3g} is 1.23e+03, {1e20:g} is 1e+20, {0.5:g} is 0.5.

To choose the width or precision at run time, put another hole inside the specifier. Both {..} and ${..} work (dynamic format specifiers).

nabi
let x = 2.0 / 3.0;
for prec in 1..=3 {
    let width = prec + 4;
    println(f"[{x:>${width}.${prec}f}] [{x:.{prec}f}]");
}
[  0.7] [0.7]
[  0.67] [0.67]
[  0.667] [0.667]

#Printing your own types: Display and Debug

f"{x}" calls the Display trait and f"{x:?}" calls Debug. Debug can be implemented automatically with #[derive(Debug)], while Display, which is meant for users, you implement yourself. fmt writes text into the Formatter it receives.

nabi
#[derive(Debug)]
struct Point { x: i32; y: i32; }

impl Display for Point {
    fn fmt(self, ref f: Formatter) throws (FmtError) {
        try f.write_str(f"({self.x}, {self.y})");
    }
}

#[derive(Debug)]
enum Shape { Circle(f64), Rect { w: f64; h: f64; } }

let p = Point { x: 1, y: -2 };
println(f"{p} | {p:?} | [{p:>10}]");
println(f"{[p, Point { x: 3, y: 4 }]:?}");
println(f"{Shape::Circle(1.5):?} {Shape::Rect { w: 2.5, h: 3.0 }:?}");
(1, -2) | Point { x: 1, y: -2 } | [   (1, -2)]
[Point { x: 1, y: -2 }, Point { x: 3, y: 4 }]
Circle(1.5) Rect { w: 2.5, h: 3.0 }

When the output is a single f-string over the fields, #[display("({self.x}, {self.y})")] on the type writes the same impl Display; on an enum, each variant takes its own, with its fields by name ({w}) or position ({_0}). Width and alignment ({p:>10}) apply to the whole output written by fmt; a precision reaches fmt itself, so a type that writes its numbers with Display::fmt(self.v, ref f) prints them rounded ({q:.2} is 1500.00 m). Once you implement Display, you can put the type directly anywhere in an f-string, and with Debug, a List or Dict holding that type also prints with {:?}. {3.0} prints 3 because the default float output is "the shortest representation that reads back as the same value"; Debug adds .0 to a whole number, so h: 3.0 still reads as a float. {p:#?} prints a struct or collection one field or element a line, indented.

#Summary

  • str is immutable UTF-8. len() and slicing work in bytes; handle characters with chars().
  • Compare text after normalize(Nfc), ignore case with eq_ignore_case / casefold(), and count what a reader sees with graphemes().
  • Build strings piece by piece in a StrBuf and take them out with to_str(). Literals are not joined automatically; join them with +.
  • Raw strings r"..." and r#"..."# do not process escapes.
  • b"..." is a [u8; N] (or a Slice<u8> where one is expected). include_str! / include_bytes! embed a file at compile time.
  • An f-string hole takes any expression, including the same quote character as is. {{ and }} are literal braces.
  • Specifiers such as :.2, :>8, :x, and :? set the format, and ${..} makes them dynamic.
  • Implement Display for your own types (or write #[display("..")]) and derive Debug. {x=} shows a name with its value, {n:,} groups digits, and to_str() turns any Display value into a str.

#Further reading

Found a mistake? Edit this page on GitHub. Last updated