File: widths.nim

package info (click to toggle)
nim-unicodedb 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 9,616 kB
  • sloc: makefile: 8
file content (90 lines) | stat: -rw-r--r-- 1,779 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
## East Asian Width - tr11 (http://www.unicode.org/reports/tr11/)

import algorithm
import strutils

import derived_data
import unicode_data
import two_stage_table
import utils

const maxCP = 0x10FFFF

type
  UnicodeWidth = enum
    uwdtAmbiguous = 0x01  # A
    uwdtFull = 0x02  # F
    uwdtHalf = 0x04  # H
    uwdtNarrow = 0x08  # Na
    uwdtWide = 0x10  # W
    uwdtNeutral = 0x20  # N

proc widthMap(uwdt: string): int =
  case uwdt
  of "A":
    uwdtAmbiguous.ord
  of "F":
    uwdtFull.ord
  of "H":
    uwdtHalf.ord
  of "Na":
    uwdtNarrow.ord
  of "W":
    uwdtWide.ord
  of "N":
    uwdtNeutral.ord
  else:
    raise newException(ValueError, "Bad value: " & uwdt)

proc parseWidth(data: seq[seq[string]]): seq[int] =
  result = newSeq[int](maxCP+1)
  result.fill("N".widthMap())
  for cp, d in data:
    if d.len == 0: continue
    result[cp] = d[0].widthMap()

proc parse(filePath: string): seq[int] =
  filePath.parseUDDNoDups.parseWidth

proc build(data: seq[int]): Stages[int] =
  buildTwoStageTable(data)

const dataTemplate = """## This is auto-generated. Do not modify it

type
  UnicodeWidth* = enum
    uwdtAmbiguous = $#
    uwdtFull = $#
    uwdtHalf = $#
    uwdtNarrow = $#
    uwdtWide = $#
    uwdtNeutral = $#

const
  widthsIndices* = [
    $#
  ]
  widthsData* = [
    $#
  ]

  blockSize* = $#
"""

when isMainModule:
  let stages = "./gen/UCD/EastAsianWidth.txt".parse.build

  var f = open("./src/unicodedb/widths_data.nim", fmWrite)
  try:
    f.write(dataTemplate % [
      $uwdtAmbiguous.ord,
      $uwdtFull.ord,
      $uwdtHalf.ord,
      $uwdtNarrow.ord,
      $uwdtWide.ord,
      $uwdtNeutral.ord,
      prettyTable(stages.stage1, 15, "'i8"),
      prettyTable(stages.stage2, 15, "'i8"),
      $stages.blockSize])
  finally:
    close(f)