Changeset: 88076744adda for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=88076744adda
Added Files:
        monetdb5/modules/atoms/Tests/strpad.mal
        monetdb5/modules/atoms/Tests/strpad.stable.err
        monetdb5/modules/atoms/Tests/strpad.stable.out
        monetdb5/modules/atoms/Tests/strtrim.mal
        monetdb5/modules/atoms/Tests/strtrim.stable.err
        monetdb5/modules/atoms/Tests/strtrim.stable.out
        sql/test/Tests/str-pad.sql
        sql/test/Tests/str-pad.stable.err
        sql/test/Tests/str-pad.stable.out
        sql/test/Tests/str-trim.sql
        sql/test/Tests/str-trim.stable.err
        sql/test/Tests/str-trim.stable.out
Modified Files:
        gdk/gdk.h
        gdk/gdk_utils.c
        monetdb5/modules/atoms/str.c
        monetdb5/modules/atoms/str.h
        monetdb5/modules/atoms/str.mal
        monetdb5/modules/kernel/batstr.c
        monetdb5/modules/kernel/batstr.mal
        sql/common/sql_types.c
Branch: default
Log Message:

Implemented PostgreSQL compatable string TRIM and PAD functions:

trim(string text [, characters text]): remove the longest string consisting 
only of characters in characters (a space by default) from the start and end of 
string
ltrim(string text [, characters text]): remove the longest string containing 
only characters from characters (a space by default) from the start of string
rtrim(string text [, characters text]): remove the longest string containing 
only characters from characters (a space by default) from the end of string

lpad(string text, length int [, fill text]): fill up the string to length 
length by prepending the characters fill (a space by default). If the string is 
already longer than length then it is truncated (on the right).
rpad(string text, length int [, fill text]): fill up the string to length 
length by appending the characters fill (a space by default). If the string is 
already longer than length then it is truncated.


diffs (truncated from 2145 to 300 lines):

diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -2223,6 +2223,8 @@ gdk_export lng IMPSimprintsize(BAT *b);
  * @tab GDKfree (void* blk)
  * @item str
  * @tab GDKstrdup (str s)
+ * @item str
+ * @tab GDKstrndup (str s, size_t n)
  * @end multitable
  *
  * These utilities are primarily used to maintain control over
@@ -2250,6 +2252,7 @@ gdk_export void *GDKzalloc(size_t size);
 gdk_export void *GDKrealloc(void *pold, size_t size);
 gdk_export void GDKfree(void *blk);
 gdk_export str GDKstrdup(const char *s);
