]> git.tdb.fi Git - libs/core.git/commitdiff
Use C++11 features with containers
authorMikko Rasa <tdb@tdb.fi>
Sun, 29 Aug 2021 12:15:50 +0000 (15:15 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 29 Aug 2021 22:42:03 +0000 (01:42 +0300)
33 files changed:
source/core/application.cpp
source/core/getopt.cpp
source/core/getopt.h
source/core/maputils.h
source/core/typeregistry.h
source/core/windows/process.cpp
source/debug/backtrace.cpp
source/debug/profiler.cpp
source/fs/dir.cpp
source/fs/filemonitor.cpp
source/fs/path.cpp
source/fs/path.h
source/fs/unix/filemonitor.cpp
source/fs/utils.cpp
source/io/eventdispatcher.cpp
source/io/poll.cpp
source/io/unix/poll.cpp
source/io/windows/poll.cpp
source/stringcodec/codec.cpp
source/stringcodec/except.cpp
source/stringcodec/iso2022jp.cpp
source/stringcodec/jisx0208.cpp
source/stringcodec/utf16.cpp
source/stringcodec/utf8.cpp
source/strings/format.cpp
source/strings/lexicalcast.cpp
source/strings/regex.cpp
source/strings/regex.h
source/strings/regmatch.cpp
source/strings/regmatch.h
source/strings/utils.cpp
source/time/datetime.cpp
source/time/timer.cpp

index 290f20b05239702c40746f9048aba993aa877b21..2c6dc967296edeba82336d724096650b4db34a94 100644 (file)
@@ -80,7 +80,7 @@ int Application::run(int argc, char **argv, void *data, void (*created_callback)
                        else
                        {
                                IO::print(IO::cerr, "  what(): %s\n", lines.front());
-                               for(vector<string>::const_iterator i=lines.begin(); ++i!=lines.end(); )
+                               for(auto i=lines.begin(); ++i!=lines.end(); )
                                        IO::print(IO::cerr, "          %s\n", *i);
                        }
                }
index 0f8886ad746a542e3a40b36ed8e7c2ddcd0c1e19..ec821e0d26aea1372c3314cc8a1a99a268d5443e 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/strings/format.h>
+#include "algorithm.h"
 #include "getopt.h"
 
 using namespace std;
@@ -13,10 +14,10 @@ GetOpt::GetOpt():
 
 GetOpt::~GetOpt()
 {
-       for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
-               delete *i;
-       for(ArgumentList::iterator i=args.begin(); i!=args.end(); ++i)
-               delete *i;
+       for(OptionImpl *i: opts)
+               delete i;
+       for(ArgumentImpl *i: args)
+               delete i;
 }
 
 GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a)
@@ -26,7 +27,7 @@ GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t,
        if(t.is_list() && a!=REQUIRED_ARG)
                throw invalid_argument("GetOpt::add_option");
 
-       for(OptionList::iterator i=opts.begin(); i!=opts.end(); )
+       for(auto i=opts.begin(); i!=opts.end(); )
        {
                if((s!=0 && (*i)->get_short()==s) || (*i)->get_long()==l)
                {
@@ -41,43 +42,43 @@ GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t,
        return *opts.back();
 }
 
-GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType a)
+GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType y)
 {
-       if(a==NO_ARG)
+       if(y==NO_ARG)
                throw invalid_argument("GetOpt::add_argument");
 
        bool have_list = false;
        bool have_optional = false;
-       for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
+       for(const ArgumentImpl *a: args)
        {
-               if((*i)->is_list_store())
+               if(a->is_list_store())
                        have_list = true;
-               else if((*i)->get_type()==OPTIONAL_ARG)
+               else if(a->get_type()==OPTIONAL_ARG)
                        have_optional = true;
        }
 
-       if(have_optional && (t.is_list() || a!=OPTIONAL_ARG))
+       if(have_optional && (t.is_list() || y!=OPTIONAL_ARG))
                throw invalid_argument("GetOpt::add_argument");
-       if(have_list && (t.is_list() || a==OPTIONAL_ARG))
+       if(have_list && (t.is_list() || y==OPTIONAL_ARG))
                throw invalid_argument("GetOpt::add_argument");
 
-       args.push_back(new ArgumentImpl(n, t, a));
+       args.push_back(new ArgumentImpl(n, t, y));
        return *args.back();
 }
 
 GetOpt::OptionImpl &GetOpt::get_option(char s)
 {
-       for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
-               if((*i)->get_short()==s)
-                       return **i;
+       auto i = find_if(opts, [s](const OptionImpl *o){ return o->get_short()==s; });
+       if(i!=opts.end())
+               return **i;
        throw usage_error(string("Unknown option -")+s);
 }
 
 GetOpt::OptionImpl &GetOpt::get_option(const string &l)
 {
-       for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
-               if((*i)->get_long()==l)
-                       return **i;
+       auto i = find_if(opts, [&l](const OptionImpl *o){ return o->get_long()==l; });
+       if(i!=opts.end())
+               return **i;
        throw usage_error(string("Unknown option --")+l);
 }
 
@@ -110,12 +111,12 @@ void GetOpt::operator()(unsigned argc, const char *const *argv)
                        args_raw.push_back(argv[i]);
 
                i = 0;
