Skip to content

Commit 1b5a571

Browse files
committed
Improve some unicode functions
1 parent b918706 commit 1b5a571

33 files changed

+8366
-197
lines changed

ape/ape.S

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,22 @@ kernel: movabs $ape_stack_vaddr,%rsp
14761476
test %rax,%rax
14771477
jz 1f
14781478
movb $METAL,(%rax)
1479-
1: xor %eax,%eax
1479+
1: push $0
1480+
mov %rsp,%rbp
1481+
mov .Lenv0(%rip),%rax
1482+
mov %rax,(%rbp) # envp[0][0]
1483+
push $0 # argv[0][0]
1484+
push $0 # auxv[1][1]
1485+
push $0 # auxv[1][0]
1486+
push %rbp # auxv[0][1]
1487+
push $31 # auxv[0][0] AT_EXECFN
1488+
push $0 # envp[1]
1489+
push $.Lenv0 # envp[0]
1490+
push $0 # argv[1]
1491+
push %rbp # argv[0]
1492+
push $1 # argc
1493+
xor %ebp,%ebp
1494+
xor %eax,%eax
14801495
xor %ecx,%ecx
14811496
xor %edx,%edx
14821497
xor %edi,%edi
@@ -1485,20 +1500,10 @@ kernel: movabs $ape_stack_vaddr,%rsp
14851500
xor %r9d,%r9d
14861501
xor %r10d,%r10d
14871502
xor %r11d,%r11d
1488-
push $0 # auxv[1][1]
1489-
push $0 # auxv[1][0]
1490-
push $.Larg0 # auxv[0][1]
1491-
push $31 # auxv[0][0] AT_EXECFN
1492-
push $0 # envp[1]
1493-
push $.Lenv0 # envp[0]
1494-
push $0 # argv[1]
1495-
push $.Larg0 # argv[0]
1496-
push $1 # argc
14971503
jmp _start
14981504
.endfn kernel
14991505

15001506
.rodata
1501-
.Larg0: .asciz "ape.com"
15021507
.Lenv0: .asciz "METAL=1"
15031508
.previous
15041509

