Changeset: b7572eea5db3 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=b7572eea5db3
Modified Files:
monetdb5/modules/atoms/str.c
monetdb5/modules/atoms/str.h
monetdb5/modules/kernel/batstr.c
Branch: alloc-less-str
Log Message:
Cleaning str.c file so the MAL callback function must look up for NULL values
instead of the string function implementations
diffs (truncated from 1573 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
@@ -3216,59 +3216,52 @@ convertCase(BAT *from, BAT *to, str *buf
char *dst;
const char *end = src + len;
bool lower_to_upper = from == UTF8_toUpperFrom;
-
- if (strNil(src)) {
- strcpy(*buf, str_nil);
- return MAL_SUCCEED;
- } else {
- Hash *h;
- size_t nextlen = len + 1;
- if (BAThash(from) != GDK_SUCCEED)
- throw(MAL, malfunc, SQLSTATE(HY013) MAL_MALLOC_FAIL);
- CHECK_STR_BUFFER_LENGTH(buf, buflen, nextlen, malfunc);
-
- h = from->thash;
- dst = *buf;
- while (src < end) {
- int c;
-
- UTF8_GETCHAR(c, src);
- if ((c & 0x80) == 0) {
- /* for ASCII characters we don't need to do a
hash
- * lookup */
- if (lower_to_upper) {
- if ('a' <= c && c <= 'z')
- c += 'A' - 'a';
- } else {
- if ('A' <= c && c <= 'Z')
- c += 'a' - 'A';
- }
+ Hash *h;
+ size_t nextlen = len + 1;
+
+ if (BAThash(from) != GDK_SUCCEED)
+ throw(MAL, malfunc, SQLSTATE(HY013) MAL_MALLOC_FAIL);
+ h = from->thash;
+ CHECK_STR_BUFFER_LENGTH(buf, buflen, nextlen, malfunc);
+ dst = *buf;
+ while (src < end) {
+ int c;
+
+ UTF8_GETCHAR(c, src);
+ if ((c & 0x80) == 0) {
+ /* for ASCII characters we don't need to do a hash
lookup */
+ if (lower_to_upper) {
+ if ('a' <= c && c <= 'z')
+ c += 'A' - 'a';
} else {
- /* use hash, even though BAT is sorted */
- for (BUN hb = HASHget(h, hash_int(h, &c));
- hb != HASHnil(h);
- hb = HASHgetlink(h, hb)) {
- if (c == ((int *)
from->theap.base)[hb]) {
- c = ((int *)
to->theap.base)[hb];
- break;
- }
+ if ('A' <= c && c <= 'Z')
+ c += 'a' - 'A';
+ }
+ } else {
+ /* use hash, even though BAT is sorted */
+ for (BUN hb = HASHget(h, hash_int(h, &c));
+ hb != HASHnil(h);
+ hb = HASHgetlink(h, hb)) {
+ if (c == ((int *) from->theap.base)[hb]) {
+ c = ((int *) to->theap.base)[hb];
+ break;
}
}
- if (dst + UTF8_CHARLEN(c) > *buf + len) {
- /* doesn't fit, so allocate more space;
- * also allocate enough for the rest of the
- * source */
- size_t off = dst - *buf;
- size_t nextlen = (len += 4 + (end - src)) + 1;
-
- CHECK_STR_BUFFER_LENGTH(buf, buflen, nextlen,
malfunc);
- dst = *buf + off;
- }
- UTF8_PUTCHAR(c, dst);
}
- *dst = 0;
- return MAL_SUCCEED;
+ if (dst + UTF8_CHARLEN(c) > *buf + len) {
+ /* doesn't fit, so allocate more space;
+ * also allocate enough for the rest of the
+ * source */
+ size_t off = dst - *buf;
+ size_t nextlen = (len += 4 + (end - src)) + 1;
+
+ CHECK_STR_BUFFER_LENGTH(buf, buflen, nextlen, malfunc);
+ dst = *buf + off;
+ }
+ UTF8_PUTCHAR(c, dst);
}
+ *dst = 0;
+ return MAL_SUCCEED;
illegal:
throw(MAL, malfunc, SQLSTATE(42000) "Illegal Unicode code point");
}
@@ -3354,11 +3347,7 @@ STRtostr(str *res, const str *src)
int
str_utf8_length(str s)
{
- size_t l;
-
- if (strNil(s))
- return int_nil;
- l = UTF8_strlen(s);
+ size_t l = UTF8_strlen(s);
assert(l < INT_MAX);
if (l > INT_MAX)
l = INT_MAX;
@@ -3368,18 +3357,16 @@ str_utf8_length(str s)
static str
STRLength(int *res, const str *arg1)
{
- *res = str_utf8_length(*arg1);
+ str s = *arg1;
+
+ *res = strNil(s) ? int_nil : str_utf8_length(s);
return MAL_SUCCEED;
}
int
str_nbytes(str s)
{
- size_t l;
-
- if (strNil(s))
- return int_nil;
- l = strlen(s);
+ size_t l = strlen(s);
assert(l < INT_MAX);
return (int) l;
}
@@ -3387,17 +3374,15 @@ str_nbytes(str s)
static str
STRBytes(int *res, const str *arg1)
{
- *res = str_nbytes(*arg1);
+ str s = *arg1;
+
+ *res = strNil(s) ? int_nil : str_nbytes(s);
return MAL_SUCCEED;
}
str
str_tail(str *buf, size_t *buflen, str s, int off)
{
- if (strNil(s) || is_int_nil(off)) {
- strcpy(*buf, str_nil);
- return MAL_SUCCEED;
- }
if (off < 0) {
size_t len = UTF8_strlen(s);
@@ -3416,18 +3401,27 @@ str_tail(str *buf, size_t *buflen, str s
static str
STRTail(str *res, const str *arg1, const int *offset)
{
- size_t buflen = INITIAL_STR_BUFFER_LENGTH;
- str buf = GDKmalloc(buflen), msg;
-
- *res = NULL;
- if (!buf)
- throw(MAL, "str.tail", SQLSTATE(HY013) MAL_MALLOC_FAIL);
- msg = str_tail(&buf, &buflen, *arg1, *offset);
- if (!msg && !(*res = GDKstrdup(buf))) {
- msg = createException(MAL, "str.tail", SQLSTATE(HY013)
MAL_MALLOC_FAIL);
+ str buf = NULL, msg = MAL_SUCCEED, s = *arg1;
+ int off = *offset;
+
+ if (strNil(s) || is_int_nil(off)) {
+ *res = GDKstrdup(str_nil);
+ } else {
+ size_t buflen = INITIAL_STR_BUFFER_LENGTH;
+
+ *res = NULL;
+ if (!(buf = GDKmalloc(buflen)))
+ throw(MAL, "str.tail", SQLSTATE(HY013) MAL_MALLOC_FAIL);
+ if ((msg = str_tail(&buf, &buflen, s, off)) != MAL_SUCCEED) {
+ GDKfree(buf);
+ return msg;
+ }
+ *res = GDKstrdup(buf);
}
GDKfree(buf);
+ if (!*res)
+ msg = createException(MAL, "str.tail", SQLSTATE(HY013)
MAL_MALLOC_FAIL);
return msg;
}
@@ -3436,10 +3430,6 @@ str_Sub_String(str *buf, size_t *buflen,
{
size_t len;
- if (strNil(s) || is_int_nil(off) || is_int_nil(l)) {
- strcpy(*buf, str_nil);
- return MAL_SUCCEED;
- }
if (off < 0) {
len = UTF8_strlen(s);
assert(len <= INT_MAX);
@@ -3466,28 +3456,33 @@ str_Sub_String(str *buf, size_t *buflen,
static str
STRSubString(str *res, const str *arg1, const int *offset, const int *length)
{
- size_t buflen = INITIAL_STR_BUFFER_LENGTH;
- str buf = GDKmalloc(buflen), msg;
-
- *res = NULL;
- if (!buf)
- throw(MAL, "str.substring", SQLSTATE(HY013) MAL_MALLOC_FAIL);
- msg = str_Sub_String(&buf, &buflen, *arg1, *offset, *length);
- if (!msg && !(*res = GDKstrdup(buf))) {
- msg = createException(MAL, "str.substring", SQLSTATE(HY013)
MAL_MALLOC_FAIL);
+ str buf = NULL, msg = MAL_SUCCEED, s = *arg1;
+ int off = *offset, len = *length;
+
+ if (strNil(s) || is_int_nil(off) || is_int_nil(len)) {
+ *res = GDKstrdup(str_nil);
+ } else {
+ size_t buflen = INITIAL_STR_BUFFER_LENGTH;
+
+ *res = NULL;
+ if (!(buf = GDKmalloc(buflen)))
+ throw(MAL, "str.substring", SQLSTATE(HY013)
MAL_MALLOC_FAIL);
+ if ((msg = str_Sub_String(&buf, &buflen, s, off, len)) !=
MAL_SUCCEED) {
+ GDKfree(buf);
+ return msg;
+ }
+ *res = GDKstrdup(buf);
}
GDKfree(buf);
+ if (!*res)
+ msg = createException(MAL, "str.substring", SQLSTATE(HY013)
MAL_MALLOC_FAIL);
return msg;
}
str
str_from_wchr(str *buf, size_t *buflen, int c)
{
- if (is_int_nil(c)) {
- strcpy(*buf, str_nil);
- return MAL_SUCCEED;
- }
CHECK_STR_BUFFER_LENGTH(buf, buflen, 5, "str.unicode");
str s = *buf;
UTF8_PUTCHAR(c, s);
@@ -3500,18 +3495,27 @@ illegal:
static str
STRFromWChr(str *res, const int *c)
{
- size_t buflen = MAX(strlen(str_nil) + 1, 8);
- str buf = GDKmalloc(buflen), msg;
-
- *res = NULL;
- if (!buf)
- throw(MAL, "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);
+ str buf = NULL, msg = MAL_SUCCEED;
+ int cc = *c;
+
+ if (is_int_nil(cc)) {
+ *res = GDKstrdup(str_nil);
+ } else {
+ size_t buflen = MAX(strlen(str_nil) + 1, 8);
+
+ *res = NULL;
+ if (!(buf = GDKmalloc(buflen)))
+ throw(MAL, "str.unicode", SQLSTATE(HY013)
MAL_MALLOC_FAIL);
+ if ((msg = str_from_wchr(&buf, &buflen, cc)) != MAL_SUCCEED) {
+ GDKfree(buf);
+ return msg;
+ }
+ *res = GDKstrdup(buf);
}
GDKfree(buf);
+ if (!*res)
+ msg = createException(MAL, "str.unicode", SQLSTATE(HY013)
MAL_MALLOC_FAIL);
return msg;
}
@@ -3545,29 +3549,22 @@ STRWChrAt(int *res, const str *arg1, con
bit
str_is_prefix(str s, str prefix)
{
- if (strNil(s) || strNil(prefix)) {
- return bit_nil;
- }
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list