TDME2  1.9.200
Integer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #if defined(_WIN32) && defined(_MSC_VER)
4  #define NOMINMAX
5  #undef max
6  #undef min
7 #endif
8 
9 #include <tdme/tdme.h>
11 
12 #include <limits>
13 #include <string>
14 #include <string_view>
15 
16 using std::numeric_limits;
17 using std::string;
18 using std::string_view;
19 
20 /**
21  * Integer class
22  * @author Andreas Drewke
23  */
25 {
26 public:
27  static constexpr int MAX_VALUE { numeric_limits<int>::max() };
28  static constexpr int MIN_VALUE { -numeric_limits<int>::max() };
29 
30  /**
31  * Check if given string is a integer string
32  * @param str string
33  * @return given string is integer
34  */
35  static bool is(const string& str);
36 
37  /**
38  * Check if given string is a integer string
39  * @param str string
40  * @return given string is integer
41  */
42  static bool viewIs(const string_view& str);
43 
44  /**
45  * Parse integer
46  * @param str string
47  * @return integer
48  */
49  static int parse(const string& str);
50 
51  /**
52  * Parse integer
53  * @param str string
54  * @return integer
55  */
56  static int viewParse(const string_view& str);
57 
58  /**
59  * @brief Encodes an 32 bit unsigned integer to a 6 char string representation
60  * @param decodedInt int value to encode
61  * @returns encodedString
62  */
63  inline static const string encode(const uint32_t decodedInt) {
64  string encodedString;
65  encode(decodedInt, encodedString);
66  return encodedString;
67  }
68 
69  /**
70  * @brief Decodes an 6 char string representation to a unsigned 32 bit integer
71  * @param encodedString encoded string
72  * @returns decodedString
73  */
74  inline static const uint32_t decode(const string& encodedString) {
75  uint32_t decodedInt;
76  decode(encodedString, decodedInt);
77  return decodedInt;
78  }
79 
80  /**
81  * @brief Decodes an 6 char string representation to a unsigned 32 bit integer
82  * @param encodedString encoded string
83  * @returns decodedString
84  */
85  inline static const uint32_t viewDecode(const string_view& encodedString) {
86  uint32_t decodedInt;
87  viewDecode(encodedString, decodedInt);
88  return decodedInt;
89  }
90 
91  /**
92  * @brief Encodes an 32 bit unsigned integer to a 6 char string representation
93  * @param decodedInt int value to encode
94  * @param encodedString string
95  */
96  static void encode(const uint32_t decodedInt, string& encodedString);
97 
98  /**
99  * @brief Decodes an 6 char string representation to a unsigned 32 bit integer
100  * @param encodedString encoded string
101  * @param decodedInt integer
102  */
103  static bool decode(const string& encodedString, uint32_t& decodedInt);
104 
105  /**
106  * @brief Decodes an 6 char string representation to a unsigned 32 bit integer
107  * @param encodedString encoded string
108  * @param decodedInt integer
109  */
110  static bool viewDecode(const string_view& encodedString, uint32_t& decodedInt);
111 
112 };
Integer class.
Definition: Integer.h:25
static bool viewIs(const string_view &str)
Check if given string is a integer string.
Definition: Integer.cpp:39
static const uint32_t viewDecode(const string_view &encodedString)
Decodes an 6 char string representation to a unsigned 32 bit integer.
Definition: Integer.h:85
static int parse(const string &str)
Parse integer.
Definition: Integer.cpp:52
static constexpr int MIN_VALUE
Definition: Integer.h:28
static const uint32_t decode(const string &encodedString)
Decodes an 6 char string representation to a unsigned 32 bit integer.
Definition: Integer.h:74
static const string encode(const uint32_t decodedInt)
Encodes an 32 bit unsigned integer to a 6 char string representation.
Definition: Integer.h:63
static int viewParse(const string_view &str)
Parse integer.
Definition: Integer.cpp:59
static constexpr int MAX_VALUE
Definition: Integer.h:27
static bool is(const string &str)
Check if given string is a integer string.
Definition: Integer.cpp:26