From 6828ae2c2f9268c8187f0fa91b3c464ed84a8476 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 26 Dec 2024 20:43:34 +0000 Subject: Add pkgCache::{Priority,DepType}_NoL10n to avoid duplication We don't have many places, but lets reduce the amount of duplicating these short strings, so that we may find all the places we have to change if that ever happens. --- apt-pkg/cachefilter-patterns.h | 7 +------ apt-pkg/edsp.cc | 23 ++++++----------------- apt-pkg/pkgcache.cc | 37 +++++++++++++++++++++++++++---------- apt-pkg/pkgcache.h | 4 +++- 4 files changed, 37 insertions(+), 34 deletions(-) (limited to 'apt-pkg') diff --git a/apt-pkg/cachefilter-patterns.h b/apt-pkg/cachefilter-patterns.h index 75784e040..1714bbb62 100644 --- a/apt-pkg/cachefilter-patterns.h +++ b/apt-pkg/cachefilter-patterns.h @@ -475,12 +475,7 @@ struct APT_HIDDEN VersionIsPriority : public VersionAnyMatcher explicit VersionIsPriority(std::string name) : name(name) {} bool operator()(pkgCache::VerIterator const &Ver) override { - std::string Mapping[] = {"", "required","important","standard", - "optional","extra"}; - if (Ver->Priority > 0 && Ver->Priority < APT_ARRAY_SIZE(Mapping)) { - return name == Mapping[Ver->Priority]; - } - return false; + return Ver->Priority > 0 && name == pkgCache::Priority_NoL10n(Ver->Priority); } }; diff --git a/apt-pkg/edsp.cc b/apt-pkg/edsp.cc index d3ce830ea..88f572f18 100644 --- a/apt-pkg/edsp.cc +++ b/apt-pkg/edsp.cc @@ -41,17 +41,6 @@ using std::string; -// we could use pkgCache::DepType and ::Priority, but these would be localized strings… -constexpr char const * const PrioMap[] = { - nullptr, "important", "required", "standard", - "optional", "extra" -}; -constexpr char const * const DepMap[] = { - nullptr, "Depends", "Pre-Depends", "Suggests", - "Recommends" , "Conflicts", "Replaces", - "Obsoletes", "Breaks", "Enhances" -}; - // WriteOkay - varaidic helper to easily Write to a FileFd /*{{{*/ static bool WriteOkay_fn(FileFd &) { return true; } template static bool WriteOkay_fn(FileFd &output, std::string_view data, Tail... more_data) @@ -99,7 +88,7 @@ static bool WriteScenarioVersion(FileFd &output, pkgCache::PkgIterator const &Pk // WriteScenarioDependency /*{{{*/ static bool WriteScenarioDependency(FileFd &output, pkgCache::VerIterator const &Ver, bool const OnlyCritical) { - std::array dependencies; + std::array dependencies; bool orGroup = false; for (pkgCache::DepIterator Dep = Ver.DependsList(); Dep.end() == false; ++Dep) { @@ -123,7 +112,7 @@ static bool WriteScenarioDependency(FileFd &output, pkgCache::VerIterator const bool Okay = output.Failed() == false; for (size_t i = 1; i < dependencies.size(); ++i) if (dependencies[i].empty() == false) - WriteOkay(Okay, output, "\n", DepMap[i], ": ", dependencies[i]); + WriteOkay(Okay, output, "\n", pkgCache::DepType_NoL10n(i), ": ", dependencies[i]); std::vector provides; for (auto Prv = Ver.ProvidesList(); not Prv.end(); ++Prv) { @@ -152,7 +141,7 @@ static bool WriteScenarioLimitedDependency(FileFd &output, std::vector const &pkgset, bool const OnlyCritical) { - std::array dependencies; + std::array dependencies; bool orGroup = false; for (pkgCache::DepIterator Dep = Ver.DependsList(); Dep.end() == false; ++Dep) { @@ -189,7 +178,7 @@ static bool WriteScenarioLimitedDependency(FileFd &output, bool Okay = output.Failed() == false; for (size_t i = 1; i < dependencies.size(); ++i) if (dependencies[i].empty() == false) - WriteOkay(Okay, output, "\n", DepMap[i], ": ", dependencies[i]); + WriteOkay(Okay, output, "\n", pkgCache::DepType_NoL10n(i), ": ", dependencies[i]); string provides; for (pkgCache::PrvIterator Prv = Ver.ProvidesList(); Prv.end() == false; ++Prv) { @@ -250,8 +239,8 @@ static bool WriteScenarioEDSPVersion(pkgDepCache &Cache, FileFd &output, pkgCach { bool Okay = WriteOkay(output, "\nSource: ", Ver.SourcePkgName(), "\nSource-Version: ", Ver.SourceVerStr()); - if (PrioMap[Ver->Priority] != nullptr) - WriteOkay(Okay, output, "\nPriority: ", PrioMap[Ver->Priority]); + if (auto const Prio = pkgCache::Priority_NoL10n(Ver->Priority); not Prio.empty()) + WriteOkay(Okay, output, "\nPriority: ", Prio); if (Ver->Section != 0) WriteOkay(Okay, output, "\nSection: ", Ver.Section()); if (Pkg.CurrentVer() == Ver) diff --git a/apt-pkg/pkgcache.cc b/apt-pkg/pkgcache.cc index ffe30fa92..57c6d2b22 100644 --- a/apt-pkg/pkgcache.cc +++ b/apt-pkg/pkgcache.cc @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -340,24 +341,40 @@ const char *pkgCache::CompType(unsigned char Comp) /* */ const char *pkgCache::DepType(unsigned char Type) { - const char *Types[] = {"",_("Depends"),_("PreDepends"),_("Suggests"), - _("Recommends"),_("Conflicts"),_("Replaces"), - _("Obsoletes"),_("Breaks"), _("Enhances")}; - if (Type < sizeof(Types)/sizeof(*Types)) + std::array Types{nullptr, _("Depends"), _("PreDepends"), _("Suggests"), + _("Recommends"), _("Conflicts"), _("Replaces"), + _("Obsoletes"), _("Breaks"), _("Enhances")}; + if (Type < Types.size()) return Types[Type]; return ""; +} +std::string_view pkgCache::DepType_NoL10n(unsigned char Type) +{ + std::array Types{"", "Depends", "PreDepends", "Suggests", + "Recommends", "Conflicts", "Replaces", + "Obsoletes", "Breaks", "Enhances"}; + if (Type < Types.size()) + return Types[Type]; + return {}; } /*}}}*/ // Cache::Priority - Convert a priority value to a string /*{{{*/ -// --------------------------------------------------------------------- -/* */ const char *pkgCache::Priority(unsigned char Prio) { - const char *Mapping[] = {0,_("required"),_("important"),_("standard"), - _("optional"),_("extra")}; - if (Prio < APT_ARRAY_SIZE(Mapping)) + std::array Mapping{nullptr, _("required"), _("important"), _("standard"), + _("optional"), _("extra")}; + if (Prio < Mapping.size()) + return Mapping[Prio]; + return nullptr; +} +std::string_view pkgCache::Priority_NoL10n(unsigned char Prio) +{ + constexpr std::array const Mapping{ + "", "required", "important", "standard", "optional", "extra" + }; + if (Prio < Mapping.size()) return Mapping[Prio]; - return 0; + return {}; } /*}}}*/ // GrpIterator::FindPkg - Locate a package by arch /*{{{*/ diff --git a/apt-pkg/pkgcache.h b/apt-pkg/pkgcache.h index 7c16f0dee..476a01514 100644 --- a/apt-pkg/pkgcache.h +++ b/apt-pkg/pkgcache.h @@ -245,7 +245,8 @@ class APT_PUBLIC pkgCache /*{{{*/ // Useful transformation things static const char *Priority(unsigned char Priority); - + static std::string_view Priority_NoL10n(unsigned char Prio); + // Accessors GrpIterator FindGrp(APT::StringView Name); PkgIterator FindPkg(APT::StringView Name); @@ -278,6 +279,7 @@ class APT_PUBLIC pkgCache /*{{{*/ static const char *CompTypeDeb(unsigned char Comp) APT_PURE; static const char *CompType(unsigned char Comp) APT_PURE; static const char *DepType(unsigned char Dep); + static std::string_view DepType_NoL10n(unsigned char Dep); pkgCache(MMap *Map,bool DoMap = true); virtual ~pkgCache(); -- cgit v1.2.3-70-g09d2