[vala] glib-2.0: fix string.joinv for empty (but not null) arrays



commit 0d8ab7be120106388b4c9777ad4715069a575228
Author: Evan Nemerson <evan coeus-group com>
Date:   Tue Jan 28 16:06:50 2014 -0800

    glib-2.0: fix string.joinv for empty (but not null) arrays
    
    Fixes bug 723195.

 tests/Makefile.am            |    1 +
 tests/methods/bug723195.vala |   25 +++++++++++++++++++++++++
 vapi/glib-2.0.vapi           |   12 ++++++++----
 3 files changed, 34 insertions(+), 4 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9c47ec5..77b2fa0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -57,6 +57,7 @@ TESTS = \
        methods/bug653908.vala \
        methods/bug663210.vala \
        methods/bug723009.vala \
+       methods/bug723195.vala \
        methods/generics.vala \
        control-flow/break.vala \
        control-flow/expressions-conditional.vala \
diff --git a/tests/methods/bug723195.vala b/tests/methods/bug723195.vala
new file mode 100644
index 0000000..1ea5990
--- /dev/null
+++ b/tests/methods/bug723195.vala
@@ -0,0 +1,25 @@
+private static int main (string[] args) {
+       string[] a = { "foo", "bar", null, "baz" };
+
+       a.length = 0;
+       assert (string.joinv (":", a) == "");
+
+       a.length = 1;
+       assert (string.joinv (":", a) == "foo");
+
+       a.length = 2;
+       assert (string.joinv (":", a) == "foo:bar");
+
+       a.length = 3;
+       assert (string.joinv (":", a) == "foo:bar:");
+
+       a.length = 4;
+       assert (string.joinv (":", a) == "foo:bar::baz");
+
+       a.length = -1;
+       assert (string.joinv (":", a) == "foo:bar");
+
+       assert (string.joinv (":", null) == "");
+
+       return 0;
+}
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index 7f867b5..434078b 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -964,19 +964,23 @@ public class string {
                if (separator == null) {
                        separator = "";
                }
-               if (str_array != null) {
+               if (str_array != null || str_array.length > 0 || (str_array.length == -1 && str_array[0] != 
null)) {
                        int i;
                        size_t len = 1;
                        for (i = 0 ; (str_array.length != -1 && i < str_array.length) || (str_array.length == 
-1 && str_array[i] != null) ; i++) {
-                               len += str_array[i].length;
+                               len += (str_array[i] != null) ? str_array[i].length : 0;
                        }
+                       if (i == 0) {
+                               return "";
+                       }
+                       str_array.length = i;
                        len += separator.length * (i - 1);
 
                        string* res = GLib.malloc (len);
                        void* ptr = string.copy_to_buffer ((void*) res, str_array[0]);
-                       for (i = 1 ; str_array[i] != null ; i++) {
+                       for (i = 1 ; i < str_array.length ; i++) {
                                ptr = string.copy_to_buffer (ptr, separator);
-                               ptr = string.copy_to_buffer (ptr, str_array[i]);
+                               ptr = string.copy_to_buffer (ptr, str_array[i] ?? "");
                        }
 
                        return (owned) res;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]