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