From: Michael Friedrich Date: Tue, 10 Mar 2015 23:11:18 +0000 (+0100) Subject: Implement String#contains X-Git-Tag: v2.3.1~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=059cda9e37f8d9a2dc15274776e925724977f6de;p=icinga2 Implement String#contains fixes #8659 --- diff --git a/doc/20-library-reference.md b/doc/20-library-reference.md index f6e8e1917..53727d05b 100644 --- a/doc/20-library-reference.md +++ b/doc/20-library-reference.md @@ -399,6 +399,20 @@ Example: "Hello World".find("World") /* Returns 6 */ +### 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 */ + ### String#len Signature diff --git a/lib/base/string-script.cpp b/lib/base/string-script.cpp index 72e1ded04..d9c8f0b69 100644 --- a/lib/base/string-script.cpp +++ b/lib/base/string-script.cpp @@ -110,6 +110,13 @@ static Value StringFind(const std::vector& 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))); }