ref: 88ea359679904cea21275be12cbc60eb3ec1c81d
parent: cfa84e404def22f3bf7ccb8f0551b864ae08d926
author: Ori Bernstein <[email protected]>
date: Tue Oct 22 20:32:16 EDT 2013
Clean up code, don't assume codepoint is 4 bytes.
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,10 @@
MYRLIB=bio
MYRSRC= \
bio.myr
+SUB=test
include config.mk
include mk/myr.mk
+
+check: all
+ make -C test check
--- a/bio.myr
+++ b/bio.myr
@@ -206,8 +206,8 @@
const getc = {f
var c
- if ensureread(f, std.Maxcharlen)
- c = std.decode(f.rbuf)
+ if ensurecodepoint(f)
+ c = std.decode(f.rbuf[f.rstart:f.rend])
f.rstart += std.charlen(c)
-> c
;;
@@ -214,6 +214,25 @@
-> -1
}
+const ensurecodepoint = {f
+ var b
+ var len
+
+ b = peekb(f)
+ if b & 0x80 == 0 /* 0b0xxx_xxxx */
+ len = 1
+ elif b & 0xe0 == 0xc0 /* 0b110x_xxxx */
+ len = 2
+ elif b & 0xf0 == 0xe0 /* 0b1110_xxxx */
+ len = 3
+ elif b & 0xf8 == 0xf0 /* 0b1111_0xxx */
+ len = 4
+ else
+ -> false
+ ;;
+ -> ensureread(f, len)
+}
+
generic putle = {f, v : @a::(tcnum,tcint,tctest)
var i
@@ -238,6 +257,7 @@
var i
ret = 0
+ ensureread(f, sizeof(@a))
for i = 0; i < sizeof(@a); i++
ret <<= 8
ret |= (getb(f) castto(@a::(tcnum,tcint,tctest)))
@@ -250,6 +270,7 @@
var i
ret = 0
+ ensureread(f, sizeof(@a))
for i = 0; i < sizeof(@a); i++
ret = ret | ((getb(f) << 8*i) castto(@a::(tcnum,tcint,tctest)))
;;
@@ -263,8 +284,10 @@
}
const peekc = {f
- ensureread(f, std.Maxcharlen)
- -> std.decode(f.rbuf)
+ if !ensurecodepoint(f)
+ -> -1
+ ;;
+ -> std.decode(f.rbuf[f.rstart:f.rend])
}
const readto = {f, c
@@ -271,21 +294,27 @@
var buf : byte[4]
var srch
var ret : byte[:]
+ var len
var i
- srch = buf[:std.encode(buf[:], c)]
+ len = std.encode(buf[:], c)
+ srch = buf[:std.charlen(c)]
if srch.len == 0
- -> [][:]
+ -> std.slalloc(0)
;;
- ret = [][:]
+ ret = std.slalloc(0)
while true
+ std.put("srch.len = %i\n", srch.len)
if !ensureread(f, srch.len)
- -> readappend(f, ret, f.rend - f.rstart)
+ -> readappend(f, ret, 0)
;;
for i = f.rstart; i < f.rend; i++
+ std.put("Taking %i from %i to %i\n", i castto(int), f.rstart castto(int), i - f.rstart castto(int))
if f.rbuf[i] == srch[0] && std.sleq(f.rbuf[i + 1:i + srch.len - 1], srch[1:])
- -> readappend(f, ret, i - f.rstart)
+ ret = readappend(f, ret, i - f.rstart)
+ f.rstart += srch.len
+ -> ret
;;
;;
ret = readappend(f, ret, i - f.rstart)
--- a/mk/myr.mk
+++ b/mk/myr.mk
@@ -11,13 +11,11 @@
myrbuild -b $(MYRBIN) $^
OBJ=$(MYRSRC:.myr=.o) $(ASMSRC:.s=.o)
-JUNKASM=$(MYRSRC:.myr=.s)
USE=$(MYRSRC:.myr=.use) $(MYRLIB)
.PHONY: clean
clean:
rm -f $(OBJ)
rm -f $(USE)
- rm -f $(JUNKASM)
rm -f lib$(MYRLIB).a
install: install-bin install-lib