Skip to content

Commit

Permalink
Array: Move Join into the base class, available for programmers
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Friedrich committed Jul 12, 2019
1 parent d4393b6 commit 38b7f10
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
17 changes: 1 addition & 16 deletions lib/base/array-script.cpp
Expand Up @@ -107,22 +107,7 @@ static Value ArrayJoin(const Value& separator)
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
Array::Ptr self = static_cast<Array::Ptr>(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()
Expand Down
20 changes: 20 additions & 0 deletions lib/base/array.cpp
Expand Up @@ -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<Value> result;
Expand Down
1 change: 1 addition & 0 deletions lib/base/array.hpp
Expand Up @@ -94,6 +94,7 @@ class Array final : public Object
void Sort(bool overrideFrozen = false);

String ToString() const override;
Value Join(const Value& separator) const;

Array::Ptr Unique() const;
void Freeze();
Expand Down

0 comments on commit 38b7f10

Please sign in to comment.