1 /* ///////////////////////////////////////////////////////////////////////
2 * File: scoped_string.h
7 * Brief: scoped_string class
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
13 #ifndef EXTL_STRING_SCOPED_STRING_H
14 #define EXTL_STRING_SCOPED_STRING_H
16 /*!\file scoped_string.h
17 * \brief scoped_string class
21 # error scoped_string.h need be supported by c++.
24 /* ///////////////////////////////////////////////////////////////////////
27 #include "string_base.h"
28 #include "../memory/scoped_buffer.h"
30 /* ///////////////////////////////////////////////////////////////////////
31 * The grow size in the scoped_string
33 #ifdef EXTL_MEMORY_SCOPED_BUFFER_DEFAULT_GROW_SIZE
34 # define EXTL_STRING_SCOPED_STRING_DEFAULT_GROW_SIZE EXTL_MEMORY_SCOPED_BUFFER_DEFAULT_GROW_SIZE
36 # define EXTL_STRING_SCOPED_STRING_DEFAULT_GROW_SIZE (32)
38 /* ///////////////////////////////////////////////////////////////////////
43 /*!\brief basic_scoped_string class
45 * \param C The character type
46 * \param GrowN The grow size
47 * \param T The traits type of character string. Default param is string_traits<C, std_char_traits<C> >
48 * \param A The allocator type.
50 * \ingroup extl_group_string
52 template< typename_param_k C
53 #ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
54 , e_size_t GrowN
= EXTL_STRING_SCOPED_STRING_DEFAULT_GROW_SIZE
55 , typename_param_k T
= string_traits
<C
>
56 , typename_param_k A
= typename_type_def_k allocator_selector
<C
>::allocator_type
63 class basic_scoped_string
64 : public string_base
< basic_scoped_string
<C
, GrowN
, T
, A
>
69 , typename_type_k memory_traits_selector
<C
>::memory_traits_type
70 , typename_type_k initialiser_selector
<C
>::initialiser_type
78 typedef scoped_buffer
< C
81 , typename_type_k memory_traits_selector
<C
>::memory_traits_type
82 , typename_type_k initialiser_selector
<C
>::initialiser_type
84 typedef basic_scoped_string
<C
, GrowN
, T
, A
> class_type
;
85 typedef string_base
<class_type
, T
, buffer_type
> base_type
;
86 typedef typename_type_k
buffer_type::allocator_type allocator_type
;
87 typedef typename_type_k
base_type::value_type value_type
;
88 typedef typename_type_k
base_type::string_traits_type string_traits_type
;
89 typedef typename_type_k
base_type::char_type char_type
;
90 typedef typename_type_k
base_type::pointer pointer
;
91 typedef typename_type_k
base_type::const_pointer const_pointer
;
92 typedef typename_type_k
base_type::reference reference
;
93 typedef typename_type_k
base_type::const_reference const_reference
;
94 typedef typename_type_k
base_type::iterator iterator
;
95 typedef typename_type_k
base_type::const_iterator const_iterator
;
96 typedef typename_type_k
base_type::reverse_iterator reverse_iterator
;
97 typedef typename_type_k
base_type::const_reverse_iterator const_reverse_iterator
;
98 typedef typename_type_k
base_type::size_type size_type
;
99 typedef typename_type_k
base_type::bool_type bool_type
;
100 typedef typename_type_k
base_type::difference_type difference_type
;
101 typedef typename_type_k
base_type::int_type int_type
;
105 #if defined(EXTL_COMPILER_IS_DMC)
106 EXTL_OPERATORS_CMP_1_2_(class_type
const&, const_pointer
)
107 /* EXTL_OPERATORS_ADD_1_2_(class_type, const_pointer) */
108 friend class_type
operator+(class_type
const& lhs
, class_type
const& rhs
)
109 { basic_scoped_string
<C
, GrowN
, T
, A
> ret(lhs
); ret
+= rhs
; return ret
; }
110 friend class_type
operator+(class_type
const& lhs
, const_pointer rhs
)
111 { basic_scoped_string
<C
, GrowN
, T
, A
> ret(lhs
); ret
+= rhs
; return ret
; }
112 friend class_type
operator+(const_pointer lhs
, class_type
const& rhs
)
113 { basic_scoped_string
<C
, GrowN
, T
, A
> ret(rhs
); ret
+= lhs
; return ret
; }
119 friend class string_base
< class_type
, T
, buffer_type
>;
122 /// \name Construction
125 /// Default Constructor
126 basic_scoped_string()
130 /// Constructs from the given c-style string
131 explicit_k
basic_scoped_string(const_pointer s
)
135 /// Constructs with n characters from the given c-style string at the specified position
136 basic_scoped_string(const_pointer s
, size_type n
, size_type pos
= 0)
137 : base_type(s
, n
, pos
)
141 /// Constructs from the given string
142 basic_scoped_string(class_type
const& s
)
147 /// Constructs with n characters from the given string at the specified position
148 basic_scoped_string(class_type
const& s
, size_type n
, size_type pos
= 0)
149 : base_type(s
, n
, pos
)
152 /// Constructs with n characters
153 /*explicit_k basic_scoped_string(const_reference ch, size_type n = 1)
158 /// Constructs from range [first, last) with the const pointer
159 basic_scoped_string(const_pointer first
, const_pointer last
)
160 : base_type(first
, last
)
163 /// Constructs from range [first, last) with the const iterator
164 basic_scoped_string(const_iterator first
, const_iterator last
)
165 : base_type(first
, last
)
168 /// Constructs from range [first, last) with the const reverse iterator
169 basic_scoped_string(const_reverse_iterator first
, const_reverse_iterator last
)
170 : base_type(first
, last
)
173 #if defined(EXTL_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED_SUPPORT) \
174 && !defined(EXTL_COMPILER_IS_DMC)
175 /// Constructs from range [first, last) with the input iterator
176 template < typename_param_k _InIt
>
177 basic_scoped_string(_InIt first
, _InIt last
)
178 : base_type(first
, last
)
188 #if defined(EXTL_COMPILER_IS_MSVC) && _MSC_VER == 1200
189 using base_type::operator==;
191 class_type
& operator=(class_type
const& s
) { return base_type::operator=(s
); }
192 class_type
& operator=(const_pointer s
) { return base_type::operator=(s
); }
193 class_type
& operator=(const_reference v
) { return base_type::operator=(v
); }
200 bool_type
is_valid() const
202 size_type length
= base_type::length();
203 const_pointer data
= base_type::data();
205 if (length
< 0) return e_false_v
;
209 /* The string is cut off */
210 for (size_type i
= 0; i
< length
; ++i
)
212 if(char_type('\0') == data
[i
])
215 /* The string is not null-terminated */
216 if (char_type('\0') != data
[length
])
229 #ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
230 typedef basic_scoped_string
< e_char_t
> scoped_string_a
;
231 typedef basic_scoped_string
< e_wchar_t
> scoped_string_w
;
232 typedef basic_scoped_string
< e_tchar_t
> scoped_string
;
234 typedef basic_scoped_string
< e_char_t
235 , EXTL_STRING_SCOPED_STRING_DEFAULT_GROW_SIZE
236 , string_traits
<e_char_t
, std_char_traits
<e_char_t
> >
237 , allocator_selector
<e_char_t
>::allocator_type
239 typedef basic_scoped_string
< e_wchar_t
240 , EXTL_STRING_SCOPED_STRING_DEFAULT_GROW_SIZE
241 , string_traits
<e_wchar_t
, std_char_traits
<e_wchar_t
> >
242 , allocator_selector
<e_wchar_t
>::allocator_type
244 typedef basic_scoped_string
< e_tchar_t
245 , EXTL_STRING_SCOPED_STRING_DEFAULT_GROW_SIZE
246 , string_traits
<e_tchar_t
, std_char_traits
<e_tchar_t
> >
247 , allocator_selector
<e_tchar_t
>::allocator_type
253 /* ///////////////////////////////////////////////////////////////////////
258 /* //////////////////////////////////////////////////////////////////// */
259 #endif /* EXTL_STRING_SCOPED_STRING_H */
260 /* //////////////////////////////////////////////////////////////////// */