X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/halibut/blobdiff_plain/c8422236d61651cdfc619244af52e3285b4aae0e..7c570bf386b055563f689fb3c424af29227db8e6:/ustring.c diff --git a/ustring.c b/ustring.c index 0e3bfee..8cf8554 100644 --- a/ustring.c +++ b/ustring.c @@ -11,10 +11,10 @@ wchar_t *ustrdup(wchar_t const *s) { wchar_t *r; if (s) { - r = mknewa(wchar_t, 1+ustrlen(s)); + r = snewn(1+ustrlen(s), wchar_t); ustrcpy(r, s); } else { - r = mknew(wchar_t); + r = snew(wchar_t); *r = 0; } return r; @@ -100,7 +100,7 @@ char *utoa_internal_dup(wchar_t const *s, int charset, int *lenp, int careful) len = ustrlen(s); outlen = len + 10; - outbuf = mknewa(char, outlen); + outbuf = snewn(outlen, char); outpos = 0; outbuf[outpos] = '\0'; @@ -116,7 +116,7 @@ char *utoa_internal_dup(wchar_t const *s, int charset, int *lenp, int careful) } if (!ret) { outlen = outlen * 3 / 2; - outbuf = resize(outbuf, outlen); + outbuf = sresize(outbuf, outlen, char); } outpos += ret; outbuf[outpos] = '\0'; @@ -125,7 +125,7 @@ char *utoa_internal_dup(wchar_t const *s, int charset, int *lenp, int careful) * Clean up */ outlen = outpos + 32; - outbuf = resize(outbuf, outlen); + outbuf = sresize(outbuf, outlen, char); ret = charset_from_unicode(NULL, 0, outbuf + outpos, outlen - outpos + 1, charset, &state, NULL); @@ -157,12 +157,12 @@ wchar_t *ufroma_dup(char const *s, int charset) { len = strlen(s) + 1; do { - buf = resize(buf, len); + buf = sresize(buf, len, wchar_t); ustrfroma(s, buf, len, charset); len = (3 * len) / 2 + 1; /* this guarantees a strict increase */ } while (ustrlen(buf) >= len-1); - buf = resize(buf, ustrlen(buf)+1); + buf = sresize(buf, ustrlen(buf)+1, wchar_t); return buf; } @@ -177,14 +177,14 @@ char *utoa_locale_dup(wchar_t const *s) len = ustrlen(s); - ret = mknewa(char, 1 + MB_CUR_MAX * len); + ret = snewn(1 + MB_CUR_MAX * len, char); siz = wcstombs(ret, s, len); if (siz) { assert(siz <= MB_CUR_MAX * len); ret[siz] = '\0'; - ret = resize(ret, siz+1); + ret = sresize(ret, siz+1, char); return ret; } @@ -208,14 +208,14 @@ wchar_t *ufroma_locale_dup(char const *s) len = strlen(s); - ret = mknewa(wchar_t, 1 + 2*len); /* be conservative */ + ret = snewn(1 + 2*len, wchar_t); /* be conservative */ siz = mbstowcs(ret, s, len); if (siz) { assert(siz <= (size_t)(2 * len)); ret[siz] = L'\0'; - ret = resize(ret, siz+1); + ret = sresize(ret, siz+1, wchar_t); return ret; } @@ -246,6 +246,15 @@ wchar_t *ustrcpy(wchar_t *dest, wchar_t const *source) { return ret; } +wchar_t *ustrncpy(wchar_t *dest, wchar_t const *source, int n) { + wchar_t *ret = dest; + do { + *dest++ = *source; + if (*source) source++; + } while (n-- > 0); + return ret; +} + int ustrcmp(wchar_t *lhs, wchar_t *rhs) { if (!lhs && !rhs) return 0; if (!lhs) return -1; @@ -279,7 +288,7 @@ int uisalpha(wchar_t c) { #endif } -int ustricmp(wchar_t *lhs, wchar_t *rhs) { +int ustricmp(wchar_t const *lhs, wchar_t const *rhs) { wchar_t lc, rc; while ((lc = utolower(*lhs)) == (rc = utolower(*rhs)) && lc && rc) lhs++, rhs++; @@ -291,6 +300,19 @@ int ustricmp(wchar_t *lhs, wchar_t *rhs) { return 1; } +int ustrnicmp(wchar_t const *lhs, wchar_t const *rhs, int maxlen) { + wchar_t lc = 0, rc = 0; + while (maxlen-- > 0 && + (lc = utolower(*lhs)) == (rc = utolower(*rhs)) && lc && rc) + lhs++, rhs++; + if (lc < rc) + return -1; + else if (lc > rc) + return 1; + else + return 0; +} + wchar_t *ustrlow(wchar_t *s) { wchar_t *p = s; while (*p) { @@ -300,7 +322,7 @@ wchar_t *ustrlow(wchar_t *s) { return s; } -int utoi(wchar_t *s) { +int utoi(wchar_t const *s) { int sign = +1; int n; @@ -319,7 +341,15 @@ int utoi(wchar_t *s) { return n; } -int utob(wchar_t *s) { +double utof(wchar_t const *s) +{ + char *cs = utoa_dup(s, CS_ASCII); + double ret = atof(cs); + sfree(cs); + return ret; +} + +int utob(wchar_t const *s) { if (!ustricmp(s, L"yes") || !ustricmp(s, L"y") || !ustricmp(s, L"true") || !ustricmp(s, L"t")) return TRUE; @@ -356,7 +386,7 @@ static void ustrftime_internal(rdstring *rs, char formatchr, size = 0; do { size += USTRFTIME_DELTA; - buf = resize(buf, size); + buf = sresize(buf, size, wchar_t); ret = (int) wcsftime(buf, size, fmt, timespec); } while (ret == 0); @@ -376,7 +406,7 @@ static void ustrftime_internal(rdstring *rs, char formatchr, size = 0; do { size += USTRFTIME_DELTA; - buf = resize(buf, size); + buf = sresize(buf, size, char); ret = (int) strftime(buf, size, fmt, timespec); } while (ret == 0);