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