]> granicus.if.org Git - icinga2/commitdiff
Implement String#contains
authorMichael Friedrich <michael.friedrich@gmail.com>
Tue, 10 Mar 2015 23:11:18 +0000 (00:11 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Wed, 11 Mar 2015 05:44:23 +0000 (06:44 +0100)
fixes #8659

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

index f6e8e19172196ae91026e2baace9adf6d019f282..53727d05b5d5a596d3bd4d98da37028c8ade70f4 100644 (file)
@@ -399,6 +399,20 @@ Example:
 
     "Hello World".find("World") /* Returns 6 */
 
+### <a id="string-contains"></a> String#contains
+
+Signature:
+
+    function contains(str);
+
+Returns `true` if the string `str` was found in the string. If the string
+was not found `false` is returned. Use [find](20-library-reference.md#string-find)
+for getting the index instead.
+
+Example:
+
+    "Hello World".contains("World") /* Returns true */
+
 ### <a id="string-len"></a> String#len
 
 Signature
index 72e1ded0498692dd761dce0a71dc2b3c560e8526..d9c8f0b69ae95076e7750a0e66010b50e578a9fc 100644 (file)
@@ -110,6 +110,13 @@ static Value StringFind(const std::vector<Value>& args)
                return result;
 }
 
+static bool StringContains(const Value& value)
+{
+       ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
+       String self = vframe->Self;
+       return self.Contains(value);
+}
+
 static Value StringReplace(const String& search, const String& replacement)
 {
        ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
@@ -133,6 +140,7 @@ Object::Ptr String::GetPrototype(void)
                prototype->Set("lower", new Function(WrapFunction(StringLower)));
                prototype->Set("split", new Function(WrapFunction(StringSplit)));
                prototype->Set("find", new Function(WrapFunction(StringFind)));
+               prototype->Set("contains", new Function(WrapFunction(StringContains)));
                prototype->Set("replace", new Function(WrapFunction(StringReplace)));
        }