📄 str.mx
字号:
RETURN_NIL_IF(strNil(s) || *c == chr_nil, TYPE_int); @:UTF8_PUTCHAR(i,p)@ *p = 0; return strReverseStrSearch(res, s, buf);}intstrStrip(str *res, str s){ str start = s; size_t len; while (GDKisspace(*start)) start++; /* Remove the trailing spaces. Make sure not to pass the start */ /* pointer in case a string only contains spaces. */ s = start + strlen(start); while (s > start && GDKisspace(*(s - 1))) s--; len = s - start + 1; *res = GDKmalloc(len); memcpy(*res, start, len - 1); (*res)[len - 1] = '\0'; return GDK_SUCCEED;}intstrLtrim(str *res, str s){ RETURN_NIL_IF(strNil(s), TYPE_str); while (GDKisspace(*s)) s++; *res = GDKstrdup(s); return GDK_SUCCEED;}intstrRtrim(str *res, str s){ size_t len = strlen(s); RETURN_NIL_IF(strNil(s), TYPE_str); while (len > 0 && GDKisspace(s[len - 1])) len--; *res = GDKmalloc(len + 1); memcpy(*res, s, len); (*res)[len] = '\0'; return GDK_SUCCEED;}intstrSubstitute(str *res, str s, str src, str dst, bit *g){ int repeat = *g; size_t lsrc = (src?strlen(src):0), ldst = (dst?strlen(dst):0); size_t l = (s?strLen(s):0), n = l + ldst; str buf, fnd, end; if (repeat && ldst > lsrc && lsrc) { n = (ldst * l) / lsrc; /* max length */ } buf = *res = (str) GDKmalloc(n); end = buf + l; fnd = buf; strcpy(buf, s); if (!lsrc) return GDK_SUCCEED; do { fnd = strstr((fnd < buf) ? buf : fnd, src); if (!fnd) break; memmove(fnd + ldst, fnd + lsrc, end - fnd); memcpy(fnd, dst, ldst); end += ldst - lsrc; fnd += ldst; } while (repeat); return GDK_SUCCEED;}intstrTranslate(str *res, str src, str mapstr, str transtr){ unsigned char *s; unsigned char *dst; int *map, *tran; int c, n, m, i; /* if any of the inputs is nil, return nil */ if (strNil(src) || strNil(mapstr) || strNil(transtr)) { * (ptr *) res = (ptr) ATOMnil(TYPE_str); return GDK_SUCCEED; } /* quick check: empty string in, empty string out; quick check: empty mapstr, return src unchanged */ if (*src == 0 || *mapstr == 0) { *res = GDKstrdup(src); return GDK_SUCCEED; } /* convert mapstr and transtr to arrays of Unicode code points */ n = 0; map = GDKmalloc(strlen(mapstr) * sizeof(int)); s = (unsigned char *) mapstr; for (;;) { @:UTF8_GETCHAR(c,s)@ if (c == int_nil) { GDKfree(map); * (ptr *) res = (ptr) ATOMnil(TYPE_str); return GDK_SUCCEED; } if (!c) break; map[n++] = c; } m = 0; tran = GDKmalloc(strlen(transtr) * sizeof(int)); s = (unsigned char *) transtr; for (;;) { @:UTF8_GETCHAR(c,s)@ if (c == int_nil) { GDKfree(map); GDKfree(tran); * (ptr *) res = (ptr) ATOMnil(TYPE_str); return GDK_SUCCEED; } if (!c) break; tran[m++] = c; } /* now do the translations */ dst = (unsigned char *) GDKmalloc(strlen(src) * 6 + 1); /* more than enough */ *res = (char *) dst; s = (unsigned char *) src; for (;;) { @:UTF8_GETCHAR(c,s)@ if (c == int_nil) { GDKfree(map); GDKfree(tran); GDKfree(*res); * (ptr *) res = (ptr) ATOMnil(TYPE_str); return GDK_SUCCEED; } if (!c) break; for (i = 0; i < n; i++) if (map[i] == c) { if (i < m) { @:UTF8_PUTCHAR(tran[i],dst)@ } break; } if (i == n) { /* not found */ @:UTF8_PUTCHAR(c,dst)@ } } *dst = 0; GDKfree(map); GDKfree(tran); *res = GDKrealloc(*res, strlen(*res) + 1); return GDK_SUCCEED;}intstrSplit(BAT **Res, str s, str sep){ size_t seplen = strlen(sep); char *d, *x; BAT *res; oid n = oid_nil; if (seplen == 0) { return GDK_FAIL; } x = d = GDKstrdup(s); if (d == NULL) return GDK_FAIL; res = BATnew(TYPE_void, TYPE_str, BUFSIZ); if (res == NULL) { GDKfree(x); return GDK_FAIL; } for (;;) { char *e = strstr(d, sep); if (!e) break; *e = 0; BUNins(res, (ptr) &n, d, FALSE); d = e + seplen; } /* remove the test if sep is to be a real separator, now we don't return an empty final string if the source ends with a separator (this is to accommodate Niels) */ if (*d) BUNins(res, (ptr) &n, d, FALSE); GDKfree(x); *Res = res; return GDK_SUCCEED;}intchrSplit(BAT **Res, str s){ char * d = s; oid n = oid_nil; BAT *res = BATnew(TYPE_void, TYPE_chr, strlen(s)); BATseqbase(res,(oid) 0); if (res == NULL) return GDK_FAIL; while (*d) BUNins(res, (ptr) &n, (ptr) d++, FALSE); *Res = res; return GDK_SUCCEED;}@@- WrappersHere you find the wrappers around the version 4 library codeIt also contains the direct implementation of the stringmatching support routines.@c#include "mal_exception.h"strSTRfindUnescapedOccurrence(str b, str c, str esc){ str t; t= strstr(b,c); while( t){ /* check for escaped version */ if( t>b && *esc == *(t-1) ) { t= strstr(t+1,c); } else return t; } return 0;}@-The SQL like function return a boolean@cintSTRlike(str s, str pat, str esc){ str t,p; t= s; for( p= pat; *p && *t; p++){ if(esc && *p == *esc) { p++; if( *p != *t) return FALSE; t++; } else if( *p == '_') t++; else if( *p == '%'){ p++; while(*p == '%') p++; if( *p == 0) return TRUE; /* tail is acceptable */ for(; *p && *t; t++) if( STRlike(t,p,esc)) return TRUE; if( *p == 0 && *t == 0) return TRUE; return FALSE; } else if( *p == *t) t++; else return FALSE; } if( *p == '%' && *(p+1)==0) return TRUE; return *t == 0 && *p == 0;}strSTRfindOccurrence(str *ret, str *s, str *pat, str *esc){ *ret = STRfindUnescapedOccurrence(*s,*pat,*esc); if( *ret) *ret= GDKstrdup(*ret); return MAL_SUCCEED;}strSTRlikewrap(bit *ret, str *s, str *pat, str *esc){ *ret = STRlike(*s,*pat,*esc); return MAL_SUCCEED;}strSTRlikewrap2(bit *ret, str *s, str *pat){ *ret = STRlike(*s,*pat,0); return MAL_SUCCEED;}strSTRtostr(str *res, str *src){ if( *src == 0) *res= GDKstrdup(str_nil); else *res = GDKstrdup(*src); return MAL_SUCCEED;}@-The concatenate operator requires a type in most cases.@cstrSTRConcat(str *res, str *val1, str *val2){ strConcat(res, *val1, *val2, TYPE_str); return MAL_SUCCEED;}strSTRLength(int *res, str *arg1){ strLength(res, *arg1); return MAL_SUCCEED;}strSTRBytes(int *res, str *arg1){ strBytes(res, *arg1); return MAL_SUCCEED;}strSTRTail(str *res, str *arg1, int *offset){ strTail(res, *arg1, offset); return MAL_SUCCEED;}strSTRSubString(str *res, str *arg1, int *offset, int *length){ strSubString(res, *arg1, offset, length); return MAL_SUCCEED;}strSTRFromWChr(str *res, int *at){ strFromWChr(res, at); return MAL_SUCCEED;}strSTRWChrAt(int *res, str *arg1, int *at){ strWChrAt(res, *arg1, at); return MAL_SUCCEED;}strSTRcodeset(str *res){ codeset(res); return MAL_SUCCEED;}strSTRIconv(str *res, str *o, str *fp, str *tp){ strIconv(res, *o, *fp, *tp); return MAL_SUCCEED;}strSTRChrAt(chr *res, str *arg1, int *at){ strChrAt(res, *arg1, at); return MAL_SUCCEED;}strSTRPrefix(bit *res, str *arg1, str *arg2){ strPrefix(res, *arg1, *arg2); return MAL_SUCCEED;}strSTRSuffix(bit *res, str *arg1, str *arg2){ strSuffix(res, *arg1, *arg2); return MAL_SUCCEED;}strSTRLower(str *res, str *arg1){ strLower(res, *arg1); return MAL_SUCCEED;}strSTRUpper(str *res, str *arg1){ strUpper(res, *arg1); return MAL_SUCCEED;}strSTRChrSearch(int *res, str *arg1, chr *c){ strChrSearch(res, *arg1, c); return MAL_SUCCEED;}strSTRstrSearch(int *res, str *arg1, str *arg2){ strStrSearch(res, *arg1, *arg2); return MAL_SUCCEED;}strSTRReverseStrSearch(int *res, str *arg1, str *arg2){ strReverseStrSearch(res, *arg1, *arg2); return MAL_SUCCEED;}strSTRchrSearch(int *res, str *arg1, chr *c){ strReverseChrSearch(res, *arg1, c); return MAL_SUCCEED;}strSTRReverseChrSearch(int *res, str *arg1, chr *c){ strReverseChrSearch(res, *arg1, c); return MAL_SUCCEED;}strSTRStrip(str *res, str *arg1){ strStrip(res, *arg1); return MAL_SUCCEED;}strSTRLtrim(str *res, str *arg1){ strLtrim(res, *arg1); return MAL_SUCCEED;}strSTRRtrim(str *res, str *arg1){ strRtrim(res, *arg1); return MAL_SUCCEED;}strSTRSubstitute(str *res, str *arg1, str *arg2, str *arg3, bit *g){ strSubstitute(res, *arg1, *arg2, *arg3, g); return MAL_SUCCEED;}@-A few old MIL procs implementations@cstr STRascii(int *ret, str *s){ int offset=0; return STRWChrAt(ret,s,&offset);}strSTRsubstringTail(str *ret, str *s, int *start){ int offset= *start; if( offset <1) offset =1; offset--; return STRTail(ret, s, &offset);}strSTRsubstring(str *ret, str *s, int *start, int *l){ int offset= *start; if( offset <1) offset =1; offset--; return STRSubString(ret, s, &offset, l);}strSTRprefix(str *ret, str *s, int *l){ int start =0; return STRSubString(ret,s,&start,l);}strSTRsuffix(str *ret, str *s, int *l){ int start = strlen(*s)- *l; return STRSubString(ret,s,&start,l);}strSTRlocate(int *ret, str *s1, str *s2){ int p; strStrSearch(&p, *s2, *s1); *ret= p>=0? p+1:0; return MAL_SUCCEED;}strSTRlocate2(int *ret, str *s1, str *s2, int *start){ int p; str dummy; strTail(&dummy, *s1, start); strStrSearch(&p, *s2, dummy); if( dummy) GDKfree(dummy); *ret= p>=0? p+1:0; return MAL_SUCCEED;}strSTRinsert(str *ret, str *s, int *start, int *l, str *s2){ str v; if(strcmp(*s2,str_nil) ==0 || strcmp(*s,str_nil)==0 ) *ret = GDKstrdup( (str)str_nil); else { if( *start <0) *start =1; v= *ret = GDKmalloc(strlen(*s)+strlen(*s2)+1 ); strncpy(v, *s,*start); v[*start]=0; strcat(v,*s2); if( *start + *l < (int) strlen(*s)) strcat(v,*s + *start + *l); } return MAL_SUCCEED;}strSTRreplace(str *ret, str *s1, str *s2, str *s3){ bit flag= TRUE; return STRSubstitute(ret,s1,s2,s3,&flag);}strSTRrepeat(str *ret, str *s, int *c){ str t; int i; size_t l; if( *c < 0 || strcmp(*s,str_nil)==0 ) *ret= GDKstrdup(str_nil); else { l= strlen(*s); t= *ret = GDKmalloc( *c * l +1); *t = 0; for(i= *c; i>0; i--,t+= l) strcat(t, *s); } return MAL_SUCCEED;}strSTRspace(str *ret, int *l){ char buf[]= " ", *s= buf; return STRrepeat(ret,&s,l);}strSTRstringLength(int *res, str *s){ str r = NULL; strRtrim(&r, *s); strLength(res, r); GDKfree(r); return MAL_SUCCEED;}@}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -