forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_string.h
114 lines (98 loc) · 2.57 KB
/
c_string.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <[email protected]>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCMS_C_STRING_H
#define CPPCMS_C_STRING_H
#include <string.h>
#include <string>
namespace cppcms {
namespace xss {
namespace details {
class c_string {
public:
typedef char const *const_iterator;
char const *begin() const
{
return begin_;
}
char const *end() const
{
return end_;
}
c_string(char const *s)
{
begin_=s;
end_=s+strlen(s);
}
c_string(char const *b,char const *e) : begin_(b), end_(e) {}
c_string() : begin_(0),end_(0) {}
bool compare(c_string const &other) const
{
return std::lexicographical_compare(begin_,end_,other.begin_,other.end_,std::char_traits<char>::lt);
}
bool icompare(c_string const &other) const
{
return std::lexicographical_compare(begin_,end_,other.begin_,other.end_,ilt);
}
explicit c_string(std::string const &other)
{
container_ = other;
begin_ = container_.c_str();
end_ = begin_ + container_.size();
}
c_string(c_string const &other)
{
if(other.begin_ == other.end_) {
begin_ = end_ = 0;
}
else if(other.container_.empty()) {
begin_ = other.begin_;
end_ = other.end_;
}
else {
container_ = other.container_;
begin_ = container_.c_str();
end_ = begin_ + container_.size();
}
}
c_string const &operator=(c_string const &other)
{
if(other.begin_ == other.end_) {
begin_ = end_ = 0;
}
else if(other.container_.empty()) {
begin_ = other.begin_;
end_ = other.end_;
}
else {
container_ = other.container_;
begin_ = container_.c_str();
end_ = begin_ + container_.size();
}
return *this;
}
private:
static bool ilt(char left,char right)
{
unsigned char l = tolower(left);
unsigned char r = tolower(right);
return l < r;
}
static char tolower(char c)
{
if('A' <= c && c<='Z')
return c-'A' + 'a';
return c;
}
char const *begin_;
char const *end_;
std::string container_;
};
} // details
} // xss
} // cppcms
#endif