+static bool FillBuffer(pkgTagFilePrivate * const d)
+{
+ unsigned long long Actual = 0;
+ // See if only a bit of the file is left
+ unsigned long long const dataSize = d->Size - ((d->End - d->Buffer) + 1);
+ if (d->Fd->Read(d->End, dataSize, &Actual) == false)
+ return false;
+ if (Actual != dataSize)
+ d->Done = true;
+ d->End += Actual;
+ return true;
+}
+static void RemoveCommentsFromBuffer(pkgTagFilePrivate * const d)
+{
+ // look for valid comments in the buffer
+ char * good_start = nullptr, * bad_start = nullptr;
+ char * current = d->Start;
+ if (d->isCommentedLine == false)
+ {
+ if (d->Start == d->Buffer)
+ {
+ // the start of the buffer is a newline as a record can't start
+ // in the middle of a line by definition.
+ if (*d->Start == '#')
+ {
+ d->isCommentedLine = true;
+ ++current;
+ if (current > d->End)
+ d->chunks.emplace_back(false, 1);
+ }
+ }
+ if (d->isCommentedLine == false)
+ good_start = d->Start;
+ else
+ bad_start = d->Start;
+ }
+ else
+ bad_start = d->Start;
+
+ std::vector<std::pair<char*, size_t>> good_parts;
+ while (current <= d->End)
+ {
+ size_t const restLength = (d->End - current) + 1;
+ if (d->isCommentedLine == false)
+ {
+ current = static_cast<char*>(memchr(current, '#', restLength));
+ if (current == nullptr)
+ {
+ size_t const goodLength = d->End - good_start;
+ d->chunks.emplace_back(true, goodLength);
+ if (good_start != d->Start)
+ good_parts.push_back(std::make_pair(good_start, goodLength));
+ break;
+ }
+ bad_start = current;
+ --current;
+ // ensure that this is really a comment and not a '#' in the middle of a line
+ if (*current == '\n')
+ {
+ size_t const goodLength = (current - good_start) + 1;
+ d->chunks.emplace_back(true, goodLength);
+ good_parts.push_back(std::make_pair(good_start, goodLength));
+ good_start = nullptr;
+ d->isCommentedLine = true;
+ }
+ current += 2;
+ }
+ else // the current line is a comment
+ {
+ current = static_cast<char*>(memchr(current, '\n', restLength));
+ if (current == nullptr)
+ {
+ d->chunks.emplace_back(false, (d->End - bad_start));
+ break;
+ }
+ ++current;
+ // is the next line a comment, too?
+ if (current > d->End || *current != '#')
+ {
+ d->chunks.emplace_back(false, (current - bad_start));
+ good_start = current;
+ bad_start = nullptr;
+ d->isCommentedLine = false;
+ }
+ ++current;
+ }
+ }
+
+ if (good_parts.empty() == false)
+ {
+ // we found comments, so move later parts over them
+ current = d->Start;
+ for (auto const &good: good_parts)
+ {
+ memmove(current, good.first, good.second);
+ current += good.second;
+ }
+ d->End = current;
+ }
+
+ if (d->isCommentedLine == true)
+ {
+ // deal with a buffer containing only comments
+ // or an (unfinished) comment at the end
+ if (good_parts.empty() == true)
+ d->End = d->Start;
+ else
+ d->Start = d->End;
+ }
+ else
+ {
+ // the buffer was all comment, but ended with the buffer
+ if (good_parts.empty() == true && good_start >= d->End)
+ d->End = d->Start;
+ else
+ d->Start = d->End;
+ }
+}