.\" Made by website/plain/build.mjs from website/words/string.md; change that file. .TH SG-STRING 7 2026-11-28 "SG" "SG reference" .SH NAME string \- text in UTF-8 that owns its memory; read string is a borrow of it .SH SYNOPSIS .TP \fB"text", """raw"""\fR a text written in the program .TP \fB$"{x} items"\fR values put into a text .TP \fBstring, read string\fR a text that owns its bytes, a look at one .TP \fBtext.length()\fR the number of bytes .TP \fBtext[i], text[a..b]\fR one byte, a part as a read string .TP \fBtext.write(x)\fR adds x at the end .TP \fB\&.clone(), .toString()\fR a text of its own, from a look or from $"..." .TP \fBstring.fromUtf8(bytes)\fR bytes as text, once checked .SH DESCRIPTION A \fBstring\fR is always valid UTF-8. It owns its bytes: it grows, and lets them go when it ends. A \fBread string\fR looks at text something else owns, as a pointer and a length, and copies nothing. Trimming, splitting and a range \fBtext[a..b]\fR give a \fBread string\fR, so they cost no allocation; the compiler checks that the look never outlives what it looks at. .PP A text written in the program is a \fBstring\fR that takes no memory until it is changed. \fB$"..."\fR writes values into a text without making one; \fB\&.toString()\fR makes it, and that is where the allocation shows. A \fBstring\fR grows with \fBwrite\fR: \fB+\fR and \fB+=\fR do not join texts. .PP \fBlength()\fR counts bytes, and \fBtext[i]\fR reads a byte, so a place is found in constant time. \fBchars()\fR goes by character. A range that would cut a character in two stops the program, and bytes become text only through \fBstring.fromUtf8\fR, which checks them: no invalid text ever exists. .PP The methods are in Text. .SH EXAMPLES .SS "Bytes and characters" .RS 4 .nf void main() { var word = "naïve" print($"{word} has {word.length()} bytes") nint letters = 0 for _ in word.chars() { letters += 1 } print($"and {letters} characters") print(word[0] == 'n') } .fi .RE .PP .RS 4 .nf naïve has 6 bytes and 5 characters true .fi .RE .PP \fBï\fR takes two bytes in UTF-8. .SS "Build a text" .RS 4 .nf void main() { var line = string.withCapacity(64) for i in 1..4 { line.write(i) line.write(',') } line.write($" and {2.5:F1}") print(line) string label = $"{line.length()} bytes".toString() print(label) } .fi .RE .PP .RS 4 .nf 1,2,3, and 2.5 14 bytes .fi .RE .PP \fBwithCapacity\fR takes room for 64 bytes at the start, so the loop allocates once. .SS "Lend it, or a part of it" .RS 4 .nf nint vowels(read string text) { nint count = 0 for c in text.chars() { if "aeiou".contains(c) { count += 1 } } return count } void main() { var name = "Ada Lovelace".clone() print(vowels(read name)) print(vowels("Grace")) print(vowels(name[4..])) string first = name[0..3].clone() name.clear() print($"[{first}] [{name}]") } .fi .RE .PP .RS 4 .nf 5 2 4 [Ada] [] .fi .RE .PP \fBname[4..]\fR lends a part with no copy. \fBfirst\fR is a text of its own, so it lives on after \fBname\fR is cleared. .SS "Split, with nothing copied" .RS 4 .nf void main() { var line = " name = Pasha ; age = 7 " for field in line.split(';') { var parts = field.split('=') var key = (parts.next() ?? "").trim() var value = (parts.next() ?? "").trim() print($"{key} -> {value}") } } .fi .RE .PP .RS 4 .nf name -> Pasha age -> 7 .fi .RE .PP Every part, trimmed or not, looks at \fBline\fR\&. .SS "Bytes as text" .RS 4 .nf void main() { var good = List { 0x53, 0x47, 0x21 } var bad = List { 0x53, 0xff, 0x21 } match string.fromUtf8(read good) { Ok(text) => print(text) Error(e) => print($"not text from byte {e.position}") } match string.fromUtf8(read bad) { Ok(text) => print(text) Error(e) => print($"not text from byte {e.position}") } print("SG".asBytes()[1]) } .fi .RE .PP .RS 4 .nf SG! not text from byte 1 71 .fi .RE .PP \fBfromUtf8\fR checks the bytes and gives a \fBread string\fR that looks at them, with no copy. \fBasBytes()\fR goes the other way, for free. .SH "WHAT STOPS THE PROGRAM" .SS "A range through a character" .RS 4 .nf void main() { var word = "naïve" print(word[0..3]) } .fi .RE .PP .RS 4 .nf panic: range 0..3 cuts a character in half `ï` takes bytes 2..4; a `string` is always valid UTF-8 --> example.sg:3:11 3 | print(word[0..3]) | ^^^^^^^^^^ .fi .RE .SH "WHAT THE COMPILER REFUSES" .SS "Joining with +=" .RS 4 .nf void main() { var text = "one".clone() text += " two" print(text) } .fi .RE .PP .RS 4 .nf error: `+=` needs a number on the left, found `string` --> example.sg:3:5 3 | text += " two" | ^^^^^^^^^^^^^^ hint: a text grows with `write`: `text.write(...)` .fi .RE .SS "$""..."" where a string goes" .RS 4 .nf void main() { int count = 3 string label = $"{count} items" print(label) } .fi .RE .PP .RS 4 .nf error: expected `string`; `$"..."` does not become a `string` on its own --> example.sg:3:20 3 | string label = $"{count} items" | ^^^^^^^^^^^^^^^^ hint: add `.toString()` at the end; that is where the allocation shows .fi .RE .SS "Changing a text a part of it still looks at" .RS 4 .nf void main() { var name = "Ada Lovelace".clone() var first = name[0..3] name.clear() print(first) } .fi .RE .PP .RS 4 .nf error: `name` cannot be changed here: `first` borrows from it and is used again later --> example.sg:4:5 4 | name.clear() | ^^^^ `first` borrowed it here: --> example.sg:3:17 3 | var first = name[0..3] | ^^^^^^^^^^ hint: finish with `first` before this line: a borrow ends at its last use .fi .RE .SS "Writing a byte" .RS 4 .nf void main() { var name = "pasha".clone() name[0] = 'P' print(name) } .fi .RE .PP .RS 4 .nf error: a byte cannot be written into a `string` a `string` is always valid UTF-8; `text[i]` only reads --> example.sg:3:5 3 | name[0] = 'P' | ^^^^^^^ .fi .RE .PP One byte written alone could leave half a character behind. .SH "SEE ALSO" .BR sg-read (7), .BR sg-char (7), .BR sg-byte (7), .BR sg-list (7) .PP https://sg\-lang.org/reference/string.html