epoc32/include/tools/stlport/stl/_string_sum_methods.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
parent 3 e1b950c65cb4
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@4
     1
/*
williamr@4
     2
 * Copyright (c) 2003
williamr@4
     3
 * Francois Dumont
williamr@4
     4
 *
williamr@4
     5
 * This material is provided "as is", with absolutely no warranty expressed
williamr@4
     6
 * or implied. Any use is at your own risk.
williamr@4
     7
 *
williamr@4
     8
 * Permission to use or copy this software for any purpose is hereby granted
williamr@4
     9
 * without fee, provided the above notices are retained on all copies.
williamr@4
    10
 * Permission to modify the code and to distribute modified code is granted,
williamr@4
    11
 * provided the above notices are retained, and a notice that the code was
williamr@4
    12
 * modified is included with the above copyright notice.
williamr@4
    13
 *
williamr@4
    14
 */
williamr@4
    15
williamr@4
    16
/*
williamr@4
    17
 * All the necessary methods used for template expressions with basic_string
williamr@4
    18
 * This file do not have to be macro guarded as it is only used in the _string.h
williamr@4
    19
 * file and it is a part of the basic_string definition.
williamr@4
    20
 */
williamr@4
    21
williamr@4
    22
public:
williamr@4
    23
  template <class _Left, class _Right, class _StorageDir>
williamr@4
    24
  basic_string(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s)
williamr@4
    25
    : _STLP_STRING_SUM_BASE(_Reserve_t(), __s.size(), __s.get_allocator())
williamr@4
    26
  { _M_append_sum(__s); }
williamr@4
    27
williamr@4
    28
  template <class _Left, class _Right, class _StorageDir>
williamr@4
    29
  basic_string(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
williamr@4
    30
               size_type __pos, size_type __n = npos,
williamr@4
    31
               const allocator_type& __a = allocator_type())
williamr@4
    32
    : _STLP_STRING_SUM_BASE(_Reserve_t(), (__pos <= __s.size()) ? ((min) (__n, __s.size() - __pos)) : 0, __a) {
williamr@4
    33
    size_type __size = __s.size();
williamr@4
    34
    if (__pos > __size)
williamr@4
    35
      this->_M_throw_out_of_range();
williamr@4
    36
    else
williamr@4
    37
      _M_append_sum_pos(__s, __pos, (min) (__n, __size - __pos));
williamr@4
    38
  }
williamr@4
    39
williamr@4
    40
private:
williamr@4
    41
  _CharT* _M_append_fast(_STLP_PRIV __char_wrapper<_CharT> __c, _CharT *__buf) {
williamr@4
    42
    _STLP_STD::_Copy_Construct(__buf, __c.getValue());
williamr@4
    43
    return __buf + 1;
williamr@4
    44
  }
williamr@4
    45
  _CharT* _M_append_fast(_CharT const* __s, size_type __s_size, _CharT *__buf)
williamr@4
    46
  { return uninitialized_copy(__s, __s + __s_size, __buf); }
williamr@4
    47
  _CharT* _M_append_fast(_STLP_PRIV __cstr_wrapper<_CharT> const& __s, _CharT *__buf)
williamr@4
    48
  { return _M_append_fast(__s.c_str(), __s.size(), __buf); }
williamr@4
    49
  _CharT* _M_append_fast(_STLP_PRIV __bstr_wrapper<_CharT, _Traits, _Alloc> __s, _CharT *__buf)
williamr@4
    50
  { return _M_append_fast(__s.b_str(), __buf); }
williamr@4
    51
  _CharT* _M_append_fast(_Self const& __s, _CharT *__buf)
williamr@4
    52
  { return _M_append_fast(__s.data(), __s.size(), __buf); }
williamr@4
    53
  _CharT* _M_append_fast(_STLP_PRIV __sum_storage_elem<_CharT, _Traits, _Alloc> const&, _CharT *__buf)
williamr@4
    54
  { return __buf; }
williamr@4
    55
  template <class _Left, class _Right, class _StorageDir>
williamr@4
    56
  _CharT* _M_append_fast(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s, _CharT *__buf)
williamr@4
    57
  { return _M_append_fast(__s.getRhs(), _M_append_fast(__s.getLhs(), __buf)); }
williamr@4
    58
williamr@4
    59
  _CharT* _M_append_fast_pos(_STLP_PRIV __char_wrapper<_CharT> __c, _CharT *__buf, size_type /*__pos*/, size_type __n) {
williamr@4
    60
    if (__n == 0)
williamr@4
    61
      return __buf;
williamr@4
    62
    _STLP_STD::_Copy_Construct(__buf, __c.getValue());
williamr@4
    63
    return __buf + 1;
williamr@4
    64
  }
williamr@4
    65
  _CharT* _M_append_fast_pos(_CharT const* __s, size_type __s_size, _CharT *__buf,
williamr@4
    66
                             size_type __pos, size_type __n)
