Cppcheck
mathlib.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 //---------------------------------------------------------------------------
20 #ifndef mathlibH
21 #define mathlibH
22 //---------------------------------------------------------------------------
23 
24 #include "config.h"
25 
26 #include <string>
27 
28 /// @addtogroup Core
29 /// @{
30 
31 /** @brief simple math functions that uses operands stored in std::string. useful when performing math on tokens. */
32 
34  friend class TestMathLib;
35 
36 public:
37  /** @brief value class */
38  class value {
39  private:
40  long long mIntValue{};
41  double mDoubleValue{};
42  enum class Type { INT, LONG, LONGLONG, FLOAT } mType;
43  bool mIsUnsigned{};
44 
45  void promote(const value &v);
46 
47  public:
48  explicit value(const std::string &s);
49  std::string str() const;
50  bool isInt() const {
51  return mType != Type::FLOAT;
52  }
53  bool isFloat() const {
54  return mType == Type::FLOAT;
55  }
56 
57  double getDoubleValue() const {
58  return isFloat() ? mDoubleValue : (double)mIntValue;
59  }
60 
61  static value calc(char op, const value &v1, const value &v2);
62  int compare(const value &v) const;
63  value add(int v) const;
64  value shiftLeft(const value &v) const;
65  value shiftRight(const value &v) const;
66  };
67 
68  using bigint = long long;
69  using biguint = unsigned long long;
70  static const int bigint_bits;
71 
72  /** @brief for conversion of numeric literals - for atoi-like conversions please use strToInt() */
73  static bigint toBigNumber(const std::string & str);
74  /** @brief for conversion of numeric literals - for atoi-like conversions please use strToInt() */
75  static biguint toBigUNumber(const std::string & str);
76 
77  template<class T> static std::string toString(T value) = delete;
78  /** @brief for conversion of numeric literals */
79  static double toDoubleNumber(const std::string & str);
80 
81  static bool isInt(const std::string & str);
82  static bool isFloat(const std::string &str);
83  static bool isDecimalFloat(const std::string &str);
84  static bool isNegative(const std::string &str);
85  static bool isPositive(const std::string &str);
86  static bool isDec(const std::string & str);
87  static bool isFloatHex(const std::string& str);
88  static bool isIntHex(const std::string& str);
89  static bool isOct(const std::string& str);
90  static bool isBin(const std::string& str);
91 
92  static std::string getSuffix(const std::string& value);
93  /**
94  * Only used in unit tests
95  *
96  * \param[in] str string
97  * \param[in] supportMicrosoftExtensions support Microsoft extension: i64
98  * \return true if str is a non-empty valid integer suffix
99  */
100  static bool isValidIntegerSuffix(const std::string& str, bool supportMicrosoftExtensions=true);
101 
102  static std::string add(const std::string & first, const std::string & second);
103  static std::string subtract(const std::string & first, const std::string & second);
104  static std::string multiply(const std::string & first, const std::string & second);
105  static std::string divide(const std::string & first, const std::string & second);
106  static std::string mod(const std::string & first, const std::string & second);
107  static std::string calculate(const std::string & first, const std::string & second, char action);
108 
109  static std::string sin(const std::string & tok);
110  static std::string cos(const std::string & tok);
111  static std::string tan(const std::string & tok);
112  static std::string abs(const std::string & tok);
113  static bool isEqual(const std::string & first, const std::string & second);
114  static bool isNotEqual(const std::string & first, const std::string & second);
115  static bool isGreater(const std::string & first, const std::string & second);
116  static bool isGreaterEqual(const std::string & first, const std::string & second);
117  static bool isLess(const std::string & first, const std::string & second);
118  static bool isLessEqual(const std::string & first, const std::string & second);
119  static bool isNullValue(const std::string & str);
120  /**
121  * Return true if given character is 0,1,2,3,4,5,6 or 7.
122  * @param[in] c The character to check
123  * @return true if given character is octal digit.
124  */
125  static bool isOctalDigit(char c);
126 
127  static unsigned int encodeMultiChar(const std::string& str);
128 
129  /**
130  * \param[in] iCode Code being considered
131  * \param[in] iPos A posision within iCode
132  * \return Whether iCode[iPos] is a C++14 digit separator
133  */
134  static bool isDigitSeparator(const std::string& iCode, std::string::size_type iPos);
135 };
136 
147 
148 template<> CPPCHECKLIB std::string MathLib::toString<double>(double value);
149 
150 /// @}
151 //---------------------------------------------------------------------------
152 #endif // mathlibH
bool isEqual(T x, T y)
Definition: calculate.h:28
R calculate(const std::string &s, const T &x, const T &y, bool *error=nullptr)
Definition: calculate.h:50
static bool isNegative(const Token *tok, const Settings &settings)
value class
Definition: mathlib.h:38
bool isInt() const
Definition: mathlib.h:50
bool isFloat() const
Definition: mathlib.h:53
double getDoubleValue() const
Definition: mathlib.h:57
simple math functions that uses operands stored in std::string.
Definition: mathlib.h:33
static std::string toString(T value)=delete
long long bigint
Definition: mathlib.h:68
static const int bigint_bits
Definition: mathlib.h:70
unsigned long long biguint
Definition: mathlib.h:69
Information about a class type.
#define CPPCHECKLIB
Definition: config.h:35
MathLib::value operator&(const MathLib::value &v1, const MathLib::value &v2)
Definition: mathlib.cpp:1340
MathLib::value operator%(const MathLib::value &v1, const MathLib::value &v2)
Definition: mathlib.cpp:1335
MathLib::value operator-(const MathLib::value &v1, const MathLib::value &v2)
Definition: mathlib.cpp:1320
MathLib::value operator|(const MathLib::value &v1, const MathLib::value &v2)
Definition: mathlib.cpp:1345
MathLib::value operator<<(const MathLib::value &v1, const MathLib::value &v2)
Definition: mathlib.cpp:1355
MathLib::value operator>>(const MathLib::value &v1, const MathLib::value &v2)
Definition: mathlib.cpp:1360
MathLib::value operator*(const MathLib::value &v1, const MathLib::value &v2)
Definition: mathlib.cpp:1325
MathLib::value operator/(const MathLib::value &v1, const MathLib::value &v2)
Definition: mathlib.cpp:1330
MathLib::value operator^(const MathLib::value &v1, const MathLib::value &v2)
Definition: mathlib.cpp:1350
MathLib::value operator+(const MathLib::value &v1, const MathLib::value &v2)
Definition: mathlib.cpp:1315
static bool isNotEqual(std::pair< const Token *, const Token * > x, std::pair< const Token *, const Token * > y)
Definition: valueflow.cpp:3845