forked from MWisBest/AmdMsrTweaker
-
Notifications
You must be signed in to change notification settings - Fork 3
/
StringUtils.h
70 lines (59 loc) · 2.01 KB
/
StringUtils.h
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
69
70
/*
* Copyright (c) Martin Kinkelin
*
* See the "License.txt" file in the root directory for infos
* about permitted and prohibited uses of this code.
*/
#pragma once
#include <sstream>
#include <vector>
/// <summary>
/// Some helper functions when working with strings.
/// </summary>
class StringUtils
{
public:
/// <summary>Returns the string representation (according to std::stringstream) of a variable.</summary>
template <typename T> static std::string ToString(const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
/// <summary>Returns the hexadecimal string representation (according to std::stringstream) of a variable.</summary>
template <typename T> static std::string ToHexString(const T& t)
{
std::stringstream ss;
ss << std::hex << t;
return ss.str();
}
/// <summary>
/// Splits a string into tokens separated by one or more delimiter characters.
/// Empty tokens may be skipped.
/// </summary>
static void Tokenize(std::vector<std::string>& result, const char* str, const char* delimiters, bool skipEmpty = false)
{
// check for NULL and empty strings
if (str == NULL || delimiters == NULL || str[0] == 0 || delimiters[0] == 0)
return;
// create a copy of the string as it will be modified during tokenization
// (every delimiter will be replaced by a NULL character)
const size_t stringSize = strlen(str) + 1; // include the terminating NULL character
char* copy = new char[stringSize];
memcpy(copy, str, stringSize);
char* remainder;
char* token = strtok_s(copy, delimiters, &remainder);
while (token != NULL)
{
// skip empty tokens?
if (!skipEmpty || token[0] != 0)
result.push_back(std::string(token));
token = strtok_s(NULL, delimiters, &remainder);
}
delete[] copy;
}
static void Tokenize(std::vector<std::string>& result, const std::string& str, const char* delimiters, bool skipEmpty = false)
{
return Tokenize(result, str.c_str(), delimiters, skipEmpty);
}
};