williamr@4
    67
  { return uninitialized_copy(__s + __pos, __s + __pos + (min)(__n, __s_size - __pos), __buf); }
williamr@4
    68
  _CharT* _M_append_fast_pos(_STLP_PRIV __cstr_wrapper<_CharT> const& __s, _CharT *__buf,
williamr@4
    69
                             size_type __pos, size_type __n)
williamr@4
    70
  { return _M_append_fast_pos(__s.c_str(), __s.size(), __buf, __pos, __n); }
williamr@4
    71
  _CharT* _M_append_fast_pos(_STLP_PRIV __bstr_wrapper<_CharT, _Traits, _Alloc> __s, _CharT *__buf,
williamr@4
    72
                             size_type __pos, size_type __n)
williamr@4
    73
  { return _M_append_fast_pos(__s.b_str(), __buf, __pos, __n); }
williamr@4
    74
  _CharT* _M_append_fast_pos(_Self const& __s, _CharT *__buf,
williamr@4
    75
                             size_type __pos, size_type __n)
williamr@4
    76
  { return _M_append_fast_pos(__s.data(), __s.size(), __buf, __pos, __n); }
williamr@4
    77
  _CharT* _M_append_fast_pos(_STLP_PRIV __sum_storage_elem<_CharT, _Traits, _Alloc> const&, _CharT *__buf,
williamr@4
    78
                             size_type, size_type)
williamr@4
    79
  { return __buf; }
williamr@4
    80
williamr@4
    81
  template <class _Left, class _Right, class _StorageDir>
williamr@4
    82
  _CharT* _M_append_fast_pos(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
williamr@4
    83
                             _CharT *__buf, size_type __pos, size_type __n) {
williamr@4
    84
    if (__n == 0) {
williamr@4
    85
      return __buf;
williamr@4
    86
    }
williamr@4
    87
    size_type __lhs_size = __s.getLhs().size();
williamr@4
    88
    if (__pos < __lhs_size) {
williamr@4
    89
      if (__n < (__lhs_size - __pos)) {
williamr@4
    90
        return _M_append_fast_pos(__s.getLhs(), __buf, __pos, __n);
williamr@4
    91
      } else {
williamr@4
    92
        return _M_append_fast_pos(__s.getRhs(), _M_append_fast_pos(__s.getLhs(), __buf, __pos, __n),
williamr@4
    93
                                  0, __n - (__lhs_size - __pos));
williamr@4
    94
      }
williamr@4
    95
    } else {
williamr@4
    96
      return _M_append_fast_pos(__s.getRhs(), __buf, __pos - __lhs_size, __n);
williamr@4
    97
    }
williamr@4
    98
  }
williamr@4
    99
williamr@4
   100
  /* Note: We always force use of dynamic buffer if the short string optim option is activated
williamr@4
   101
   * to avoid complicated code if the basic_string was instanciated with a non POD type.
williamr@4
   102
   * In such a case we should use assignment for objects in the static array something that we
williamr@4
   103
   * do not do.
williamr@4
   104
   */
williamr@4
   105
  size_type _M_get_additional_size(size_type __new_size, const __false_type& /*_Char_Is_POD*/) const {
williamr@4
   106
#if defined (_STLP_USE_SHORT_STRING_OPTIM)
williamr@4
   107
    //To avoid problem with the string assumptions, never allocate a dynamic buffer smaller or equal
williamr@4
   108
    //than the static one:
williamr@4
   109
    if (__new_size < _Base::_DEFAULT_SIZE + 1)
williamr@4
   110
      return (_Base::_DEFAULT_SIZE + 1) - __new_size;
williamr@4
   111
#endif /* _STLP_USE_SHORT_STRING_OPTIM */
williamr@4
   112
    return 0;
williamr@4
   113
  }
williamr@4
   114
  size_type _M_get_additional_size(size_type, const __true_type& /*_Char_Is_POD*/) const
williamr@4
   115
  { return 0; }
williamr@4
   116
williamr@4
   117
  template <class _Left, class _Right, class _StorageDir>
