core:strings in Odin
Prefix, search, and count on a workshop motto, split by a zero-allocation iterator.
package main
import "core:fmt"
import "core:strings"
main :: proc() {
motto := "measure twice, cut once"
fmt.println("starts well?", strings.has_prefix(motto, "measure"))
fmt.println("'cut' at:", strings.index(motto, "cut"))
fmt.println("commas:", strings.count(motto, ","))
// The iterator form splits without allocating anything.
rest := motto
for word in strings.split_iterator(&rest, " ") {
fmt.printf("[%s] ", word)
}
fmt.println()
}
How it works
has_prefix,index, andcountanswer the everyday questions.split_iteratortakes a ^string cursor and yields word after word.- Nothing here allocates — the words are slices of the original motto.
Keywords and builtins used here
forimportinpackageproc
The run, in numbers
- Lines
- 19
- Characters to type
- 442
- Tokens
- 115
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 106 seconds.
Step 1 of 3 in The core library, step 22 of 27 in Language basics.