String Char Slice
string_char_slice.nr42 lines
string_char_slice.nr· neuro
// `.char_slice(range)`: the codepoint-indexed companion to `.slice(range)`. Both return a
// borrowed `&string` view. `.slice` counts bytes and `.char_slice` counts Unicode code
// points, so they agree on ASCII and differ elsewhere. A code point index cannot split a
// code point, so the only runtime failure is a range that runs past the last character or
// has reversed bounds, which panics as `.slice` does.
func main() -> i32 {
val s = "héllo" // 5 characters, 6 bytes: 'é' takes two
println("s = {s}")
println("s.len() = {s.len()} (bytes, not characters)")
// The same range, read two ways.
val by_char = s.char_slice(0..3) // 3 characters -> "hél"
val by_byte = s.slice(0..3) // 3 bytes -> "hé"
println("s.char_slice(0..3) = {by_char}")
println("s.slice(0..3) = {by_byte}")
// Inclusive ranges work the same as on `.slice`.
val tail = s.char_slice(3..=4) // characters 3 and 4 -> "lo"
println("s.char_slice(3..=4)= {tail}")
// The character count is a legal upper bound, so the end of the string is
// addressable and yields an empty slice.
val empty = s.char_slice(5..5)
println("s.char_slice(5..5) = [{empty}] (empty slice)")
// The result is an ordinary `&string`, so it chains and compares byte-wise.
val nested = s.char_slice(0..3).char_slice(1..2)
println("nested = {nested}")
mut score: i32 = 0
if by_char == "hél" { score += 1 }
if by_byte == "hé" { score += 2 }
if by_char != by_byte { score += 4 }
if nested == "é" { score += 8 }
score += (by_char.len() as i32) // + 4 bytes for those 3 characters
println("score = {score}")
return score // 1 + 2 + 4 + 8 + 4 = 19
}
neuroc run string_char_slice.nr
s = héllo
s.len() = 6 (bytes, not characters)
s.char_slice(0..3) = hél
s.slice(0..3) = hé
s.char_slice(3..=4)= lo
s.char_slice(5..5) = [] (empty slice)
nested = é
score = 19