[icinga-checkins] icinga.org: icinga2/feature/array-match: Implement Array#match

git at icinga.org git at icinga.org
Mon Dec 5 15:00:14 CET 2016


Module: icinga2
Branch: feature/array-match
Commit: 0091f7bf673efc65d322083197bbc571977718b9
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=0091f7bf673efc65d322083197bbc571977718b9

Author: Michael Friedrich <michael.friedrich at icinga.com>
Date:   Mon Dec  5 14:56:08 2016 +0100

Implement Array#match

---

 doc/18-library-reference.md |    8 ++++++++
 lib/base/array-script.cpp   |   18 ++++++++++++++++++
 lib/base/array.cpp          |    2 +-
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/doc/18-library-reference.md b/doc/18-library-reference.md
index 8020283..499ff3b 100644
--- a/doc/18-library-reference.md
+++ b/doc/18-library-reference.md
@@ -1156,6 +1156,14 @@ Signature:
 Returns a copy of the array with all duplicate elements removed. The original order
 of the array is not preserved.
 
+### <a id="array-match"></a> Array#match
+
+Signature:
+
+    function match(pattern);
+
+Returns true if any string element matches the given string pattern, false otherwise
+
 ## <a id="dictionary-type"></a> Dictionary type
 
 Inherits methods from the [Object type](18-library-reference.md#object-type).
diff --git a/lib/base/array-script.cpp b/lib/base/array-script.cpp
index 6eeaf8e..89b12bf 100644
--- a/lib/base/array-script.cpp
+++ b/lib/base/array-script.cpp
@@ -222,6 +222,23 @@ static Array::Ptr ArrayUnique(void)
 	return Array::FromSet(result);
 }
 
+static bool ArrayMatch(const Value& value)
+{
+	ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
+	Array::Ptr self = static_cast<Array::Ptr>(vframe->Self);
+
+	ObjectLock olock(self);
+	for (const Value& item : self) {
+		if (!item.IsString())
+			continue;
+
+		if (Utility::Match(value, item))
+			return true;
+	}
+
+	return false;
+}
+
 Object::Ptr Array::GetPrototype(void)
 {
 	static Dictionary::Ptr prototype;
@@ -243,6 +260,7 @@ Object::Ptr Array::GetPrototype(void)
 		prototype->Set("reduce", new Function("Array#reduce", WrapFunction(ArrayReduce), true));
 		prototype->Set("filter", new Function("Array#filter", WrapFunction(ArrayFilter), true));
 		prototype->Set("unique", new Function("Array#unique", WrapFunction(ArrayUnique), true));
+		prototype->Set("match", new Function("Array#match", WrapFunction(ArrayMatch), true));
 	}
 
 	return prototype;
diff --git a/lib/base/array.cpp b/lib/base/array.cpp
index 916b497..90b503a 100644
--- a/lib/base/array.cpp
+++ b/lib/base/array.cpp
@@ -31,7 +31,7 @@ using namespace icinga;
 REGISTER_PRIMITIVE_TYPE(Array, Object, Array::GetPrototype());
 
 /**
- * Restrieves a value from an array.
+ * Retrieves a value from an array.
  *
  * @param index The index.
  * @returns The value.



More information about the icinga-checkins mailing list