xref: /trafficserver/plugins/esi/lib/Expression.h (revision be5d6920)
1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #pragma once
25 
26 #include <string>
27 #include <cstdlib>
28 
29 #include "ComponentBase.h"
30 #include "Variables.h"
31 
32 namespace EsiLib
33 {
34 class Expression : private ComponentBase
35 {
36 public:
37   Expression(const char *debug_tag, ComponentBase::Debug debug_func, ComponentBase::Error error_func, Variables &variables);
38 
39   /** substitutes variables (if any) in given expression */
40   const std::string &expand(const char *expr, int expr_len = -1);
41 
42   /** convenient alternative for method above */
43   const std::string &
expand(const std::string & expr)44   expand(const std::string &expr)
45   {
46     return expand(expr.data(), expr.size());
47   }
48 
49   /** evaluates boolean value of given expression */
50   bool evaluate(const char *expr, int expr_len = -1);
51 
52   /** convenient alternative for method above */
53   bool
evaluate(const std::string & expr)54   evaluate(const std::string &expr)
55   {
56     return evaluate(expr.data(), expr.size());
57   }
58 
~Expression()59   ~Expression() override{};
60 
61 private:
62   static const std::string EMPTY_STRING;
63   static const std::string TRUE_STRING;
64 
65   Variables &_variables;
66   std::string _value;
67 
68   // these are arranged in parse priority format indices correspond to op strings array
69   enum Operator {
70     OP_EQ,
71     OP_NEQ,
72     OP_LTEQ,
73     OP_GTEQ,
74     OP_LT,
75     OP_GT,
76     OP_NOT,
77     OP_OR,
78     OP_AND,
79     N_OPERATORS,
80   };
81 
82   struct OperatorString {
83     const char *str;
84     int str_len;
OperatorStringEsiLib::Expression::OperatorString85     OperatorString(const char *s = nullptr, int s_len = -1) : str(s), str_len(s_len){};
86   };
87 
88   static const OperatorString OPERATOR_STRINGS[N_OPERATORS];
89 
90   inline void _trimWhiteSpace(const char *&expr, int &expr_len) const;
91 
92   inline bool _stripQuotes(const char *&expr, int &expr_len) const;
93 
94   inline int _findOperator(const char *expr, int expr_len, Operator &op) const;
95 
96   inline bool
_isBinaryOperator(Operator & op) const97   _isBinaryOperator(Operator &op) const
98   {
99     return ((op == OP_EQ) || (op == OP_NEQ) || (op == OP_LT) || (op == OP_GT) || (op == OP_LTEQ) || (op == OP_GTEQ) ||
100             (op == OP_OR) || (op == OP_AND));
101   }
102 
103   inline bool
_convert(const std::string & str,double & value)104   _convert(const std::string &str, double &value)
105   {
106     size_t str_size = str.size();
107     if (str_size) {
108       char *endp;
109       const char *str_ptr = str.c_str();
110       // Solaris is messed up, in that strtod() does not honor C99/SUSv3 mode.
111       value = strtold(str_ptr, &endp);
112       return (static_cast<unsigned int>(endp - str_ptr) == str_size);
113     }
114     return false;
115   }
116 
117   inline bool _evalSimpleExpr(const char *expr, int expr_len);
118 };
119 }; // namespace EsiLib
120