-               for(ArgumentList::const_iterator j=args.begin(); j!=args.end(); ++j)
+               for(auto j=args.begin(); j!=args.end(); ++j)
                {
                        if((*j)->is_list_store())
                        {
                                unsigned end = args_raw.size();
-                               for(ArgumentList::const_iterator k=j; ++k!=args.end(); )
+                               for(auto k=j; ++k!=args.end(); )
                                        --end;
                                if(i==end && (*j)->get_type()==REQUIRED_ARG)
                                        throw usage_error((*j)->get_name()+" is required");
@@ -213,41 +214,41 @@ string GetOpt::generate_usage(const string &argv0, bool compact) const
                result += " [options]";
        else
        {
-               for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+               for(const OptionImpl *o: opts)
                {
                        result += " [";
-                       if((*i)->get_short())
+                       if(o->get_short())
                        {
-                               result += format("-%c", (*i)->get_short());
-                               if(!(*i)->get_long().empty())
+                               result += format("-%c", o->get_short());
+                               if(!o->get_long().empty())
                                        result += '|';
-                               else if((*i)->get_arg_type()==OPTIONAL_ARG)
-                                       result += format("[%s]", (*i)->get_metavar());
-                               else if((*i)->get_arg_type()==REQUIRED_ARG)
-                                       result += format(" %s", (*i)->get_metavar());
+                               else if(o->get_arg_type()==OPTIONAL_ARG)
+                                       result += format("[%s]", o->get_metavar());
+                               else if(o->get_arg_type()==REQUIRED_ARG)
+                                       result += format(" %s", o->get_metavar());
                        }
-                       if(!(*i)->get_long().empty())
+                       if(!o->get_long().empty())
                        {
-                               result += format("--%s", (*i)->get_long());
+                               result += format("--%s", o->get_long());
 
-                               if((*i)->get_arg_type()==OPTIONAL_ARG)
-                                       result += format("[=%s]", (*i)->get_metavar());
-                               else if((*i)->get_arg_type()==REQUIRED_ARG)
-                                       result += format("=%s", (*i)->get_metavar());
+                               if(o->get_arg_type()==OPTIONAL_ARG)
+                                       result += format("[=%s]", o->get_metavar());
+                               else if(o->get_arg_type()==REQUIRED_ARG)
+                                       result += format("=%s", o->get_metavar());
                        }
                        result += ']';
                }
        }
 
-       for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
+       for(const ArgumentImpl *a: args)
        {
                result += ' ';
-               if((*i)->get_type()==OPTIONAL_ARG)
+               if(a->get_type()==OPTIONAL_ARG)
                        result += '[';
-               result += format("<%s>", (*i)->get_name());
-               if((*i)->is_list_store())
+               result += format("<%s>", a->get_name());
+               if(a->is_list_store())
                        result += " ...";
-               if((*i)->get_type()==OPTIONAL_ARG)
+               if(a->get_type()==OPTIONAL_ARG)
                        result += ']';
        }
 
@@ -256,58 +257,56 @@ string GetOpt::generate_usage(const string &argv0, bool compact) const
 
 string GetOpt::generate_help() const
 {
-       bool any_short = false;
-       for(OptionList::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
-               any_short = (*i)->get_short();
+       bool any_short = any_of(opts.begin(), opts.end(), [](const OptionImpl *o){ return o->get_short(); });
 
        string::size_type maxw = 0;
        list<string> switches;
-       for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+       for(const OptionImpl *o: opts)
        {
                string swtch;
-               if((*i)->get_short())
+               if(o->get_short())
                {
-                       swtch += format("-%c", (*i)->get_short());
-                       if(!(*i)->get_long().empty())
+                       swtch += format("-%c", o->get_short());
+                       if(!o->get_long().empty())
                                swtch += ", ";
-                       else if((*i)->get_arg_type()==OPTIONAL_ARG)
-                               swtch += format("[%s]", (*i)->get_metavar());
-                       else if((*i)->get_arg_type()==REQUIRED_ARG)
-                               swtch += format(" %s", (*i)->get_metavar());
+                       else if(o->get_arg_type()==OPTIONAL_ARG)
+                               swtch += format("[%s]", o->get_metavar());
+                       else if(o->get_arg_type()==REQUIRED_ARG)
+                               swtch += format(" %s", o->get_metavar());
                }
                else if(any_short)
                        swtch += "    ";
-               if(!(*i)->get_long().empty())
+               if(!o->get_long().empty())
                {
-                       swtch += format("--%s", (*i)->get_long());
+                       swtch += format("--%s", o->get_long());
 
-                       if((*i)->get_arg_type()==OPTIONAL_ARG)
-                               swtch += format("[=%s]", (*i)->get_metavar());
-                       else if((*i)->get_arg_type()==REQUIRED_ARG)
-                               swtch += format("=%s", (*i)->get_metavar());
+                       if(o->get_arg_type()==OPTIONAL_ARG)
+                               swtch += format("[=%s]", o->get_metavar());
+                       else if(o->get_arg_type()==REQUIRED_ARG)
+                               swtch += format("=%s", o->get_metavar());
                }
                switches.push_back(swtch);
                maxw = max(maxw, swtch.size());
        }
 
        list<string> pargs;
-       for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
+       for(const ArgumentImpl *a: args)
        {
-               string parg = format("<%s>", (*i)->get_name());
+               string parg = format("<%s>", a->get_name());
                pargs.push_back(parg);
                maxw = max(maxw, parg.size());
        }
 
        string result;
        result += "Options:\n";
-       list<string>::const_iterator j = switches.begin();
-       for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
+       auto j = switches.begin();
+       for(auto i=opts.begin(); i!=opts.end(); ++i, ++j)
                result += format("  %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
        if(!pargs.empty())
        {
                result += "\nArguments:\n";
                j = pargs.begin();
-               for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i, ++j)
+               for(auto i=args.begin(); i!=args.end(); ++i, ++j)
                        result += format("  %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
        }
 
index 3b579a664feff1caec0786de762b305647e5fe78..27ce4cf2fdb05824b3834eb0d972a7192ec3a1fd 100644 (file)
@@ -203,12 +203,9 @@ private:
                { data.push_back(lexical_cast<typename T::value_type>(a)); }
        };
 
-       typedef std::list<OptionImpl *> OptionList;
-       typedef std::list<ArgumentImpl *> ArgumentList;
-
        bool help;
-       OptionList opts;
-       ArgumentList args;
+       std::list<OptionImpl *> opts;
+       std::list<ArgumentImpl *> args;
        std::vector<std::string> args_raw;
 
 public:
index 66b4e3baf82c50d815f5bcf4ce0615729ebbf0fb..f5faa1271a2e1317523767c5e9c3e6a858792027 100644 (file)
@@ -57,7 +57,7 @@ private:
 template<typename T>
 typename T::mapped_type &get_item(T &map, const typename T::key_type &key)
 {
-       typename T::iterator i = map.find(key);
+       auto i = map.find(key);
        if(i==map.end())
                throw key_error(key);
 
@@ -67,7 +67,7 @@ typename T::mapped_type &get_item(T &map, const typename T::key_type &key)
 template<typename T>
 const typename T::mapped_type &get_item(const T &map, const typename T::key_type &key)
 {
-       typename T::const_iterator i = map.find(key);
+       auto i = map.find(key);
        if(i==map.end())
                throw key_error(key);
 
@@ -86,7 +86,7 @@ typename T::iterator insert_unique(T &map, const typename T::key_type &key, cons
        if(map.count(key))
                throw key_error(key);
 
-       return map.insert(typename T::value_type(key, item)).first;
+       return map.insert(std::make_pair(key, item)).first;
 }
 
 template<typename T>
index 27610859c75105d4b774e2d4a8e44493cb415d67..dd5dc01ba2e1e96db0e9056d7d5a9202669814cb 100644 (file)
@@ -43,9 +43,7 @@ private:
                virtual void invoke(T arg) const { action(this->keyword, arg); }
        };
 
-       typedef std::map<std::string, TypeBase *> TypeMap;
-
-       TypeMap types;
+       std::map<std::string, TypeBase *> types;
 
 public:
        ~TypeRegistry();
@@ -65,8 +63,8 @@ public:
 template<template<typename> class A, typename T>
 TypeRegistry<A, T>::~TypeRegistry()
 {
-       for(typename TypeMap::iterator i=types.begin(); i!=types.end(); ++i)
-               delete i->second;
+       for(auto &kvp: types)
+               delete kvp.second;
 }
 
 template<template<typename> class A, typename T>
@@ -88,8 +86,8 @@ void TypeRegistry<A, T>::invoke(const std::string &kw, T arg) const
 template<template<typename> class A, typename T>
 void TypeRegistry<A, T>::invoke_all(T arg) const
 {
-       for(typename TypeMap::const_iterator i=types.begin(); i!=types.end(); ++i)
-               i->second->invoke(arg);
+       for(auto &kvp: types)
+               kvp.second->invoke(arg);
 }
 
 } // namespace Msp
index 2f7b50547a4e8ff8ef493f58bcbdd90692379933..da76fe38dd3551f82dbfd3910d995a0058191108 100644 (file)
@@ -14,19 +14,19 @@ string quote_argument(const string &arg)
        string result;
        bool need_quotes = false;
        bool backslash = false;
-       for(string::const_iterator i=arg.begin(); i!=arg.end(); ++i)
+       for(char c: arg)
        {
-               if(*i=='\\')
+               if(c=='\\')
                        backslash = true;
-               else if(*i=='"')
+               else if(c=='"')
                {
                        if(backslash)
                                result += '\\';
                        result += '\\';
                }
-               else if(*i==' ')
+               else if(c==' ')
                        need_quotes = true;
-               result += *i;
+               result += c;
        }
 
        if(need_quotes)
@@ -56,8 +56,8 @@ void Process::platform_get_self_info(Private &priv)
 void Process::execute(const string &command, bool path_search, const Arguments &args)
 {
        string cmdline = quote_argument(command);
-       for(Arguments::const_iterator i=args.begin(); i!=args.end(); ++i)
-               append(cmdline, " ", quote_argument(*i));
+       for(const string &a: args)
+               append(cmdline, " ", quote_argument(a));
 
        STARTUPINFO startup;
        startup.cb = sizeof(STARTUPINFO);
index f9057dce833d95b9231917bd763a489f7a856fb1..de1f76e910611ee6dd95be1fadcda9dc727e4ff8 100644 (file)
@@ -43,9 +43,8 @@ Backtrace Backtrace::create()
 
 ostream &operator<<(ostream &out, const Backtrace &bt)
 {
-       const list<Backtrace::StackFrame> &frames = bt.get_frames();
-       for(list<Backtrace::StackFrame>::const_iterator i=frames.begin(); i!=frames.end(); ++i)
-               out<<*i<<'\n';
+       for(const Backtrace::StackFrame &f: bt.get_frames())
+               out<<f<<'\n';
 
        return out;
 }
index c4c880b70241c826d87e7db7ba3598bc8034be84..c82558b01f6e119720e7d119d5c2cf6d2ef36a65 100644 (file)
@@ -18,9 +18,9 @@ void Profiler::set_period(unsigned p)
                return;
 
        period = p;
-       for(map<string, ScopeInfo>::iterator i=scopes.begin(); i!=scopes.end(); ++i)
+       for(auto &kvp: scopes)
        {
-               ScopeInfo &si = i->second;
+               ScopeInfo &si = kvp.second;
                if(p==0)
                        si.history.clear();
                else
@@ -34,7 +34,7 @@ void Profiler::add_scope(const string &name)
 {
        if(!scopes.count(name))
        {
-               map<string, ScopeInfo>::iterator i = scopes.insert(map<string, ScopeInfo>::value_type(name, ScopeInfo())).first;
+               auto i = scopes.insert(make_pair(name, ScopeInfo())).first;
                i->second.history.resize(period);
        }
 }
@@ -48,10 +48,10 @@ ProfilingScope *Profiler::enter(ProfilingScope *ps)
 
 void Profiler::record(const ProfilingScope &scope)
 {
-       map<string, ScopeInfo>::iterator i = scopes.find(scope.get_name());
+       auto i = scopes.find(scope.get_name());
        if(i==scopes.end())
        {
-               i = scopes.insert(map<string, ScopeInfo>::value_type(scope.get_name(), ScopeInfo())).first;
+               i = scopes.insert(make_pair(scope.get_name(), ScopeInfo())).first;
                i->second.first_call = scope.get_entry_time();
                i->second.history.resize(period);
        }
index 7f81e4ea8301ef07ce7c4062bf65323de6f9dfc8..99de180d20ed026812e7fcb2e371dc843a0c333f 100644 (file)
@@ -40,11 +40,10 @@ const Path &get_bin_dir(const string &argv0)
                if(argv0.find(DIRSEP)==string::npos)
                        if(const char *path = getenv("PATH"))
                        {
-                               vector<string> dirs = split(path, ITEMSEP);
-                               for(vector<string>::const_iterator i=dirs.begin(); i!=dirs.end(); ++i)
-                                       if(exists(Path(*i)/argv0))
+                               for(const string &d: split(path, ITEMSEP))
+                                       if(exists(Path(d)/argv0))
                                        {
-                                               exe = realpath(Path(*i)/argv0);
+                                               exe = realpath(Path(d)/argv0);
                                                break;
                                        }
                        }
@@ -70,9 +69,9 @@ not_a_directory::not_a_directory(const Path &p):
 void mkpath(const Path &path, int mode)
 {
        Path p;
-       for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
+       for(const string &c: path)
        {
-               p /= *i;
+               p /= c;
 #ifdef _WIN32
                if(p.size()==1 && p.is_absolute())
                        continue;
@@ -90,10 +89,9 @@ void mkpath(const Path &path, int mode)
 
 void rmpath(const Path &path)
 {
-       list<string> files = list_files(path);
-       for(list<string>::iterator i=files.begin(); i!=files.end(); ++i)
+       for(const string &f: list_files(path))
        {
-               Path p = path / *i;
+               Path p = path/f;
                if(is_dir(p))
                        rmpath(p);
                else
@@ -181,9 +179,9 @@ Path get_sys_lib_dir()
 
 Path path_lookup(const string &name, const list<Path> &paths)
 {
-       for(list<Path>::const_iterator i=paths.begin(); i!=paths.end(); ++i)
+       for(const Path &p: paths)
        {
-               Path full = *i/name;
+               Path full = p/name;
                if(exists(full))
                        return realpath(full);
        }
@@ -194,7 +192,7 @@ Path path_lookup(const string &name, const list<Path> &paths)
 Path path_lookup(const string &name)
 {
        const char *path = getenv("PATH");
-       vector<string> dirs = split(path, ITEMSEP);
+       list<string> dirs = split(path, ITEMSEP);
        return path_lookup(name, list<Path>(dirs.begin(), dirs.end()));
 }
 
index 5cc990d2d52d6df1b6f3237db6c68b92c1879e20..5ff5b7818b59f5d6c16b4d5da042eb0ec3e197cc 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/algorithm.h>
 #include "filemonitor.h"
 #include "filemonitor_platform.h"
 
@@ -36,15 +37,14 @@ void FileMonitor::add_file(const FS::Path &path)
 
 void FileMonitor::remove_file(const FS::Path &path)
 {
-       for(vector<MonitoredFile>::iterator i=files.begin(); i!=files.end(); ++i)
-               if(i->path==path)
-               {
-                       cleanup_file(*i);
-                       if(&*i!=&files.back())
-                               *i = files.back();
-                       files.pop_back();
-                       break;
-               }
+       auto i = find_member(files, path, &MonitoredFile::path);
+       if(i!=files.end())
+       {
+               cleanup_file(*i);
+               if(&*i!=&files.back())
+                       *i = files.back();
+               files.pop_back();
+       }
 }
 
 } // namespace FS
index 353c63eca182d97ed1c5daa5e2c15bab7857665e..a5a5a10a5532133582172da6abcdf93acdf62ea6 100644 (file)
@@ -68,7 +68,7 @@ bool Path::is_absolute() const
 Path Path::subpath(unsigned start, unsigned count) const
 {
        Path result;
-       Iterator i = begin();
+       auto i = begin();
        for(unsigned j=0; (j<start && i!=end()); ++j)
                ++i;
        for(unsigned j=0; (j<count && i!=end()); ++j)
@@ -92,8 +92,8 @@ Path &Path::operator/=(const Path &p)
                *this = p;
        else
        {
-               for(Iterator i=p.begin(); i!=p.end(); ++i)
-                       add_component(*i);
+               for(const string &c: p)
+                       add_component(c);
        }
        return *this;
 }
@@ -173,13 +173,13 @@ string Path::operator[](int n) const
 {
        if(n>=0)
        {
-               for(Iterator i=begin(); i!=end(); ++i, --n)
+               for(auto i=begin(); i!=end(); ++i, --n)
                        if(!n)
                                return *i;
        }
        else
        {
-               for(Iterator i=end(); i!=begin();)
+               for(auto i=end(); i!=begin();)
                {
                        --i;
                        if(!++n)
@@ -264,10 +264,7 @@ void Path::Iterator::update()
 
        string::size_type start = 0;
        if(iter!=path->separators.begin())
-       {
-               PositionArray::const_iterator prev = iter;
-               start = *--prev+1;
-       }
+               start = *prev(iter)+1;
 
        string::size_type slash = string::npos;
        if(iter!=path->separators.end())
index 82600dc35c28d5787f69b56f0af3b8b12af50f22..e0b593caf2099839d2f517deb2a3b2b0e4e89ffb 100644 (file)
@@ -67,7 +67,7 @@ public:
 
 private:
        std::string path;
-       std::vector<std::string::size_type> separators;
+       PositionArray separators;
 
 public:
        Path();
index e2ca3b74ebcde44bcc85f562e67ea20d989074a4..11b2a8d74b1705ba4b16c38d447e7c8e9516fdb7 100644 (file)
@@ -1,5 +1,6 @@
 #include <sys/inotify.h>
 #include <linux/limits.h>
+#include <msp/core/algorithm.h>
 #include <msp/core/systemerror.h>
 #include <msp/io/handle_private.h>
 #include "filemonitor.h"
@@ -99,27 +100,26 @@ void FileMonitor::Private::events_available()
        for(unsigned i=0; i<len; )
        {
                struct inotify_event *event = reinterpret_cast<struct inotify_event *>(event_buf+i);
-               for(vector<MonitoredFile>::iterator j=monitor.files.begin(); j!=monitor.files.end(); ++j)
-                       if(j->tag==event->wd)
+               auto j = find_member(monitor.files, event->wd, &MonitoredFile::tag);
+               if(j!=monitor.files.end())
+               {
+                       if(event->mask&IN_MODIFY)
+                               j->modified = true;
+                       if(((event->mask&IN_CLOSE_WRITE) && j->modified) || (event->mask&IN_DELETE_SELF))
                        {
-                               if(event->mask&IN_MODIFY)
-                                       j->modified = true;
-                               if(((event->mask&IN_CLOSE_WRITE) && j->modified) || (event->mask&IN_DELETE_SELF))
-                               {
-                                       j->modified = false;
-                                       changed_files.push_back(j->path);
-                               }
-                               if(event->mask&IN_IGNORED)
-                                       j->tag = -1;
-                               break;
+                               j->modified = false;
+                               changed_files.push_back(j->path);
                        }
+                       if(event->mask&IN_IGNORED)
+                               j->tag = -1;
+               }
                i += sizeof(struct inotify_event)+event->len;
        }
 
-       for(vector<FS::Path>::const_iterator i=changed_files.begin(); i!=changed_files.end(); ++i)
-               monitor.signal_file_modified.emit(*i);
+       for(const FS::Path &p: changed_files)
+               monitor.signal_file_modified.emit(p);
 
-       for(vector<MonitoredFile>::iterator j=monitor.files.begin(); j!=monitor.files.end(); )
+       for(auto j=monitor.files.begin(); j!=monitor.files.end(); )
        {
                if(j->tag==-1)
                        monitor.files.erase(j++);
index a2fe40be72ee6f39eef6909e576c5703fe9787da..e15aad4d7b8b63980e45f9b514e16f22e57ec3e7 100644 (file)
@@ -43,10 +43,10 @@ Path fix_case(const Path &path)
 {
        bool found = true;
        Path result;
-       for(Path::Iterator i=path.begin(); i!=path.end(); ++i)
+       for(const string &c: path)
        {
-               if(!found || (result.empty() && (*i=="/" || *i==".")))
-                       result /= *i;
+               if(!found || (result.empty() && (c=="/" || c==".")))
+                       result /= c;
                else
                {
                        list<string> files;
@@ -56,15 +56,15 @@ Path fix_case(const Path &path)
                                files = list_files(".");
 
                        found = false;
-                       for(list<string>::iterator j=files.begin(); (j!=files.end() && !found); ++j)
-                               if(!strcasecmp(*j,*i))
+                       for(const string &f: files)
+                               if(!strcasecmp(f, c))
                                {
-                                       result /= *j;
+                                       result /= f;
                                        found = true;
                                }
 
                        if(!found)
-                               result /= *i;
+                               result /= c;
                }
        }
 
@@ -76,8 +76,8 @@ Path relative(const Path &path, const Path &base)
        if(path.is_absolute()!=base.is_absolute())
                throw invalid_argument("FS::relative");
 
-       Path::Iterator i = path.begin();
-       Path::Iterator j = base.begin();
+       auto i = path.begin();
+       auto j = base.begin();
        for(; (i!=path.end() && j!=base.end() && *i==*j); ++i, ++j) ;
 
        Path result;
@@ -91,8 +91,8 @@ Path relative(const Path &path, const Path &base)
 
 Path common_ancestor(const Path &path1, const Path &path2)
 {
-       Path::Iterator i = path1.begin();
-       Path::Iterator j = path2.begin();
+       auto i = path1.begin();
+       auto j = path2.begin();
        Path result;
        for(; (i!=path1.end() && j!=path2.end() && *i==*j); ++i, ++j)
                result /= *i;
@@ -104,8 +104,8 @@ int descendant_depth(const Path &path, const Path &parent)
        if(path.is_absolute()!=parent.is_absolute())
                throw invalid_argument("FS::descendant_depth");
 
-       Path::Iterator i = path.begin();
-       Path::Iterator j = parent.begin();
+       auto i = path.begin();
+       auto j = parent.begin();
        for(; (i!=path.end() && j!=parent.end() && *i==*j); ++i, ++j) ;
 
        if(j!=parent.end())
index 49b28ac78bf436997f74d2d8fecb740aebf47fb5..9c233ee6a2a0cae226b972179d2519a0a51fc794 100644 (file)
@@ -12,7 +12,7 @@ namespace IO {
 void EventDispatcher::add(EventObject &obj)
 {
        Slot slot(*this, obj);
-       set<Slot>::iterator i = objects.find(slot);
+       auto i = objects.find(slot);
        if(i==objects.end())
        {
                i = objects.insert(slot).first;
@@ -25,7 +25,7 @@ void EventDispatcher::add(EventObject &obj)
 
 void EventDispatcher::remove(EventObject &obj)
 {
-       set<Slot>::iterator i = objects.find(Slot(*this, obj));
+       auto i = objects.find(Slot(*this, obj));
        if(i!=objects.end())
        {
                objects.erase(i);
@@ -63,10 +63,9 @@ void EventDispatcher::tick(const Time::Timer &timer)
 
 void EventDispatcher::dispatch()
 {
-       const vector<Poller::PolledObject> &result = poller.get_result();
-       for(vector<Poller::PolledObject>::const_iterator i=result.begin(); i!=result.end(); ++i)
-               if(objects.count(Slot(*this, *i->object)))
-                       i->object->event(i->events);
+       for(const Poller::PolledObject &po: poller.get_result())
+               if(objects.count(Slot(*this, *po.object)))
+                       po.object->event(po.events);
 }
 
 
index a5d2d023c81d3f1ae730fc69d8d903b5546926db..4ca2f3d9767ea3315d172ba25dd51831afb7f660 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdexcept>
+#include <msp/core/algorithm.h>
 #include "eventobject.h"
 #include "poll.h"
 #include "poll_platform.h"
@@ -25,20 +26,20 @@ void Poller::set_object(EventObject &obj, PollEvent ev)
        if(ev)
                obj.get_event_handle();
 
-       for(vector<PolledObject>::iterator i=objects.begin(); i!=objects.end(); ++i)
-               if(i->object==&obj)
+       auto i = find_member(objects, &obj, &PolledObject::object);
+       if(i!=objects.end())
+       {
+               if(ev)
+                       i->events = ev;
+               else
                {
-                       if(ev)
-                               i->events = ev;
-                       else
-                       {
-                               *i = objects.back();
-                               objects.pop_back();
-                               objs_changed = true;
-                       }
-                       events_changed = true;
-                       return;
+                       *i = objects.back();
+                       objects.pop_back();
+                       objs_changed = true;
                }
+               events_changed = true;
+               return;
+       }
 
        if(!ev)
                return;
index 7df113e539609238e56d64bfd90662c0449c9af4..28d3aebc5acba72970c0176a2b17581d4cf168ab 100644 (file)
@@ -62,11 +62,11 @@ void Poller::rebuild_array()
                priv->pfd.clear();
                priv->pfd.reserve(objects.size());
 
-               for(vector<PolledObject>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
+               for(const PolledObject &po: objects)
                {
                        pollfd p;
-                       p.fd = *i->object->get_event_handle();
-                       p.events = sys_poll_event(i->events);
+                       p.fd = *po.object->get_event_handle();
+                       p.events = sys_poll_event(po.events);
                        priv->pfd.push_back(p);
                }
        }
index 7aafc6e1c2fcf74d6da3a1ca34777652da795489..0d644cd0a7cf6cfa3573df8444f465543a50f537 100644 (file)
@@ -18,8 +18,8 @@ void Poller::rebuild_array()
        priv->handles.clear();
        priv->handles.reserve(objects.size());
 
-       for(vector<PolledObject>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
-               priv->handles.push_back(*i->object->get_event_handle());
+       for(const PolledObject &po: objects)
+               priv->handles.push_back(*po.object->get_event_handle());
 }
 
 void Poller::platform_poll(int timeout)
index 21ddf8a7307ff8071551f4eb0473dfff48f05898..abec134b1a0fd00a25f936952368beba22c62742 100644 (file)
@@ -20,7 +20,7 @@ bool Codec::detect(const string &str) const
        Decoder *dec = create_decoder(IGNORE_ERRORS);
 
        bool result = true;
-       for(string::const_iterator i=str.begin(); (result && i!=str.end()); )
+       for(auto i=str.begin(); (result && i!=str.end()); )
                result = (dec->decode_char(str, i)!=-1);
 
        delete dec;
@@ -30,8 +30,8 @@ bool Codec::detect(const string &str) const
 
 void Codec::Encoder::encode(const ustring &str, string &buf)
 {
-       for(ustring::const_iterator i=str.begin(); i!=str.end(); ++i)
-               encode_char(*i, buf);
+       for(unichar c: str)
+               encode_char(c, buf);
 }
 
 string Codec::Encoder::encode(const ustring &str)
@@ -46,7 +46,7 @@ string Codec::Encoder::encode(const ustring &str)
 
 void Codec::Decoder::decode(const string &str, ustring &buf)
 {
-       for(string::const_iterator i=str.begin(); i!=str.end();)
+       for(auto i=str.begin(); i!=str.end();)
        {
                unichar c = decode_char(str, i);
                if(c!=-1)
@@ -111,9 +111,8 @@ Codec *detect_codec(const string &str)
        bool is_latin1 = true;
        unsigned utf8_mb = 0;
 
-       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
+       for(char c: str)
        {
-               unsigned char c = *i;
                if(c&0x80)
                {
                        is_ascii = false;
index 23ac3cd64f8f48c6992600c6641b887924f27a73..db09a02e0685afa5ab47ab18fdb9caa4948e0d24 100644 (file)
@@ -19,7 +19,7 @@ invalid_sequence::invalid_sequence(const string::const_iterator &begin, const st
 string invalid_sequence::format_sequence(const string::const_iterator &begin, const string::const_iterator &end)
 {
        string result;
-       for(string::const_iterator i=begin; i!=end; ++i)
+       for(auto i=begin; i!=end; ++i)
                append(result, " ", lexical_cast<string>(static_cast<unsigned char>(*i), Fmt().fill('0').width(2).hex().uppercase()));
        return result;
 }
index ebfe5f79acd210f61b7159900b0129ab899517a4..296c1efe1ed479bd930a8b7f768d87b9a970d010 100644 (file)
@@ -91,7 +91,7 @@ unichar Iso2022Jp::Decoder::decode_char(const string &str, string::const_iterato
 
        while(i!=str.end())
        {
-               string::const_iterator j = i;
+               auto j = i;
 
                unichar result = -1;
                if(*j==033)
index 008a5e14fbd3dd59d4f4d5ecd455045f3b1a3d51..c09782481537acd4a12a9cb342ce0997dfd20d1d 100644 (file)
@@ -32,7 +32,7 @@ unichar JisX0208::Decoder::decode_char(const string &str, string::const_iterator
        if(i==str.end())
                return -1;
 
-       string::const_iterator j = i;
+       auto j = i;
        Kuten jis;
        jis.ku = *j++-0x20;
 
index 09fb3b6f76c80a214b882bbf105405b68a5ec9a2..13d49ad4ed6250e1a9e95ee5084e20f424608796 100644 (file)
@@ -66,7 +66,7 @@ unichar Utf16::Decoder::decode_char(const string &str, string::const_iterator &i
        if(i==str.end())
                return -1;
 
-       string::const_iterator j = i;
+       auto j = i;
 
        unichar unit = decode_unit(str, i, j);
        if(unit!=-1)
@@ -97,7 +97,7 @@ unichar Utf16::Decoder::decode_char(const string &str, string::const_iterator &i
        {
                if(unit>=0xD800 && unit<=0xDBFF)
                {
-                       string::const_iterator k = j;
+                       auto k = j;
 
                        unichar unit2 = -2;
                        if(k!=str.end())
index b75b39780edfa6269ad09fa780ca9a565f574872..177386b65e7254a3624ad166f579de5bd4dcadb0 100644 (file)
@@ -59,7 +59,7 @@ unichar Utf8::Decoder::decode_char(const string &str, string::const_iterator &i)
                for(; *i&mask; mask>>=1)
                        ++bytes;
 
-               string::const_iterator j = i;
+               auto j = i;
 
                unichar result = (*j++)&(mask-1);
 
index 773eefac7fc30698bab7c8d6ebfca8fb1188de18..d2065e91cad058cd74f63fa5a34dcec45f258cde 100644 (file)
@@ -55,7 +55,7 @@ Fmt Formatter::get_conversion()
        if(pos==fmt.end())
                throw format_error("Too many arguments for format");
 
-       string::iterator i = pos;
+       auto i = pos;
        for(; i!=fmt.end(); ++i)
                if(isalpha(*i))
                        break;
index f5b3a95ee9af7ea0db2e1865b709dadf77dbf1de..2fc4be35c182f2d3dde801212580b2329df7a4b1 100644 (file)
@@ -113,7 +113,7 @@ T str_to_int(const string &s, const Fmt &f)
        if(s.empty())
                throw lexical_error("conversion of '' to integer");
 
-       string::const_iterator i = s.begin();
+       auto i = s.begin();
 
        // See if the input starts with a sign
        bool neg = false;
@@ -389,7 +389,7 @@ T str_to_flt(const string &s, const Fmt &)
        if(s.empty())
                throw lexical_error("conversion of '' to floating-point");
 
-       string::const_iterator i = s.begin();
+       auto i = s.begin();
 
        // See if the input starts with a sign
        bool neg = false;
index 68cf374dcd2544064e45ce661ddb01768f962f1d..9167c6b8a9e6ae0f75fd52206bb96efe45a9424b 100644 (file)
@@ -56,7 +56,7 @@ string bad_regex::make_where(const string &e, const string::const_iterator &i)
 Regex::Regex(const string &expr)
 {
        n_groups = 0;
-       string::const_iterator iter = expr.begin();
+       auto iter = expr.begin();
        code = compile(expr, iter, n_groups, false);
        ++n_groups;
 }
@@ -123,7 +123,7 @@ Regex::Code Regex::compile(const string &expr, string::const_iterator &iter, uns
 
        if(!has_branches)
        {
-               for(string::const_iterator i=iter; i!=end;)
+               for(auto i=iter; i!=end;)
                {
                        Code atom = parse_atom(expr, i, group);
 
@@ -158,7 +158,7 @@ Regex::Code Regex::compile(const string &expr, string::const_iterator &iter, uns
        else
        {
                list<Code> branches;
-               for(string::const_iterator i=iter;;)
+               for(auto i=iter;;)
                {
                        branches.push_back(compile(expr, i, group, true));
                        if(i==end)
@@ -169,14 +169,14 @@ Regex::Code Regex::compile(const string &expr, string::const_iterator &iter, uns
                unsigned n_branches = branches.size();
 
                Offset offset = (n_branches-1)*jump_size+branches.front().size();
-               for(list<Code>::iterator i=++branches.begin(); i!=branches.end(); ++i)
+               for(auto i=++branches.begin(); i!=branches.end(); ++i)
                {
                        result += ND_JUMP;
                        write_int<Offset>(offset, result);
                        offset += i->size();
                }
 
-               for(list<Code>::iterator i=branches.begin(); i!=branches.end();)
+               for(auto i=branches.begin(); i!=branches.end();)
                {
                        result += *i;
                        offset -= i->size()+jump_size;
@@ -258,7 +258,7 @@ bool Regex::parse_repeat(const string &expr, string::const_iterator &i, Count &r
                rmin = 0;
        if(*i=='{')
        {
-               string::const_iterator begin = i;
+               auto begin = i;
 
                rmin = 0;
                for(++i; isdigit(*i); ++i)
@@ -291,7 +291,7 @@ bool Regex::parse_repeat(const string &expr, string::const_iterator &i, Count &r
 
 Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &iter)
 {
-       string::const_iterator begin = iter;
+       auto begin = iter;
        Code result;
 
        ++iter;
@@ -302,7 +302,7 @@ Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &ite
                ++iter;
        }
 
-       string::const_iterator end = iter;
+       auto end = iter;
        for(; (end!=str.end() && (end==iter || *end!=']')); ++end) ;
        if(end==str.end())
                throw bad_regex("unmatched '['", str, begin);
@@ -311,7 +311,7 @@ Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &ite
        unsigned type = 0;
        bool range = false;
        unsigned char first = 0, last = 0;
-       for(string::const_iterator i=iter; i!=end; ++i)
+       for(auto i=iter; i!=end; ++i)
        {
                unsigned char c = *i;
                if(range)
@@ -364,16 +364,16 @@ Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &ite
 
 RegMatch Regex::match(const string &str) const
 {
-       RegMatch::GroupArray groups(n_groups);
+       vector<RegMatch::Group> groups(n_groups);
 
-       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
+       for(auto i=str.begin(); i!=str.end(); ++i)
                if(run(str, i, groups))
                        return RegMatch(str, groups);
 
        return RegMatch();
 }
 
-bool Regex::run(const string &str, const string::const_iterator &begin, RegMatch::GroupArray &groups) const
+bool Regex::run(const string &str, const string::const_iterator &begin, vector<RegMatch::Group> &groups) const
 {
        bool result = false;
        list<RunContext> ctx;
@@ -381,7 +381,7 @@ bool Regex::run(const string &str, const string::const_iterator &begin, RegMatch
        ctx.front().citer = code.begin();
        ctx.front().groups.resize(groups.size());
 
-       for(string::const_iterator i=begin;;)
+       for(auto i=begin;;)
        {
                int c;
                if(i!=str.end())
@@ -389,7 +389,7 @@ bool Regex::run(const string &str, const string::const_iterator &begin, RegMatch
                else
                        c = -1;
 
-               for(list<RunContext>::iterator j=ctx.begin(); j!=ctx.end();)
+               for(auto j=ctx.begin(); j!=ctx.end();)
                {
                        bool terminate = false;
                        bool negate_match = false;
@@ -524,9 +524,9 @@ string Regex::disassemble() const
 {
        string result;
 
-       for(Code::const_iterator i=code.begin(); i!=code.end();)
+       for(auto i=code.begin(); i!=code.end();)
        {
-               Code::const_iterator j = i;
+               auto j = i;
                Offset offset = i-code.begin();
                string decompiled = disassemble_instruction(i);
                string bytes;
index 67925023e544e76cd3f594addd0693d2b860d2c4..094a295cdb77bc230dd0c79254a4459f1567d945 100644 (file)
@@ -103,7 +103,7 @@ private:
        struct RunContext
        {
                Code::const_iterator citer;
-               RegMatch::GroupArray groups;
+               std::vector<RegMatch::Group> groups;
        };
 
        Code code;
@@ -128,7 +128,7 @@ public:
        RegMatch match(const std::string &str) const;
 
 private:
-       bool run(const std::string &, const std::string::const_iterator &, RegMatch::GroupArray &) const;
+       bool run(const std::string &, const std::string::const_iterator &, std::vector<RegMatch::Group> &) const;
        bool group_compare(const RegMatch::Group &, const RegMatch::Group &) const;
 
 public:
index 4799cfbd4e536b1634ba48b5d5bf625e8da06eb6..0e702b5e9568c5a3e5a2dd9a3fc6b49dce1d402a 100644 (file)
@@ -5,14 +5,14 @@ using namespace std;
 
 namespace Msp {
 
-RegMatch::RegMatch(const string &str, const GroupArray &g):
-       groups(g)
+RegMatch::RegMatch(const string &str, const vector<Group> &grps):
+       groups(grps)
 {
-       for(GroupArray::iterator i=groups.begin(); i!=groups.end(); ++i)
-               if(i->match)
+       for(Group &g: groups)
+               if(g.match)
                {
-                       i->length = i->end-i->begin;
-                       i->str = str.substr(i->begin, i->length);
+                       g.length = g.end-g.begin;
+                       g.str = str.substr(g.begin, g.length);
                }
 }
 
index c5116cdc5408ec7238c65f6a250feb9bc23f8308..b79104bad530f9f6846791e97cda7771f9252d37 100644 (file)
@@ -36,10 +36,8 @@ public:
                operator bool() const { return match; }
        };
 
-       typedef std::vector<Group> GroupArray;
-
 private:
-       GroupArray groups;
+       std::vector<Group> groups;
 
 public:
        /** Constructs a RegMatch representing a non-match. */
index 1b4cad080dee85950740b58de822484be2c2214e..6b659d0bd46a30a23f1a719dad1e79aa940a2678 100644 (file)
@@ -40,14 +40,6 @@ vector<string> do_split(const string &str, const string &sep, int max_split)
        return result;
 }
 
-bool check_str(const string &str, int (*pred)(int))
-{
-       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
-               if(!pred(*i))
-                       return false;
-       return true;
-}
-
 }
 
 
@@ -55,8 +47,8 @@ namespace Msp {
 
 int strcasecmp(const string &s1, const string &s2)
 {
-       string::const_iterator i1 = s1.begin();
-       string::const_iterator i2 = s2.begin();
+       auto i1 = s1.begin();
+       auto i2 = s2.begin();
        for(; (i1!=s1.end() && i2!=s2.end()); ++i1, ++i2)
        {
                const char c1 = ::tolower(*i1);
@@ -71,30 +63,30 @@ int strcasecmp(const string &s1, const string &s2)
 string tolower(const string &str)
 {
        string result(str);
-       transform(result.begin(), result.end(), result.begin(), ::tolower);
+       transform(result.begin(), result.end(), result.begin(), [](char c){ return std::tolower(c); });
        return result;
 }
 
 string toupper(const string &str)
 {
        string result(str);
-       transform(result.begin(), result.end(), result.begin(), ::toupper);
+       transform(result.begin(), result.end(), result.begin(), [](char c){ return std::toupper(c); });
        return result;
 }
 
 bool isnumrc(const string &str)
 {
-       return check_str(str, isdigit);
+       return all_of(str.begin(), str.end(), [](char c){ return std::isdigit(c); });
 }
 
 bool isalpha(const string &str)
 {
-       return check_str(str, isalpha);
+       return all_of(str.begin(), str.end(), [](char c){ return std::isalpha(c); });
 }
 
 bool isalnum(const string &str)
 {
-       return check_str(str, isalnum);
+       return all_of(str.begin(), str.end(), [](char c){ return std::isalnum(c); });
 }
 
 vector<string> split(const string &str, const string &sep, int max_split)
@@ -151,17 +143,17 @@ string c_unescape(const string &str)
        unsigned numeric_pos = 0;
        unsigned numeric_value = 0;
        string result;
-       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
+       for(auto c: str)
        {
                if(numeric_type==16)
                {
                        unsigned digit = 0;
-                       if(*i>='0' && *i<='9')
-                               digit = *i-'0';
-                       else if(*i>='a' && *i<='f')
-                               digit = *i-'a'+10;
-                       else if(*i>='A' && *i<='F')
-                               digit = *i-'A'+10;
+                       if(c>='0' && c<='9')
+                               digit = c-'0';
+                       else if(c>='a' && c<='f')
+                               digit = c-'a'+10;
+                       else if(c>='A' && c<='F')
+                               digit = c-'A'+10;
                        else
                                throw invalid_argument("c_unescape");
 
@@ -176,8 +168,8 @@ string c_unescape(const string &str)
                else if(numeric_type==8)
                {
                        unsigned digit = 0;
-                       if(*i>='0' && *i<='7')
-                               digit = *i-'0';
+                       if(c>='0' && c<='7')
+                               digit = c-'0';
                        else
                                throw invalid_argument("c_unescape");
 
@@ -191,47 +183,47 @@ string c_unescape(const string &str)
                }
                else if(escape)
                {
-                       if(*i=='x')
+                       if(c=='x')
                        {
                                numeric_type = 16;
                                numeric_pos = 0;
                                numeric_value = 0;
                        }
-                       else if(*i>='0' && *i<='3')
+                       else if(c>='0' && c<='3')
                        {
                                numeric_type = 8;
                                numeric_pos = 1;
-                               numeric_value = *i-'0';
+                               numeric_value = c-'0';
                        }
-                       else if(*i=='n')
+                       else if(c=='n')
                                result += '\n';
-                       else if(*i=='t')
+                       else if(c=='t')
                                result += '\t';
-                       else if(*i=='r')
+                       else if(c=='r')
                                result += '\r';
-                       else if(*i=='b')
+                       else if(c=='b')
                                result += '\b';
-                       else if(*i=='v')
+                       else if(c=='v')
                                result += '\v';
-                       else if(*i=='a')
+                       else if(c=='a')
                                result += '\a';
-                       else if(*i=='f')
+                       else if(c=='f')
                                result += '\f';
-                       else if(*i=='\"')
+                       else if(c=='\"')
                                result += '\"';
-                       else if(*i=='\'')
+                       else if(c=='\'')
                                result += '\'';
-                       else if(*i=='\\')
+                       else if(c=='\\')
                                result += '\\';
                        else
                                throw invalid_argument("c_unescape");
 
                        escape = false;
                }
-               else if(*i=='\\')
+               else if(c=='\\')
                        escape = true;
                else
-                       result += *i;
+                       result += c;
        }
 
        if(escape)      
@@ -244,37 +236,37 @@ string c_escape(const string &str, bool escape_8bit)
 {
        string result;
 
-       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
+       for(char c: str)
        {
-               if(*i=='\n')
+               if(c=='\n')
                        result += "\\n";
-               else if(*i=='\t')
+               else if(c=='\t')
                        result += "\\t";
-               else if(*i=='\r')
+               else if(c=='\r')
                        result += "\\r";
-               else if(*i=='\b')
+               else if(c=='\b')
                        result += "\\b";
-               else if(*i=='\v')
+               else if(c=='\v')
                        result += "\\v";
-               else if(*i=='\a')
+               else if(c=='\a')
                        result += "\\a";
-               else if(*i=='\f')
+               else if(c=='\f')
                        result += "\\f";
-               else if(*i=='\"')
+               else if(c=='\"')
                        result += "\\\"";
-               else if(*i=='\'')
+               else if(c=='\'')
                        result += "\\\'";
-               else if(*i=='\\')
+               else if(c=='\\')
                        result += "\\\\";
-               else if(static_cast<unsigned char>(*i)<' ' || (escape_8bit && (*i&0x80)))
+               else if(static_cast<unsigned char>(c)<' ' || (escape_8bit && (c&0x80)))
                {
                        char buf[4] = { '\\', 0 };
                        for(unsigned j=0; j<3; ++j)
-                               buf[1+j] = '0'+((static_cast<unsigned char>(*i)>>(6-j*3))&7);
+                               buf[1+j] = '0'+((static_cast<unsigned char>(c)>>(6-j*3))&7);
                        result.append(buf, 4);
                }
                else
-                       result += *i;
+                       result += c;
        }
 
        return result;
index e8170cc0246d26393b34d2d102d7005aa6d91e2e..e1abcf7da81aade456c90ce6f438fd61ea2958c7 100644 (file)
@@ -288,7 +288,7 @@ TimeStamp DateTime::get_timestamp() const
 string DateTime::format(const string &fmt) const
 {
        string result;
-       for(string::const_iterator i=fmt.begin(); i!=fmt.end(); ++i)
+       for(auto i=fmt.begin(); i!=fmt.end(); ++i)
        {
                if(*i=='%')
                {
index c15cd28eaed1752f6cc3c904ba57d2171dd030c5..9411facc5d08bf667d73373e03eba43d0acd350c 100644 (file)
@@ -1,4 +1,4 @@
-#include <algorithm>
+#include <msp/core/algorithm.h>
 #include <msp/core/raii.h>
 #include "timer.h"
 #include "utils.h"
@@ -15,8 +15,8 @@ Timer::Timer():
 
 Timer::~Timer()
 {
-       for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
-               delete i->slot;
+       for(const SlotProxy &s: slots)
+               delete s.slot;
 }
 
 Timer::Slot &Timer::add(const TimeDelta &td)
@@ -44,14 +44,13 @@ Timer::Slot &Timer::add(const TimeStamp &ts)
 void Timer::cancel(Slot &slot)
 {
        MutexLock l(mutex);
-       for(vector<SlotProxy>::iterator i=slots.begin(); i!=slots.end(); ++i)
-               if(i->slot==&slot)
-               {
-                       delete i->slot;
-                       slots.erase(i);
-                       make_heap(slots.begin(), slots.end());
-                       return;
-               }
+       auto i = find_member(slots, &slot, &SlotProxy::slot);
+       if(i!=slots.end())
+       {
+               delete i->slot;
+               slots.erase(i);
+               make_heap(slots.begin(), slots.end());
+       }
 }
 
 void Timer::tick()