+gdk_export str GDKstrndup(const char *s, size_t n);
 
 #if !defined(NDEBUG) && !defined(__clang_analyzer__)
 /* In debugging mode, replace GDKmalloc and other functions with a
@@ -2321,6 +2324,19 @@ gdk_export str GDKstrdup(const char *s);
                                __func__, __FILE__, __LINE__);          \
                _res;                                                   \
        })
+#define GDKstrndup(s,n)                                                        
\
+       ({                                                              \
+               const char *_str = (s);                                 \
+               void *_res = GDKstrndup(_str,n);                                
\
+               ALLOCDEBUG                                              \
+                       fprintf(stderr,                                 \
+                               "#GDKstrndup(len=" SZFMT ") -> " PTRFMT \
+                               " %s[%s:%d]\n",                         \
+                               n,                              \
+                               PTRFMTCAST _res,                        \
+                               __func__, __FILE__, __LINE__);          \
+               _res;                                                   \
+       })
 #define GDKmmap(p, m, l)                                               \
        ({                                                              \
                const char *_path = (p);                                \
@@ -2436,6 +2452,16 @@ GDKstrdup_debug(const char *str, const c
        return res;
 }
 #define GDKstrdup(s)   GDKstrdup_debug((s), __FILE__, __LINE__)
+static inline char *
+GDKstrndup_debug(const char *str, size_t n, const char *filename, int lineno)
+{
+       void *res = GDKstrndup(str,n);
+       ALLOCDEBUG fprintf(stderr, "#GDKstrndup(len=" SZFMT ") -> "
+                          PTRFMT " [%s:%d]\n",
+                          n, PTRFMTCAST res, filename, lineno);
+       return res;
+}
+#define GDKstrndup(s)  GDKstrndup_debug((s), __FILE__, __LINE__)
 static inline void *
 GDKmmap_debug(const char *path, int mode, size_t len, const char *filename, 
int lineno)
 {
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -846,6 +846,18 @@ GDKstrdup(const char *s)
        return n;
 }
 
+#undef GDKstrndup
+char *
+GDKstrndup(const char *s, size_t n)
+{
+       char *r = (char *) GDKmalloc(n+1);
+
+       if (r) {
+               memcpy(r, s, n);
+               r[n] = '\0';
+       }
+       return r;
+}
 
 /*
  * @- virtual memory
diff --git a/monetdb5/modules/atoms/Tests/strpad.mal 
b/monetdb5/modules/atoms/Tests/strpad.mal
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/atoms/Tests/strpad.mal
@@ -0,0 +1,26 @@
+a := str.lpad("hi", 7);
+io.print(a);
+a := str.lpad("hixyäbcdef", 7);
+io.print(a);
+a := str.lpad("hi", 7, "xya");
+io.print(a);
+a := str.lpad("hi", 7, "xyä");
+io.print(a);
+a := str.lpad("hi", 7, "xyäbcdef");
+io.print(a);
+a := str.lpad("hixyäbcdef", 7, "lmn");
+io.print(a);
+
+a := str.rpad("hi", 7);
+io.print(a);
+a := str.rpad("hixyäbcdef", 7);
+io.print(a);
+a := str.rpad("hi", 7, "xya");
+io.print(a);
+a := str.rpad("hi", 7, "xyä");
+io.print(a);
+a := str.rpad("hi", 7, "xyäbcdef");
+io.print(a);
+a := str.rpad("hixyäbcdef", 7, "lmn");
+io.print(a);
+
diff --git a/monetdb5/modules/atoms/Tests/strpad.stable.err 
b/monetdb5/modules/atoms/Tests/strpad.stable.err
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/atoms/Tests/strpad.stable.err
@@ -0,0 +1,30 @@
+stderr of test 'strpad` in directory 'monetdb5/modules/atoms` itself:
+
+
+# 16:40:08 >  
+# 16:40:08 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=31145" "--set" 
"mapi_usock=/var/tmp/mtest-5336/.s.monetdb.31145" "--set" "monet_prompt=" 
"--forcemito" "--set" "mal_listing=2" 
"--dbpath=/export/scratch2/zhang/monet-install/default/debug/var/MonetDB/mTests_monetdb5_modules_atoms"
 "strpad.mal"
+# 16:40:08 >  
+
+# builtin opt  gdk_dbpath = 
/export/scratch2/zhang/monet-install/default/debug/var/monetdb5/dbfarm/demo
+# builtin opt  gdk_debug = 0
+# builtin opt  gdk_vmtrim = no
+# builtin opt  monet_prompt = >
+# builtin opt  monet_daemon = no
+# builtin opt  mapi_port = 50000
+# builtin opt  mapi_open = false
+# builtin opt  mapi_autosense = false
+# builtin opt  sql_optimizer = default_pipe
+# builtin opt  sql_debug = 0
+# cmdline opt  gdk_nr_threads = 0
+# cmdline opt  mapi_open = true
+# cmdline opt  mapi_port = 31145
+# cmdline opt  mapi_usock = /var/tmp/mtest-5336/.s.monetdb.31145
+# cmdline opt  monet_prompt = 
+# cmdline opt  mal_listing = 2
+# cmdline opt  gdk_dbpath = 
/export/scratch2/zhang/monet-install/default/debug/var/MonetDB/mTests_monetdb5_modules_atoms
+# cmdline opt  gdk_debug = 536870922
+
+# 16:40:08 >  
+# 16:40:08 >  "Done."
+# 16:40:08 >  
+
diff --git a/monetdb5/modules/atoms/Tests/strpad.stable.out 
b/monetdb5/modules/atoms/Tests/strpad.stable.out
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/atoms/Tests/strpad.stable.out
@@ -0,0 +1,63 @@
+stdout of test 'strpad` in directory 'monetdb5/modules/atoms` itself:
+
+
+# 16:40:08 >  
+# 16:40:08 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=31145" "--set" 
"mapi_usock=/var/tmp/mtest-5336/.s.monetdb.31145" "--set" "monet_prompt=" 
"--forcemito" "--set" "mal_listing=2" 
"--dbpath=/export/scratch2/zhang/monet-install/default/debug/var/MonetDB/mTests_monetdb5_modules_atoms"
 "strpad.mal"
+# 16:40:08 >  
+
+# MonetDB 5 server v11.18.0
+# This is an unreleased version
+# Serving database 'mTests_monetdb5_modules_atoms', using 8 threads
+# Compiled for x86_64-unknown-linux-gnu/64bit with 64bit OIDs dynamically 
linked
+# Found 15.591 GiB available main-memory.
+# Copyright (c) 1993-July 2008 CWI.
+# Copyright (c) August 2008-2014 MonetDB B.V., all rights reserved
+# Visit http://www.monetdb.org/ for further information
+# Listening for connection requests on mapi:monetdb://riga.ins.cwi.nl:31145/
+# Listening for UNIX domain connection requests on 
mapi:monetdb:///var/tmp/mtest-5336/.s.monetdb.31145
+# MonetDB/GIS module loaded
+# MonetDB/JAQL module loaded
+# MonetDB/SQL module loaded
+function user.main():void;
+    a := str.lpad("hi",7);
+    io.print(a);
+    a := str.lpad("hixyäbcdef",7);
+    io.print(a);
+    a := str.lpad("hi",7,"xya");
+    io.print(a);
+    a := str.lpad("hi",7,"xyä");
+    io.print(a);
+    a := str.lpad("hi",7,"xyäbcdef");
+    io.print(a);
+    a := str.lpad("hixyäbcdef",7,"lmn");
+    io.print(a);
+    a := str.rpad("hi",7);
+    io.print(a);
+    a := str.rpad("hixyäbcdef",7);
+    io.print(a);
+    a := str.rpad("hi",7,"xya");
+    io.print(a);
+    a := str.rpad("hi",7,"xyä");
+    io.print(a);
+    a := str.rpad("hi",7,"xyäbcdef");
+    io.print(a);
+    a := str.rpad("hixyäbcdef",7,"lmn");
+    io.print(a);
+end main;
+[ "     hi" ]
+[ "hixyäbc" ]
+[ "xyaxyhi" ]
+[ "xyäxyhi" ]
+[ "xyäbchi" ]
+[ "hixyäbc" ]
+[ "hi     " ]
+[ "hixyäbc" ]
+[ "hixyaxy" ]
+[ "hixyäxy" ]
+[ "hixyäbc" ]
+[ "hixyäbc" ]
+
+# 16:40:08 >  
+# 16:40:08 >  "Done."
+# 16:40:08 >  
+
diff --git a/monetdb5/modules/atoms/Tests/strtrim.mal 
b/monetdb5/modules/atoms/Tests/strtrim.mal
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/atoms/Tests/strtrim.mal
@@ -0,0 +1,21 @@
+a := str.trim("zzzytrimxxxx", "zyx");
+io.print(a);
+a := str.trim("zzëzytrimxxëxx", "zëyx");
+io.print(a);
+a := str.trim("zzë颖zytrimxx颖ëxx", "zëy颖x");
+io.print(a);
+
+a := str.ltrim("zzzytrim", "zyx");
+io.print(a);
+a := str.ltrim("zzëzytrim", "zëyx");
+io.print(a);
+a := str.ltrim("zzë颖zytrim", "zëy颖x");
+io.print(a);
+
+a := str.rtrim("trimxxxx", "zyx");
+io.print(a);
+a := str.rtrim("trimxxëxx", "zëyx");
+io.print(a);
+a := str.rtrim("trimxx颖ëxx", "zëy颖x");
+io.print(a);
+
diff --git a/monetdb5/modules/atoms/Tests/strtrim.stable.err 
b/monetdb5/modules/atoms/Tests/strtrim.stable.err
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/atoms/Tests/strtrim.stable.err
@@ -0,0 +1,30 @@
+stderr of test 'strtrim` in directory 'monetdb5/modules/atoms` itself:
+
+
+# 16:41:06 >  
+# 16:41:06 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=35634" "--set" 
"mapi_usock=/var/tmp/mtest-5410/.s.monetdb.35634" "--set" "monet_prompt=" 
"--forcemito" "--set" "mal_listing=2" 
"--dbpath=/export/scratch2/zhang/monet-install/default/debug/var/MonetDB/mTests_monetdb5_modules_atoms"
 "strtrim.mal"
+# 16:41:06 >  
+
+# builtin opt  gdk_dbpath = 
/export/scratch2/zhang/monet-install/default/debug/var/monetdb5/dbfarm/demo
+# builtin opt  gdk_debug = 0
+# builtin opt  gdk_vmtrim = no
+# builtin opt  monet_prompt = >
+# builtin opt  monet_daemon = no
+# builtin opt  mapi_port = 50000
+# builtin opt  mapi_open = false
+# builtin opt  mapi_autosense = false
+# builtin opt  sql_optimizer = default_pipe
+# builtin opt  sql_debug = 0
+# cmdline opt  gdk_nr_threads = 0
+# cmdline opt  mapi_open = true
+# cmdline opt  mapi_port = 35634
+# cmdline opt  mapi_usock = /var/tmp/mtest-5410/.s.monetdb.35634
+# cmdline opt  monet_prompt = 
+# cmdline opt  mal_listing = 2
+# cmdline opt  gdk_dbpath = 
/export/scratch2/zhang/monet-install/default/debug/var/MonetDB/mTests_monetdb5_modules_atoms
+# cmdline opt  gdk_debug = 536870922
+
+# 16:41:06 >  
+# 16:41:06 >  "Done."
+# 16:41:06 >  
+
diff --git a/monetdb5/modules/atoms/Tests/strtrim.stable.out 
b/monetdb5/modules/atoms/Tests/strtrim.stable.out
new file mode 100644
--- /dev/null
+++ b/monetdb5/modules/atoms/Tests/strtrim.stable.out
@@ -0,0 +1,54 @@
+stdout of test 'strtrim` in directory 'monetdb5/modules/atoms` itself:
+
+
+# 16:41:06 >  
+# 16:41:06 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=35634" "--set" 
"mapi_usock=/var/tmp/mtest-5410/.s.monetdb.35634" "--set" "monet_prompt=" 
"--forcemito" "--set" "mal_listing=2" 
"--dbpath=/export/scratch2/zhang/monet-install/default/debug/var/MonetDB/mTests_monetdb5_modules_atoms"
 "strtrim.mal"
+# 16:41:06 >  
+
+# MonetDB 5 server v11.18.0
+# This is an unreleased version
+# Serving database 'mTests_monetdb5_modules_atoms', using 8 threads
+# Compiled for x86_64-unknown-linux-gnu/64bit with 64bit OIDs dynamically 
linked
+# Found 15.591 GiB available main-memory.
+# Copyright (c) 1993-July 2008 CWI.
+# Copyright (c) August 2008-2014 MonetDB B.V., all rights reserved
+# Visit http://www.monetdb.org/ for further information
+# Listening for connection requests on mapi:monetdb://riga.ins.cwi.nl:35634/
+# Listening for UNIX domain connection requests on 
mapi:monetdb:///var/tmp/mtest-5410/.s.monetdb.35634
+# MonetDB/GIS module loaded
+# MonetDB/JAQL module loaded
+# MonetDB/SQL module loaded
+function user.main():void;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to