Skip to content

nitely/nim-strunicode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

strunicode

Build Status licence

A library for unicode string handling, inspired by the Swift language.

Install

nimble install strunicode

Compatibility

Nim +0.19.0

Usage

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

Tests

nimble test

LICENSE

MIT