Skip to content

Commit

Permalink
is_unicodepoint
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Apr 5, 2024
1 parent ea40031 commit 7158c37
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
26 changes: 17 additions & 9 deletions aheui/aheui.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,26 @@ def read_number(input_buffer=input_buffer):
return num


@jit.dont_look_inside
def write_number(value):
os.write(outfp, value)
def write_number(value_str):
os.write(outfp, value_str)


@jit.dont_look_inside
def write_utf8(warnings, value):
if not (0 <= value < 0x110000):
warnings.warn(b'write-utf8-range', value)
value = 0xfffd
os.write(outfp, unichr(value).encode('utf-8'))
REPLACE_CHAR = unichr(0xfffd).encode('utf-8')

if bigint.is_unicodepoint(value):
codepoint = bigint.toint(value)
unicode_char = unichr(codepoint)
bytes = unicode_char.encode('utf-8')
else:
bytes = REPLACE_CHAR

os.write(outfp, bytes)


def warn_utf8_range(warnings, value):
warnings.warn(b'write-utf8-range', value)
os.write(outfp, unichr(0xfffd).encode('utf-8'))

class Program(object):
_immutable_fields_ = ['labels[**]', 'opcodes[*]', 'values[*]', 'size']
Expand Down Expand Up @@ -417,7 +425,7 @@ def mainloop(program, debug):
r = selected.pop()
write_number(bigint.str(r))
elif op == c.OP_POPCHAR:
r = selected.pop_longlong()
r = selected.pop()
write_utf8(warnings, r)
elif op == c.OP_PUSHNUM:
num = read_number()
Expand Down
5 changes: 5 additions & 0 deletions aheui/int/bigint.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ def ge(r1, r2):
def is_zero(r):
# return r.sign == 0
return r._size == 0 # pypy 7.3.15


@jit.elidable
def is_unicodepoint(r):
return 0 <= r._size and r.int_le(0x110000)
10 changes: 9 additions & 1 deletion aheui/int/smallint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

try:
import builtins
except ImportError: # python2
except ImportError:
builtins = __builtins__
from aheui._compat import _bytestr

Expand Down Expand Up @@ -37,6 +37,10 @@ def str(r):
return _bytestr(r)


def hex(r):
return hex(r)


def add(r1, r2):
return r1 + r2

Expand All @@ -63,3 +67,7 @@ def ge(r1, r2):

def is_zero(r):
return r == 0


def is_unicodepoint(r):
return 0 < r <= 0x110000

0 comments on commit 7158c37

Please sign in to comment.