ref: 00a119d9f78148039fcd0e0641f67e3847c8f357
parent: add4086c3f6fe23957c9d7707fc3c7720319bf21
author: Ori Bernstein <[email protected]>
date: Sun Sep 14 21:19:56 EDT 2014
Fix up character type checks.
--- a/libstd/chartype.myr
+++ b/libstd/chartype.myr
@@ -1,5 +1,6 @@
use "die.use"
use "sys.use"
+use "types.use"
/*
Tables adapted from plan 9's runetype.c,
@@ -27,6 +28,8 @@
generic charval : (c : char, base : int -> @a::(integral,numeric))
;;
+extern const put : (fmt : byte[:], args : ... -> size)
+
/*
* alpha ranges -
* only covers ranges not in lower||upper
@@ -1058,20 +1061,21 @@
0x01f3, 499 /* dz Dz */
]
-const findc = {c, t, sz, nelt, ret
- var l
- var m
+const findc = {c, t, n, nelt, ret
+ var p, m
- /* we're processing in chunks of size
- nelt, so 1 chunk is of length 'nelt' */
- while t.len > nelt
- sz /= 2
- m = sz*nelt
- l = t[m:]
- if c >= l[0]
- t = l[0:m]
+ /*
+ we're processing in chunks of size
+ nelt, so 1 chunk is of length 'nelt'
+ */
+ while n > 1
+ m = n/2
+ p = t[m*nelt:]
+ if c >= p[0]
+ t = p
+ n = n - m
else
- t = t[0:m]
+ n = m
;;
;;
@@ -1147,11 +1151,12 @@
const islower = {c
var l
- if findc(c, rtoupper2[:], rtoupper2.len, 2, &l)
+ /* the first character in the toupper table is the lowercase char */
+ if findc(c, rtoupper2[:], rtoupper2.len/3, 3, &l)
if (c >= l[0] && c <= l[1])
-> true
;;
- elif findc(c, rtoupper1[:], rtoupper1.len, 1, &l)
+ elif findc(c, rtoupper1[:], rtoupper1.len/2, 2, &l)
if (c == l[0])
-> true
;;
@@ -1162,11 +1167,12 @@
const isupper = {c
var l
- if findc(c, rtolower2[:], rtolower2.len, 2, &l)
+ /* the first character in the tolower table is the uppercase char */
+ if findc(c, rtolower2[:], rtolower2.len/3, 3, &l)
if (c >= l[0] && c <= l[1])
-> true
;;
- elif findc(c, rtolower1[:], rtolower1.len, 1, &l)
+ elif findc(c, rtolower1[:], rtolower1.len/2, 2, &l)
if (c == l[0])
-> true
;;
--- /dev/null
+++ b/test/stdchartype.myr
@@ -1,0 +1,23 @@
+use std
+
+const main = {
+ std.assert(std.isalpha('a'), "a should be alpha\n")
+ std.assert(std.isupper('A'), "A should be upper\n")
+ std.assert(std.islower('a'), "a should be lower\n")
+ std.assert(std.isdigit('0'), "0 should be isdigit\n")
+ std.assert(std.isnum('\u{0c66}'), "\u{0c66} should be isnum\n")
+ std.assert(std.isalnum('a'), "a should be isalnum\n")
+ std.assert(std.isalnum('0'), "0 should be isalnum\n")
+ std.assert(std.isspace(' '), "' ' should be isspace\n")
+ std.assert(std.isblank(' '), "' ' should be isblank\n")
+
+ std.assert(!std.isalpha('0'), "0 should not be alpha\n")
+ std.assert(!std.isupper('a'), "a should not be upper\n")
+ std.assert(!std.islower('A'), "A should not be lower\n")
+ std.assert(!std.isdigit('a'), "a should not be isdigit\n")
+ std.assert(!std.isnum('a'), " should not be isnum\n")
+ std.assert(!std.isalnum('}'), "a should not be isalnum\n")
+ std.assert(!std.isalnum('!'), "! should not be isalnum\n")
+ std.assert(!std.isspace('@'), "@ should not be isspace\n")
+ std.assert(!std.isblank('@'), "@ should not be isblank\n")
+}
--- a/test/tests
+++ b/test/tests
@@ -122,6 +122,7 @@
B helloworld P Hello-世界
B encodechar P 1世界äa
B stdsearch E 0
+B stdchartype E 0
B stdtry C
B strtab C
B catfile C