Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nitely/nim-graphemes
Grapheme aware string handling (Unicode tr29)
https://github.com/nitely/nim-graphemes
graphemeclusters nim unicode
Last synced: 29 days ago
JSON representation
Grapheme aware string handling (Unicode tr29)
- Host: GitHub
- URL: https://github.com/nitely/nim-graphemes
- Owner: nitely
- License: mit
- Created: 2017-09-05T05:41:58.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-09-19T20:02:41.000Z (5 months ago)
- Last Synced: 2024-11-10T06:42:25.611Z (3 months ago)
- Topics: graphemeclusters, nim, unicode
- Language: Nim
- Homepage: https://nitely.github.io/nim-graphemes/
- Size: 695 KB
- Stars: 11
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# nim-graphemes
[![Build Status](https://img.shields.io/travis/nitely/nim-graphemes.svg?style=flat-square)](https://travis-ci.org/nitely/nim-graphemes)
[![licence](https://img.shields.io/github/license/nitely/nim-graphemes.svg?style=flat-square)](https://raw.githubusercontent.com/nitely/nim-graphemes/master/LICENSE)A Nim library for grapheme aware string handling (Unicode tr29).
The splitting is made through a fast DFA.## Install
```
nimble install graphemes
```## Compatibility
* Nim +1.0.0
## Usage
```nim
import graphemes# Iterate over graphemes
block:
let expected = [
"u̲", "n̲", "d̲", "e̲", "r̲", "l̲", "i̲", "n̲", "e̲", "d̲"]
var i = 0
for c in graphemes("u̲n̲d̲e̲r̲l̲i̲n̲e̲d̲"):
doAssert c == expected[i]
inc iassert graphemesCount("u̲n̲d̲e̲r̲l̲i̲n̲e̲d̲") == 10
assert graphemesCount("ю́") == 1
assert graphemesCount("👨👩👧👦") == 1 # Family of 4 emoji# Get number of bytes the grapheme at index 0 takes
assert graphemeLenAt("u̲n̲d̲e̲r̲", 0) == 3
assert graphemeLenAt("ю́", 0) == 4# Reverse graphemes
block:
var s = "🇦🇷🇺🇾🇨🇱"
s.graphemesReverse
assert s == "🇨🇱🇺🇾🇦🇷"# Remove last grapheme
block:
var s = "u̲n̲d̲e̲r̲l̲i̲n̲e̲d̲"
s.setLen(s.len - s.graphemeLenAt(^1))
assert s == "u̲n̲d̲e̲r̲l̲i̲n̲e̲"# Get sub-string
block:
# This string contains the following emoji:
# "flag, family, yawn, vampire, pinch, and diving mask"
const s = "🇦🇷👨👩👧👦🥱🧛🏻♂️🤏🤿"
assert s.graphemesSubStr(1, 3) == "👨👩👧👦🥱🧛🏻♂️"# Truncate to a limit of graphemes
block:
var s = "🇦🇷🇺🇾🇨🇱"
graphemesTruncate(s, 2)
assert s == "🇦🇷🇺🇾"
block:
var s = "u̲n̲d̲e̲r̲l̲i̲n̲e̲d̲"
graphemesTruncate(s, 8, "...")
assert s == "u̲n̲d̲e̲r̲..."# Truncate to a limit of bytes preserving the graphemes
block:
var s = "🇦🇷🇺🇾🇨🇱"
graphemesTruncateBytes(s, 20)
assert s == "🇦🇷🇺🇾"
assert s.len == 16
```
|
|
-> [docs](https://nitely.github.io/nim-graphemes/)## Tests
```
$ nimble test
```## License
MIT