Changeset 3093


Ignore:
Timestamp:
Oct 21, 2013, 6:07:37 AM (8 years ago)
Author:
Peter
Message:

implement replace(3), which fixes #768

Location:
branches/0.10-stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/0.10-stable/NEWS

    r3019 r3093  
    66
    77version 0.10.3 (released NOT YET)
     8  - utility::replace(string&, string, string) is now implemented (bug #768)
    89
    910  A complete list of closed tickets can be found here [[br]]
  • branches/0.10-stable/test/utility.cc

    r2940 r3093  
    5959void test_fnmatch(test::Suite& suite);
    6060bool test_fnmatch(bool, std::string, std::string);
     61void test_replace(test::Suite& suite);
    6162
    6263template<typename InputIterator, typename Key>
     
    216217  test_dirname(suite);
    217218  test_fnmatch(suite);
     219  test_replace(suite);
    218220
    219221  return suite.return_value();
     
    638640  return false;
    639641}
     642
     643
     644void test_replace(test::Suite& suite)
     645{
     646  std::string s = "Some magic string!!!";
     647  utility::replace(s, "magic", "arbitrary");
     648  if (!suite.add(s=="Some arbitrary string!!!"))
     649    suite.err() << "error: " << s << "\n";
     650}
  • branches/0.10-stable/yat/utility/utility.cc

    r2940 r3093  
    66  Copyright (C) 2010 Peter Johansson
    77  Copyright (C) 2012 Jari Häkkinen, Peter Johansson
     8  Copyright (C) 2013 Peter Johansson
    89
    910  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    187188
    188189
     190  void replace(std::string& str, std::string old_value, std::string new_value)
     191  {
     192    assert(old_value.size());
     193    std::string result;
     194    std::back_insert_iterator<std::string> out(result);
     195    std::string::iterator c = str.begin();
     196    while (c!=str.end()) {
     197      if (str.end() - c >= static_cast<int>(old_value.size()) &&
     198          std::equal(old_value.begin(), old_value.end(), c)) {
     199        std::copy(new_value.begin(), new_value.end(), out);
     200        c += old_value.size();
     201      }
     202      else {
     203        *out = *c;
     204        ++c;
     205      }
     206    }
     207    str.swap(result);
     208  }
     209
     210
    189211}}} // end of namespace utility, yat and thep
Note: See TracChangeset for help on using the changeset viewer.