williamr@4
   118
  _Self& _M_append_sum (_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s) {
williamr@4
   119
    size_type __s_size = __s.size();
williamr@4
   120
    if (__s_size == 0)
williamr@4
   121
      return *this;
williamr@4
   122
    const size_type __old_size = this->size();
williamr@4
   123
    if (__s_size > this->max_size() || __old_size > (this->max_size() - __s_size))
williamr@4
   124
      this->_M_throw_length_error();
williamr@4
   125
    size_type __offset_size = _M_get_additional_size(__old_size + __s_size, _Char_Is_POD());
williamr@4
   126
    if (__old_size + __s_size + __offset_size > this->capacity()) {
williamr@4
   127
      const size_type __len = __old_size + __offset_size + (max)(__old_size, __s_size) + 1;
williamr@4
   128
      pointer __new_start = this->_M_end_of_storage.allocate(__len);
williamr@4
   129
      pointer __new_finish = __new_start;
williamr@4
   130
      _STLP_TRY {
williamr@4
   131
        __new_finish = uninitialized_copy(this->_M_Start(), this->_M_Finish(), __new_start);
williamr@4
   132
        __new_finish = this->_M_append_fast(__s, __new_finish);
williamr@4
   133
        this->_M_construct_null(__new_finish);
williamr@4
   134
      }
williamr@4
   135
      _STLP_UNWIND((_STLP_STD::_Destroy_Range(__new_start,__new_finish),
williamr@4
   136
                   this->_M_end_of_storage.deallocate(__new_start,__len)))
williamr@4
   137
      this->_M_destroy_range();
williamr@4
   138
      this->_M_deallocate_block();
williamr@4
   139
      this->_M_reset(__new_start, __new_finish, __new_start + __len);
williamr@4
   140
    }
williamr@4
   141
    else {
williamr@4
   142
      _M_append_sum_no_overflow(__s, 0, __s_size);
williamr@4
   143
    }
williamr@4
   144
    return *this;
williamr@4
   145
  }
williamr@4
   146
williamr@4
   147
  template <class _Left, class _Right, class _StorageDir>
williamr@4
   148
  _Self& _M_append_sum_pos(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
williamr@4
   149
                           size_type __pos, size_type __n) {
williamr@4
   150
    size_type __s_size = (min)(__s.size() - __pos, __n);
williamr@4
   151
    if (__s_size == 0)
williamr@4
   152
      return *this;
williamr@4
   153
    const size_type __old_size = this->size();
williamr@4
   154
    if (__s_size > this->max_size() || __old_size > (this->max_size() - __s_size))
williamr@4
   155
      this->_M_throw_length_error();
williamr@4
   156
    size_type __offset_size = _M_get_additional_size(__old_size + __s_size, _Char_Is_POD());
williamr@4
   157
    if (__old_size + __s_size + __offset_size > this->capacity()) {
williamr@4
   158
      const size_type __len = __old_size + __offset_size + (max)(__old_size, __s_size) + 1;
williamr@4
   159
      pointer __new_start = this->_M_end_of_storage.allocate(__len);
williamr@4
   160
      pointer __new_finish = __new_start;
williamr@4
   161
      _STLP_TRY {
williamr@4
   162
        __new_finish = uninitialized_copy(this->_M_Start(), this->_M_Finish(), __new_start);
williamr@4
   163
        __new_finish = _M_append_fast_pos(__s, __new_finish, __pos, __s_size);
williamr@4
   164
        this->_M_construct_null(__new_finish);
williamr@4
   165
      }
williamr@4
   166
      _STLP_UNWIND((_STLP_STD::_Destroy_Range(__new_start,__new_finish),
williamr@4
   167
                   this->_M_end_of_storage.deallocate(__new_start,__len)))
williamr@4
   168
      this->_M_destroy_range();
williamr@4
   169
      this->_M_deallocate_block();
williamr@4
   170
      this->_M_reset(__new_start, __new_finish, __new_start + __len);
williamr@4
   171
    }
williamr@4
   172
    else {
williamr@4
   173
      _M_append_sum_no_overflow(__s, __pos, __s_size);
williamr@4
   174
    }
williamr@4
   175
    return *this;
williamr@4
   176
  }
williamr@4
   177
williamr@4
   178
  template <class _Left, class _Right, class _StorageDir>
williamr@4
   179
  void _M_append_sum_no_overflow(_STLP_PRIV __bstr_sum<_CharT, _Traits, _Alloc, _Left, _Right, _StorageDir> const& __s,
williamr@4
   180
                                 size_type __pos, size_type __n) {
williamr@4
   181
    pointer __finish = this->_M_Finish();
williamr@4
   182
    _M_append_fast_pos(__s, __finish + 1, __pos + 1, __n - 1);
williamr@4
   183
    _STLP_TRY {
williamr@4
   184
      this->_M_construct_null(__finish + __n);
williamr@4
   185
    }
williamr@4
   186
    _STLP_UNWIND(this->_M_destroy_ptr_range(__finish + 1, __finish + __n))
williamr@4
   187
    /* The call to the traits::assign method is only important for non POD types because the instance
williamr@4
   188
     * pointed to by _M_finish has been constructed (default constructor) and should not be constructed
williamr@4
   189
     * (copy constructor) once again. For POD it is irrelevant, uninitialized_copy could be fine,
williamr@4
   190
     * but we are not going to make two implementations just for that.
williamr@4
   191
     */
williamr@4
   192
    _Traits::assign(*this->_M_finish, __s[__pos]);
williamr@4
   193
    this->_M_finish += __n;
williamr@4
   194
  }