From 256b44a5009467171af53316141277027bcc0ba4 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 29 Aug 2021 21:29:32 +0300 Subject: [PATCH] Use C++11 features to manipulate containers --- source/binaryparser.cpp | 4 +- source/binarywriter.cpp | 30 +++++++-------- source/builtinsource.cpp | 10 ++--- source/builtinsource.h | 4 +- source/collection.cpp | 75 ++++++++++++++++++-------------------- source/collection.h | 56 ++++++++++++++-------------- source/directorysource.cpp | 15 +++----- source/jsonparser.cpp | 2 +- source/loader.cpp | 24 ++++++------ source/loaderaction.h | 4 +- source/packsource.cpp | 72 ++++++++++++++++++------------------ source/statement.cpp | 8 ++-- source/textparser.cpp | 20 +++++----- source/textwriter.cpp | 26 ++++++------- tool/compiler.cpp | 9 ++--- 15 files changed, 172 insertions(+), 187 deletions(-) diff --git a/source/binaryparser.cpp b/source/binaryparser.cpp index 4ed2877..e4282ae 100644 --- a/source/binaryparser.cpp +++ b/source/binaryparser.cpp @@ -91,8 +91,8 @@ void BinaryParser::process_control_statement(const Statement &st) const string &kw = st.args[1].get(); const string &args = st.args[2].get(); - for(string::const_iterator i=args.begin(); i!=args.end(); ++i) - for(unsigned j=0; valid_signatures[j]!=*i; ++j) + for(char c: args) + for(unsigned j=0; valid_signatures[j]!=c; ++j) if(!valid_signatures[j]) throw bad_definition("__kwd"); diff --git a/source/binarywriter.cpp b/source/binarywriter.cpp index 0fbf8ce..be54844 100644 --- a/source/binarywriter.cpp +++ b/source/binarywriter.cpp @@ -43,19 +43,19 @@ void BinaryWriter::write_(const Statement &st) int id = get_item(dict, StatementKey(st.keyword, st.get_signature())); write_int(id); - for(Statement::Arguments::const_iterator j = st.args.begin(); j!=st.args.end(); ++j) - switch(j->get_signature()) + for(const Value &a: st.args) + switch(a.get_signature()) { - case IntType::signature: write_int (j->get()); break; - case StringType::signature: write_string(j->get()); break; - case BoolType::signature: write_int (j->get()); break; - case FloatType::signature: write_float (j->get()); break; - case SymbolType::signature: write_symbol(j->get()); break; + case IntType::signature: write_int (a.get()); break; + case StringType::signature: write_string(a.get()); break; + case BoolType::signature: write_int (a.get()); break; + case FloatType::signature: write_float (a.get()); break; + case SymbolType::signature: write_symbol(a.get()); break; } write_int(st.sub.size()); - for(list::const_iterator j = st.sub.begin(); j!=st.sub.end(); ++j) - write(*j); + for(const Statement &s: st.sub) + write(s); } void BinaryWriter::collect_keywords(const Statement &st) @@ -74,15 +74,15 @@ void BinaryWriter::collect_keywords(const Statement &st) dict[key] = next_kwd_id++; } - for(vector::const_iterator i = st.args.begin(); i!=st.args.end(); ++i) + for(const Value &a: st.args) { - char sig = i->get_signature(); + char sig = a.get_signature(); string str; if(sig==SymbolType::signature) - str = i->get().name; + str = a.get().name; else if(sig==StringType::signature) { - str = i->get(); + str = a.get(); if(str.size()>32) continue; } @@ -101,8 +101,8 @@ void BinaryWriter::collect_keywords(const Statement &st) strings[str] = next_str_id++; } - for(list::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i) - collect_keywords(*i); + for(const Statement &s: st.sub) + collect_keywords(s); } void BinaryWriter::write_int(IntType::Store n) diff --git a/source/builtinsource.cpp b/source/builtinsource.cpp index bdccd77..65b2718 100644 --- a/source/builtinsource.cpp +++ b/source/builtinsource.cpp @@ -26,15 +26,15 @@ bool BuiltinSource::is_loadable(const CollectionItemTypeBase &, const string &na CollectionSource::NameList BuiltinSource::get_names(const CollectionItemTypeBase &type) const { NameList names; - for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i) - if(type.match_name(i->first)) - names.push_back(i->first); + for(const auto &kvp: objects) + if(type.match_name(kvp.first)) + names.push_back(kvp.first); return names; } void BuiltinSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const { - ObjectMap::const_iterator i = objects.find(name); + auto i = objects.find(name); if(i!=objects.end()) { IO::Memory in(i->second.data, i->second.size); @@ -45,7 +45,7 @@ void BuiltinSource::load(Collection &coll, const CollectionItemTypeBase &type, c IO::Seekable *BuiltinSource::open(const string &name) const { - ObjectMap::const_iterator i = objects.find(name); + auto i = objects.find(name); if(i!=objects.end()) return new IO::Memory(i->second.data, i->second.size); diff --git a/source/builtinsource.h b/source/builtinsource.h index 244191c..ec53963 100644 --- a/source/builtinsource.h +++ b/source/builtinsource.h @@ -18,9 +18,7 @@ private: Object(const char *, unsigned); }; - typedef std::map ObjectMap; - - ObjectMap objects; + std::map objects; public: void add_object(const std::string &, const char *, unsigned); diff --git a/source/collection.cpp b/source/collection.cpp index f86adf6..c168d8a 100644 --- a/source/collection.cpp +++ b/source/collection.cpp @@ -1,4 +1,5 @@ #include +#include #include "collection.h" using namespace std; @@ -12,8 +13,8 @@ Collection::Collection(): Collection::~Collection() { - for(TypeList::iterator i = types.begin(); i!=types.end(); ++i) - delete *i; + for(CollectionItemTypeBase *t: types) + delete t; } void Collection::add_var(const string &name, const CollectionItemTypeBase *type, const Variant &var) @@ -46,7 +47,7 @@ const Variant *Collection::find_var(const string &name, const CollectionItemType type->create_item(*this, name); loaded = items.count(name); } - for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j) + for(auto j=sources.begin(); (!loaded && j!=sources.end()); ++j) { (*j)->load(*this, *type, name); loaded = items.count(name); @@ -63,13 +64,13 @@ const Variant *Collection::find_var(const string &name, const CollectionItemType void Collection::gather_items(vector *vars, list *names, const CollectionItemTypeBase &type, bool include_sources) const { - for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i) - if(type.check_item_type(i->second)) + for(const auto &kvp: items) + if(type.check_item_type(kvp.second)) { if(vars) - vars->push_back(&i->second); + vars->push_back(&kvp.second); if(names) - names->push_back(i->first); + names->push_back(kvp.first); } if(include_sources && names) @@ -81,9 +82,9 @@ unsigned Collection::get_status(const string &name, const CollectionItemTypeBase ItemMap::const_iterator i = items.find(name); if(i==items.end()) { - for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j) - if((*j)->is_loadable(type, name)) - return 2; + auto j = find_if(sources, [&name, &type](const CollectionSource *s){ return s->is_loadable(type, name); }); + if(j!=sources.end()) + return 2; if(fallback) if(CollectionItemTypeBase *fb_type = fallback->get_type(type)) return fallback->get_status(name, *fb_type); @@ -95,17 +96,17 @@ unsigned Collection::get_status(const string &name, const CollectionItemTypeBase CollectionItemTypeBase *Collection::get_type(const CollectionItemTypeBase &type) const { - for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j) - if((*j)->is_same_type(type)) - return *j; + for(CollectionItemTypeBase *t: types) + if(t->is_same_type(type)) + return t; return 0; } CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const { - for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i) - if((*i)->check_item_type(var)) - return *i; + for(CollectionItemTypeBase *t: types) + if(t->check_item_type(var)) + return t; return 0; } @@ -116,8 +117,8 @@ void Collection::add_source(const CollectionSource &s) IO::Seekable *Collection::open_raw(const string &name) const { - for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i) - if(IO::Seekable *io = (*i)->open(name)) + for(const CollectionSource *s: sources) + if(IO::Seekable *io = s->open(name)) return io; return 0; @@ -126,34 +127,28 @@ IO::Seekable *Collection::open_raw(const string &name) const void Collection::gather_names_from_sources(list &names, const CollectionItemTypeBase &type) const { set new_names; - for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i) - { - std::list available_names = (*i)->get_names(type); - for(std::list::iterator j=available_names.begin(); j!=available_names.end(); ++j) - if(!items.count(*j)) - new_names.insert(*j); - } + for(const CollectionSource *s: sources) + for(const string &n: s->get_names(type)) + if(!items.count(n)) + new_names.insert(n); names.insert(names.end(), new_names.begin(), new_names.end()); } void Collection::load_items_from_sources(const CollectionItemTypeBase &type) { - for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i) - { - std::list available_names = (*i)->get_names(type); - for(std::list::iterator j=available_names.begin(); j!=available_names.end(); ++j) - if(!items.count(*j)) + for(const CollectionSource *s: sources) + for(const string &n: s->get_names(type)) + if(!items.count(n)) { bool loaded = false; if(type.can_create()) { - type.create_item(*this, *j); - loaded = items.count(*j); + type.create_item(*this, n); + loaded = items.count(n); } if(!loaded) - (*i)->load(*this, type, *j); + s->load(*this, type, n); } - } } void Collection::set_fallback(Collection *f) @@ -165,15 +160,15 @@ void Collection::set_fallback(Collection *f) Collection::Loader::Loader(Collection &c): coll(c) { - for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i) - (*i)->add_to_loader(*this); + for(const CollectionItemTypeBase *t: coll.types) + t->add_to_loader(*this); } CollectionItemTypeBase::~CollectionItemTypeBase() { - for(vector::iterator i=extractors.begin(); i!=extractors.end(); ++i) - delete *i; + for(ExtractorBase *e: extractors) + delete e; } void CollectionItemTypeBase::set_keyword(const string &k) @@ -190,8 +185,8 @@ void CollectionItemTypeBase::add_suffix(const string &s) bool CollectionItemTypeBase::match_name(const string &name) const { - for(vector::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i) - if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i)) + for(const string &s: suffixes) + if(name.size()>s.size() && !name.compare(name.size()-s.size(), string::npos, s)) return true; return false; } diff --git a/source/collection.h b/source/collection.h index 473b58e..98b359e 100644 --- a/source/collection.h +++ b/source/collection.h @@ -85,12 +85,10 @@ public: private: typedef std::map ItemMap; - typedef std::vector TypeList; - typedef std::vector SourceList; - TypeList types; + std::vector types; ItemMap items; - SourceList sources; + std::vector sources; Collection *fallback; public: @@ -166,8 +164,8 @@ private: std::list extract_list(const std::vector &vars) const { std::list result; - for(std::vector::const_iterator i=vars.begin(); i!=vars.end(); ++i) - result.push_back(&extract(**i)); + for(const Variant *v: vars) + result.push_back(&extract(*v)); return result; } @@ -257,10 +255,10 @@ public: { typedef RefPtr::type> RPNCT; - for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i) - if(i->second.check_type()) - if(i->second.value().get()==d) - return i->first; + for(const auto &kvp: items) + if(kvp.second.check_type()) + if(kvp.second.value().get()==d) + return kvp.first; // XXX Need better exception class throw std::runtime_error("Item not found in collection"); @@ -371,8 +369,8 @@ public: template bool can_extract() const { - for(std::vector::const_iterator i=extractors.begin(); i!=extractors.end(); ++i) - if(dynamic_cast *>(*i)) + for(ExtractorBase *e: extractors) + if(dynamic_cast *>(e)) return true; return false; } @@ -380,8 +378,8 @@ public: template T *extract(const Variant &var) const { - for(std::vector::const_iterator i=extractors.begin(); i!=extractors.end(); ++i) - if(Extractor *ex = dynamic_cast *>(*i)) + for(ExtractorBase *e: extractors) + if(Extractor *ex = dynamic_cast *>(e)) return &ex->extract(var); return 0; } @@ -454,8 +452,8 @@ public: ~CollectionItemType() { delete creat; - for(typename std::vector::const_iterator i=notif.begin(); i!=notif.end(); ++i) - delete *i; + for(NotifyeeBase *n: notif) + delete n; } /** Sets a datafile keyword for this item type. The Collection's loader @@ -535,8 +533,8 @@ public: virtual void notify_item(Collection &coll, const std::string &name, const Variant &var) const { RefPtr obj = var.value >(); - for(typename std::vector::const_iterator i=notif.begin(); i!=notif.end(); ++i) - (*i)->notify(coll, name, *obj); + for(NotifyeeBase *n: notif) + n->notify(coll, name, *obj); } }; @@ -581,9 +579,9 @@ typename CollectionItemTypeChooser::Type &Collection::add_type() template typename CollectionItemTypeChooser::Type &Collection::modify_type() { - for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j) - if(CollectionItemType *t = dynamic_cast *>(*j)) - return *t; + for(CollectionItemTypeBase *t: types) + if(CollectionItemType *tt = dynamic_cast *>(t)) + return *tt; throw std::logic_error("type not found in collection"); } @@ -591,16 +589,16 @@ typename CollectionItemTypeChooser::Type &Collection::modify_type() template CollectionItemTypeBase *Collection::get_type(const std::string &name) const { - for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j) - if(dynamic_cast *>(*j)) - return *j; + for(CollectionItemTypeBase *t: types) + if(dynamic_cast *>(t)) + return t; CollectionItemTypeBase *type = 0; - for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j) - if((*j)->can_extract()) + for(CollectionItemTypeBase *t: types) + if(t->can_extract()) { - if(!name.empty() && (*j)->match_name(name)) - return *j; - type = *j; + if(!name.empty() && t->match_name(name)) + return t; + type = t; } return type; } diff --git a/source/directorysource.cpp b/source/directorysource.cpp index 207f30d..e9f95fc 100644 --- a/source/directorysource.cpp +++ b/source/directorysource.cpp @@ -10,12 +10,9 @@ namespace DataFile { void DirectorySource::add_directory(const FS::Path &d, bool replace) { - list files = FS::list_files(d); - for(list::const_iterator i=files.begin(); i!=files.end(); ++i) - { - if(!objects.count(*i) || replace) - objects[*i] = d / *i; - } + for(const string &f: FS::list_files(d)) + if(!objects.count(f) || replace) + objects[f] = d/f; } bool DirectorySource::is_loadable(const CollectionItemTypeBase &, const string &name) const @@ -26,9 +23,9 @@ bool DirectorySource::is_loadable(const CollectionItemTypeBase &, const string & CollectionSource::NameList DirectorySource::get_names(const CollectionItemTypeBase &type) const { NameList names; - for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i) - if(type.match_name(i->first)) - names.push_back(i->first); + for(const auto &kvp: objects) + if(type.match_name(kvp.first)) + names.push_back(kvp.first); return names; } diff --git a/source/jsonparser.cpp b/source/jsonparser.cpp index 4b93703..44c7b06 100644 --- a/source/jsonparser.cpp +++ b/source/jsonparser.cpp @@ -312,7 +312,7 @@ string JsonParser::unescape(const string &str) StringCodec::Utf8::Encoder enc; bool escape = false; - for(string::const_iterator i=str.begin(); i!=str.end(); ) + for(auto i=str.begin(); i!=str.end(); ) { StringCodec::unichar c = dec.decode_char(str, i); diff --git a/source/loader.cpp b/source/loader.cpp index 3484799..98a0234 100644 --- a/source/loader.cpp +++ b/source/loader.cpp @@ -24,7 +24,7 @@ int signature_match(const string &st_sig, const string &act_sig) else if(act_sig.size()==2 && act_sig[1]=='*') { int match = 3; - for(string::const_iterator i=st_sig.begin(); (i!=st_sig.end() && match); ++i) + for(auto i=st_sig.begin(); (i!=st_sig.end() && match); ++i) match = min(match, signature_match(*i, act_sig[0])); return match; @@ -79,8 +79,8 @@ void Loader::load(const Statement &st) if(!actions) throw logic_error("no actions"); - for(list::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i) - load_statement(*i); + for(const Statement &s: st.sub) + load_statement(s); finish(); } @@ -117,9 +117,9 @@ void Loader::load_statement(const Statement &st) if(!aux_loaders.empty() && !has_action(key)) { - for(vector::const_iterator i=aux_loaders.begin(); i!=aux_loaders.end(); ++i) - if((*i)->has_action(key)) - return (*i)->load_statement(st); + for(Loader *l: aux_loaders) + if(l->has_action(key)) + return l->load_statement(st); } LoaderAction *act = find_action(key); @@ -196,7 +196,7 @@ bool Loader::has_action(const StatementKey &key) const if(!actions) return false; - ActionMap::const_iterator i = actions->lower_bound(StatementKey(key.keyword, string())); + auto i = actions->lower_bound(StatementKey(key.keyword, string())); for(; (i!=actions->end() && i->first.keyword==key.keyword); ++i) if(signature_match(key.signature, i->first.signature)) return true; @@ -208,15 +208,15 @@ LoaderAction *Loader::find_action(const StatementKey &key) const if(!actions) return 0; - ActionMap::const_iterator begin = actions->lower_bound(StatementKey(key.keyword, string())); - ActionMap::const_iterator end = actions->upper_bound(StatementKey(key.keyword, "~")); + auto begin = actions->lower_bound(StatementKey(key.keyword, string())); + auto end = actions->upper_bound(StatementKey(key.keyword, "~")); if(begin==end) throw unknown_keyword(key.keyword); LoaderAction *act = 0; int match = 0; - for(ActionMap::const_iterator i=begin; i!=end; ++i) + for(auto i=begin; i!=end; ++i) { int m = signature_match(key.signature, i->first.signature); if(m>match) @@ -249,8 +249,8 @@ const string &Loader::get_keyword() const Loader::ActionMap::~ActionMap() { - for(iterator i=begin(); i!=end(); ++i) - delete i->second; + for(const auto &kvp: *this) + delete kvp.second; } } // namespace DataFile diff --git a/source/loaderaction.h b/source/loaderaction.h index 6931f48..4666687 100644 --- a/source/loaderaction.h +++ b/source/loaderaction.h @@ -113,8 +113,8 @@ public: { std::vector values; values.reserve(st.args.size()); - for(Statement::Arguments::const_iterator i=st.args.begin(); i!=st.args.end(); ++i) - values.push_back(i->get()); + for(const Value &a: st.args) + values.push_back(a.get()); (dynamic_cast(l).*func)(values); } diff --git a/source/packsource.cpp b/source/packsource.cpp index b9f6016..b2fb17a 100644 --- a/source/packsource.cpp +++ b/source/packsource.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -65,44 +66,41 @@ void PackSource::add_pack_io(IO::Seekable &io, const string &fn, const string &f void PackSource::add_pack(IO::Seekable *io, const string &fn, const string &filter) { - Pack *pack = 0; - for(list::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i) - if(i->get_filename()==fn || (io && i->get_io()==io)) - pack = &*i; - if(!pack) + auto i = find_if(packs, [io, &fn](const Pack &p){ return p.get_filename()==fn || (io && p.get_io()==io); }); + if(i==packs.end()) { packs.push_back(Pack(io, fn)); - pack = &packs.back(); + i = prev(packs.end()); if(io) { DataFile::Parser parser(*io, fn); - Pack::Loader loader(*pack); + Pack::Loader loader(*i); loader.load(parser); } else - DataFile::load(*pack, fn); + DataFile::load(*i, fn); } FileMap pack_files; - pack->collect_files(pack_files, filter); - for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i) + i->collect_files(pack_files, filter); + for(const auto &kvp: pack_files) { - files[i->first] = i->second; - i->second->collect_objects(objects); + files[kvp.first] = kvp.second; + kvp.second->collect_objects(objects); } } list PackSource::list_files() const { list result; - for(FileMap::const_iterator i=files.begin(); i!=files.end(); ++i) - result.push_back(i->second->get_info()); + for(const auto &kvp: files) + result.push_back(kvp.second->get_info()); return result; } bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const { - ObjectMap::const_iterator i = objects.find(name); + auto i = objects.find(name); if(i==objects.end()) return false; @@ -116,17 +114,17 @@ bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &n CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &type) const { NameList names; - for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i) + for(const auto &kvp: objects) { - if(!i->second->get_keyword().empty()) + if(!kvp.second->get_keyword().empty()) { - if(i->second->get_keyword()!=type.get_keyword()) + if(kvp.second->get_keyword()!=type.get_keyword()) continue; } - else if(!type.match_name(i->first)) + else if(!type.match_name(kvp.first)) continue; - names.push_back(i->first); + names.push_back(kvp.first); } return names; @@ -134,7 +132,7 @@ CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &t void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const { - ObjectMap::const_iterator i = objects.find(name); + auto i = objects.find(name); if(i==objects.end()) return; @@ -153,7 +151,7 @@ void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, cons IO::Seekable *PackSource::open(const string &fn) const { - FileMap::const_iterator i = files.find(fn); + auto i = files.find(fn); if(i!=files.end()) return i->second->open().release(); @@ -172,31 +170,31 @@ PackSource::Pack::Pack(const Pack &other): io(other.io), base_offset(other.base_offset) { - for(list::const_iterator i=other.files.begin(); i!=other.files.end(); ++i) - files.push_back(File(*i, *this)); + for(const File &f: other.files) + files.push_back(File(f, *this)); } void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const { if(filter.empty()) { - for(list::const_iterator i=files.begin(); i!=files.end(); ++i) - fm[i->get_filename()] = &*i; + for(const File &f: files) + fm[f.get_filename()] = &f; } else { Regex re(filter); - for(list::const_iterator i=files.begin(); i!=files.end(); ++i) - if(re.match(i->get_filename())) - fm[i->get_filename()] = &*i; + for(const File &f: files) + if(re.match(f.get_filename())) + fm[f.get_filename()] = &f; } } void PackSource::Pack::translate_files(FileMap &fm, const Pack &source) const { - for(list::const_iterator i=files.begin(), j=source.files.begin(); (i!=files.end() && j!=source.files.end()); ++i, ++j) + for(auto i=files.begin(), j=source.files.begin(); (i!=files.end() && j!=source.files.end()); ++i, ++j) { - FileMap::iterator k = fm.find(i->get_filename()); + auto k = fm.find(i->get_filename()); if(k!=fm.end() && k->second==&*j) k->second = &*i; } @@ -218,8 +216,8 @@ PackSource::File::File(const File &other, const Pack &p): length(other.length), collection(other.collection) { - for(list::const_iterator i=other.objects.begin(); i!=other.objects.end(); ++i) - objects.push_back(Object(*i, *this)); + for(const Object &o: other.objects) + objects.push_back(Object(o, *this)); } RefPtr PackSource::File::open() const @@ -251,15 +249,15 @@ string PackSource::File::get_full_name() const void PackSource::File::collect_objects(ObjectMap &objs) const { - for(list::const_iterator i=objects.begin(); i!=objects.end(); ++i) - objs[i->get_name()] = &*i; + for(const Object &o: objects) + objs[o.get_name()] = &o; } void PackSource::File::translate_objects(ObjectMap &objs, const File &source) const { - for(list::const_iterator i=objects.begin(), j=source.objects.begin(); (i!=objects.end() && j!=source.objects.end()); ++i, ++j) + for(auto i=objects.begin(), j=source.objects.begin(); (i!=objects.end() && j!=source.objects.end()); ++i, ++j) { - ObjectMap::iterator k = objs.find(i->get_name()); + auto k = objs.find(i->get_name()); if(k!=objs.end() && k->second==&*j) k->second = &*i; } diff --git a/source/statement.cpp b/source/statement.cpp index 6d1c3b1..baa9054 100644 --- a/source/statement.cpp +++ b/source/statement.cpp @@ -32,8 +32,8 @@ string Statement::get_location() const string Statement::get_signature() const { string result; - for(Arguments::const_iterator i=args.begin(); i!=args.end(); ++i) - result += i->get_signature(); + for(const Value &a: args) + result += a.get_signature(); return result; } @@ -67,10 +67,10 @@ StatementInfo::StatementInfo(const string &k, const string &s): key(k, s), args_size(0) { - for(string::const_iterator i=key.signature.begin(); i!=key.signature.end(); ++i) + for(char c: key.signature) { arg_offsets.push_back(args_size); - switch(*i) + switch(c) { case IntType::signature: args_size += sizeof(IntType::Store); diff --git a/source/textparser.cpp b/source/textparser.cpp index d467de8..b8515a9 100644 --- a/source/textparser.cpp +++ b/source/textparser.cpp @@ -317,20 +317,20 @@ string TextParser::base64_decode(const string &data) bin.reserve(data.size()*3/4); unsigned accum = 0; unsigned a_bits = 0; - for(string::const_iterator i=data.begin(); i!=data.end(); ++i) + for(char c: data) { unsigned d; - if(*i>='A' && *i<='Z') - d = *i-'A'; - else if(*i>='a' && *i<='z') - d = 26+(*i-'a'); - else if(*i>='0' && *i<='9') - d = 52+(*i-'0'); - else if(*i=='+') + if(c>='A' && c<='Z') + d = c-'A'; + else if(c>='a' && c<='z') + d = 26+(c-'a'); + else if(c>='0' && c<='9') + d = 52+(c-'0'); + else if(c=='+') d = 62; - else if(*i=='/') + else if(c=='/') d = 63; - else if(*i=='=') + else if(c=='=') continue; else throw invalid_argument("TextParser::base64_decode"); diff --git a/source/textwriter.cpp b/source/textwriter.cpp index 9218422..04fccff 100644 --- a/source/textwriter.cpp +++ b/source/textwriter.cpp @@ -30,20 +30,20 @@ void TextWriter::write_(const Statement &st, unsigned level) string indent(level, '\t'); out.write(format("%s%s", indent, st.keyword)); - for(vector::const_iterator i = st.args.begin(); i!=st.args.end(); ++i) + for(const Value &v: st.args) { out.put(' '); - if(i->get_signature()==StringType::signature) - out.write(format("\"%s\"", c_escape(i->get(), false))); - else if(i->get_signature()==BoolType::signature) - out.write(i->get() ? "true" : "false"); - else if(i->get_signature()==IntType::signature) - out.write(lexical_cast(i->get())); - else if(i->get_signature()==FloatType::signature) - out.write(lexical_cast(i->get(), float_format)); - else if(i->get_signature()==SymbolType::signature) + if(v.get_signature()==StringType::signature) + out.write(format("\"%s\"", c_escape(v.get(), false))); + else if(v.get_signature()==BoolType::signature) + out.write(v.get() ? "true" : "false"); + else if(v.get_signature()==IntType::signature) + out.write(lexical_cast(v.get())); + else if(v.get_signature()==FloatType::signature) + out.write(lexical_cast(v.get(), float_format)); + else if(v.get_signature()==SymbolType::signature) { - string name = i->get().name; + string name = v.get().name; if(isdigit(name[0])) out.write("\\"+name); else @@ -53,8 +53,8 @@ void TextWriter::write_(const Statement &st, unsigned level) if(!st.sub.empty()) { out.write(format("\n%s{\n", indent)); - for(list::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i) - write_(*i, level+1); + for(const Statement &s: st.sub) + write_(s, level+1); out.write(format("%s}", indent)); } out.write(";\n"); diff --git a/tool/compiler.cpp b/tool/compiler.cpp index 49dd190..8360ee6 100644 --- a/tool/compiler.cpp +++ b/tool/compiler.cpp @@ -166,16 +166,15 @@ ForEach::ForEach(Compiler &c, const FS::Path &b, const list &p): void ForEach::finish() { - list files = FS::list_files(base); - for(list::iterator i = files.begin(); i!=files.end(); ++i) + for(const string &f: FS::list_files(base)) { bool match = false; for(list::const_iterator j = patterns.begin(); (j!=patterns.end() && !match); ++j) - match = Regex(*j).match(*i); + match = Regex(*j).match(f); for(list::const_iterator j = excludes.begin(); (j!=excludes.end() && match); ++j) - match = !Regex(*j).match(*i); + match = !Regex(*j).match(f); if(match) - compiler.process_file(base / *i, write_st); + compiler.process_file(base/f, write_st); } } -- 2.43.0