-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringHelpers.h
70 lines (65 loc) · 1.5 KB
/
StringHelpers.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
#ifndef __STRINGHELPERS_H__
#define __STRINGHELPERS_H__
#include <string>
#include <cctype>
//----------------------------------------------------------------------------------------
template<class ElementType, class traits, class Alloc>
bool StringStartsWith(const std::basic_string<ElementType, traits, Alloc>& targetString, const std::basic_string<ElementType, traits, Alloc>& searchString, bool caseInsensitive = false)
{
if (targetString.size() < searchString.size())
{
return false;
}
if (caseInsensitive)
{
for (size_t i = 0; i < searchString.size(); ++i)
{
if (std::toupper(targetString[i]) != std::toupper(searchString[i]))
{
return false;
}
}
}
else
{
for (size_t i = 0; i < searchString.size(); ++i)
{
if (targetString[i] != searchString[i])
{
return false;
}
}
}
return true;
}
//----------------------------------------------------------------------------------------
template<class ElementType, class traits, class Alloc>
bool StringEquals(const std::basic_string<ElementType, traits, Alloc>& value1, const std::basic_string<ElementType, traits, Alloc>& value2, bool caseInsensitive = false)
{
if (value1.size() != value2.size())
{
return false;
}
if (caseInsensitive)
{
for (size_t i = 0; i < value1.size(); ++i)
{
if (std::toupper(value1[i]) != std::toupper(value2[i]))
{
return false;
}
}
}
else
{
for (size_t i = 0; i < value1.size(); ++i)
{
if (value1[i] != value2[i])
{
return false;
}
}
}
return true;
}
#endif