libc/runtime/getdosargv.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,16 @@ textwindows noasan int GetDosArgv(const char16_t *cmdline, char *buf,
8989
argc = 0;
9090
st.wc = DecodeDosArgv(&st.s);
9191
while (st.wc) {
92-
while (st.wc && isspace(st.wc)) st.wc = DecodeDosArgv(&st.s);
92+
while (st.wc && (st.wc == ' ' || st.wc == '\t')) {
93+
st.wc = DecodeDosArgv(&st.s);
94+
}
9395
if (!st.wc) break;
9496
if (++argc < max) {
9597
argv[argc - 1] = st.p < st.pe ? st.p : NULL;
9698
}
9799
inquote = false;
98100
while (st.wc) {
99-
if (!inquote && isspace(st.wc)) break;
101+
if (!inquote && (st.wc == ' ' || st.wc == '\t')) break;
100102
if (st.wc == '"' || st.wc == '\\') {
101103
slashes = 0;
102104
quotes = 0;

libc/str/isspace.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#include "libc/str/str.h"
2020

2121
/**
22-
* Returns true if c is space, \t, \r, \n, \f, or \v.
22+
* Returns nonzero if c is space, \t, \r, \n, \f, or \v.
23+
* @see isblank()
2324
*/
2425
int isspace(int c) {
2526
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' ||

libc/str/iswalnum.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/str/str.h"
2020

21-
int iswalnum(wint_t wc) {
22-
return isalnum(wc);
21+
/**
22+
* Returns nonzero if c is lower, alpha, or digit.
23+
*/
24+
int iswalnum(wint_t c) {
25+
return iswdigit(c) || iswalpha(c);
2326
}

libc/str/iswalpha.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/str/str.h"
2020

21-
int iswalpha(wint_t wc) {
22-
return isalpha(wc);
21+
/**
22+
* Returns nonzero if c is alphabetical.
23+
*/
24+
int iswalpha(wint_t c) {
25+
return iswupper(c) || iswlower(c);
2326
}

libc/str/iswblank.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/str/str.h"
2020

21-
int iswblank(wint_t wc) {
22-
return isblank(wc);
21+
/**
22+
* Returns nonzero if c is space or tab.
23+
*/
24+
int iswblank(wint_t c) {
25+
return c == ' ' || c == '\t';
2326
}

libc/str/iswcntrl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "libc/str/str.h"
2020

2121
/**
22-
* Returns nonzero if wc is C0 or C1 control code.
22+
* Returns nonzero if c is C0 or C1 control code.
2323
*/
24-
int iswcntrl(wint_t wc) {
25-
return (0x00 <= wc && wc <= 0x1F) || (0x7F <= wc && wc <= 0x9F);
24+
int iswcntrl(wint_t c) {
25+
return (0x00 <= c && c <= 0x1F) || (0x7F <= c && c <= 0x9F);
2626
}

libc/str/iswctype.c

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,71 +19,30 @@
1919
#include "libc/macros.internal.h"
2020
#include "libc/str/str.h"
2121

22-
#define ALNUM 1
23-
#define ALPHA 2
24-
#define BLANK 3
25-
#define CNTRL 4
26-
#define DIGIT 5
27-
#define GRAPH 6
28-
#define LOWER 7
29-
#define PRINT 8
30-
#define PUNCT 9
31-
#define SPACE 10
32-
#define UPPER 11
33-
#define XDIGIT 12
34-
35-
static const struct {
36-
char name[7];
37-
char type;
38-
} kWcTypes[] = {
39-
{"alnum", ALNUM}, {"alpha", ALPHA}, {"blank", BLANK}, {"cntrl", CNTRL},
40-
{"digit", DIGIT}, {"graph", GRAPH}, {"lower", LOWER}, {"print", PRINT},
41-
{"punct", PUNCT}, {"space", SPACE}, {"upper", UPPER}, {"xdigit", XDIGIT},
22+
static const int (*const kWcTypeFuncs[])(wint_t) = {
23+
iswalnum, //
24+
iswalpha, //
25+
iswblank, //
26+
iswcntrl, //
27+
iswdigit, //
28+
iswgraph, //
29+
iswlower, //
30+
iswprint, //
31+
iswpunct, //
32+
iswspace, //
33+
iswupper, //
34+
iswxdigit, //
4235
};
4336

44-
static int CompareStrings(const char *l, const char *r) {
45-
size_t i = 0;
46-
while (l[i] == r[i] && r[i]) ++i;
47-
return (l[i] & 0xff) - (r[i] & 0xff);
48-
}
49-
50-
wctype_t wctype(const char *name) {
51-
unsigned i;
52-
for (i = 0; i < ARRAYLEN(kWcTypes); ++i) {
53-
if (CompareStrings(name, kWcTypes[i].name) == 0) {
54-
return kWcTypes[i].type;
55-
}
56-
}
57-
return 0;
58-
}
59-
60-
int iswctype(wint_t wc, wctype_t type) {
61-
switch (type) {
62-
case ALNUM:
63-
return iswalnum(wc);
64-
case ALPHA:
65-
return iswalpha(wc);
66-
case BLANK:
67-
return iswblank(wc);
68-
case CNTRL:
69-
return iswcntrl(wc);
70-
case DIGIT:
71-
return iswdigit(wc);
72-
case GRAPH:
73-
return iswgraph(wc);
74-
case LOWER:
75-
return iswlower(wc);
76-
case PRINT:
77-
return iswprint(wc);
78-
case PUNCT:
79-
return iswpunct(wc);
80-
case SPACE:
81-
return iswspace(wc);
82-
case UPPER:
83-
return iswupper(wc);
84-
case XDIGIT:
85-
return iswxdigit(wc);
86-
default:
87-
return 0;
37+
/**
38+
* Returns nonzero if c has property.
39+
*
40+
* @param t is number returned by wctype
41+
*/
42+
int iswctype(wint_t c, wctype_t t) {
43+
if (1 <= t && t <= ARRAYLEN(kWcTypeFuncs)) {
44+
return kWcTypeFuncs[t - 1](c);
45+
} else {
46+
return 0;
8847
}
8948
}

libc/str/iswdigit.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/str/str.h"
2020

21-
int iswdigit(wint_t wc) {
22-
return isdigit(wc);
21+
/**
22+
* Returns nonzero if c is decimal digit.
23+
*/
24+
int iswdigit(wint_t c) {
25+
return '0' <= c && c <= '9';
2326
}

libc/str/iswgraph.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/str/str.h"
2020

21-
int iswgraph(wint_t wc) {
22-
return isgraph(wc);
21+
/**
22+
* Returns nonzero if c is printable and not a space.
23+
*/
24+
int iswgraph(wint_t c) {
25+
return iswprint(c) && !iswspace(c);
2326
}

0 commit comments

Comments
 (0)