Changeset: 9d1d04ffa824 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=9d1d04ffa824
Modified Files:
        monetdb5/modules/atoms/str.c
        monetdb5/modules/atoms/str.h
        monetdb5/modules/kernel/batstr.c
Branch: alloc-less-str
Log Message:

Cleaned unicode conversion string functions


diffs (truncated from 423 to 300 lines):

diff --git a/monetdb5/modules/atoms/str.c b/monetdb5/modules/atoms/str.c
--- a/monetdb5/modules/atoms/str.c
+++ b/monetdb5/modules/atoms/str.c
@@ -3499,51 +3499,65 @@ STRSubString(str *res, const str *arg1, 
 }
 
 str
+str_from_wchr(str *buf, int *buflen, int c)
+{
+       if (is_int_nil(c)) {
+               strcpy(*buf, str_nil);
+               return MAL_SUCCEED;
+       }
+       CHECK_BUFFER_LENGTH(buf, buflen, 5, "str.unicode");
+       str s = *buf;
+       UTF8_PUTCHAR(c, s);
+       *s = 0;
+       return MAL_SUCCEED;
+illegal:
+       throw(MAL, "str.unicode", SQLSTATE(42000) "Illegal Unicode code point");
+}
+
+str
 STRFromWChr(str *res, const int *c)
 {
-       str s;
+       int buflen = INITIAL_STR_BUFFER_LENGTH;
+       str buf = GDKmalloc(buflen), msg;
 
-       if (is_int_nil(*c)) {
-               *res = GDKstrdup(str_nil);
-               if (*res == NULL)
-                       throw(MAL, "str.unicode", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
-               return MAL_SUCCEED;
+       *res = NULL;
+       if (!buf)
+               throw(SQL, "str.unicode", SQLSTATE(HY013) MAL_MALLOC_FAIL);
+       msg = str_from_wchr(&buf, &buflen, *c);
+       if (!msg && !(*res = GDKstrdup(buf))) {
+               msg = createException(MAL, "str.unicode", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
        }
 
-       s = *res = GDKmalloc(5);
-       if (*res == NULL)
-               throw(MAL, "str.unicode", SQLSTATE(HY013) MAL_MALLOC_FAIL);
-       UTF8_PUTCHAR(*c, s);
-       *s = 0;
-       return MAL_SUCCEED;
-  illegal:
-       GDKfree(*res);
-       *res = NULL;
-       throw(MAL, "str.unicode", SQLSTATE(42000) "Illegal Unicode code point");
+       GDKfree(buf);
+       return msg;
 }
 
 /* return the Unicode code point of arg1 at position at */
 str
-STRWChrAt(int *res, const str *arg1, const int *at)
+str_wchr_at(int *res, const char *s, int at)
 {
-/* 64bit: should have lng arg */
-       const char *s = *arg1;
-
-       if (strNil(s) || is_int_nil(*at) || *at < 0) {
+       /* 64bit: should have lng arg */
+       if (strNil(s) || is_int_nil(at) || at < 0) {
                *res = int_nil;
                return MAL_SUCCEED;
        }
-       s = UTF8_strtail(s, *at);
+       s = UTF8_strtail(s, at);
        if (s == NULL || *s == 0) {
                *res = int_nil;
                return MAL_SUCCEED;
        }
        UTF8_GETCHAR(*res, s);
        return MAL_SUCCEED;
-  illegal:
+illegal:
        throw(MAL, "str.unicodeAt", SQLSTATE(42000) "Illegal Unicode code 
point");
 }
 
+str
+STRWChrAt(int *res, const str *arg1, const int *at)
+{
+       return str_wchr_at(res, *arg1, *at);
+}
+
 /* returns whether arg1 starts with arg2 */
 str
 STRPrefix(bit *res, const str *arg1, const str *arg2)
diff --git a/monetdb5/modules/atoms/str.h b/monetdb5/modules/atoms/str.h
--- a/monetdb5/modules/atoms/str.h
+++ b/monetdb5/modules/atoms/str.h
@@ -18,6 +18,9 @@
 int str_length(const char *s);
 int str_bytes(const char *s);
 
+str str_from_wchr(str *buf, int *buflen, int c);
+str str_wchr_at(int *res, const char *s, int at);
+
 str str_tail(str *buf, int *buflen, const char *s, int off);
 str str_Sub_String(str *buf, int *buflen, const char *s, int off, int l);
 str str_substring_tail(str *buf, int *buflen, const char *s, int start);
diff --git a/monetdb5/modules/kernel/batstr.c b/monetdb5/modules/kernel/batstr.c
--- a/monetdb5/modules/kernel/batstr.c
+++ b/monetdb5/modules/kernel/batstr.c
@@ -169,85 +169,98 @@ bunins_failed:
 }
 
 static str
-STRbatFromWChr(bat *ret, const bat *l)
+STRbatFromWChr(bat *res, const bat *l)
 {
-       BAT *bn, *b;
-       BUN q;
-       str y;
-       int *restrict input;
-       str msg = MAL_SUCCEED;
-
-       prepareOperand(b, l, "batstr.unicode");
-       prepareResult(bn, b, TYPE_str, "batstr.unicode");
-       input = Tloc(b, 0);
-       q = BATcount(b);
+       BAT *bn = NULL, *b = NULL;
+       BUN p, q;
+       int buflen = INITIAL_STR_BUFFER_LENGTH, *restrict vals;
+       str buf = GDKmalloc(buflen), msg = MAL_SUCCEED;
+       bool nils = false;
 
-       for (BUN p = 0 ; p < q; p++) {
-               int x = input[p];
-               gdk_return res;
+       if ((b = BATdescriptor(*l)) == NULL) {
+               msg = createException(MAL, "batstr.unicode", SQLSTATE(HY005) 
RUNTIME_OBJECT_MISSING);
+               goto bailout;
+       }
+       q = BATcount(b);
+       if (!(bn = COLnew(b->hseqbase, TYPE_str, q, TRANSIENT))) {
+               msg = createException(MAL, "batstr.unicode", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
+               goto bailout;
+       }
 
-               if ((msg = STRFromWChr(&y, &x)) != MAL_SUCCEED)
+       vals = Tloc(b, 0);
+       for (p = 0; p < q ; p++) {
+               if ((msg = str_from_wchr(&buf, &buflen, vals[p])) != 
MAL_SUCCEED)
                        goto bailout;
-               res = bunfastappVAR(bn, y);
-               GDKfree(y);
-               if (res != GDK_SUCCEED)
+               if (tfastins_nocheckVAR(bn, p, buf, Tsize(bn)) != GDK_SUCCEED) {
+                       msg = createException(MAL, "batstr.unicode", 
SQLSTATE(HY013) MAL_MALLOC_FAIL);
                        goto bailout;
+               }
+               nils |= strNil(buf);
        }
-       bn->tnonil = b->tnonil;
-       bn->tnil = b->tnonil;
-       finalizeResult(ret, bn, b);
-       return MAL_SUCCEED;
 
 bailout:
-       BBPunfix(b->batCacheid);
-       BBPunfix(bn->batCacheid);
-       if (msg != MAL_SUCCEED)
-               return msg;
-       throw(MAL, "batstr.unicode", OPERATION_FAILED " During bulk operation");
+       GDKfree(buf);
+       if (b)
+               BBPunfix(b->batCacheid);
+       if (bn && !msg) {
+               BATsetcount(bn, q);
+               bn->tnil = nils;
+               bn->tnonil = !nils;
+               bn->tkey = BATcount(bn) <= 1;
+               bn->tsorted = BATcount(bn) <= 1;
+               bn->trevsorted = BATcount(bn) <= 1;
+               BBPkeepref(*res = bn->batCacheid);
+       } else if (bn)
+               BBPreclaim(bn);
+       return msg;
 }
 
 static str
-STRbatSpace(bat *ret, const bat *l)
+STRbatSpace(bat *res, const bat *l)
 {
-       BAT *bn, *b;
-       BUN q;
-       str y, msg = MAL_SUCCEED;
-       int *restrict input;
+       BAT *bn = NULL, *b = NULL;
+       BUN p, q;
+       int buflen = INITIAL_STR_BUFFER_LENGTH, *restrict vals;
+       str buf = GDKmalloc(buflen), msg = MAL_SUCCEED;
+       bool nils = false;
 
-       prepareOperand(b, l, "batstr.Space");
-       prepareResult(bn, b, TYPE_str, "batstr.Space");
-       bn->tnonil=true;
-       bn->tnil=false;
-
-       input = Tloc(b, 0);
+       if ((b = BATdescriptor(*l)) == NULL) {
+               msg = createException(MAL, "batstr.space", SQLSTATE(HY005) 
RUNTIME_OBJECT_MISSING);
+               goto bailout;
+       }
        q = BATcount(b);
+       if (!(bn = COLnew(b->hseqbase, TYPE_str, q, TRANSIENT))) {
+               msg = createException(MAL, "batstr.space", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
+               goto bailout;
+       }
 
-       for (BUN p = 0; p < q; p++) {
-               if ((msg = STRspace(&y, &input[p])) != MAL_SUCCEED)
+       vals = Tloc(b, 0);
+       for (p = 0; p < q ; p++) {
+               char space[]= " ", *s = space;
+
+               if ((msg = str_repeat(&buf, &buflen, s, vals[p])) != 
MAL_SUCCEED)
                        goto bailout;
-               if (bunfastappVAR(bn, y) != GDK_SUCCEED) {
-                       GDKfree(y);
+               if (tfastins_nocheckVAR(bn, p, buf, Tsize(bn)) != GDK_SUCCEED) {
+                       msg = createException(MAL, "batstr.space", 
SQLSTATE(HY013) MAL_MALLOC_FAIL);
                        goto bailout;
                }
-               if (strNil(y)) {
-                       bn->tnonil = false;
-                       bn->tnil = true;
-               }
-               GDKfree(y);
+               nils |= strNil(buf);
        }
 
 bailout:
-       if (msg) {
-               BBPreclaim(bn);
-       } else {
+       GDKfree(buf);
+       if (b)
+               BBPunfix(b->batCacheid);
+       if (bn && !msg) {
                BATsetcount(bn, q);
+               bn->tnil = nils;
+               bn->tnonil = !nils;
                bn->tkey = BATcount(bn) <= 1;
                bn->tsorted = BATcount(bn) <= 1;
                bn->trevsorted = BATcount(bn) <= 1;
-               BBPkeepref(*ret = bn->batCacheid);
-       }
-       if (b)
-               BBPunfix(b->batCacheid);
+               BBPkeepref(*res = bn->batCacheid);
+       } else if (bn)
+               BBPreclaim(bn);
        return msg;
 }
 
@@ -1130,66 +1143,114 @@ STRbatRstrSearchcst(bat *ret, const bat 
 }
 
 static str
-STRbatWChrAt(bat *ret, const bat *l, const bat *r)
+STRbatWChrAt(bat *res, const bat *l, const bat *r)
 {
-       BATiter lefti, righti;
-       BAT *bn, *left, *right;
-       BUN p,q;
-       int v;
+       BATiter lefti;
+       BAT *bn = NULL, *left = NULL, *right = NULL;
+       BUN p, q;
+       int buflen = INITIAL_STR_BUFFER_LENGTH, *restrict righti, *restrict 
vals, next;
+       str x, buf = GDKmalloc(buflen), msg = MAL_SUCCEED;
+       bool nils = false;
 
-       prepareOperand2(left,l,right,r,"batstr.unicodeAt");
-       if(BATcount(left) != BATcount(right)) {
-               BBPunfix(left->batCacheid);
-               BBPunfix(right->batCacheid);
-               throw(MAL, "batstr.unicodeAt", ILLEGAL_ARGUMENT " Requires bats 
of identical size");
+       if (!buf) {
+               msg = createException(MAL, "batstr.unicodeAt", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
+               goto bailout;
+       }
+       if (!(left = BATdescriptor(*l)) || !(right = BATdescriptor(*r))) {
+               msg = createException(MAL, "batstr.unicodeAt", SQLSTATE(HY005) 
RUNTIME_OBJECT_MISSING);
+               goto bailout;
        }
-       prepareResult2(bn,left,right,TYPE_int,"batstr.unicodeAt");
+       if (BATcount(left) != BATcount(right)) {
+               msg = createException(MAL, "batstr.unicodeAt", ILLEGAL_ARGUMENT 
" Requires bats of identical size");
+               goto bailout;
+       }
+       q = BATcount(left);
+       if (!(bn = COLnew(left->hseqbase, TYPE_int, q, TRANSIENT))) {
+               msg = createException(MAL, "batstr.unicodeAt", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to