forked from bruderstein/PythonScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstString.h
More file actions
97 lines (88 loc) · 2.87 KB
/
ConstString.h
File metadata and controls
97 lines (88 loc) · 2.87 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once
/**
* @author Francois-R.Boyer@PolyMtl.ca
* @date March, 2013
* @copyright GPL; see license.txt in Notepad++ source for complete license text.
* @file
*/
#include <string>
/// Simple string class with length and pointer to const array of some type, without allocation.
template <class CharT>
class ConstString {
public:
typedef unsigned int size_type;
ConstString() : _length(0), _str(0) { }
ConstString(const CharT* str, size_type length) : _length(length), _str(str) { }
ConstString(const ConstString<CharT>& source) {
*this = source;
}
ConstString(const std::basic_string<CharT>& source) {
*this = source;
}
template <class string_type> // string_type can by a ConstString or std::basic_string; anything that has length() and c_str() of same type.
ConstString<CharT>& operator=(const string_type& source) {
_length = source.length();
_str = source.c_str();
return *this;
}
size_type length() const {
return _length;
}
const CharT* c_str() const {
return _str;
}
const CharT& operator[](int i) const {
return _str[i];
}
std::basic_string<CharT> toString() const {
return std::basic_string<CharT>(_str, _length);
}
private:
size_type _length;
const CharT* _str;
};
/// Generic comparison of strings of different character sizes.
/// @note Characters are not encoded/decoded, the comparison is value by value. Thus it can compare latin-1, UCS-2, and UTF-32 strings, but not UTF-8, UTF-16 and UTF-32.
template <class CharT1, class CharT2>
bool operator==(const ConstString<CharT1>& a, const ConstString<CharT2>& b)
{
unsigned int a_length = a.length();
if (a_length != b.length())
return false;
for (unsigned int i = 0; i < a_length; i++)
if (a[i] != b[i])
return false;
return true;
}
template <class CharT1, class CharT2>
bool operator!=(const ConstString<CharT1>& a, const ConstString<CharT2>& b)
{
return !(a == b);
}
/// Specialized comparison when both strings have same character type.
template <class CharT>
bool operator==(const ConstString<CharT>& a, const ConstString<CharT>& b)
{
return a.length() == b.length() && memcmp(a.c_str(), b.c_str(), a.length() * sizeof(CharT)) == 0;
}
// Comparison with other string types:
template <class CharT1, class CharT2>
bool operator==(const ConstString<CharT1>& a, const std::basic_string<CharT2>& b)
{
return a == ConstString<CharT2>(b.c_str(), b.length());
}
template <class CharT1, class CharT2>
bool operator==(const ConstString<CharT1>& a, const CharT2* b)
{
return a == ConstString<CharT2>(b, std::char_traits<CharT2>::length(b));
}
template <class CharT1, class CharT2>
bool operator!=(const ConstString<CharT1>& a, const std::basic_string<CharT2>& b)
{
return !(a == b);
}
template <class CharT1, class CharT2>
bool operator!=(const ConstString<CharT1>& a, const CharT2* b)
{
return !(a == b);
}