|
25 | 25 |
|
26 | 26 | namespace casbin { |
27 | 27 |
|
28 | | -FunctionMap :: FunctionMap(){ |
| 28 | +FunctionMap::FunctionMap(){ |
29 | 29 | scope = NULL; |
30 | 30 | } |
31 | 31 |
|
32 | | -void FunctionMap :: ProcessFunctions(std::string expression){ |
33 | | - for(auto func: func_list){ |
34 | | - int index = int(expression.find(func+"(")); |
| 32 | +void FunctionMap::ProcessFunctions(const std::string& expression){ |
| 33 | + for(const std::string& func: func_list) { |
| 34 | + size_t index = expression.find(func+"("); |
35 | 35 |
|
36 | 36 | if (index != std::string::npos) { |
37 | | - int close_index = int(expression.find(")", index)); |
38 | | - int start = index + int((func+"(").length()); |
| 37 | + size_t close_index = expression.find(")", index); |
| 38 | + size_t start = index + func.length() + 1; |
39 | 39 |
|
40 | 40 | std::string function_params = expression.substr(start, close_index - start); |
41 | 41 | FetchIdentifier(this->scope, func); |
42 | 42 | std::vector<std::string> params = Split(function_params, ","); |
43 | 43 |
|
44 | | - for(int i=0;i<params.size();i++){ |
45 | | - int quote_index = int(params[i].find("\"")); |
| 44 | + for(std::string& param : params) { |
| 45 | + size_t quote_index = param.find("\""); |
| 46 | + |
46 | 47 | if (quote_index == std::string::npos) |
47 | | - Get(this->scope, Trim(params[i])); |
48 | | - else{ |
49 | | - params[i] = params[i].replace(quote_index, 1, "'"); |
50 | | - int second_quote_index = int(params[i].find("\"", quote_index+1)); |
51 | | - params[i] = params[i].replace(second_quote_index, 1, "'"); |
52 | | - Get(this->scope, Trim(params[i])); |
| 48 | + Get(this->scope, Trim(param)); |
| 49 | + |
| 50 | + else { |
| 51 | + param = param.replace(quote_index, 1, "'"); |
| 52 | + size_t second_quote_index = param.find("\"", quote_index + 1); |
| 53 | + param = param.replace(second_quote_index, 1, "'"); |
| 54 | + Get(this->scope, Trim(param)); |
53 | 55 | } |
54 | 56 | } |
55 | 57 | } |
56 | 58 | } |
57 | 59 | } |
58 | 60 |
|
59 | | -int FunctionMap :: GetRLen(){ |
| 61 | +int FunctionMap::GetRLen(){ |
60 | 62 | bool found = FetchIdentifier(scope, "rlen"); |
61 | 63 | if(found) |
62 | 64 | return GetInt(scope); |
63 | 65 | return -1; |
64 | 66 | } |
65 | 67 |
|
66 | | -bool FunctionMap :: Evaluate(std::string expression){ |
| 68 | +bool FunctionMap::Evaluate(const std::string& expression){ |
67 | 69 | ProcessFunctions(expression); |
68 | 70 | return Eval(scope, expression); |
69 | 71 | } |
70 | 72 |
|
71 | | -bool FunctionMap :: GetBooleanResult(){ |
72 | | - return bool(duk_get_boolean(scope, -1)); |
| 73 | +bool FunctionMap::GetBooleanResult() { |
| 74 | + return static_cast<bool>(duk_get_boolean(scope, -1)); |
73 | 75 | } |
74 | 76 |
|
75 | 77 | // AddFunction adds an expression function. |
76 | | -void FunctionMap :: AddFunction(std::string func_name, Function f, Index nargs) { |
| 78 | +void FunctionMap::AddFunction(const std::string& func_name, Function f, Index nargs) { |
77 | 79 | func_list.push_back(func_name); |
78 | 80 | PushFunction(scope, f, func_name, nargs); |
79 | 81 | } |
80 | 82 |
|
81 | | -void FunctionMap :: AddFunctionPropToR(std::string identifier, Function func, Index nargs){ |
| 83 | +void FunctionMap::AddFunctionPropToR(const std::string& identifier, Function func, Index nargs){ |
82 | 84 | PushFunctionPropToObject(scope, "r", func, identifier, nargs); |
83 | 85 | } |
84 | 86 |
|
85 | | -void FunctionMap :: AddBooleanPropToR(std::string identifier, bool val){ |
| 87 | +void FunctionMap::AddBooleanPropToR(const std::string& identifier, bool val){ |
86 | 88 | PushBooleanPropToObject(scope, "r", val, identifier); |
87 | 89 | } |
88 | 90 |
|
89 | | -void FunctionMap :: AddTruePropToR(std::string identifier){ |
| 91 | +void FunctionMap::AddTruePropToR(const std::string& identifier){ |
90 | 92 | PushTruePropToObject(scope, "r", identifier); |
91 | 93 | } |
92 | 94 |
|
93 | | -void FunctionMap :: AddFalsePropToR(std::string identifier){ |
| 95 | +void FunctionMap::AddFalsePropToR(const std::string& identifier){ |
94 | 96 | PushFalsePropToObject(scope, "r", identifier); |
95 | 97 | } |
96 | 98 |
|
97 | | -void FunctionMap :: AddIntPropToR(std::string identifier, int val){ |
| 99 | +void FunctionMap::AddIntPropToR(const std::string& identifier, int val){ |
98 | 100 | PushIntPropToObject(scope, "r", val, identifier); |
99 | 101 | } |
100 | 102 |
|
101 | | -void FunctionMap :: AddFloatPropToR(std::string identifier, float val){ |
| 103 | +void FunctionMap::AddFloatPropToR(const std::string& identifier, float val){ |
102 | 104 | PushFloatPropToObject(scope, "r", val, identifier); |
103 | 105 | } |
104 | 106 |
|
105 | | -void FunctionMap :: AddDoublePropToR(std::string identifier, double val){ |
| 107 | +void FunctionMap::AddDoublePropToR(const std::string& identifier, double val){ |
106 | 108 | PushDoublePropToObject(scope, "r", val, identifier); |
107 | 109 | } |
108 | 110 |
|
109 | | -void FunctionMap :: AddStringPropToR(std::string identifier, std::string val){ |
| 111 | +void FunctionMap::AddStringPropToR(const std::string& identifier, const std::string& val){ |
110 | 112 | PushStringPropToObject(scope, "r", val, identifier); |
111 | 113 | } |
112 | 114 |
|
113 | | -void FunctionMap :: AddPointerPropToR(std::string identifier, void* val){ |
| 115 | +void FunctionMap::AddPointerPropToR(const std::string& identifier, void* val){ |
114 | 116 | PushPointerPropToObject(scope, "r", val, identifier); |
115 | 117 | } |
116 | 118 |
|
117 | | -void FunctionMap :: AddObjectPropToR(std::string identifier){ |
| 119 | +void FunctionMap::AddObjectPropToR(const std::string& identifier){ |
118 | 120 | PushObjectPropToObject(scope, "r", identifier); |
119 | 121 | } |
120 | 122 |
|
121 | 123 | // LoadFunctionMap loads an initial function map. |
122 | | -void FunctionMap :: LoadFunctionMap() { |
| 124 | +void FunctionMap::LoadFunctionMap() { |
123 | 125 | AddFunction("keyMatch", KeyMatch, 2); |
124 | 126 | AddFunction("keyMatch2", KeyMatch2, 2); |
125 | 127 | AddFunction("keyMatch3", KeyMatch3, 2); |
|
0 commit comments