1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
// (C) Copyright Gennadiy Rozental 2001-2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org/libs/test for the library home page.
//
// File : $RCSfile$
//
// Version : $Revision$
//
// Description : simple string class definition
// ***************************************************************************
#ifndef CONST_STRING_HPP
#define CONST_STRING_HPP
// STL
#include <iterator>
#include <string>
#include <cstring>
using std::string;
namespace common_layer {
// ************************************************************************** //
// ************** const_string ************** //
// ************************************************************************** //
class const_string {
public:
// Subtypes
typedef char const* iterator;
typedef char const* const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef reverse_iterator const_reverse_iterator;
// Constructor
const_string()
: m_begin( "" ), m_end( m_begin ) {}
// Copy constructor is generated by compiler
const_string( const std::string& s )
: m_begin( s.c_str() ),
m_end( m_begin + s.length() ) {}
const_string( char const* s )
: m_begin( s ? s : "" )
, m_end( s ? m_begin + std::strlen( s ) : m_begin )
{}
const_string( char const* s, size_t length )
: m_begin( s ), m_end( m_begin + length ) { if( length == 0 ) erase(); }
const_string( char const* first, char const* last )
: m_begin( first ), m_end( last ) {}
// data access methods
char operator[]( size_t index ) const { return m_begin[index]; }
char at( size_t index ) const { return m_begin[index]; }
char const* data() const { return m_begin; }
// length operators
size_t length() const { return m_end - m_begin; }
bool is_empty() const { return m_end == m_begin; }
void erase() { m_begin = m_end = ""; }
void resize( size_t new_len ) { if( m_begin + new_len < m_end ) m_end = m_begin + new_len; }
void rshorten( size_t shift = 1 ) { m_end -= shift; if( m_end <= m_begin ) erase(); }
void lshorten( size_t shift = 1 ) { m_begin += shift; if( m_end <= m_begin ) erase(); }
// Assignment operators
const_string& operator=( const_string const& s );
const_string& operator=( string const& s ) { return *this = const_string( s ); }
const_string& operator=( char const* s ) { return *this = const_string( s ); }
const_string& assign( const_string const& s ) { return *this = s; }
const_string& assign( string const& s, size_t len ) { return *this = const_string( s.data(), len ); }
const_string& assign( string const& s ) { return *this = const_string( s ); }
const_string& assign( char const* s ) { return *this = const_string( s ); }
const_string& assign( char const* s, size_t len ) { return *this = const_string( s, len ); }
const_string& assign( char const* f, char const* l ) { return *this = const_string( f, l ); }
void swap( const_string& s ) {
// do not want to include alogrithm
char const* tmp1 = m_begin;
char const* tmp2 = m_end;
m_begin = s.m_begin;
m_end = s.m_end;
s.m_begin = tmp1;
s.m_end = tmp2;
}
// Comparison operators
friend bool operator==( const_string const& s1, const_string const& s2 )
{
return s1.length() == s2.length() && std::strncmp( s1.data(), s2.data(), s1.length() ) == 0;
}
friend bool operator==( const_string const& s1, char const* s2 ) { return s1 == const_string( s2 ); }
friend bool operator==( const_string const& s1, const string& s2 ) { return s1 == const_string( s2 ); }
friend bool operator!=( const_string const& s1, const_string const& s2 ) { return !(s1 == s2); }
friend bool operator!=( const_string const& s1, char const* s2 ) { return !(s1 == s2); }
friend bool operator!=( const_string const& s1, const string& s2 ) { return !(s1 == s2); }
friend bool operator==( char const* s2, const_string const& s1 ) { return s1 == s2; }
friend bool operator==( const string& s2, const_string const& s1 ) { return s1 == s2; }
friend bool operator!=( char const* s2, const_string const& s1 ) { return !(s1 == s2); }
friend bool operator!=( const string& s2, const_string const& s1 ) { return !(s1 == s2); }
// Iterators
iterator begin() const { return m_begin; }
iterator end() const { return m_end; }
reverse_iterator rbegin() const { return reverse_iterator( m_end ); }
reverse_iterator rend() const { return reverse_iterator( m_begin ); }
private:
// Data members
char const* m_begin;
char const* m_end;
};
//____________________________________________________________________________//
// first character
class first_char {
public:
char operator()( const_string source, char default_char = '\0' ) const {
return source.is_empty() ? default_char : *source.data();
}
};
//____________________________________________________________________________//
// last character
class last_char {
public:
char operator()( const_string source, char default_char = '\0' ) const {
return source.is_empty() ? default_char : *source.rbegin();
}
};
//____________________________________________________________________________//
inline const_string&
const_string::operator=( const_string const& s ) {
if( &s != this ) {
m_begin = s.m_begin;
m_end = s.m_end;
}
return *this;
}
//____________________________________________________________________________//
typedef const_string const literal;
//____________________________________________________________________________//
inline std::ostream&
operator<<( std::ostream& os, const_string const& str )
{
os << std::string( str.begin(), str.length() );
return os;
}
//____________________________________________________________________________//
}; // namespace common_layer
#endif // CONST_STRING_HPP
|