Orcus
pstring.hpp
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6  */
7 
8 #ifndef __ORCUS_PSTRING_HPP__
9 #define __ORCUS_PSTRING_HPP__
10 
11 #include "orcus/env.hpp"
12 
13 #include <cstdlib>
14 #include <string>
15 #include <cstring>
16 #include <ostream>
17 
18 namespace orcus {
19 
24 class ORCUS_PSR_DLLPUBLIC pstring
25 {
26  friend ::std::ostream& operator<< (::std::ostream& os, const pstring& str);
27 
28 public:
29 
30  pstring() : m_pos(NULL), m_size(0) {}
31  pstring(const char* _pos) : m_pos(_pos) { m_size = std::strlen(_pos); }
32  pstring(const char* _pos, size_t _size) : m_pos(_pos), m_size(_size) {}
33 
34  ::std::string str() const { return ::std::string(m_pos, m_size); }
35 
36  size_t size() const { return m_size; }
37  const char& operator[](size_t idx) const { return m_pos[idx]; }
38 
39  pstring& operator= (const pstring& r)
40  {
41  m_pos = r.m_pos;
42  m_size = r.m_size;
43  return *this;
44  }
45 
46  const char* get() const { return m_pos; }
47 
48  bool operator== (const pstring& r) const;
49 
50  bool operator!= (const pstring& r) const
51  {
52  return !operator==(r);
53  }
54 
55  bool operator< (const pstring& r) const;
56 
57  bool operator== (const char* _str) const;
58 
59  bool operator!= (const char* _str) const
60  {
61  return !operator==(_str);
62  }
63 
64  pstring trim() const;
65 
66  bool empty() const { return m_size == 0; }
67 
68  void clear()
69  {
70  m_pos = NULL;
71  m_size = 0;
72  }
73 
74  void resize(size_t new_size);
75 
76  struct ORCUS_PSR_DLLPUBLIC hash
77  {
78  size_t operator() (const pstring& val) const;
79  };
80 
81 private:
82  const char* m_pos;
83  size_t m_size;
84 };
85 
86 inline ::std::ostream& operator<< (::std::ostream& os, const pstring& str)
87 {
88  return os << str.str();
89 }
90 
91 ORCUS_PSR_DLLPUBLIC std::string operator+ (const std::string& left, const pstring& right);
92 ORCUS_PSR_DLLPUBLIC std::string& operator+= (std::string& left, const pstring& right);
93 
94 }
95 
96 #endif
97 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: pstring.hpp:76
Definition: pstring.hpp:24
Definition: base64.hpp:15