typestar

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

  1. has_prefix, index, and count answer the everyday questions.
  2. split_iterator takes a ^string cursor and yields word after word.
  3. Nothing here allocates — the words are slices of the original motto.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in The core library, step 22 of 27 in Language basics.

← Previous Next →