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(nullptr), m_size(0) {}
31  pstring(const char* _pos);
32  pstring(const char* _pos, size_t _size) : m_pos(_pos), m_size(_size) {}
33  pstring(const std::string& s) : m_pos(s.data()), m_size(s.size()) {}
34 
35  ::std::string str() const { return ::std::string(m_pos, m_size); }
36 
37  size_t size() const { return m_size; }
38  const char& operator[](size_t idx) const { return m_pos[idx]; }
39 
40  pstring& operator= (const pstring& r)
41  {
42  m_pos = r.m_pos;
43  m_size = r.m_size;
44  return *this;
45  }
46 
47  const char* get() const { return m_pos; }
48 
49  const char* data() const { return m_pos; }
50 
51  bool operator== (const pstring& r) const;
52 
53  bool operator!= (const pstring& r) const
54  {
55  return !operator==(r);
56  }
57 
58  bool operator< (const pstring& r) const;
59 
60  bool operator== (const char* _str) const;
61 
62  bool operator!= (const char* _str) const
63  {
64  return !operator==(_str);
65  }
66 
67  pstring trim() const;
68 
69  bool empty() const { return m_size == 0; }
70 
71  void clear()
72  {
73  m_pos = nullptr;
74  m_size = 0;
75  }
76 
77  void resize(size_t new_size);
78 
79  struct ORCUS_PSR_DLLPUBLIC hash
80  {
81  size_t operator() (const pstring& val) const;
82  };
83 
84 private:
85  const char* m_pos;
86  size_t m_size;
87 };
88 
89 inline ::std::ostream& operator<< (::std::ostream& os, const pstring& str)
90 {
91  return os << str.str();
92 }
93 
94 ORCUS_PSR_DLLPUBLIC std::string operator+ (const std::string& left, const pstring& right);
95 ORCUS_PSR_DLLPUBLIC std::string& operator+= (std::string& left, const pstring& right);
96 
97 }
98 
99 #endif
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: pstring.hpp:79
Definition: pstring.hpp:24