-
Notifications
You must be signed in to change notification settings - Fork 0
/
fstring.h
executable file
·144 lines (134 loc) · 2.59 KB
/
fstring.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* fstring.h
*
* Created on: 17 Apr 2014
* Author: lester
*/
#ifndef FSTRING_H_
#define FSTRING_H_
#include <string>
#include <string.h>
template<unsigned N>
class stackstring
{
char c[N];
unsigned size = 0;
public:
bool set(unsigned v)
{
if (N < 20)
return false;
return true;
}
unsigned len() const
{
return this->size;
}
char* begin()
{
return c;
}
};
/*
* Fast string holder and comparator
* invariant object use to handle string in temporal memory base on start end position
*
*/
class fstring
{
const char* const start; // string first character
const unsigned len;
public:
fstring(const fstring &str) :
start(str.start), len(str.len)
{
}
// end is one character pass the end of string
fstring(const char* start, const char* end) :
start(start), len(end - start)
{
}
// end is one character pass the end of string
fstring(const char* start) :
start(start), len(strlen(start))
{
}
fstring(const char* start, unsigned len) :
start(start), len(len)
{
}
std::string getString() const
{
return
{ start,static_cast<size_t>(len)};
}
~fstring()
{
}
// Comparation operators
bool operator ==(const char* str) const
{
return str && (strncmp(str, start, len) == 0) && str[len] == 0;
}
bool operator ==(const fstring & str) const
{
return len == str.len && strncmp(str.start, start, len) == 0;
}
bool operator !=(const char* str) const
{
return !(operator ==(str));
}
bool operator !=(const fstring & str) const
{
return !(operator ==(str));
}
operator bool() const
{
return len != 0;
}
unsigned size() const
{
return len;
}
// convert string to unsigned
bool get(unsigned &v)
{
return false;
}
/*
* Be carefully using this function,
* if fstring does not come from a zero-end string do not use it
*/
const char* c_str() const
{
return start;
}
const char* begin() const
{
return start;
}
const char* end() const
{
return start + len;
}
const int alphabetical_compare(const & fstring str,bool casesensitive = true)
{
int s = str.len > len ? len : str.len;
int r = casesensitive ? strncmp(start,str.start,s) : strncmp(start,str.start,s);
if (r == 0 && len != str.len)
{
if (len > str.len) r =1;
else r = -1;
}
return r;
}
// TODO . case insentive compare
bool operator < (const & fstring str) const
{
return alphabetical_compare(str) < 0;
}
// TODO to be implemented, and use as comparation function
static bool compare_case_sensitive(const &fstring str1,const &fstring str2);
static bool compare_case_unsensitive(const &fstring str1,const &fstring str2);
};
#endif /* FSTRING_H_ */