diff --git a/doc/20-library-reference.md b/doc/20-library-reference.md index b262c715963..409013cea2e 100644 --- a/doc/20-library-reference.md +++ b/doc/20-library-reference.md @@ -499,6 +499,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 @@ -583,7 +591,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#clone diff --git a/lib/base/array-script.cpp b/lib/base/array-script.cpp index 625c476f123..d3346dbbb75 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))); prototype->Set("clone", new Function(WrapFunction(ArrayClone))); prototype->Set("join", new Function(WrapFunction(ArrayJoin))); + prototype->Set("reverse", new Function(WrapFunction(ArrayReverse))); } return prototype; diff --git a/lib/base/array.cpp b/lib/base/array.cpp index a09610855f3..da9f4b8ac85 100644 --- a/lib/base/array.cpp +++ b/lib/base/array.cpp @@ -210,3 +210,14 @@ Array::Ptr Array::ShallowClone(void) const return clone; } +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 34db5847731..106978c84e0 100644 --- a/lib/base/array.hpp +++ b/lib/base/array.hpp @@ -69,6 +69,8 @@ class I2_BASE_API Array : public Object static Object::Ptr GetPrototype(void); + 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 c76ef90dc1e..f3498b73be5 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))); prototype->Set("contains", new Function(WrapFunction(StringContains))); prototype->Set("replace", new Function(WrapFunction(StringReplace))); + prototype->Set("reverse", new Function(WrapFunction(StringReverse))); } return prototype; diff --git a/lib/base/string.hpp b/lib/base/string.hpp index 74c93f18825..d0945be32f2 100644 --- a/lib/base/string.hpp +++ b/lib/base/string.hpp @@ -209,6 +209,13 @@ class I2_BASE_API String m_Data.append(count, ch); } + 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);