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 wchar_t *ustrfroma(char *s
, wchar_t *outbuf
, int size
) {
42 for (p
= outbuf
; *s
&& p
< outbuf
+size
; p
++,s
++)
47 outbuf
[size
-1] = '\0';
51 char *utoa_dup(wchar_t *s
) {
57 buf
= resize(buf
, len
);
59 len
= (3 * len
) / 2 + 1; /* this guarantees a strict increase */
60 } while ((int)strlen(buf
) >= len
-1);
62 buf
= resize(buf
, strlen(buf
)+1);
66 wchar_t *ufroma_dup(char *s
) {
72 buf
= resize(buf
, len
);
73 ustrfroma(s
, buf
, len
);
74 len
= (3 * len
) / 2 + 1; /* this guarantees a strict increase */
75 } while (ustrlen(buf
) >= len
-1);
77 buf
= resize(buf
, ustrlen(buf
)+1);
81 int ustrlen(wchar_t const *s
) {
87 wchar_t *uadv(wchar_t *s
) {
88 return s
+ 1 + ustrlen(s
);
91 wchar_t *ustrcpy(wchar_t *dest
, wchar_t const *source
) {
99 int ustrcmp(wchar_t *lhs
, wchar_t *rhs
) {
100 if (!lhs
&& !rhs
) return 0;
103 while (*lhs
&& *rhs
&& *lhs
==*rhs
)
107 else if (*lhs
> *rhs
)
112 wchar_t utolower(wchar_t c
) {
114 return c
; /* this property needed by ustricmp */
115 /* FIXME: this doesn't even come close */
116 if (c
>= 'A' && c
<= 'Z')
121 int uisalpha(wchar_t c
) {
122 /* FIXME: this doesn't even come close */
123 return (c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z');
126 int ustricmp(wchar_t *lhs
, wchar_t *rhs
) {
128 while ((lc
= utolower(*lhs
)) == (rc
= utolower(*rhs
)) && lc
&& rc
)
138 wchar_t *ustrlow(wchar_t *s
) {
147 int utoi(wchar_t *s
) {
157 while (*s
&& *s
>= L
'0' && *s
<= L
'9') {
166 int utob(wchar_t *s
) {
167 if (!ustricmp(s
, L
"yes") || !ustricmp(s
, L
"y") ||
168 !ustricmp(s
, L
"true") || !ustricmp(s
, L
"t"))
173 int uisdigit(wchar_t c
) {
174 return c
>= L
'0' && c
<= L
'9';
177 #define USTRFTIME_DELTA 128
178 wchar_t *ustrftime(wchar_t *wfmt
, struct tm
*timespec
) {
181 char *fmt
, *text
, *p
;
186 * strftime has the entertaining property that it returns 0
187 * _either_ on out-of-space _or_ on successful generation of
188 * the empty string. Hence we must ensure our format can never
189 * generate the empty string. Somebody throw a custard pie at
190 * whoever was responsible for that. Please?
194 fmt
= mknewa(char, 2+len
);
195 ustrtoa(wfmt
, fmt
+1, len
+1);
201 size
+= USTRFTIME_DELTA
;
202 blk
= resize((char *)blk
, size
);
203 len
= strftime((char *)blk
, size
-1, fmt
, timespec
);
208 /* Note: +1 for the terminating 0, -1 for the initial space in fmt */
209 wblk
= resize((wchar_t *)blk
, len
);
210 text
= mknewa(char, len
);
211 strftime(text
, len
, fmt
+1, timespec
);
213 * We operate in the C locale, so this all ought to be kosher
214 * ASCII. If we ever move outside ASCII machines, we may need
215 * to make this more portable...
217 for (wp
= wblk
, p
= text
; *p
; p
++, wp
++)