📄 str.mx
字号:
{ 0x1044C, 0x10424, }, { 0x1044D, 0x10425, }, { 0x1044E, 0x10426, }, { 0x1044F, 0x10427, },#endif};#define UTF8_CONVERSIONS (sizeof(UTF8_lower_upper) / sizeof(UTF8_lower_upper[0]))static BAT *UTF8_toupperBat = NULL, *UTF8_tolowerBat;bat *strPrelude(void){ if (!UTF8_toupperBat) { int i = UTF8_CONVERSIONS; UTF8_toupperBat = BATnew(TYPE_int, TYPE_int, UTF8_CONVERSIONS); if (UTF8_toupperBat == NULL) return NULL; while (--i >= 0) { int lower = UTF8_lower_upper[i].lower; int upper = UTF8_lower_upper[i].upper; BUNins(UTF8_toupperBat, &lower, &upper, FALSE); } UTF8_tolowerBat = BATmirror(UTF8_toupperBat); BATname(UTF8_toupperBat, "monet_unicode_case"); } return NULL;}str strEpilogue(int *ret){ (void)ret; if (UTF8_toupperBat) { BBPreclaim(UTF8_toupperBat); BBPreclaim(UTF8_tolowerBat); } return MAL_SUCCEED;}@= UTF8_CONV{ BUN UTF8_CONV_r; int UTF8_CONV_v = (@1); HASHfnd_int(UTF8_CONV_r, UTF8_@2Bat, &UTF8_CONV_v); if (UTF8_CONV_r) (@1) = *(int*) BUNtloc(UTF8_@2Bat, UTF8_CONV_r);}@= UTF8_GETCHAR if (*@2 < 0x80) { (@1) = *(@2)++; } else if (*(@2) < 0xE0) { (@1) = (*(@2)++ & 0x1F) << 6; (@1) |= (*(@2)++ & 0x3F); } else if (*(@2) < 0xF0) { (@1) = (*(@2)++ & 0x0F) << 12; (@1) |= (*(@2)++ & 0x3F) << 6; (@1) |= (*(@2)++ & 0x3F); } else if (*@2 < 0xF8) { (@1) = (*(@2)++ & 0x07) << 18; (@1) |= (*(@2)++ & 0x3F) << 12; (@1) |= (*(@2)++ & 0x3F) << 6; (@1) |= (*(@2)++ & 0x3F); } else if (*@2 < 0xFC) { (@1) = (*(@2)++ & 0x03) << 24; (@1) |= (*(@2)++ & 0x3F) << 18; (@1) |= (*(@2)++ & 0x3F) << 12; (@1) |= (*(@2)++ & 0x3F) << 6; (@1) |= (*(@2)++ & 0x3F); } else if (*@2 < 0xFE) { (@1) = (*(@2)++ & 0x01) << 30; (@1) |= (*(@2)++ & 0x3F) << 24; (@1) |= (*(@2)++ & 0x3F) << 18; (@1) |= (*(@2)++ & 0x3F) << 12; (@1) |= (*(@2)++ & 0x3F) << 6; (@1) |= (*(@2)++ & 0x3F); } else { (@1) = int_nil; }@= UTF8_PUTCHAR if ((@1) < 0#if SIZEOF_INT > 4 || (int) (@1) >= 0x80000000#endif ) { *(@2)++ = chr_nil; } else if ((@1) < 0x80) { *(@2)++ = (@1); } else if ((@1) < 0x800) { *(@2)++ = 0xC0 | ((@1) >> 6); *(@2)++ = 0x80 | ((@1) & 0x3F); } else if ((@1) < 0x10000) { *(@2)++ = 0xE0 | ((@1) >> 12); *(@2)++ = 0x80 | (((@1) >> 6) & 0x3F); *(@2)++ = 0x80 | ((@1) & 0x3F); } else if ((@1) < 0x200000) { *(@2)++ = 0xF0 | ((@1) >> 18); *(@2)++ = 0x80 | (((@1) >> 12) & 0x3F); *(@2)++ = 0x80 | (((@1) >> 6) & 0x3F); *(@2)++ = 0x80 | ((@1) & 0x3F); } else if ((@1) < 0x4000000) { *(@2)++ = 0xF8 | ((@1) >> 24); *(@2)++ = 0x80 | (((@1) >> 18) & 0x3F); *(@2)++ = 0x80 | (((@1) >> 12) & 0x3F); *(@2)++ = 0x80 | (((@1) >> 6) & 0x3F); *(@2)++ = 0x80 | ((@1) & 0x3F); } else /* if ((@1) < 0x80000000) */ { *(@2)++ = 0xFC | ((@1) >> 30); *(@2)++ = 0x80 | (((@1) >> 24) & 0x3F); *(@2)++ = 0x80 | (((@1) >> 18) & 0x3F); *(@2)++ = 0x80 | (((@1) >> 12) & 0x3F); *(@2)++ = 0x80 | (((@1) >> 6) & 0x3F); *(@2)++ = 0x80 | ((@1) & 0x3F); }@cstatic INLINE intUTF8_strlen(str val){ unsigned char *s = (unsigned char *) val; int pos = 0; while (*s) { int c = *s++; pos++; if (c < 0xC0) continue; if (*s++ < 0x80) return int_nil; if (c < 0xE0) continue; if (*s++ < 0x80) return int_nil; if (c < 0xF0) continue; if (*s++ < 0x80) return int_nil; if (c < 0xF8) continue; if (*s++ < 0x80) return int_nil; if (c < 0xFC) continue; if (*s++ < 0x80) return int_nil; } return pos;}static INLINE intUTF8_strpos(str val, str end){ unsigned char *s = (unsigned char *) val; int pos = 0; if (s > (unsigned char *) end) { return -1; } while (s < (unsigned char *) end) { int c = *s++; pos++; if (c == 0) return -1; if (c < 0xC0) continue; if (*s++ < 0x80) return -1; if (c < 0xE0) continue; if (*s++ < 0x80) return -1; if (c < 0xF0) continue; if (*s++ < 0x80) return -1; if (c < 0xF8) continue; if (*s++ < 0x80) return -1; if (c < 0xFC) continue; if (*s++ < 0x80) return -1; } return pos;}static INLINE strUTF8_strtail(str val, int pos){ unsigned char *s = (unsigned char *) val; while (*s && pos-- > 0) { int c = *s++; if (c < 0xC0) continue; if (*s++ < 0x80) return NULL; if (c < 0xE0) continue; if (*s++ < 0x80) return NULL; if (c < 0xF0) continue; if (*s++ < 0x80) return NULL; if (c < 0xF8) continue; if (*s++ < 0x80) return NULL; if (c < 0xFC) continue; if (*s++ < 0x80) return NULL; } return (str) s;}#define RETURN_NIL_IF(b,t) \ if (b) { \ if (ATOMextern(t)) { \ *(ptr*) res = (ptr) ATOMnil(t); \ } else { \ memcpy(res, ATOMnilptr(t), ATOMsize(t)); \ } \ return GDK_SUCCEED; \ }#ifdef MAX#undef MAX#endif#define MAX(x, y) ((x) > (y) ? (x) : (y))#ifdef MIN#undef MIN#endif#define MIN(x, y) ((x) < (y) ? (x) : (y))intstrConcat(str *res, str s, ptr val, int t){ str valstr = NULL; size_t l1; int l2 = 0; char buf[7], *p = buf; RETURN_NIL_IF(strNil(s) || ATOMcmp(t, val, ATOMnilptr(t)) == 0, TYPE_str); if (t <= 0) return GDK_FAIL; l1 = strlen(s); if (t != TYPE_str) { if (t == TYPE_chr) { /* put value in int to avoid warning from compiler */ l2 = * (char *) val; @:UTF8_PUTCHAR(l2,p)@ l2 = p - buf; val = (ptr) buf; } else { BATatoms[t].atomToStr(&valstr, &l2, val); val = (ptr) valstr; } } else { l2 = strlen((str) val); } *res = (str) GDKmalloc(l1 + l2 + 1); memcpy(*res, s, l1); memcpy(*res + l1, (str) val, l2); (*res)[l1 + l2] = '\0'; if (valstr) GDKfree(valstr); return GDK_SUCCEED;}intstrLength(int *res, str s){ RETURN_NIL_IF(strNil(s), TYPE_int); *res = UTF8_strlen(s); return GDK_SUCCEED;}intstrBytes(int *res, str s){ *res = strlen(s); return GDK_SUCCEED;}intstrTail(str *res, str s, int *offset){ int off = *offset; RETURN_NIL_IF(strNil(s) || off == int_nil, TYPE_str); if (off < 0) { int len = UTF8_strlen(s); RETURN_NIL_IF(len == int_nil, TYPE_str); off = len + off; if (off < 0) off = 0; } *res = (char *) GDKstrdup(UTF8_strtail(s, off)); return GDK_SUCCEED;}intstrSubString(str *res, str s, int *offset, int *length){ int len, off = *offset; RETURN_NIL_IF(strNil(s) || off == int_nil || *length == int_nil, TYPE_str); if (off < 0) { len = UTF8_strlen(s); RETURN_NIL_IF(len == int_nil, TYPE_str); off = len + off; if (off < 0) { *length += off; off = 0; } } if (*length < 0) { *res = GDKstrdup(""); return GDK_SUCCEED; } s = UTF8_strtail(s, MAX(0, off)); len = UTF8_strtail(s, *length) - s; if (off < 0) { len += off; off = 0; } *res = (char *) GDKmalloc(len + 1); strncpy(*res, s, len); (*res)[len] = 0; return GDK_SUCCEED;}intstrFromWChr(str *res, int *c){ str s = *res = GDKmalloc(7); @:UTF8_PUTCHAR(*c,s)@ *s = 0; return GDK_SUCCEED;}intstrWChrAt(int *res, str val, int *at){ unsigned char *s = (unsigned char *) val; RETURN_NIL_IF(strNil(val) || *at == int_nil || *at < 0, TYPE_chr); s = (unsigned char *) UTF8_strtail((str) s, *at); RETURN_NIL_IF(*s == 0, TYPE_chr); @:UTF8_GETCHAR(*res,s)@ return GDK_SUCCEED;}intcodeset(str *res){#ifdef HAVE_NL_LANGINFO char *codeset = nl_langinfo(CODESET); if (!codeset) return GDK_FAIL; *res = GDKstrdup(codeset); return GDK_SUCCEED;#else *res = GDKstrdup("UTF-8"); return GDK_SUCCEED;#endif}intstrIconv(str *res, str org, str f, str t){#ifdef HAVE_ICONV size_t len = strlen(org); iconv_t cd = iconv_open(t, f); size_t size = 4 * len; /* make sure enough memory is claimed */ char *r; ICONV_CONST char *from = org; if (!cd) { GDKerror("strIconv: Cannot convert strings from (%s) to (%s)\n", f, t); return GDK_FAIL; } *res = r = GDKmalloc(size); if (iconv(cd, &from, &len, &r, &size) == (size_t) - 1) { GDKfree(*res); *res = NULL; GDKerror("strIconv: String conversion failed from (%s) to (%s)\n", f, t); return GDK_FAIL; } *r = 0; iconv_close(cd); return GDK_SUCCEED;#else *res = NULL; if (strcmp(f, t) == 0) { *res = GDKstrdup(org); return GDK_SUCCEED; } return GDK_FAIL;#endif}intstrChrAt(chr *res, str val, int *at){ int v; strWChrAt(&v, val, at); if (0 <= v && v <= 127) *res = (chr) v; else *res = chr_nil; return GDK_SUCCEED;}intstrPrefix(bit *res, str s, str prefix){ size_t pl, i; RETURN_NIL_IF(strNil(s) || strNil(prefix), TYPE_bit); pl = strlen(prefix); if (strlen(s) < pl) { *res = 0; return GDK_SUCCEED; } *res = 1; for (i = 0; i < pl; i++) { if (s[i] != prefix[i]) { *res = 0; return GDK_SUCCEED; } } return GDK_SUCCEED;}intstrSuffix(bit *res, str s, str suffix){ size_t i, sl, sul; RETURN_NIL_IF(strNil(s) || strNil(suffix), TYPE_bit); sl = strlen(s); sul = strlen(suffix); if (sl < sul) { *res = 0; return GDK_SUCCEED; } *res = 1; for (i = 0; i < sul; i++) { if (s[sl - 1 - i] != suffix[sul - 1 - i]) { *res = 0; return GDK_SUCCEED; } } return GDK_SUCCEED;}intstrLower(str *res, str s){ size_t len = strlen(s); unsigned char *dst, *src = (unsigned char *) s, *end = (unsigned char *) (src + len); RETURN_NIL_IF(strNil(s), TYPE_str); *res = GDKmalloc(len + 1); dst = (unsigned char *) *res; while (src < end) { int c; @:UTF8_GETCHAR(c,src)@ @:UTF8_CONV(c,tolower)@ if (dst + 6 > (unsigned char *) *res + len) { /* not guaranteed to fit, so allocate more space; also allocate enough for the rest of the source */ size_t off = dst - (unsigned char *) *res; *res = GDKrealloc(*res, (len += 6 + (end - src)) + 1); dst = (unsigned char *) *res + off; } @:UTF8_PUTCHAR(c,dst)@ } *dst = 0; return GDK_SUCCEED;}intstrUpper(str *res, str s){ size_t len = strlen(s); unsigned char *dst, *src = (unsigned char *) s, *end = (unsigned char *) (src + len); RETURN_NIL_IF(strNil(s), TYPE_str); *res = GDKmalloc(len + 1); dst = (unsigned char *) *res; while (src < end) { int c; @:UTF8_GETCHAR(c,src)@ @:UTF8_CONV(c,toupper)@ if (dst + 6 > (unsigned char *) *res + len) { /* not guaranteed to fit, so allocate more space; also allocate enough for the rest of the source */ size_t off = dst - (unsigned char *) *res; *res = GDKrealloc(*res, (len += 6 + (end - src)) + 1); dst = (unsigned char *) *res + off; } @:UTF8_PUTCHAR(c,dst)@ } *dst = 0; return GDK_SUCCEED;}intstrStrSearch(int *res, str s, str s2){ char *p; RETURN_NIL_IF(strNil(s) || strNil(s2), TYPE_int); if ((p = strstr(s, s2)) != 0) *res = UTF8_strpos(s, p); else *res = -1; return GDK_SUCCEED;}intstrReverseStrSearch(int *res, str s, str s2){ size_t len, slen; char *p, *q; size_t i; RETURN_NIL_IF(strNil(s) || strNil(s2), TYPE_int); *res = -1; len = strlen(s); slen = strlen(s2); for (p = s + len - slen; p >= s; p--) { for (i = 0, q = p; i < slen && *q == s2[i]; i++, q++) ; if (i == slen) { *res = UTF8_strpos(s, p); break; } } return GDK_SUCCEED;}intstrChrSearch(int *res, str s, chr *c){ int i = (int) *c; char buf[7], *p = buf; RETURN_NIL_IF(strNil(s) || *c == chr_nil, TYPE_int); @:UTF8_PUTCHAR(i,p)@ *p = 0; return strStrSearch(res, s, buf);}intstrReverseChrSearch(int *res, str s, chr *c){ int i = (int) *c; char buf[7], *p = buf;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -