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);
}
}
#include <msp/strings/format.h>
+#include "algorithm.h"
#include "getopt.h"
using namespace std;
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)
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)
{
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);
}
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");
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 += ']';
}
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());
}
{ 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:
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);
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);
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>
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();
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>
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
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)
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);
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;
}
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
{
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);
}
}
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);
}
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;
}
}
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;
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
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);
}
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()));
}
+#include <msp/core/algorithm.h>
#include "filemonitor.h"
#include "filemonitor_platform.h"
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
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)
*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;
}
{
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)
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())
private:
std::string path;
- std::vector<std::string::size_type> separators;
+ PositionArray separators;
public:
Path();
#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"
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++);
{
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;
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;
}
}
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;
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;
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())
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;
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);
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);
}
#include <stdexcept>
+#include <msp/core/algorithm.h>
#include "eventobject.h"
#include "poll.h"
#include "poll_platform.h"
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;
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);
}
}
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)
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;
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)
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)
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;
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;
}
while(i!=str.end())
{
- string::const_iterator j = i;
+ auto j = i;
unichar result = -1;
if(*j==033)
if(i==str.end())
return -1;
- string::const_iterator j = i;
+ auto j = i;
Kuten jis;
jis.ku = *j++-0x20;
if(i==str.end())
return -1;
- string::const_iterator j = i;
+ auto j = i;
unichar unit = decode_unit(str, i, j);
if(unit!=-1)
{
if(unit>=0xD800 && unit<=0xDBFF)
{
- string::const_iterator k = j;
+ auto k = j;
unichar unit2 = -2;
if(k!=str.end())
for(; *i&mask; mask>>=1)
++bytes;
- string::const_iterator j = i;
+ auto j = i;
unichar result = (*j++)&(mask-1);
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;
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;
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;
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;
}
if(!has_branches)
{
- for(string::const_iterator i=iter; i!=end;)
+ for(auto i=iter; i!=end;)
{
Code atom = parse_atom(expr, i, group);
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)
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;
rmin = 0;
if(*i=='{')
{
- string::const_iterator begin = i;
+ auto begin = i;
rmin = 0;
for(++i; isdigit(*i); ++i)
Regex::Code Regex::parse_brackets(const string &str, string::const_iterator &iter)
{
- string::const_iterator begin = iter;
+ auto begin = iter;
Code result;
++iter;
++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);
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)
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;
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())
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;
{
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;
struct RunContext
{
Code::const_iterator citer;
- RegMatch::GroupArray groups;
+ std::vector<RegMatch::Group> groups;
};
Code code;
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:
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);
}
}
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. */
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;
-}
-
}
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);
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)
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");
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");
}
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)
{
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;
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=='%')
{
-#include <algorithm>
+#include <msp/core/algorithm.h>
#include <msp/core/raii.h>
#include "timer.h"
#include "utils.h"
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)
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()