Changeset 2d302d6 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:18Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ed81b1f
Parents:
d7f0b3f7
git-author:
Jaroslav Jindrak <dzejrou@…> (2017-10-31 22:31:32)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:18)
Message:

cpp: fixed bugs found by the insert tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/string.hpp

    rd7f0b3f7 r2d302d6  
    6464        /* using state_type = mbstate_t; */
    6565
    66         static void assign(char_type& c1, char_type& c2) noexcept
     66        static void assign(char_type& c1, const char_type& c2) noexcept
    6767        {
    6868            c1 = c2;
     
    8686        static size_t length(const char_type* s)
    8787        {
    88             return std::str_size(s) + 1;
     88            return std::str_size(s);
    8989        }
    9090
     
    167167        /* using state_type = mbstate_t; */
    168168
    169         static void assign(char_type& c1, char_type& c2) noexcept
     169        static void assign(char_type& c1, const char_type& c2) noexcept
    170170        {
    171171            c1 = c2;
     
    191191        static size_t length(const char_type* s)
    192192        {
    193             return std::wstr_size(s) + 1;
     193            return std::wstr_size(s);
    194194        }
    195195
     
    331331                : data_{}, size_{}, capacity_{}, allocator_{alloc}
    332332            {
    333                 init_(str, traits_type::length(str));
     333                init_(str, traits_type::length(str) + 1);
    334334            }
    335335
     
    661661            basic_string& append(const value_type* str)
    662662            {
    663                 return append(str, traits_type::length(str));
     663                return append(str, traits_type::length(str) + 1);
    664664            }
    665665
     
    764764            basic_string& insert(size_type pos, const value_type* str, size_type n)
    765765            {
    766                 ensure_free_space_(size_ + n);
     766                // TODO: throw out_of_range if pos > size()
     767                // TODO: throw length_error if size() + n > max_size()
     768                ensure_free_space_(n);
     769
    767770                copy_backward_(begin() + pos, end(), end() + n);
    768                 copy_(begin() + pos, begin() + pos + n, str);
    769 
     771                std::printf("|%s|\n", data_);
     772                copy_(str, str + n, begin() + pos);
     773                size_ += n;
     774
     775                ensure_null_terminator_();
    770776                return *this;
    771777            }
     
    786792
    787793                ensure_free_space_(1);
    788                 copy_backward_(begin() + pos, end(), end() + 1);
     794                copy_backward_(begin() + idx, end(), end() + 1);
     795                traits_type::assign(data_[idx], c);
     796
     797                ++size_;
    789798                ensure_null_terminator_();
    790799
     
    800809
    801810                ensure_free_space_(n);
    802                 copy_backward_(begin() + pos, end(), end() + n);
    803 
    804                 auto it = pos;
     811                copy_backward_(begin() + idx, end(), end() + n);
     812
     813                auto it = begin() + idx;
    805814                for (size_type i = 0; i < n; ++i)
    806815                    traits_type::assign(*it++, c);
     816                size_ += n;
    807817                ensure_null_terminator_();
    808818
     
    814824                            InputIterator last)
    815825            {
    816                 return insert(pos - begin(), basic_string(first, last));
     826                if (first == last)
     827                    return const_cast<iterator>(pos);
     828
     829                auto idx = static_cast<size_type>(pos - begin());
     830                auto str = basic_string{first, last};
     831                insert(idx, str);
     832
     833                return begin() + idx;
    817834            }
    818835
     
    13371354            }
    13381355
    1339             iterator copy_(const_iterator first, const_iterator last,
    1340                            iterator result)
     1356            template<class Iterator1, class Iterator2>
     1357            Iterator2 copy_(Iterator1 first, Iterator1 last,
     1358                            Iterator2 result)
    13411359            {
    13421360                while (first != last)
    13431361                    traits_type::assign(*result++, *first++);
    13441362
    1345                 ensure_null_terminator_();
    13461363                return result;
    13471364            }
    13481365
    1349             iterator copy_backward_(const_iterator first, const_iterator last,
    1350                                     iterator result)
    1351             {
    1352                 while (last-- != first)
    1353                     traits_type::assign(*result--, *last);
    1354 
    1355                 ensure_null_terminator_();
     1366            template<class Iterator1, class Iterator2>
     1367            Iterator2 copy_backward_(Iterator1 first, Iterator1 last,
     1368                                     Iterator2 result)
     1369            {
     1370                while (last != first)
     1371                    traits_type::assign(*--result, *--last);
     1372
    13561373                return result;
    13571374            }
Note: See TracChangeset for help on using the changeset viewer.