From eeb01831c01dc1b6af028817f4adac602f9acf78 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 23 Sep 2015 09:06:15 +0200 Subject: [PATCH] Implement the Array#reverse and String#reverse methods fixes #10197 --- doc/21-library-reference.md | 16 +++++++++++++++- lib/base/array-script.cpp | 8 ++++++++ lib/base/array.cpp | 11 +++++++++++ lib/base/array.hpp | 2 ++ lib/base/string-script.cpp | 7 +++++++ lib/base/string.hpp | 7 +++++++ 6 files changed, 50 insertions(+), 1 deletion(-) diff --git a/doc/21-library-reference.md b/doc/21-library-reference.md index be7faa159..d8caed59b 100644 --- a/doc/21-library-reference.md +++ b/doc/21-library-reference.md @@ -498,6 +498,14 @@ Signature: Returns a copy of the string. +### String#reverse + +Signature: + + function reverse(); + +Returns a copy of the string in reverse order. + ## Array type ### Array#add @@ -582,7 +590,13 @@ Signature: Joins all elements of the array using the specified separator. -## Dictionary type +### Array#reverse + +Signature: + + function reverse(); + +Returns a new array with all elements of the current array in reverse order. ### Dictionary#shallow_clone diff --git a/lib/base/array-script.cpp b/lib/base/array-script.cpp index e80042aad..8d8c84b7b 100644 --- a/lib/base/array-script.cpp +++ b/lib/base/array-script.cpp @@ -130,6 +130,13 @@ static Value ArrayJoin(const Value& separator) return result; } +static Array::Ptr ArrayReverse(void) +{ + ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); + Array::Ptr self = static_cast(vframe->Self); + return self->Reverse(); +} + Object::Ptr Array::GetPrototype(void) { static Dictionary::Ptr prototype; @@ -146,6 +153,7 @@ Object::Ptr Array::GetPrototype(void) prototype->Set("sort", new Function(WrapFunction(ArraySort), true)); prototype->Set("shallow_clone", new Function(WrapFunction(ArrayShallowClone), true)); prototype->Set("join", new Function(WrapFunction(ArrayJoin), true)); + prototype->Set("reverse", new Function(WrapFunction(ArrayReverse), true)); } return prototype; diff --git a/lib/base/array.cpp b/lib/base/array.cpp index 399d442b8..b069dc78b 100644 --- a/lib/base/array.cpp +++ b/lib/base/array.cpp @@ -200,3 +200,14 @@ Object::Ptr Array::Clone(void) const return arr; } +Array::Ptr Array::Reverse(void) const +{ + Array::Ptr result = new Array(); + + ObjectLock olock(this); + ObjectLock xlock(result); + + std::copy(m_Data.rbegin(), m_Data.rend(), std::back_inserter(result->m_Data)); + + return result; +} diff --git a/lib/base/array.hpp b/lib/base/array.hpp index 083818955..8aff1b041 100644 --- a/lib/base/array.hpp +++ b/lib/base/array.hpp @@ -112,6 +112,8 @@ public: virtual Object::Ptr Clone(void) const override; + Array::Ptr Reverse(void) const; + private: std::vector m_Data; /**< The data for the array. */ }; diff --git a/lib/base/string-script.cpp b/lib/base/string-script.cpp index ed5a18ba5..114f11aa3 100644 --- a/lib/base/string-script.cpp +++ b/lib/base/string-script.cpp @@ -126,6 +126,12 @@ static Value StringReplace(const String& search, const String& replacement) return self; } +static String StringReverse(void) +{ + ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); + String self = vframe->Self; + return self.Reverse(); +} Object::Ptr String::GetPrototype(void) { @@ -142,6 +148,7 @@ Object::Ptr String::GetPrototype(void) prototype->Set("find", new Function(WrapFunction(StringFind), true)); prototype->Set("contains", new Function(WrapFunction(StringContains), true)); prototype->Set("replace", new Function(WrapFunction(StringReplace), true)); + prototype->Set("reverse", new Function(WrapFunction(StringReverse), true)); } return prototype; diff --git a/lib/base/string.hpp b/lib/base/string.hpp index 786e7b28b..2de4b5a44 100644 --- a/lib/base/string.hpp +++ b/lib/base/string.hpp @@ -234,6 +234,13 @@ public: return t; } + inline String Reverse(void) const + { + String t = m_Data; + std::reverse(t.m_Data.begin(), t.m_Data.end()); + return t; + } + inline bool Contains(const String& str) const { return (m_Data.find(str) != std::string::npos);