Line data Source code
1 : // file : xsd/cxx/ro-string.hxx
2 : // copyright : Copyright (c) 2005-2017 Code Synthesis Tools CC
3 : // license : GNU GPL v2 + exceptions; see accompanying LICENSE file
4 :
5 : #ifndef XSD_CXX_RO_STRING_HXX
6 : #define XSD_CXX_RO_STRING_HXX
7 :
8 : #include <string>
9 : #include <cstddef> // std::size_t
10 : #include <ostream>
11 :
12 : namespace xsd
13 : {
14 : namespace cxx
15 : {
16 : // Read-only string class template.
17 : //
18 : template <typename C>
19 : class ro_string
20 : {
21 : public:
22 : typedef std::char_traits<C> traits_type;
23 : typedef std::size_t size_type;
24 :
25 : static const size_type npos = ~(size_type (0));
26 :
27 : public:
28 : ro_string ()
29 : : data_ (0), size_ (0)
30 : {
31 : }
32 :
33 : ro_string (const C* s)
34 : : data_ (s), size_ (traits_type::length (s))
35 : {
36 : }
37 :
38 : ro_string (const C* s, size_type size)
39 : : data_ (s), size_ (size)
40 : {
41 : }
42 :
43 0 : ro_string (const std::basic_string<C>& s)
44 0 : : data_ (s.data ()), size_ (s.size ())
45 : {
46 0 : }
47 :
48 0 : operator std::basic_string<C> () const
49 : {
50 0 : return std::basic_string<C> (data (), size ());
51 : }
52 :
53 : private:
54 : ro_string (const ro_string&);
55 :
56 : ro_string&
57 : operator= (const ro_string&);
58 :
59 : public:
60 : // The returned string is not necessarily terminated with '\0'.
61 : // If size() returns 0, the returned pointer may be 0.
62 : //
63 : const C*
64 0 : data () const
65 : {
66 0 : return data_;
67 : }
68 :
69 : size_type
70 0 : size () const
71 : {
72 0 : return size_;
73 : }
74 :
75 : size_type
76 : length () const
77 : {
78 : return size ();
79 : }
80 :
81 : public:
82 : bool
83 : empty () const
84 : {
85 : return size () == 0;
86 : }
87 :
88 : const C&
89 : operator[] (size_type pos) const
90 : {
91 : return data ()[pos];
92 : }
93 :
94 : public:
95 : void
96 : assign (const C* s)
97 : {
98 : data_ = s;
99 : size_ = traits_type::length (s);
100 : }
101 :
102 : void
103 0 : assign (const C* s, size_type size)
104 : {
105 0 : data_ = s;
106 0 : size_ = size;
107 0 : }
108 :
109 : void
110 : assign (const std::basic_string<C>& s)
111 : {
112 : data_ = s.c_str ();
113 : size_ = s.size ();
114 : }
115 :
116 : public:
117 : int
118 : compare (const ro_string& str) const
119 : {
120 : return compare (str.data (), str.size ());
121 : }
122 :
123 : int
124 : compare (const std::basic_string<C>& str) const
125 : {
126 : return compare (str.c_str (), str.size ());
127 : }
128 :
129 : int
130 : compare (const C* str) const
131 : {
132 : return compare (str, traits_type::length (str));
133 : }
134 :
135 : int
136 : compare (const C* str, size_type n) const
137 : {
138 : size_type s1 (size ());
139 : size_type s (s1 < n ? s1 : n);
140 :
141 : int r (s != 0 ? traits_type::compare (data (), str, s) : 0);
142 :
143 : if (!r && s1 != n)
144 : r = s1 < n ? -1 : 1;
145 :
146 : return r;
147 : }
148 :
149 : public:
150 : size_type
151 : find (C c, size_type pos = 0) const;
152 :
153 : private:
154 : const C* data_;
155 : size_type size_;
156 : };
157 :
158 : // operator==
159 : //
160 : template <typename C>
161 : inline bool
162 : operator== (const ro_string<C>& a, const ro_string<C>& b)
163 : {
164 : return a.compare (b) == 0;
165 : }
166 :
167 : template <typename C>
168 : inline bool
169 : operator== (const ro_string<C>& a, const std::basic_string<C>& b)
170 : {
171 : return a.compare (b) == 0;
172 : }
173 :
174 : template <typename C>
175 : inline bool
176 : operator== (const std::basic_string<C>& a, const ro_string<C>& b)
177 : {
178 : return b.compare (a) == 0;
179 : }
180 :
181 : template <typename C>
182 : inline bool
183 : operator== (const ro_string<C>& a, const C* b)
184 : {
185 : return a.compare (b) == 0;
186 : }
187 :
188 : template <typename C>
189 : inline bool
190 : operator== (const C* a, const ro_string<C>& b)
191 : {
192 : return b.compare (a) == 0;
193 : }
194 :
195 : // operator!=
196 : //
197 : template <typename C>
198 : inline bool
199 : operator!= (const ro_string<C>& a, const ro_string<C>& b)
200 : {
201 : return a.compare (b) != 0;
202 : }
203 :
204 : template <typename C>
205 : inline bool
206 : operator!= (const ro_string<C>& a, const std::basic_string<C>& b)
207 : {
208 : return a.compare (b) != 0;
209 : }
210 :
211 : template <typename C>
212 : inline bool
213 : operator!= (const std::basic_string<C>& a, const ro_string<C>& b)
214 : {
215 : return b.compare (a) != 0;
216 : }
217 :
218 : template <typename C>
219 : inline bool
220 : operator!= (const ro_string<C>& a, const C* b)
221 : {
222 : return a.compare (b) != 0;
223 : }
224 :
225 : template <typename C>
226 : inline bool
227 : operator!= (const C* a, const ro_string<C>& b)
228 : {
229 : return b.compare (a) != 0;
230 : }
231 :
232 : // operator<
233 : //
234 : template <typename C>
235 : inline bool
236 : operator< (const ro_string<C>& l, const ro_string<C>& r)
237 : {
238 : return l.compare (r) < 0;
239 : }
240 :
241 : template <typename C>
242 : inline bool
243 : operator< (const ro_string<C>& l, const std::basic_string<C>& r)
244 : {
245 : return l.compare (r) < 0;
246 : }
247 :
248 : template <typename C>
249 : inline bool
250 : operator< (const std::basic_string<C>& l, const ro_string<C>& r)
251 : {
252 : return r.compare (l) > 0;
253 : }
254 :
255 : template <typename C>
256 : inline bool
257 : operator< (const ro_string<C>& l, const C* r)
258 : {
259 : return l.compare (r) < 0;
260 : }
261 :
262 : template <typename C>
263 : inline bool
264 : operator< (const C* l, const ro_string<C>& r)
265 : {
266 : return r.compare (l) > 0;
267 : }
268 :
269 :
270 : // operator>
271 : //
272 : template <typename C>
273 : inline bool
274 : operator> (const ro_string<C>& l, const ro_string<C>& r)
275 : {
276 : return l.compare (r) > 0;
277 : }
278 :
279 : template <typename C>
280 : inline bool
281 : operator> (const ro_string<C>& l, const std::basic_string<C>& r)
282 : {
283 : return l.compare (r) > 0;
284 : }
285 :
286 : template <typename C>
287 : inline bool
288 : operator> (const std::basic_string<C>& l, const ro_string<C>& r)
289 : {
290 : return r.compare (l) < 0;
291 : }
292 :
293 : template <typename C>
294 : inline bool
295 : operator> (const ro_string<C>& l, const C* r)
296 : {
297 : return l.compare (r) > 0;
298 : }
299 :
300 : template <typename C>
301 : inline bool
302 : operator> (const C* l, const ro_string<C>& r)
303 : {
304 : return r.compare (l) < 0;
305 : }
306 :
307 : // operator<=
308 : //
309 : template <typename C>
310 : inline bool
311 : operator<= (const ro_string<C>& l, const ro_string<C>& r)
312 : {
313 : return l.compare (r) <= 0;
314 : }
315 :
316 : template <typename C>
317 : inline bool
318 : operator<= (const ro_string<C>& l, const std::basic_string<C>& r)
319 : {
320 : return l.compare (r) <= 0;
321 : }
322 :
323 : template <typename C>
324 : inline bool
325 : operator<= (const std::basic_string<C>& l, const ro_string<C>& r)
326 : {
327 : return r.compare (l) >= 0;
328 : }
329 :
330 : template <typename C>
331 : inline bool
332 : operator<= (const ro_string<C>& l, const C* r)
333 : {
334 : return l.compare (r) <= 0;
335 : }
336 :
337 : template <typename C>
338 : inline bool
339 : operator<= (const C* l, const ro_string<C>& r)
340 : {
341 : return r.compare (l) >= 0;
342 : }
343 :
344 :
345 : // operator>=
346 : //
347 : template <typename C>
348 : inline bool
349 : operator>= (const ro_string<C>& l, const ro_string<C>& r)
350 : {
351 : return l.compare (r) >= 0;
352 : }
353 :
354 : template <typename C>
355 : inline bool
356 : operator>= (const ro_string<C>& l, const std::basic_string<C>& r)
357 : {
358 : return l.compare (r) >= 0;
359 : }
360 :
361 : template <typename C>
362 : inline bool
363 : operator>= (const std::basic_string<C>& l, const ro_string<C>& r)
364 : {
365 : return r.compare (l) <= 0;
366 : }
367 :
368 : template <typename C>
369 : inline bool
370 : operator>= (const ro_string<C>& l, const C* r)
371 : {
372 : return l.compare (r) >= 0;
373 : }
374 :
375 : template <typename C>
376 : inline bool
377 : operator>= (const C* l, const ro_string<C>& r)
378 : {
379 : return r.compare (l) <= 0;
380 : }
381 :
382 : // operator<<
383 : //
384 : template<typename C>
385 : std::basic_ostream<C>&
386 : operator<< (std::basic_ostream<C>& os, const ro_string<C>& str)
387 : {
388 : if (str.size () != 0)
389 : os.write (str.data (), static_cast<std::streamsize> (str.size ()));
390 :
391 : return os;
392 : }
393 :
394 : // operator+=
395 : //
396 : template<typename C>
397 : std::basic_string<C>&
398 : operator+= (std::basic_string<C>& l, const ro_string<C>& r)
399 : {
400 : l.append (r.data (), r.size ());
401 : return l;
402 : }
403 :
404 : // Trim leading and trailing XML whitespaces. Return the new
405 : // string size.
406 : //
407 : template <typename C>
408 : typename ro_string<C>::size_type
409 : trim_left (ro_string<C>&);
410 :
411 : template <typename C>
412 : typename ro_string<C>::size_type
413 : trim_right (ro_string<C>&);
414 :
415 : template <typename C>
416 : typename ro_string<C>::size_type
417 : trim (ro_string<C>&);
418 :
419 : // Trim leading and trailing XML whitespaces.
420 : //
421 : template<typename C>
422 : std::basic_string<C>
423 : trim (const std::basic_string<C>&);
424 : }
425 : }
426 :
427 : #include <xsd/cxx/ro-string.txx>
428 :
429 : #endif // XSD_CXX_RO_STRING_HXX
|