📄 simple_string.hpp
字号:
{
# if defined(STLSOFT_COMPILER_IS_GCC) && \
__GNUC__ < 3
typedef ss_typename_type_k std::iterator_traits<II> traits_t;
return append_(first, last, traits_t::iterator_category());
# elif defined(STLSOFT_COMPILER_IS_MWERKS) || \
defined(STLSOFT_COMPILER_IS_DMC)
return append_(first, last, stlsoft_iterator_query_category_ptr(II, first));
# else /* ? compiler */
return append_(first, last, stlsoft_iterator_query_category(II, first));
# endif /* compiler */
}
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT */
/// Concatenation operator
class_type& operator +=(char_type ch);
/// Concatenation operator
class_type& operator +=(char_type const* s);
/// Concatenation operator
class_type& operator +=(class_type const& rhs);
/// Appends a single character
void push_back(char_type ch);
/// @}
/// \name Operations
/// @{
public:
/// Reserves at least cch characters
void reserve(size_type cch);
/// Swaps the contents between \c this and \c other
void swap(class_type &other);
/// Resizes the string
///
/// \param cch The new size of the string
/// \param ch The value with which to initialise additional items if the string is expanded
void resize(size_type cch, value_type ch = value_type());
/// Empties the string
void clear();
/// @}
/// \name Attributes
/// @{
public:
/// The number of elements in the string
size_type size() const;
/// The maximum number of elements that can be stored in the string
size_type max_size() const;
/// The number of elements in the string
size_type length() const;
/// The storage currently allocated by the string
size_type capacity() const;
/// Indicates whether the string is empty
ss_bool_t empty() const;
/// @}
/// \name Comparison
/// @{
public:
#if 0
/// Compares \c this with the given string
ss_sint_t equal(size_type pos, size_type cch, value_type const* s, size_type cchRhs) const;
/// Compares \c this with the given string
ss_sint_t equal(size_type pos, size_type cch, value_type const* s) const;
/// Compares \c this with the given string
ss_sint_t equal(value_type const* s) const;
/// Compares \c this with the given string
ss_sint_t equal(size_type pos, size_type cch, class_type const& rhs, size_type posRhs, size_type cchRhs) const;
/// Compares \c this with the given string
ss_sint_t equal(size_type pos, size_type cch, class_type const& rhs) const;
/// Compares \c this with the given string
ss_sint_t equal(class_type const& rhs) const;
#endif /* 0 */
/// Compares \c this with the given string
ss_sint_t compare(size_type pos, size_type cch, value_type const* s, size_type cchRhs) const;
/// Compares \c this with the given string
ss_sint_t compare(size_type pos, size_type cch, value_type const* s) const;
/// Compares \c this with the given string
ss_sint_t compare(value_type const* s) const;
/// Compares \c this with the given string
ss_sint_t compare(size_type pos, size_type cch, class_type const& rhs, size_type posRhs, size_type cchRhs) const;
/// Compares \c this with the given string
ss_sint_t compare(size_type pos, size_type cch, class_type const& rhs) const;
/// Compares \c this with the given string
ss_sint_t compare(class_type const& rhs) const;
/// @}
/// \name Accessors
/// @{
public:
/// Returns mutable reference at the given index
reference operator [](size_type index);
/// Returns non-mutable (const) reference at the given index
const_reference operator [](size_type index) const;
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
/// Returns mutable (non-const) reference at the given index
///
/// \note Throws std::out_of_range if index >= size()
reference at(size_type index);
/// Returns non-mutable (const) reference at the given index
///
/// \note Throws std::out_of_range if index >= size()
const_reference at(size_type index) const;
/// Returns a string of maximum length cch, from the position pos
class_type substr(size_type pos, size_type cch) const;
class_type substr(size_type pos) const;
class_type substr() const;
#endif /* !STLSOFT_CF_EXCEPTION_SUPPORT */
/// Returns null-terminated non-mutable (const) pointer to string data
value_type const *c_str() const;
/// Returns non-mutable (const) pointer to string data
value_type const *data() const;
/// Returns the first character in the string
///
/// \note It is us to the user to ensure that the string is not empty
reference front();
/// Returns the last character in the string
///
/// \note It is us to the user to ensure that the string is not empty
reference back();
/// Returns the first character in the string
///
/// \note It is us to the user to ensure that the string is not empty
const_reference front() const;
/// Returns the last character in the string
///
/// \note It is us to the user to ensure that the string is not empty
const_reference back() const;
/// Copies elements into the given destination
size_type copy(value_type *dest, size_type cch, size_type pos = 0) const;
/// @}
/// \name Iteration
/// @{
public:
/// Begins the iteration
///
/// \return A non-mutable (const) iterator representing the start of the sequence
#ifdef STLSOFT_SIMPLE_STRING_ITERATOR_METHODS_INLINE
const_iterator begin() const
{
return const_cast<class_type*>(this)->begin_();
}
#else /* ? STLSOFT_SIMPLE_STRING_ITERATOR_METHODS_INLINE */
const_iterator begin() const;
#endif /* STLSOFT_SIMPLE_STRING_ITERATOR_METHODS_INLINE */
/// Ends the iteration
///
/// \return A non-mutable (const) iterator representing the end of the sequence
#ifdef STLSOFT_SIMPLE_STRING_ITERATOR_METHODS_INLINE
const_iterator end() const
{
return const_cast<class_type*>(this)->end_();
}
#else /* ? STLSOFT_SIMPLE_STRING_ITERATOR_METHODS_INLINE */
const_iterator end() const;
#endif /* STLSOFT_SIMPLE_STRING_ITERATOR_METHODS_INLINE */
/// Begins the iteration
///
/// \return An iterator representing the start of the sequence
iterator begin();
/// Ends the iteration
///
/// \return An iterator representing the end of the sequence
iterator end();
#if defined(STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT)
/// Begins the reverse iteration
///
/// \return A non-mutable (const) iterator representing the start of the reverse sequence
const_reverse_iterator rbegin() const;
/// Ends the reverse iteration
///
/// \return A non-mutable (const) iterator representing the end of the reverse sequence
const_reverse_iterator rend() const;
/// Begins the reverse iteration
///
/// \return An iterator representing the start of the reverse sequence
reverse_iterator rbegin();
/// Ends the reverse iteration
///
/// \return An iterator representing the end of the reverse sequence
reverse_iterator rend();
#endif /* STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT */
/// @}
/// \name Implementation
/// @{
private:
enum { alloc_quantum = 31 }; // Must be (2^n - 1)
struct string_buffer
{
size_type capacity; // The number of char places in the buffer
size_type length; // The number of chars in the string (<= capacity)
char_type contents[1]; // The first element in the array
};
typedef auto_buffer_old<char_type
, allocator_type
> buffer_type_;
#ifdef STLSOFT_SIMPLE_STRING_NO_PTR_ADJUST
typedef string_buffer *member_pointer;
typedef string_buffer const* member_const_pointer;
#else /* ? STLSOFT_SIMPLE_STRING_NO_PTR_ADJUST */
typedef char_type *member_pointer;
typedef char_type const *member_const_pointer;
#endif /* STLSOFT_SIMPLE_STRING_NO_PTR_ADJUST */
// Conversion between member pointer and character pointer
static char_type *char_pointer_from_member_pointer_(member_pointer );
// Conversion between pointer and buffer
static string_buffer *string_buffer_from_member_pointer_(member_pointer );
static string_buffer const *string_buffer_from_member_pointer_(member_const_pointer );
// Conversion between buffer and pointer
static member_pointer member_pointer_from_string_buffer_(string_buffer *);
// Creating buffer
static member_pointer alloc_buffer_(char_type const* s, size_type capacity, size_type length);
static member_pointer alloc_buffer_(char_type const* s, size_type cch);
static member_pointer alloc_buffer_(char_type const* s);
// Copying a buffer
static member_pointer copy_buffer_(member_pointer );
// Destroying buffer
static void destroy_buffer_(string_buffer *);
static void destroy_buffer_(char_type *);
// Iteration
pointer begin_();
pointer end_();
// Invariance
ss_bool_t is_valid() const;
// Empty string
static char_type const* empty_string_();
// Comparison
static ss_sint_t compare_(char_type const* lhs, size_type lhs_len, char_type const* rhs, size_type rhs_len);
// Assignment
#if defined(STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT)
template <ss_typename_param_k II>
# if defined(STLSOFT_COMPILER_IS_MWERKS) || \
defined(STLSOFT_COMPILER_IS_DMC)
// There seems to be a bug in CodeWarrior that makes it have a cow with iterator tags by value, so we just use a ptr
class_type &assign_(II first, II last, stlsoft_ns_qual_std(input_iterator_tag) const*)
# else /* ? compiler */
class_type &assign_(II first, II last, stlsoft_ns_qual_std(input_iterator_tag))
# endif /* compiler */
{
stlsoft_ns_qual_std(copy)(first, last, stlsoft_ns_qual_std(back_inserter)(*this));
STLSOFT_ASSERT(is_valid());
return *this;
}
template <ss_typename_param_k II>
# if defined(STLSOFT_COMPILER_IS_MWERKS) || \
defined(STLSOFT_COMPILER_IS_DMC)
// There seems to be a bug in CodeWarrior that makes it have a cow with iterator tags by value, so we just use a ptr
class_type &assign_(II first, II last, stlsoft_ns_qual_std(forward_iterator_tag) const*)
# else /* ? compiler */
class_type &assign_(II first, II last, stlsoft_ns_qual_std(forward_iterator_tag))
# endif /* compiler */
{
const ss_size_t n = static_cast<ss_size_t>(stlsoft_ns_qual_std(distance)(first, last));
buffer_type_ buffer(n);
copy_n(first, buffer.size(), &buffer[0]);
assign(buffer.data(), buffer.size());
STLSOFT_ASSERT(is_valid());
return *this;
}
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT */
// Appending
#if defined(STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT)
template <ss_typename_param_k II>
# if defined(STLSOFT_COMPILER_IS_MWERKS) || \
defined(STLSOFT_COMPILER_IS_DMC)
class_type &append_(II first, II last, stlsoft_ns_qual_std(input_iterator_tag) const*)
# else /* ? compiler */
class_type &append_(II first, II last, stlsoft_ns_qual_std(input_iterator_tag))
# endif /* compiler */
{
stlsoft_ns_qual_std(copy)(first, last, stlsoft_ns_qual_std(back_inserter)(*this));
STLSOFT_ASSERT(is_valid());
return *this;
}
template <ss_typename_param_k II>
# if defined(STLSOFT_COMPILER_IS_MWERKS) || \
defined(STLSOFT_COMPILER_IS_DMC)
class_type &append_(II first, II last, stlsoft_ns_qual_std(forward_iterator_tag) const*)
# else /* ? compiler */
class_type &append_(II first, II last, stlsoft_ns_qual_std(forward_iterator_tag))
# endif /* compiler */
{
buffer_type_ buffer(static_cast<ss_size_t>(stlsoft_ns_qual_std(distance)(first, last)));
stlsoft_ns_qual_std(copy)(first, last, &buffer[0]);
append(buffer.data(), buffer.size());
STLSOFT_ASSERT(is_valid());
return *this;
}
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT */
/// @}
/// \name Members
/// @{
private:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -