Line data Source code
1 : /*************************************************************************
2 : *
3 : * Copyright (c) 2010 Kohei Yoshida
4 : *
5 : * Permission is hereby granted, free of charge, to any person
6 : * obtaining a copy of this software and associated documentation
7 : * files (the "Software"), to deal in the Software without
8 : * restriction, including without limitation the rights to use,
9 : * copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : * copies of the Software, and to permit persons to whom the
11 : * Software is furnished to do so, subject to the following
12 : * conditions:
13 : *
14 : * The above copyright notice and this permission notice shall be
15 : * included in all copies or substantial portions of the Software.
16 : *
17 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 : * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 : * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 : * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 : * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 : * OTHER DEALINGS IN THE SOFTWARE.
25 : *
26 : ************************************************************************/
27 :
28 : #ifndef __ORCUS_PSTRING_HPP__
29 : #define __ORCUS_PSTRING_HPP__
30 :
31 : #include "orcus/env.hpp"
32 :
33 : #include <cstdlib>
34 : #include <string>
35 : #include <cstring>
36 : #include <ostream>
37 :
38 : namespace orcus {
39 :
40 : /**
41 : * This string class does not store any char arrays, but it only stores the
42 : * position of the first char in the memory, and the size of the char array.
43 : */
44 : class ORCUS_DLLPUBLIC pstring
45 : {
46 : friend ::std::ostream& operator<< (::std::ostream& os, const pstring& str);
47 :
48 : public:
49 : /**
50 : * Create a new string instance and hold it internally until
51 : * intern::dispose() gets called.
52 : *
53 : * @param str string to intern.
54 : *
55 : * @return pstring instance pointing to the interned string.
56 : */
57 : static pstring intern(const char* str);
58 :
59 : static pstring intern(const char* str, size_t n);
60 :
61 : struct ORCUS_DLLPUBLIC intern
62 : {
63 : /**
64 : * Destroy all interned string instances. Call this before the
65 : * program exits.
66 : */
67 : static void dispose();
68 :
69 : /**
70 : * Return how many strings have been interned so far.
71 : *
72 : * @return size_t number of interned string instances.
73 : */
74 : static size_t size();
75 :
76 : static void dump();
77 :
78 : private:
79 : intern();
80 : intern(const intern&);
81 : ~intern();
82 : };
83 :
84 0 : pstring() : m_pos(NULL), m_size(0) {}
85 2088 : pstring(const char* _pos) : m_pos(_pos) { m_size = std::strlen(_pos); }
86 0 : pstring(const char* _pos, size_t _size) : m_pos(_pos), m_size(_size) {}
87 :
88 0 : ::std::string str() const { return ::std::string(m_pos, m_size); }
89 :
90 0 : size_t size() const { return m_size; }
91 0 : const char& operator[](size_t idx) const { return m_pos[idx]; }
92 :
93 : pstring& operator= (const pstring& r)
94 : {
95 0 : m_pos = r.m_pos;
96 0 : m_size = r.m_size;
97 : return *this;
98 : }
99 :
100 0 : const char* get() const { return m_pos; }
101 :
102 : bool operator== (const pstring& r) const;
103 :
104 0 : bool operator!= (const pstring& r) const
105 : {
106 0 : return !operator==(r);
107 : }
108 :
109 : bool operator< (const pstring& r) const;
110 :
111 : bool operator== (const char* _str) const;
112 :
113 : bool operator!= (const char* _str) const
114 : {
115 : return !operator==(_str);
116 : }
117 :
118 : pstring trim() const;
119 :
120 0 : bool empty() const { return m_size == 0; }
121 :
122 : void clear()
123 : {
124 : m_pos = NULL;
125 : m_size = 0;
126 : }
127 :
128 : struct hash
129 : {
130 : size_t operator() (const pstring& val) const;
131 : };
132 :
133 : pstring intern() const;
134 :
135 : private:
136 : const char* m_pos;
137 : size_t m_size;
138 : };
139 :
140 0 : inline ::std::ostream& operator<< (::std::ostream& os, const pstring& str)
141 : {
142 0 : return os << str.str();
143 : }
144 :
145 : }
146 :
147 : #endif
|