Implement the Array#join method

fixes #8322
This commit is contained in:
Gunnar Beutner 2015-02-02 08:37:55 +01:00
parent 173f5241c4
commit fec8e15d9e
2 changed files with 32 additions and 0 deletions

View 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

View 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;