Changeset b0b46d59 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:
e07bbbc
Parents:
681fdcca
git-author:
Jaroslav Jindrak <dzejrou@…> (2017-10-27 19:26:11)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:18)
Message:

cpp: implemented most of std::string::replace

File:
1 edited

Legend:

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

    r681fdcca rb0b46d59  
    830830            }
    831831
    832             basic_string& replace(size_type pos, size_type n, const basic_string& str);
     832            basic_string& replace(size_type pos, size_type n, const basic_string& str)
     833            {
     834                // TODO: throw out_of_range if pos > size()
     835                return replace(pos, n, str.data(), str.size());
     836            }
    833837
    834838            basic_string& replace(size_type pos1, size_type n1, const basic_string& str
    835                                   size_type pos2, size_type n2);
     839                                  size_type pos2, size_type n2 = npos)
     840            {
     841                // TODO: throw out_of_range if pos1 > size() or pos2 > str.size()
     842                auto len = min(n2, str.size() - pos2);
     843                return replace(pos1, n1, str.data() + pos2, len);
     844            }
    836845
    837846            basic_string& replace(size_type pos, size_type n1, const value_type* str,
    838                                   size_type n2);
    839 
    840             basic_string& replace(size_type pos, size_type n, const value_type* str);
     847                                  size_type n2)
     848            {
     849                // TODO: format and comment this properly
     850                // TODO: throw out_of_range if pos > size()
     851                auto len = min(n1, size_ - pos);
     852                // TODO: if size() - len > max_size() - n2 throw length_error
     853                basic_string tmp{};
     854                tmp.resize_without_copy_(size_ - len + n2);
     855                copy_(begin(), begin() + pos, tmp.begin());
     856                traits_type::copy(tmp.begin() + pos + 1, str, n2);
     857                copy_(begin() + pos + len, end(), tmp.begin() + pos + 1 + n2);
     858                swap(tmp);
     859                return *this;
     860            }
     861
     862            basic_string& replace(size_type pos, size_type n, const value_type* str)
     863            {
     864                return replace(pos, n, str, traits_type::length(str));
     865            }
    841866
    842867            basic_string& replace(size_type pos, size_type n1, size_type n2,
    843                                   value_type c);
     868                                  value_type c)
     869            {
     870                return replace(pos, n1, basic_string(n2, c));
     871            }
    844872
    845873            basic_string& replace(const_iterator i1, const_iterator i2,
    846                                   const basic_string& str);
     874                                  const basic_string& str)
     875            {
     876                return replace(i1 - begin(), i2 - i1, str);
     877            }
    847878
    848879            basic_string& replace(const_iterator i1, const_iterator i2,
    849                                   const value_type* str, size_type n);
     880                                  const value_type* str, size_type n)
     881            {
     882                return replace(i1 - begin(), i2 - i1, s, n);
     883            }
    850884
    851885            basic_string& replace(const_iterator i1, const_iterator i2,
    852                                   const value_type* str);
     886                                  const value_type* str)
     887            {
     888                return replace(i1 - begin(), i2 - i1, str, traits_type::length(str));
     889            }
    853890
    854891            basic_string& replace(const_iterator i1, const_iterator i2,
    855                                   value_type c);
     892                                  size_type n, value_type c)
     893            {
     894                return replace(i1 - begin(), i2 - i1, basic_string(n, c));
     895            }
    856896
    857897            template<class InputIterator>
    858898            basic_string& replace(const_iterator i1, const_iterator i2,
    859                                   InputIterator j1, InputIterator j2);
     899                                  InputIterator j1, InputIterator j2)
     900            {
     901                return replace(i1 - begin(), i2 - i1, basic_string(j1, j2));
     902            }
    860903
    861904            basic_string& replace(const_iterator i1, const_iterator i2,
    862                                   initializer_list<value_type> init);
     905                                  initializer_list<value_type> init)
     906            {
     907                return replace(i1 - begin(), i2 - i1, init.begin(), init.size());
     908            }
    863909
    864910            size_type copy(value_type* str, size_type n, size_type pos = 0) const;
Note: See TracChangeset for help on using the changeset viewer.