]> granicus.if.org Git - icinga2/commitdiff
Implement the Array#reverse and String#reverse methods
authorGunnar Beutner <gunnar@beutner.name>
Wed, 23 Sep 2015 07:06:15 +0000 (09:06 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Tue, 13 Oct 2015 10:08:42 +0000 (12:08 +0200)
fixes #10197

doc/20-library-reference.md
lib/base/array-script.cpp
lib/base/array.cpp
lib/base/array.hpp
lib/base/string-script.cpp
lib/base/string.hpp

index b262c715963fce9a5ccb56b20a0957e7a4c4ce96..409013cea2e0afdd2997b0e59895a4a938d250e9 100644 (file)
@@ -499,6 +499,14 @@ Signature:
 
 Returns a copy of the string.
 
+### <a id="string-reverse"></a> String#reverse
+
+Signature:
+
+    function reverse();
+
+Returns a copy of the string in reverse order.
+
 ## <a id="array-type"></a> Array type
 
 ### <a id="array-add"></a> Array#add
@@ -583,7 +591,13 @@ Signature:
 
 Joins all elements of the array using the specified separator.
 
-## <a id="dictionary-type"></a> Dictionary type
+### <a id="array-reverse"></a> Array#reverse
+
+Signature:
+
+    function reverse();
+
+Returns a new array with all elements of the current array in reverse order.
 
 ### <a id="dictionary-clone"></a> Dictionary#clone
 
index 625c476f1234f9bf1cda3eb803a55b3025880b7d..d3346dbbb7534ead8cb5c3f7bcb382b6d3d1b550 100644 (file)
@@ -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<Array::Ptr>(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;
index a09610855f3ae0c2e9f3b4514786c227003f423c..da9f4b8ac85d78a5221614680ef043a92b41243c 100644 (file)
@@ -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;
+}
index 34db5847731e51d79f59932b4bd520b6d69efe55..106978c84e0932c301a244b43004f2fd15fffdab 100644 (file)
@@ -69,6 +69,8 @@ public:
 
        static Object::Ptr GetPrototype(void);
 
+       Array::Ptr Reverse(void) const;
+
 private:
        std::vector<Value> m_Data; /**< The data for the array. */
 };
index c76ef90dc1ee8470b6234ece05a56aa96bde050a..f3498b73be5c2d1319071c8d5481fd602eb2dff5 100644 (file)
@@ -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;
index 74c93f188254457d8beffb8d1f834f73266d72f2..d0945be32f202ab50e0f00122e979f6b73deb6fa 100644 (file)
@@ -209,6 +209,13 @@ public:
                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);