A library for unicode string handling, inspired by the Swift language.
nimble install strunicode
Nim +0.19.0
import strunicode
# both of these strings read as "CafΓ©"
# when printed, but have different
# unicode representation
const
cafeA = "Caf\u00E9".Unicode
cafeB = "Caf\u0065\u0301".Unicode
# canonical comparison
# (note: none of this will copy)
assert cafeA == cafeB
assert cafeA == cafeB.string
assert cafeA == "Caf\u00E9"
assert cafeA == "Caf\u0065\u0301"
assert cafeA.string != cafeB.string
# count characters
assert cafeA.count == cafeB.count
# get character at position 3
assert cafeA.at(3) == cafeB.at(3)
assert cafeA.at(3) == "\u00E9"
assert cafeB.at(3) == "\u0065\u0301"
assert cafeA.reversed == cafeB.reversed
assert cafeB.reversed == "\u0065\u0301faC"
# iterate over characters
block:
var
expected = ["C", "a", "f", "\u0065\u0301"]
i = 0
for c in cafeB:
assert c == expected[i]
inc i
# reverse characters
block:
var s = "π¦π·πΊπΎπ¨π±".Unicode
s.reverse
assert s == "π¨π±πΊπΎπ¦π·"
# remove last character
block:
var s = "Caf\u0065\u0301"
s.setLen(s.len - s.Unicode.at(^1).len)
assert s == "Caf"
# character/string interop with openArray[char]
block:
proc isDiacriticE(s: openArray[char]): bool =
# regular string comparison
s == "\u0065\u0301"
assert cafeB.at(^1).toOpenArray.isDiacriticE
|
|
-> There's more, read the docs
nimble test
MIT