summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2016-09-27 16:49:25 +0200
committerJulian Andres Klode <jak@debian.org>2016-11-22 22:56:46 +0100
commit45ecab4458c4015b7635042064165c229e099c28 (patch)
tree14b0cad45123fb6478c01af960a4a2e0e14b4e55
parent3e633069c9c187dc51c12e59a7ddba4b68a76c4f (diff)
TagSection: Extract Find() methods taking Pos instead of Key
This allows us to add a perfect hash function to the tag file without having to reimplement the methods a second time.
-rw-r--r--apt-pkg/tagfile.cc74
-rw-r--r--apt-pkg/tagfile.h11
2 files changed, 65 insertions, 20 deletions
diff --git a/apt-pkg/tagfile.cc b/apt-pkg/tagfile.cc
index bf8320c31..b81efe42a 100644
--- a/apt-pkg/tagfile.cc
+++ b/apt-pkg/tagfile.cc
@@ -645,13 +645,9 @@ bool pkgTagSection::Find(StringView TagView,unsigned int &Pos) const
return false;
}
-bool pkgTagSection::Find(StringView Tag,const char *&Start,
+bool pkgTagSection::FindInternal(unsigned int Pos, const char *&Start,
const char *&End) const
{
- unsigned int Pos;
- if (Find(Tag, Pos) == false)
- return false;
-
Start = Section + d->Tags[Pos].StartValue;
// Strip off the gunk from the end
End = Section + d->Tags[Pos + 1].StartTag;
@@ -662,6 +658,12 @@ bool pkgTagSection::Find(StringView Tag,const char *&Start,
return true;
}
+bool pkgTagSection::Find(StringView Tag,const char *&Start,
+ const char *&End) const
+{
+ unsigned int Pos;
+ return Find(Tag, Pos) && FindInternal(Pos, Start, End);
+}
/*}}}*/
// TagSection::FindS - Find a string /*{{{*/
StringView pkgTagSection::Find(StringView Tag) const
@@ -674,12 +676,8 @@ StringView pkgTagSection::Find(StringView Tag) const
}
/*}}}*/
// TagSection::FindRawS - Find a string /*{{{*/
-StringView pkgTagSection::FindRaw(StringView Tag) const
+StringView pkgTagSection::FindRawInternal(unsigned int Pos) const
{
- unsigned int Pos;
- if (Find(Tag, Pos) == false)
- return "";
-
char const *Start = (char const *) memchr(Section + d->Tags[Pos].EndTag, ':', d->Tags[Pos].StartValue - d->Tags[Pos].EndTag);
++Start;
char const *End = Section + d->Tags[Pos + 1].StartTag;
@@ -690,15 +688,20 @@ StringView pkgTagSection::FindRaw(StringView Tag) const
return StringView(Start, End - Start);
}
+StringView pkgTagSection::FindRaw(StringView Tag) const
+{
+ unsigned int Pos;
+ return Find(Tag, Pos) ? FindRawInternal(Pos) : "";
+}
/*}}}*/
// TagSection::FindI - Find an integer /*{{{*/
// ---------------------------------------------------------------------
/* */
-signed int pkgTagSection::FindI(StringView Tag,signed long Default) const
+signed int pkgTagSection::FindIInternal(unsigned int Pos,signed long Default) const
{
const char *Start;
const char *Stop;
- if (Find(Tag,Start,Stop) == false)
+ if (FindInternal(Pos,Start,Stop) == false)
return Default;
// Copy it into a temp buffer so we can use strtol
@@ -720,15 +723,21 @@ signed int pkgTagSection::FindI(StringView Tag,signed long Default) const
return Default;
return Result;
}
+signed int pkgTagSection::FindI(StringView Tag,signed long Default) const
+{
+ unsigned int Pos;
+
+ return Find(Tag, Pos) ? FindIInternal(Pos, Default) : Default;
+}
/*}}}*/
// TagSection::FindULL - Find an unsigned long long integer /*{{{*/
// ---------------------------------------------------------------------
/* */
-unsigned long long pkgTagSection::FindULL(StringView Tag, unsigned long long const &Default) const
+unsigned long long pkgTagSection::FindULLInternal(unsigned int Pos, unsigned long long const &Default) const
{
const char *Start;
const char *Stop;
- if (Find(Tag,Start,Stop) == false)
+ if (FindInternal(Pos,Start,Stop) == false)
return Default;
// Copy it into a temp buffer so we can use strtoull
@@ -744,30 +753,49 @@ unsigned long long pkgTagSection::FindULL(StringView Tag, unsigned long long con
return Default;
return Result;
}
+unsigned long long pkgTagSection::FindULL(StringView Tag, unsigned long long const &Default) const
+{
+ unsigned int Pos;
+
+ return Find(Tag, Pos) ? FindULLInternal(Pos, Default) : Default;
+}
/*}}}*/
// TagSection::FindB - Find boolean value /*{{{*/
// ---------------------------------------------------------------------
/* */
-bool pkgTagSection::FindB(StringView Tag, bool Default) const
+bool pkgTagSection::FindBInternal(unsigned int Pos, bool Default) const
{
const char *Start, *Stop;
- if (Find(Tag, Start, Stop) == false)
+ if (FindInternal(Pos, Start, Stop) == false)
return Default;
return StringToBool(string(Start, Stop));
}
+bool pkgTagSection::FindB(StringView Tag, bool Default) const
+{
+ unsigned int Pos;
+ return Find(Tag, Pos) ? FindBInternal(Pos, Default) : Default;
+}
/*}}}*/
// TagSection::FindFlag - Locate a yes/no type flag /*{{{*/
// ---------------------------------------------------------------------
/* The bits marked in Flag are masked on/off in Flags */
-bool pkgTagSection::FindFlag(StringView Tag, uint8_t &Flags,
+bool pkgTagSection::FindFlagInternal(unsigned int Pos, uint8_t &Flags,
uint8_t const Flag) const
{
const char *Start;
const char *Stop;
- if (Find(Tag,Start,Stop) == false)
+ if (FindInternal(Pos,Start,Stop) == false)
return true;
return FindFlag(Flags, Flag, Start, Stop);
}
+bool pkgTagSection::FindFlag(StringView Tag, uint8_t &Flags,
+ uint8_t const Flag) const
+{
+ unsigned int Pos;
+ if (Find(Tag,Pos) == false)
+ return true;
+ return FindFlagInternal(Pos, Flags, Flag);
+}
bool pkgTagSection::FindFlag(uint8_t &Flags, uint8_t const Flag,
char const* const Start, char const* const Stop)
{
@@ -787,15 +815,21 @@ bool pkgTagSection::FindFlag(uint8_t &Flags, uint8_t const Flag,
}
return true;
}
-bool pkgTagSection::FindFlag(StringView Tag,unsigned long &Flags,
+bool pkgTagSection::FindFlagInternal(unsigned int Pos,unsigned long &Flags,
unsigned long Flag) const
{
const char *Start;
const char *Stop;
- if (Find(Tag,Start,Stop) == false)
+ if (FindInternal(Pos,Start,Stop) == false)
return true;
return FindFlag(Flags, Flag, Start, Stop);
}
+bool pkgTagSection::FindFlag(StringView Tag,unsigned long &Flags,
+ unsigned long Flag) const
+{
+ unsigned int Pos;
+ return Find(Tag, Pos) ? FindFlagInternal(Pos, Flags, Flag) : true;
+}
bool pkgTagSection::FindFlag(unsigned long &Flags, unsigned long Flag,
char const* Start, char const* Stop)
{
diff --git a/apt-pkg/tagfile.h b/apt-pkg/tagfile.h
index f0f2f48c6..cdca54756 100644
--- a/apt-pkg/tagfile.h
+++ b/apt-pkg/tagfile.h
@@ -54,6 +54,17 @@ class pkgTagSection
pkgTagSectionPrivate * const d;
+ APT_HIDDEN bool FindInternal(unsigned int Pos,const char *&Start, const char *&End) const;
+#if defined(APT_PKG_EXPOSE_STRING_VIEW)
+ APT_HIDDEN APT::StringView FindInternal(unsigned int Pos) const;
+ APT_HIDDEN APT::StringView FindRawInternal(unsigned int Pos) const;
+#endif
+ APT_HIDDEN signed int FindIInternal(unsigned int Pos,signed long Default = 0) const;
+ APT_HIDDEN bool FindBInternal(unsigned int Pos, bool Default = false) const;
+ APT_HIDDEN unsigned long long FindULLInternal(unsigned int Pos, unsigned long long const &Default = 0) const;
+ APT_HIDDEN bool FindFlagInternal(unsigned int Pos,uint8_t &Flags, uint8_t const Flag) const;
+ APT_HIDDEN bool FindFlagInternal(unsigned int Pos,unsigned long &Flags, unsigned long Flag) const;
+
protected:
const char *Stop;