2 * ustring.c: Unicode string routines
9 wchar_t *ustrdup(wchar_t *s
) {
12 r
= mknewa(wchar_t, 1+ustrlen(s
));
21 char *ustrtoa(wchar_t *s
, char *outbuf
, int size
) {
27 for (p
= outbuf
; *s
&& p
< outbuf
+size
; p
++,s
++)
32 outbuf
[size
-1] = '\0';
36 char *utoa_dup(wchar_t *s
) {
42 buf
= resize(buf
, len
);
44 len
= (3 * len
) / 2 + 1; /* this guarantees a strict increase */
45 } while ((int)strlen(buf
) >= len
-1);
47 buf
= resize(buf
, strlen(buf
)+1);
51 int ustrlen(wchar_t *s
) {
57 wchar_t *uadv(wchar_t *s
) {
58 return s
+ 1 + ustrlen(s
);
61 wchar_t *ustrcpy(wchar_t *dest
, wchar_t *source
) {
69 int ustrcmp(wchar_t *lhs
, wchar_t *rhs
) {
70 if (!lhs
&& !rhs
) return 0;
73 while (*lhs
&& *rhs
&& *lhs
==*rhs
)
82 wchar_t utolower(wchar_t c
) {
84 return c
; /* this property needed by ustricmp */
85 /* FIXME: this doesn't even come close */
86 if (c
>= 'A' && c
<= 'Z')
91 int uisalpha(wchar_t c
) {
92 /* FIXME: this doesn't even come close */
93 return (c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z');
96 int ustricmp(wchar_t *lhs
, wchar_t *rhs
) {
98 while ((lc
= utolower(*lhs
)) == (rc
= utolower(*rhs
)) && lc
&& rc
)
108 wchar_t *ustrlow(wchar_t *s
) {
117 int utoi(wchar_t *s
) {
127 while (*s
&& *s
>= L
'0' && *s
<= L
'9') {
136 int utob(wchar_t *s
) {
137 if (!ustricmp(s
, L
"yes") || !ustricmp(s
, L
"y") ||
138 !ustricmp(s
, L
"true") || !ustricmp(s
, L
"t"))
143 int uisdigit(wchar_t c
) {
144 return c
>= L
'0' && c
<= L
'9';
147 #define USTRFTIME_DELTA 128
148 wchar_t *ustrftime(wchar_t *wfmt
, struct tm
*timespec
) {
151 char *fmt
, *text
, *p
;
156 * strftime has the entertaining property that it returns 0
157 * _either_ on out-of-space _or_ on successful generation of
158 * the empty string. Hence we must ensure our format can never
159 * generate the empty string. Somebody throw a custard pie at
160 * whoever was responsible for that. Please?
164 fmt
= mknewa(char, 2+len
);
165 ustrtoa(wfmt
, fmt
+1, len
+1);
171 size
+= USTRFTIME_DELTA
;
172 blk
= resize((char *)blk
, size
);
173 len
= strftime((char *)blk
, size
-1, fmt
, timespec
);
178 /* Note: +1 for the terminating 0, -1 for the initial space in fmt */
179 wblk
= resize((wchar_t *)blk
, len
);
180 text
= mknewa(char, len
);
181 strftime(text
, len
, fmt
+1, timespec
);
183 * We operate in the C locale, so this all ought to be kosher
184 * ASCII. If we ever move outside ASCII machines, we may need
185 * to make this more portable...
187 for (wp
= wblk
, p
= text
; *p
; p
++, wp
++)