TDME2  1.9.200
Integer.cpp
Go to the documentation of this file.
2 
3 #include <string.h>
4 
5 #include <algorithm>
6 #include <cctype>
7 #include <charconv>
8 #include <string>
9 #include <string_view>
10 
11 #include <tdme/tdme.h>
13 
15 
16 using std::find_if;
17 using std::from_chars;
18 using std::isdigit;
19 using std::stoi;
20 using std::string;
21 using std::string_view;
22 using std::to_string;
23 
25 
26 bool Integer::is(const string& str) {
27  auto trimmedStr = StringTools::trim(str);
28  return
29  trimmedStr.empty() == false &&
30  find_if(
31  trimmedStr.begin() + (trimmedStr[0] == '-'?1:0),
32  trimmedStr.end(),
33  [](unsigned char c) {
34  return isdigit(c) == 0;
35  }
36  ) == trimmedStr.end();
37 }
38 
39 bool Integer::viewIs(const string_view& str) {
40  auto trimmedStr = StringTools::viewTrim(str);
41  return
42  trimmedStr.empty() == false &&
43  find_if(
44  trimmedStr.begin() + (trimmedStr[0] == '-'?1:0),
45  trimmedStr.end(),
46  [](unsigned char c) {
47  return isdigit(c) == 0;
48  }
49  ) == trimmedStr.end();
50 }
51 
52 int Integer::parse(const string& str) {
53  auto trimmedStr = StringTools::trim(str);
54  if (trimmedStr.empty() == true) return 0;
55  if (trimmedStr == "-") return -0;
56  return stoi(trimmedStr);
57 }
58 
59 int Integer::viewParse(const string_view& str) {
60  auto trimmedStr = StringTools::viewTrim(str);
61  if (trimmedStr.empty() == true) return 0;
62  if (trimmedStr == "-") return -0;
63  int result;
64  from_chars(&trimmedStr[0], &trimmedStr[trimmedStr.size()], result);
65  return result;
66 }
67 
68 void Integer::encode(const uint32_t decodedInt, string& encodedString) {
69  encodedString = "";
70  char encodingCharSet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW-+/*.";
71  for (auto i = 0; i < 6; i++) {
72  auto charIdx = (decodedInt >> (i * 6)) & 63;
73  encodedString = encodingCharSet[charIdx] + encodedString;
74  }
75 }
76 
77 bool Integer::decode(const string& encodedString, uint32_t& decodedInt) {
78  char encodingCharSet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW-+/*.";
79  decodedInt = 0;
80  for (auto i = 0; i < encodedString.length(); i++) {
81  auto codeIdx = -1;
82  char c = encodedString[encodedString.length() - i - 1];
83  char* codePtr = strchr(encodingCharSet, c);
84  if (codePtr == NULL) {
85  return false;
86  } else {
87  codeIdx = codePtr - encodingCharSet;
88  }
89  decodedInt+= codeIdx << (i * 6);
90  }
91  return true;
92 }
93 
94 bool Integer::viewDecode(const string_view& encodedString, uint32_t& decodedInt) {
95  char encodingCharSet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW-+/*.";
96  decodedInt = 0;
97  for (auto i = 0; i < encodedString.length(); i++) {
98  auto codeIdx = -1;
99  char c = encodedString[encodedString.length() - i - 1];
100  char* codePtr = strchr(encodingCharSet, c);
101  if (codePtr == NULL) {
102  return false;
103  } else {
104  codeIdx = codePtr - encodingCharSet;
105  }
106  decodedInt+= codeIdx << (i * 6);
107  }
108  return true;
109 }
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 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
String tools class.
Definition: StringTools.h:22
static const string trim(const string &src)
Trim string.
Definition: StringTools.cpp:51
static const string_view viewTrim(const string_view &src)
Trim string.
Definition: StringTools.cpp:76