30 const size_t DEFAULT_STRING_SIZE = 128;
32 template<
int S=DEFAULT_STRING_SIZE>
34 static const size_t STRING_SIZE = S;
37 friend std::ostream& operator<<(std::ostream& os,
const ZString<U>& s);
61 friend bool operator==(
const ZString<U>& s1,
const char* s2);
63 friend bool operator!=(
const ZString<U>& s1,
const char* s2);
65 friend bool operator<=(
const ZString<U>& s1,
const char* s2);
67 friend bool operator<(
const ZString<U>& s1,
const char* s2);
69 friend bool operator>=(
const ZString<U>& s1,
const char* s2);
71 friend bool operator>(
const ZString<U>& s1,
const char* s2);
74 ZString(
const char* s) { strncpy_priv(v, s, S-1); }
75 ZString(
const char* s,
const char* e) {
76 memcpy(v, s, std::min(e-s,
static_cast<ssize_t
>(S-1)));
77 v[std::min(e-s,
static_cast<ssize_t
>(S-1))] = 0;
79 ZString(
const std::string& s) { strncpy_priv(v, s.c_str(), S-1); }
81 strncpy_priv(v, s.v, S-1);
84 auto len = std::min(n,
static_cast<size_t>(S-1));
89 ZString& operator=(
const ZString& s) { strncpy_priv(v, s.v, S-1);
return *
this; }
90 operator std::string()
const {
return v; }
92 inline char operator[](
unsigned pos)
const {
93 if (pos < size())
return v[pos];
94 else throw std::range_error(
"zstring out of boundary subset");
96 size_t find (
char c,
size_t pos = 0)
const {
97 while (pos < size() && pos < S && v[pos] != c) {
102 const char* c_str()
const {
return v; }
104 size_t length()
const {
return strlen(v); }
105 size_t size()
const {
return strlen(v); }
110 static char* strncpy_priv(
char* d,
const char*s,
size_t n) {
111 return static_cast<char*
>(memcpy(d, s, std::min(strlen(s) + 1, n)));
117 std::ostream& operator<<(std::ostream& os,
const ZString<U>& s) {
123 ZString<U> operator+(
const ZString<U>& s1,
const ZString<U>& s2) {
125 ZString<U>::strncpy_priv(&s.v[s.length()], s2.v, U - s.length() - 1);
129 ZString<U> operator+(
const ZString<U>& s1,
const char* s2) {
130 return s1 + ZString<U>(s2);
133 ZString<U> operator+(
const char* s1,
const ZString<U>& s2) {
134 return ZString<U>(s1) + s2;
137 ZString<U> operator+(
const ZString<U>& s1,
const std::string& s2) {
138 return s1 + ZString<U>(s2);
141 ZString<U> operator+(
const std::string& s1,
const ZString<U>& s2) {
142 return ZString<U>(s1) + s2;
146 bool operator==(
const ZString<U>& s1,
const ZString<U>& s2) {
147 return strncmp(s1.v, s2.v, U) == 0;
150 bool operator!=(
const ZString<U>& s1,
const ZString<U>& s2) {
151 return strncmp(s1.v, s2.v, U) != 0;
154 bool operator<=(
const ZString<U>& s1,
const ZString<U>& s2) {
155 return strncmp(s1.v, s2.v, U) <= 0;
158 bool operator<(
const ZString<U>& s1,
const ZString<U>& s2) {
159 return strncmp(s1.v, s2.v, U) < 0;
162 bool operator>=(
const ZString<U>& s1,
const ZString<U>& s2) {
163 return strncmp(s1.v, s2.v, U) >= 0;
166 bool operator>(
const ZString<U>& s1,
const ZString<U>& s2) {
167 return strncmp(s1.v, s2.v, U) > 0;
171 bool operator==(
const ZString<U>& s1,
const char* s2) {
172 return strncmp(s1.v, s2, U) == 0;
175 bool operator!=(
const ZString<U>& s1,
const char* s2) {
176 return strncmp(s1.v, s2, U) != 0;
179 bool operator<=(
const ZString<U>& s1,
const char* s2) {
180 return strncmp(s1.v, s2, U) <= 0;
183 bool operator<(
const ZString<U>& s1,
const char* s2) {
184 return strncmp(s1.v, s2, U) < 0;
187 bool operator>=(
const ZString<U>& s1,
const char* s2) {
188 return strncmp(s1.v, s2, U) >= 0;
191 bool operator>(
const ZString<U>& s1,
const char* s2) {
192 return strncmp(s1.v, s2, U) > 0;
195 using zstring = ZString<>;