Skip to content

Commit

Permalink
Width for old nim (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely authored Nov 21, 2024
1 parent 3f60813 commit 451d7a5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 47 deletions.
66 changes: 30 additions & 36 deletions src/unicodeplus.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import pkg/unicodedb/types
import pkg/unicodedb/casing
import pkg/unicodedb/widths
import pkg/segmentation

const enableWidth = (NimMajor, NimMinor) >= (2, 0)
when enableWidth:
import pkg/graphemes
import pkg/graphemes

export Rune

Expand Down Expand Up @@ -465,35 +462,32 @@ func toValidUtf8*(s: string, replacement = "\uFFFD"): string =
i += j+1
result.add2 toOpenArray(s, i, s.len-1)

when enableWidth:
func width(c: Rune, cjk: bool): int =
if c.int == 0xFE0F: # emoji style
return 2
result = case c.unicodeWidth()
of uwdtFull, uwdtWide: 2
of uwdtAmbiguous:
if cjk: 1 else: 2
else: 1

func width*(s: string, cjk = false): int =
## Return the display width of `s`.
## This is usually correct for monospaced fonts,
## but it may not be accurate in some cases.
## Requires Nim >= 2.
debugVerifyUtf8(s)
result = 0
var i = 0
var i2 = 0
var c = Rune(0)
var w = 0
for bounds in s.graphemeBounds:
i = 0
i2 = -1
c = Rune(0)
w = 0
while i < bounds.b-bounds.a+1:
doAssert i > i2, "invalid utf8"
i2 = i
fastRuneAt(toOpenArray(s, bounds.a, bounds.b), i, c)
w = max(w, c.width(cjk))
result += w
func width(c: Rune, cjk: bool): int =
if c.int == 0xFE0F: # emoji style
return 2
result = case c.unicodeWidth()
of uwdtFull, uwdtWide: 2
of uwdtAmbiguous:
if cjk: 1 else: 2
else: 1

func width*(s: string, cjk = false): int =
## Return the display width of `s`.
## This is usually correct for monospaced fonts,
## but it may not be accurate in some cases.
debugVerifyUtf8(s)
result = 0
var i = 0
var i2 = 0
var c = Rune(0)
var w = 0
for bounds in s.graphemeBounds:
i = bounds.a
i2 = i-1
c = Rune(0)
w = 0
while i < bounds.b+1:
doAssert i > i2, "invalid utf8"; i2 = i
fastRuneAt(s, i, c)
w = max(w, c.width(cjk))
result += w
21 changes: 10 additions & 11 deletions tests/tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -583,14 +583,13 @@ when true:
check toValidUtf8("\xF0\x80\x80") == "\uFFFD"
check toValidUtf8("\x80\x80") == "\uFFFD"

when (NimMajor, NimMinor) >= (2, 0):
test "width":
doAssert width("이건 테스트야", cjk=true) == 13
doAssert width("🐤node お名前=☜(゚ヮ゚☜)") == 22
doAssert width("👨‍👩‍👧‍👦") == 2
doAssert width("👨‍👩‍👧‍👦👨‍👩‍👧‍👦") == 4
doAssert width("👨‍👩‍👧‍👦🥱🧛🏻‍♂️") == 6
doAssert width("u̲n̲d̲e̲r̲l̲i̲n̲e̲d̲") == 20
doAssert width("a") == 1
doAssert width("ab") == 2
doAssert width("abc") == 3
test "width":
doAssert width("이건 테스트야", cjk=true) == 13
doAssert width("🐤node お名前=☜(゚ヮ゚☜)") == 22
doAssert width("👨‍👩‍👧‍👦") == 2
doAssert width("👨‍👩‍👧‍👦👨‍👩‍👧‍👦") == 4
doAssert width("👨‍👩‍👧‍👦🥱🧛🏻‍♂️") == 6
doAssert width("u̲n̲d̲e̲r̲l̲i̲n̲e̲d̲") == 20
doAssert width("a") == 1
doAssert width("ab") == 2
doAssert width("abc") == 3

0 comments on commit 451d7a5

Please sign in to comment.