-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathstr.h
More file actions
68 lines (56 loc) · 1.55 KB
/
str.h
File metadata and controls
68 lines (56 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cctype>
#include <locale>
void ltrim(std::string &s);
void rtrim(std::string &s);
void trim(std::string &s);
bool is_number(const std::string& s);
std::vector<std::string> split(const std::string &str, const std::string &delim);
std::string join(const std::vector<std::string> &elements, const std::string &separator);
template<const unsigned num, const char separator>
static void separate(std::string & input)
{
for (auto it = input.begin(); (num + 1) <= std::distance(it, input.end()); ++it)
{
std::advance(it, num);
it = input.insert(it, separator);
}
}
static unsigned char hex_char_to_byte(char Input)
{
return
((Input >= 'a') && (Input <= 'f'))
? (Input - 87)
: ((Input >= 'A') && (Input <= 'F'))
? (Input - 55)
: ((Input >= '0') && (Input <= '9'))
? (Input - 48)
: 0;//throw std::exception{};
}
/* Position the characters into the appropriate nibble */
static unsigned char transform_hex_to_byte(char High, char Low)
{
return (hex_char_to_byte(High) << 4) | (hex_char_to_byte(Low));
}
template <typename InputIterator>
static std::string from_hex(InputIterator first, InputIterator last)
{
std::ostringstream oss;
while (first != last)
{
char highValue = *first++;
if (highValue == ' ')
continue;
if (first == last)
break;
char lowValue = *first++;
//char ch = (hex_to_byte::high(highValue) | hex_to_byte::low(lowValue));
unsigned char ch = transform_hex_to_byte(highValue, lowValue);
oss << ch;
}
return oss.str();
}