Skip to content

Commit 40297be

Browse files
committed
fix(NIM-BUG): cast[char] not trunc on JS
1 parent 24ccb78 commit 40297be

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

Modules/unicodedata/decimalAndSpace.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
from std/unicode import `<%`
44
include ./common_h
55
import ./[rune_decl, decimal, space]
6+
import ../../Utils/castChar
67

78
const AsciiDigits = "0123456789"
89
proc transformDecimalAndSpaceToASCII*(unicodeStr: openArray[Rune]): string =
910
result = (when declared(newStringUninit): newStringUninit else: newString)(unicodeStr.len)
1011
for i, rune in unicodeStr:
1112
template st(val) = result[i] = val
12-
if rune <% Rune(127): st cast[char](rune)
13+
if rune <% Rune(127): st castChar(rune)
1314
elif rune.isspace(): st ' '
1415
else:
1516
decimalItOr(rune):

Objects/stringobject.nim

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import ./hash_def
33
import std/unicode
44
export Rune, unicode.`==`, toRunes
55
from std/strutils import join
6-
import macros
6+
import std/macros
77

8-
import pyobject
8+
import ./pyobject
9+
import ../Utils/castChar
910

1011
type UnicodeVariant* = ref object
1112
## UCS1 or UCS4 variant
@@ -366,7 +367,7 @@ proc copy_characters(dest: PyStrObject, dest_start: int, frm: PyStrObject, frm_s
366367
loopAsgn dest.str.asciiStr:
367368
if it >% Rune(255):
368369
when check_maxchar: return true
369-
else: cast[char](it)
370+
else: castChar(it)
370371
else:
371372
cast[char](it)
372373
else:

Utils/castChar.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
from std/unicode import Rune
3+
when defined(js):
4+
template castChar*(i: SomeInteger): char = cast[char](i and 255)
5+
template castChar*(i: Rune): char = char cast[uint8](i)
6+
else:
7+
template castChar*(i: SomeInteger|Rune): char = cast[char](i)

0 commit comments

Comments
 (0)