Strings and Runes in Nim
import unicode
proc examineRune(r: Rune) =
if r == 't'.Rune:
echo "found tee"
elif r == 'ส'.Rune:
echo "found so sua"
proc main() =
const s = "สวัสดี"
# Since strings are sequences of bytes in Nim, this
# will produce the length of the raw bytes stored within.
echo "Len: ", s.len
# Indexing into a string produces the raw byte values at
# each index. This loop generates the hex values of all
# the bytes that constitute the code points in `s`.
for i in 0..<s.len:
stdout.write s[i].uint8.toHex & " "
echo ""
# To count how many runes are in a string, we can use
# the `runeLen` proc from the `unicode` module.
echo "Rune count: ", s.runeLen
# A `for` loop with `runes` iterator handles strings specially
# and decodes each rune along with its offset in the string.
for i, runeValue in s.runes:
echo runeValue, " starts at ", i
echo "\nUsing runeAt"
var i = 0
while i < s.len:
let runeValue = s.runeAt(i)
echo runeValue, " starts at ", i
i += runeValue.size
# This demonstrates passing a rune value to a procedure.
examineRune(runeValue)
main()
This Nim code demonstrates working with strings and runes, which are similar to Go’s concept of strings and runes. Here’s a breakdown of the code:
We import the
unicode
module, which provides utilities for working with Unicode strings.The
examineRune
procedure is defined to demonstrate passing a rune value to a procedure.In the
main
procedure:- We define a constant string
s
containing Thai characters. - We print the length of the string, which gives the number of bytes.
- We iterate over the string’s bytes and print their hexadecimal values.
- We use
runeLen
to count the number of runes in the string. - We use a
for
loop with therunes
iterator to iterate over each rune in the string. - We demonstrate manual iteration over runes using
runeAt
.
- We define a constant string
The
examineRune
procedure shows how to compare rune values.
Nim strings are similar to Go strings in that they are sequences of bytes. However, Nim provides utilities in the unicode
module to work with Unicode characters (runes).
To run this program, save it as strings_and_runes.nim
and use the Nim compiler:
$ nim c -r strings_and_runes.nim
This will compile and run the program, displaying output similar to the Go version, demonstrating how Nim handles Unicode strings and runes.