ztsdb
string.hpp
1 // (C) 2016 Leonardo Silvestri
2 //
3 // This file is part of ztsdb.
4 //
5 // ztsdb is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // ztsdb is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with ztsdb. If not, see <http://www.gnu.org/licenses/>.
17 
18 
19 #ifndef ZSTRING_HPP
20 #define ZSTRING_HPP
21 
22 
23 #include <string>
24 #include <cstring>
25 #include <iostream>
26 #include <stdexcept>
27 
28 namespace arr {
29 
30  const size_t DEFAULT_STRING_SIZE = 128;
31 
32  template<int S=DEFAULT_STRING_SIZE> // why int LLL
33  struct ZString {
34  static const size_t STRING_SIZE = S;
35 
36  template<int U>
37  friend std::ostream& operator<<(std::ostream& os, const ZString<U>& s);
38  template<int U>
39  friend ZString<U> operator+(const ZString<U>& s1, const ZString<U>& s2);
40  template<int U>
41  friend ZString<U> operator+(const ZString<U>& s1, const char* s2);
42  template<int U>
43  friend ZString<U> operator+(const char* s1, const ZString<U>& s2);
44  template<int U>
45  friend ZString<U> operator+(const ZString<U>& s1, const std::string& s2);
46  template<int U>
47  friend ZString<U> operator+(const std::string& s1, const ZString<U>& s2);
48  template<int U>
49  friend bool operator==(const ZString<U>& s1, const ZString<U>& s2);
50  template<int U>
51  friend bool operator!=(const ZString<U>& s1, const ZString<U>& s2);
52  template<int U>
53  friend bool operator<=(const ZString<U>& s1, const ZString<U>& s2);
54  template<int U>
55  friend bool operator<(const ZString<U>& s1, const ZString<U>& s2);
56  template<int U>
57  friend bool operator>=(const ZString<U>& s1, const ZString<U>& s2);
58  template<int U>
59  friend bool operator>(const ZString<U>& s1, const ZString<U>& s2);
60  template<int U>
61  friend bool operator==(const ZString<U>& s1, const char* s2);
62  template<int U>
63  friend bool operator!=(const ZString<U>& s1, const char* s2);
64  template<int U>
65  friend bool operator<=(const ZString<U>& s1, const char* s2);
66  template<int U>
67  friend bool operator<(const ZString<U>& s1, const char* s2);
68  template<int U>
69  friend bool operator>=(const ZString<U>& s1, const char* s2);
70  template<int U>
71  friend bool operator>(const ZString<U>& s1, const char* s2);
72 
73  ZString() { v[0] = 0; }
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;
78  }
79  ZString(const std::string& s) { strncpy_priv(v, s.c_str(), S-1); }
80  ZString(const ZString& s) {
81  strncpy_priv(v, s.v, S-1);
82  }
83  ZString(char c, size_t n) {
84  auto len = std::min(n, static_cast<size_t>(S-1));
85  memset(v, c, len);
86  v[len] = '\0';
87  }
88 
89  ZString& operator=(const ZString& s) { strncpy_priv(v, s.v, S-1); return *this; }
90  operator std::string() const { return v; }
91 
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");
95  }
96  size_t find (char c, size_t pos = 0) const {
97  while (pos < size() && pos < S && v[pos] != c) {
98  ++pos;
99  }
100  return pos;
101  }
102  const char* c_str() const { return v; }
103 
104  size_t length() const { return strlen(v); }
105  size_t size() const { return strlen(v); }
106 
107  private:
108  char v[S];
109 
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)));
112  }
113 
114  };
115 
116  template<int U>
117  std::ostream& operator<<(std::ostream& os, const ZString<U>& s) {
118  os << s.v;
119  return os;
120  }
121 
122  template<int U>
123  ZString<U> operator+(const ZString<U>& s1, const ZString<U>& s2) {
124  ZString<U> s(s1);
125  ZString<U>::strncpy_priv(&s.v[s.length()], s2.v, U - s.length() - 1);
126  return s;
127  }
128  template<int U>
129  ZString<U> operator+(const ZString<U>& s1, const char* s2) {
130  return s1 + ZString<U>(s2);
131  }
132  template<int U>
133  ZString<U> operator+(const char* s1, const ZString<U>& s2) {
134  return ZString<U>(s1) + s2;
135  }
136  template<int U>
137  ZString<U> operator+(const ZString<U>& s1, const std::string& s2) {
138  return s1 + ZString<U>(s2);
139  }
140  template<int U>
141  ZString<U> operator+(const std::string& s1, const ZString<U>& s2) {
142  return ZString<U>(s1) + s2;
143  }
144 
145  template<int U>
146  bool operator==(const ZString<U>& s1, const ZString<U>& s2) {
147  return strncmp(s1.v, s2.v, U) == 0;
148  }
149  template<int U>
150  bool operator!=(const ZString<U>& s1, const ZString<U>& s2) {
151  return strncmp(s1.v, s2.v, U) != 0;
152  }
153  template<int U>
154  bool operator<=(const ZString<U>& s1, const ZString<U>& s2) {
155  return strncmp(s1.v, s2.v, U) <= 0;
156  }
157  template<int U>
158  bool operator<(const ZString<U>& s1, const ZString<U>& s2) {
159  return strncmp(s1.v, s2.v, U) < 0;
160  }
161  template<int U>
162  bool operator>=(const ZString<U>& s1, const ZString<U>& s2) {
163  return strncmp(s1.v, s2.v, U) >= 0;
164  }
165  template<int U>
166  bool operator>(const ZString<U>& s1, const ZString<U>& s2) {
167  return strncmp(s1.v, s2.v, U) > 0;
168  }
169 
170  template<int U>
171  bool operator==(const ZString<U>& s1, const char* s2) {
172  return strncmp(s1.v, s2, U) == 0;
173  }
174  template<int U>
175  bool operator!=(const ZString<U>& s1, const char* s2) {
176  return strncmp(s1.v, s2, U) != 0;
177  }
178  template<int U>
179  bool operator<=(const ZString<U>& s1, const char* s2) {
180  return strncmp(s1.v, s2, U) <= 0;
181  }
182  template<int U>
183  bool operator<(const ZString<U>& s1, const char* s2) {
184  return strncmp(s1.v, s2, U) < 0;
185  }
186  template<int U>
187  bool operator>=(const ZString<U>& s1, const char* s2) {
188  return strncmp(s1.v, s2, U) >= 0;
189  }
190  template<int U>
191  bool operator>(const ZString<U>& s1, const char* s2) {
192  return strncmp(s1.v, s2, U) > 0;
193  }
194 
195  using zstring = ZString<>;
196 
197 
198 }
199 
200 
201 #endif
arr::ZString
Definition: string.hpp:33
arr
Contains the classes and functions that implement a multidimentional array type.
Definition: allocator.hpp:29