]> 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>
Wed, 23 Sep 2015 07:06:15 +0000 (09:06 +0200)
fixes #10197

doc/21-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 be7faa159b14fc13184f526fc589a281fe220506..d8caed59bf92135eb32b55ab2d5d3c7cacf5dbfb 100644 (file)
@@ -498,6 +498,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
@@ -582,7 +590,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-shallow-clone"></a> Dictionary#shallow_clone
 
index e80042aad4365bbb14b088de3a917bb265509615..8d8c84b7babc8daf8d592127a86c3488e0e3b0c9 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), 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;
index 399d442b86d0a77af53a68defdcbcd1424bf909b..b069dc78bb657155b0a651257eb57fbfed7093e3 100644 (file)
@@ -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;
+}
index 0838189552d7c78f8baff6413be7badb1d947c80..8aff1b041d7bbaf8b65a72057cc3c110c4a8b258 100644 (file)
@@ -112,6 +112,8 @@ public:
        
        virtual Object::Ptr Clone(void) const override;
 
+       Array::Ptr Reverse(void) const;
+
 private:
        std::vector<Value> m_Data; /**< The data for the array. */
 };
index ed5a18ba56e439afd6e44182fdf9a972184d5885..114f11aa34d2327395b84ec8188b804b6e0e3f75 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), 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;
index 786e7b28b4cd49992e702edcef83b0fd330eea88..2de4b5a444a310b86c5dcbcfc8072d67dba3b7c0 100644 (file)
@@ -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);