]> granicus.if.org Git - icinga2/commitdiff
Implement the Array#join method
authorGunnar Beutner <gunnar@beutner.name>
Mon, 2 Feb 2015 07:37:55 +0000 (08:37 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Mon, 2 Feb 2015 07:39:16 +0000 (08:39 +0100)
fixes #8322

doc/16-library-reference.md
lib/base/array-script.cpp

index 45dd327444b665e4c6c4d2f4690efce22297b3d1..f14829f05de157350fb56522b5d397fc255a5d28 100644 (file)
@@ -417,6 +417,14 @@ Returns a copy of the array where all items are sorted. The items are
 compared using the `<` (less-than) operator. A custom comparator function
 can be specified with the `less_cmp` argument.
 
+### <a id="array-join"></a> Array#join
+
+Signature:
+
+    function join(separator);
+
+Joins all elements of the array using the specified separator.
+
 ## <a id="dictionary-type"></a> Dictionary type
 
 ### <a id="dictionary-clone"></a> Dictionary#clone
index 265899c0e6277ab2f6c101f42f6f2ed2e5bf7782..6118d11b41b0b9aef8419d7a08fdd0c8203cfaa0 100644 (file)
@@ -22,6 +22,7 @@
 #include "base/functionwrapper.hpp"
 #include "base/scriptframe.hpp"
 #include "base/objectlock.hpp"
+#include <boost/foreach.hpp>
 
 using namespace icinga;
 
@@ -100,6 +101,28 @@ static Array::Ptr ArrayClone(void)
        return self->ShallowClone();
 }
 
+static Value ArrayJoin(const Value& separator)
+{
+       ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
+       Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
+
+       Value result;
+       bool first = true;
+
+       ObjectLock olock(self);
+       BOOST_FOREACH(const Value& item, self) {
+               if (first) {
+                       first = false;
+               } else {
+                       result = result + separator;
+               }
+
+               result = result + item;
+       }
+
+       return result;
+}
+
 Object::Ptr Array::GetPrototype(void)
 {
        static Dictionary::Ptr prototype;
@@ -114,6 +137,7 @@ Object::Ptr Array::GetPrototype(void)
                prototype->Set("clear", new Function(WrapFunction(ArrayClear)));
                prototype->Set("sort", new Function(WrapFunction(ArraySort)));
                prototype->Set("clone", new Function(WrapFunction(ArrayClone)));
+               prototype->Set("join", new Function(WrapFunction(ArrayJoin)));
        }
 
        return prototype;