1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/object.hpp"
4 #include "base/dictionary.hpp"
5 #include "base/function.hpp"
6 #include "base/functionwrapper.hpp"
7 #include "base/scriptframe.hpp"
8 #include "base/exception.hpp"
9 #include <boost/algorithm/string.hpp>
11 using namespace icinga;
13 static int StringLen()
15 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
16 String self = vframe->Self;
17 return self.GetLength();
20 static String StringToString()
22 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
26 static String StringSubstr(const std::vector<Value>& args)
28 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
29 String self = vframe->Self;
32 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments"));
34 if (static_cast<double>(args[0]) < 0 || static_cast<double>(args[0]) >= self.GetLength())
35 BOOST_THROW_EXCEPTION(std::invalid_argument("String index is out of range"));
38 return self.SubStr(args[0], args[1]);
40 return self.SubStr(args[0]);
43 static String StringUpper()
45 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
46 String self = vframe->Self;
47 return boost::to_upper_copy(self);
50 static String StringLower()
52 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
53 String self = vframe->Self;
54 return boost::to_lower_copy(self);
57 static Array::Ptr StringSplit(const String& delims)
59 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
60 String self = vframe->Self;
61 std::vector<String> tokens = self.Split(delims.CStr());
63 return Array::FromVector(tokens);
66 static int StringFind(const std::vector<Value>& args)
68 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
69 String self = vframe->Self;
72 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments"));
74 String::SizeType result;
76 if (args.size() > 1) {
77 if (static_cast<double>(args[1]) < 0)
78 BOOST_THROW_EXCEPTION(std::invalid_argument("String index is out of range"));
80 result = self.Find(args[0], args[1]);
82 result = self.Find(args[0]);
84 if (result == String::NPos)
90 static bool StringContains(const String& str)
92 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
93 String self = vframe->Self;
94 return self.Contains(str);
97 static Value StringReplace(const String& search, const String& replacement)
99 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
100 String self = vframe->Self;
102 boost::algorithm::replace_all(self, search, replacement);
106 static String StringReverse()
108 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
109 String self = vframe->Self;
110 return self.Reverse();
113 static String StringTrim()
115 ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
116 String self = vframe->Self;
120 Object::Ptr String::GetPrototype()
122 static Dictionary::Ptr prototype = new Dictionary({
123 { "len", new Function("String#len", StringLen, {}, true) },
124 { "to_string", new Function("String#to_string", StringToString, {}, true) },
125 { "substr", new Function("String#substr", StringSubstr, { "start", "len" }, true) },
126 { "upper", new Function("String#upper", StringUpper, {}, true) },
127 { "lower", new Function("String#lower", StringLower, {}, true) },
128 { "split", new Function("String#split", StringSplit, { "delims" }, true) },
129 { "find", new Function("String#find", StringFind, { "str", "start" }, true) },
130 { "contains", new Function("String#contains", StringContains, { "str" }, true) },
131 { "replace", new Function("String#replace", StringReplace, { "search", "replacement" }, true) },
132 { "reverse", new Function("String#reverse", StringReverse, {}, true) },
133 { "trim", new Function("String#trim", StringTrim, {}, true) }