From 38b7f10e0e1f9dcf8448c4b6fe2edb1577729071 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Fri, 12 Jul 2019 14:36:55 +0200 Subject: [PATCH] Array: Move Join into the base class, available for programmers --- lib/base/array-script.cpp | 17 +---------------- lib/base/array.cpp | 20 ++++++++++++++++++++ lib/base/array.hpp | 1 + 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/base/array-script.cpp b/lib/base/array-script.cpp index fa7a52dcb..1c00f1106 100644 --- a/lib/base/array-script.cpp +++ b/lib/base/array-script.cpp @@ -107,22 +107,7 @@ static Value ArrayJoin(const Value& separator) ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); Array::Ptr self = static_cast(vframe->Self); REQUIRE_NOT_NULL(self); - - Value result; - bool first = true; - - ObjectLock olock(self); - for (const Value& item : self) { - if (first) { - first = false; - } else { - result = result + separator; - } - - result = result + item; - } - - return result; + return self->Join(separator); } static Array::Ptr ArrayReverse() diff --git a/lib/base/array.cpp b/lib/base/array.cpp index d9f360c77..08e06fad2 100644 --- a/lib/base/array.cpp +++ b/lib/base/array.cpp @@ -297,6 +297,26 @@ String Array::ToString() const return msgbuf.str(); } +Value Array::Join(const Value& separator) const +{ + Value result; + bool first = true; + + ObjectLock olock(this); + + for (const Value& item : m_Data) { + if (first) { + first = false; + } else { + result = result + separator; + } + + result = result + item; + } + + return result; +} + Array::Ptr Array::Unique() const { std::set result; diff --git a/lib/base/array.hpp b/lib/base/array.hpp index eaecfdf24..2c9a9dda7 100644 --- a/lib/base/array.hpp +++ b/lib/base/array.hpp @@ -94,6 +94,7 @@ public: void Sort(bool overrideFrozen = false); String ToString() const override; + Value Join(const Value& separator) const; Array::Ptr Unique() const; void Freeze();