No description
Find a file
2020-05-07 08:59:23 -03:00
docs make docs more clear 2020-04-23 05:50:25 -03:00
src fix docs 2020-05-07 08:59:23 -03:00
.gitignore runnable examples 2020-02-20 21:49:35 -03:00
.travis.yml make docs more clear 2020-04-23 05:50:25 -03:00
CHANGELOG.md add reversed 2020-02-20 23:24:59 -03:00
LICENSE init 2018-04-25 18:08:49 -03:00
README.md fix tests 2020-02-20 23:34:11 -03:00
strunicode.nimble add reversed 2020-02-20 23:24:59 -03:00

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