From 9c36f4065615a2ba54b3e523be528d5d5f3716d5 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 14 Jan 2015 09:08:31 +0100 Subject: [PATCH] Implement more methods for the String class fixes #8169 --- lib/base/string-script.cpp | 85 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/lib/base/string-script.cpp b/lib/base/string-script.cpp index 2dfd7ba55..321994300 100644 --- a/lib/base/string-script.cpp +++ b/lib/base/string-script.cpp @@ -22,6 +22,9 @@ #include "base/scriptfunction.hpp" #include "base/scriptfunctionwrapper.hpp" #include "base/scriptframe.hpp" +#include "base/exception.hpp" +#include +#include using namespace icinga; @@ -38,6 +41,82 @@ static String StringToString(void) return vframe->Self; } +static String StringSubstr(const std::vector& args) +{ + ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); + String self = vframe->Self; + + if (args.empty()) + BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments")); + + if (static_cast(args[0]) >= self.GetLength()) + BOOST_THROW_EXCEPTION(std::invalid_argument("String index is out of range")); + + if (args.size() > 1) + return self.SubStr(args[0], args[1]); + else + return self.SubStr(args[0]); +} + +static String StringUpper(void) +{ + ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); + String self = vframe->Self; + return boost::to_upper_copy(self); +} + +static String StringLower(void) +{ + ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); + String self = vframe->Self; + return boost::to_lower_copy(self); +} + +static Array::Ptr StringSplit(const String& delims) +{ + ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); + String self = vframe->Self; + std::vector tokens; + boost::algorithm::split(tokens, self, boost::is_any_of(delims)); + + Array::Ptr result = new Array(); + BOOST_FOREACH(const String& token, tokens) { + result->Add(token); + } + return result; +} + +static Value StringFind(const std::vector& args) +{ + ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); + String self = vframe->Self; + + if (args.empty()) + BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments")); + + String::SizeType result; + + if (args.size() > 1) + result = self.Find(args[0], args[1]); + else + result = self.Find(args[0]); + + if (result == String::NPos) + return -1; + else + return result; +} + +static Value StringReplace(const String& search, const String& replacement) +{ + ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); + String self = vframe->Self; + + boost::algorithm::replace_all(self, search, replacement); + return self; +} + + Object::Ptr String::GetPrototype(void) { static Dictionary::Ptr prototype; @@ -46,6 +125,12 @@ Object::Ptr String::GetPrototype(void) prototype = new Dictionary(); prototype->Set("len", new ScriptFunction(WrapScriptFunction(StringLen))); prototype->Set("to_string", new ScriptFunction(WrapScriptFunction(StringToString))); + prototype->Set("substr", new ScriptFunction(WrapScriptFunction(StringSubstr))); + prototype->Set("upper", new ScriptFunction(WrapScriptFunction(StringUpper))); + prototype->Set("lower", new ScriptFunction(WrapScriptFunction(StringLower))); + prototype->Set("split", new ScriptFunction(WrapScriptFunction(StringSplit))); + prototype->Set("find", new ScriptFunction(WrapScriptFunction(StringFind))); + prototype->Set("replace", new ScriptFunction(WrapScriptFunction(StringReplace))); } return prototype; -- 2.40.0