8 static inline char *cstrcat(int n, ...) {
9 int len = 0; va_list va; va_start(va,n);
10 for(int i=0; i<n; ++i) len += strlen(va_arg(va,char*));
11 va_end(va); char *cp = new char[len+1], *bp = cp; va_start(va,n);
12 for(int i=0; i<n; ++i) for(char*ap=va_arg(va,char*); *ap; *bp++=*ap++);
16 static inline char *cstrdup(const char *cp) {
17 return strcpy(new char[strlen(cp)+1],cp);
21 #define lengthof(ary) ((int)(sizeof(ary)/sizeof(ary[0])))
24 static inline int butf8(const char *&cp)
26 const unsigned char *bp = (const unsigned char *)cp;
30 static const int64_t sz = 0x5433222211111111;
31 int n = v < 0 ? 0 : (sz >> (v&0x3c)) & 0x0f;
32 for( int i=n; --i>=0; ret+=*bp++ ) ret <<= 6;
33 static const uint32_t ofs[6] = {
34 0x00000000U, 0x00003080U, 0x000E2080U,
35 0x03C82080U, 0xFA082080U, 0x82082080U
39 cp = (const char *)bp;
42 static inline int butf8(unsigned int v, char *&cp)
44 unsigned char *bp = (unsigned char *)cp;
45 if( v >= 0x00000080 ) {
46 int i = v < 0x00000800 ? 2 : v < 0x00010000 ? 3 :
47 v < 0x00200000 ? 4 : v < 0x04000000 ? 5 : 6;
49 *bp++ = (v>>(6*--i)) | m;
50 while( --i >= 0 ) *bp++ = ((v>>(6*i)) & 0x3f) | 0x80;
54 int ret = bp - (unsigned char *)cp;
59 static inline int bstrcasecmp(const char *ap, const char *bp)
60 { // not really correct, but what was left after MS port
63 if( (a=butf8(ap)) >= 'A' && a <= 'Z' ) a += 'a' - 'A';
64 if( (b=butf8(bp)) >= 'A' && b <= 'Z' ) b += 'a' - 'A';
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);
73 uint32_t wtgt[tsz+1], *tp = wtgt;
76 if( wch >= 'A' && wch <= 'Z' ) wch += 'a' - 'A';
80 for( tsz=tp-wtgt; ssz>=tsz; ++src,--ssz ) {
83 for( int i=tsz; --i>=0 && !ret && (wch=butf8(cp)); ) {
84 if( wch >= 'A' && wch <= 'Z' ) wch += 'a' - 'A';
87 if( !ret ) return src;