9 static inline char *cstrcat(int n, ...) {
10 int len = 0; va_list va; va_start(va,n);
11 for(int i=0; i<n; ++i) len += strlen(va_arg(va,char*));
12 va_end(va); char *cp = new char[len+1], *bp = cp; va_start(va,n);
13 for(int i=0; i<n; ++i) for(char*ap=va_arg(va,char*); *ap; *bp++=*ap++);
17 static inline char *cstrdup(const char *cp) {
18 return strcpy(new char[strlen(cp)+1],cp);
22 #define lengthof(ary) ((int)(sizeof(ary)/sizeof(ary[0])))
25 static inline int butf8(const char *&cp)
27 const unsigned char *bp = (const unsigned char *)cp;
31 static const int64_t sz = 0x5433222211111111;
32 int n = v < 0 ? 0 : (sz >> (v&0x3c)) & 0x0f;
33 for( int i=n; --i>=0; ret+=*bp++ ) ret <<= 6;
34 static const uint32_t ofs[6] = {
35 0x00000000U, 0x00003080U, 0x000E2080U,
36 0x03C82080U, 0xFA082080U, 0x82082080U
40 cp = (const char *)bp;
43 static inline int butf8(unsigned int v, char *&cp)
45 unsigned char *bp = (unsigned char *)cp;
46 if( v >= 0x00000080 ) {
47 int i = v < 0x00000800 ? 2 : v < 0x00010000 ? 3 :
48 v < 0x00200000 ? 4 : v < 0x04000000 ? 5 : 6;
50 *bp++ = (v>>(6*--i)) | m;
51 while( --i >= 0 ) *bp++ = ((v>>(6*i)) & 0x3f) | 0x80;
55 int ret = bp - (unsigned char *)cp;
60 static inline int bstrcasecmp(const char *ap, const char *bp)
64 a = towlower(butf8(ap)); b = towlower(butf8(bp));
65 } while( !(ret=a-b) && a && b );
69 static inline const char *bstrcasestr(const char *src, const char *tgt)
71 int ssz = strlen(src), tsz = strlen(tgt), ret = 0;
73 wchar_t wtgt[tsz + 1], *tp = wtgt;
74 while( *cp ) *tp++ = towlower(butf8(cp));
75 for( tsz=tp-wtgt; ssz>=tsz; ++src,--ssz ) {
77 for( int i=tsz; --i>=0 && !(ret=towlower(butf8(cp))-*tp); ++tp );
78 if( !ret ) return src;