2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-12-12 15:19:23 +01:00
|
|
|
|
|
|
|
#include "base/object.hpp"
|
|
|
|
#include "base/dictionary.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.hpp"
|
|
|
|
#include "base/functionwrapper.hpp"
|
2014-12-12 15:33:02 +01:00
|
|
|
#include "base/scriptframe.hpp"
|
2015-01-14 09:08:31 +01:00
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2014-12-12 15:19:23 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static int StringLen()
|
2014-12-12 15:19:23 +01:00
|
|
|
{
|
2014-12-12 15:33:02 +01:00
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
2014-12-12 15:19:23 +01:00
|
|
|
String self = vframe->Self;
|
|
|
|
return self.GetLength();
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static String StringToString()
|
2014-12-12 15:19:23 +01:00
|
|
|
{
|
2014-12-12 15:33:02 +01:00
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
2014-12-12 15:19:23 +01:00
|
|
|
return vframe->Self;
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:08:31 +01:00
|
|
|
static String StringSubstr(const std::vector<Value>& args)
|
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
String self = vframe->Self;
|
|
|
|
|
|
|
|
if (args.empty())
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments"));
|
|
|
|
|
2015-01-14 17:02:10 +01:00
|
|
|
if (static_cast<double>(args[0]) < 0 || static_cast<double>(args[0]) >= self.GetLength())
|
2015-01-14 09:08:31 +01:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static String StringUpper()
|
2015-01-14 09:08:31 +01:00
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
String self = vframe->Self;
|
|
|
|
return boost::to_upper_copy(self);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static String StringLower()
|
2015-01-14 09:08:31 +01:00
|
|
|
{
|
|
|
|
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;
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> tokens = self.Split(delims.CStr());
|
2015-01-14 09:08:31 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return Array::FromVector(tokens);
|
2015-01-14 09:08:31 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 07:54:41 +02:00
|
|
|
static int StringFind(const std::vector<Value>& args)
|
2015-01-14 09:08:31 +01:00
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
String self = vframe->Self;
|
|
|
|
|
|
|
|
if (args.empty())
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments"));
|
|
|
|
|
|
|
|
String::SizeType result;
|
|
|
|
|
2015-01-14 17:02:10 +01:00
|
|
|
if (args.size() > 1) {
|
|
|
|
if (static_cast<double>(args[1]) < 0)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("String index is out of range"));
|
|
|
|
|
2015-01-14 09:08:31 +01:00
|
|
|
result = self.Find(args[0], args[1]);
|
2015-01-14 17:02:10 +01:00
|
|
|
} else
|
2015-01-14 09:08:31 +01:00
|
|
|
result = self.Find(args[0]);
|
|
|
|
|
|
|
|
if (result == String::NPos)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-03-11 06:42:45 +01:00
|
|
|
static bool StringContains(const String& str)
|
2015-03-11 00:11:18 +01:00
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
String self = vframe->Self;
|
2015-03-11 06:42:45 +01:00
|
|
|
return self.Contains(str);
|
2015-03-11 00:11:18 +01:00
|
|
|
}
|
|
|
|
|
2015-01-14 09:08:31 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static String StringReverse()
|
2015-09-23 09:06:15 +02:00
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
String self = vframe->Self;
|
|
|
|
return self.Reverse();
|
|
|
|
}
|
2015-01-14 09:08:31 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static String StringTrim()
|
2016-01-26 15:59:29 +01:00
|
|
|
{
|
|
|
|
ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
|
|
|
|
String self = vframe->Self;
|
|
|
|
return self.Trim();
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Object::Ptr String::GetPrototype()
|
2014-12-12 15:19:23 +01:00
|
|
|
{
|
2018-01-11 11:17:38 +01:00
|
|
|
static Dictionary::Ptr prototype = new Dictionary({
|
|
|
|
{ "len", new Function("String#len", StringLen, {}, true) },
|
|
|
|
{ "to_string", new Function("String#to_string", StringToString, {}, true) },
|
|
|
|
{ "substr", new Function("String#substr", StringSubstr, { "start", "len" }, true) },
|
|
|
|
{ "upper", new Function("String#upper", StringUpper, {}, true) },
|
|
|
|
{ "lower", new Function("String#lower", StringLower, {}, true) },
|
|
|
|
{ "split", new Function("String#split", StringSplit, { "delims" }, true) },
|
|
|
|
{ "find", new Function("String#find", StringFind, { "str", "start" }, true) },
|
|
|
|
{ "contains", new Function("String#contains", StringContains, { "str" }, true) },
|
|
|
|
{ "replace", new Function("String#replace", StringReplace, { "search", "replacement" }, true) },
|
|
|
|
{ "reverse", new Function("String#reverse", StringReverse, {}, true) },
|
|
|
|
{ "trim", new Function("String#trim", StringTrim, {}, true) }
|
|
|
|
});
|
2014-12-12 15:19:23 +01:00
|
|
|
|
|
|
|
return prototype;
|
|
|
|
}
|
|
|
|
|