Skip to content

Commit

Permalink
Unify naming of parameters in AbslStringify(), operator<<,
Browse files Browse the repository at this point in the history
`AbslFormatFlush()`, and `Sink::Append(length, fill)`.

Use `src` and `dest`, especially as the parameter order differs from the regular
Riegeli convention of putting the output parameter last.

Avoid `self` in extension points using FTADLE, because in some contexts they are
defined as global functions. `self` is reserved for cases where something like
`this` is meant but cannot be written as such because we are in a helper object,
like a visitor for `absl::variant`.

PiperOrigin-RevId: 693377499
  • Loading branch information
QrczakMK committed Nov 5, 2024
1 parent a9f00e3 commit 23438c3
Show file tree
Hide file tree
Showing 31 changed files with 400 additions and 402 deletions.
1 change: 0 additions & 1 deletion riegeli/base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ cc_library(
hdrs = ["type_traits.h"],
deps = [
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/meta:type_traits",
"@com_google_absl//absl/strings",
],
Expand Down
2 changes: 1 addition & 1 deletion riegeli/base/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class UnreachableStream {
}

template <typename T>
UnreachableStream& operator<<(ABSL_ATTRIBUTE_UNUSED T&& value) {
UnreachableStream& operator<<(ABSL_ATTRIBUTE_UNUSED T&& src) {
return *this;
}
};
Expand Down
12 changes: 6 additions & 6 deletions riegeli/base/buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@

namespace riegeli {

void Buffer::DumpStructure(absl::string_view substr, std::ostream& out) const {
out << "[buffer] {";
void Buffer::DumpStructure(absl::string_view substr, std::ostream& dest) const {
dest << "[buffer] {";
if (!substr.empty()) {
if (substr.data() != data()) {
out << " space_before: " << PtrDistance(data(), substr.data());
dest << " space_before: " << PtrDistance(data(), substr.data());
}
out << " space_after: "
<< PtrDistance(substr.data() + substr.size(), data() + capacity());
dest << " space_after: "
<< PtrDistance(substr.data() + substr.size(), data() + capacity());
}
out << " }";
dest << " }";
}

} // namespace riegeli
6 changes: 3 additions & 3 deletions riegeli/base/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class

// Support `ExternalRef` and `Chain::Block`.
friend void RiegeliDumpStructure(const Buffer* self, absl::string_view substr,
std::ostream& out) {
self->DumpStructure(substr, out);
std::ostream& dest) {
self->DumpStructure(substr, dest);
}

// Support `MemoryEstimator`.
Expand All @@ -90,7 +90,7 @@ class
private:
void AllocateInternal(size_t min_capacity);
void DeleteInternal();
void DumpStructure(absl::string_view substr, std::ostream& out) const;
void DumpStructure(absl::string_view substr, std::ostream& dest) const;

char* data_ = nullptr;
size_t capacity_ = 0;
Expand Down
18 changes: 9 additions & 9 deletions riegeli/base/byte_fill.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ absl::Cord ByteFill::ZeroBlock::ToCord(absl::string_view substr) {
return absl::MakeCordFromExternal(substr, kNullReleaser);
}

void ByteFill::ZeroBlock::DumpStructure(std::ostream& out) {
out << "[zero_fill] { }";
void ByteFill::ZeroBlock::DumpStructure(std::ostream& dest) {
dest << "[zero_fill] { }";
}

void ByteFill::LargeBlock::DumpStructure(absl::string_view substr,
std::ostream& out) const {
out << "[large_fill] {";
std::ostream& dest) const {
dest << "[large_fill] {";
const size_t ref_count = buffer_.GetRefCount();
if (ref_count != 1) out << " ref_count: " << ref_count;
if (ref_count != 1) dest << " ref_count: " << ref_count;
if (buffer_.capacity() != substr.size()) {
out << " capacity: " << buffer_.capacity();
dest << " capacity: " << buffer_.capacity();
}
out << " }";
dest << " }";
}

ByteFill::Blocks::Blocks(Position size, char fill) {
Expand Down Expand Up @@ -366,9 +366,9 @@ void ByteFill::PrependTo(absl::Cord& dest) const {
}
}

void ByteFill::Output(std::ostream& out) const {
void ByteFill::Output(std::ostream& dest) const {
for (const absl::string_view fragment : blocks()) {
out.write(fragment.data(), IntCast<std::streamsize>(fragment.size()));
dest.write(fragment.data(), IntCast<std::streamsize>(fragment.size()));
}
}

Expand Down
28 changes: 14 additions & 14 deletions riegeli/base/byte_fill.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,19 @@ class ByteFill {

// Default stringification by `absl::StrCat()` etc.
template <typename Sink>
friend void AbslStringify(Sink& sink, const ByteFill& self) {
Position length = self.size_;
friend void AbslStringify(Sink& dest, const ByteFill& src) {
Position length = src.size_;
while (ABSL_PREDICT_FALSE(length > std::numeric_limits<size_t>::max())) {
sink.Append(std::numeric_limits<size_t>::max(), self.fill_);
dest.Append(std::numeric_limits<size_t>::max(), src.fill_);
length -= std::numeric_limits<size_t>::max();
}
if (length > 0) sink.Append(IntCast<size_t>(length), self.fill_);
if (length > 0) dest.Append(IntCast<size_t>(length), src.fill_);
}

// Writes the occurrences to `out` as unformatted bytes.
friend std::ostream& operator<<(std::ostream& out, const ByteFill& self) {
self.Output(out);
return out;
friend std::ostream& operator<<(std::ostream& dest, const ByteFill& src) {
src.Output(dest);
return dest;
}

private:
Expand All @@ -125,7 +125,7 @@ class ByteFill {

void AssignTo(Chain& dest) const;
void AssignTo(absl::Cord& dest) const;
void Output(std::ostream& out) const;
void Output(std::ostream& dest) const;

Position size_;
char fill_;
Expand Down Expand Up @@ -163,14 +163,14 @@ class ByteFill::ZeroBlock {

// Support `ExternalRef` and `Chain::Block`.
friend void RiegeliDumpStructure(ABSL_ATTRIBUTE_UNUSED const ZeroBlock* self,
std::ostream& out) {
DumpStructure(out);
std::ostream& dest) {
DumpStructure(dest);
}

private:
static Chain::Block ToChainBlock(absl::string_view substr);
static absl::Cord ToCord(absl::string_view substr);
static void DumpStructure(std::ostream& out);
static void DumpStructure(std::ostream& dest);
};

class ByteFill::SmallBlock {
Expand Down Expand Up @@ -225,8 +225,8 @@ class ByteFill::LargeBlock {
// Support `ExternalRef` and `Chain::Block`.
friend void RiegeliDumpStructure(const LargeBlock* self,
absl::string_view substr,
std::ostream& out) {
self->DumpStructure(substr, out);
std::ostream& dest) {
self->DumpStructure(substr, dest);
}

// Support `MemoryEstimator`.
Expand All @@ -237,7 +237,7 @@ class ByteFill::LargeBlock {
}

private:
void DumpStructure(absl::string_view substr, std::ostream& out) const;
void DumpStructure(absl::string_view substr, std::ostream& dest) const;

SharedBuffer buffer_;
};
Expand Down
82 changes: 41 additions & 41 deletions riegeli/base/chain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ constexpr Chain::BlockPtrPtr Chain::BlockIterator::kEndShortData;

namespace {

void WritePadding(std::ostream& out, size_t length) {
void WritePadding(std::ostream& dest, size_t length) {
char buffer[64];
std::memset(buffer, out.fill(), sizeof(buffer));
std::memset(buffer, dest.fill(), sizeof(buffer));
while (length > sizeof(buffer)) {
out.write(buffer, std::streamsize{sizeof(buffer)});
dest.write(buffer, std::streamsize{sizeof(buffer)});
length -= sizeof(buffer);
}
out.write(buffer, IntCast<std::streamsize>(length));
dest.write(buffer, IntCast<std::streamsize>(length));
}

// Stores an `absl::Cord` which must be flat, i.e.
Expand All @@ -90,8 +90,8 @@ class FlatCordBlock {

// Support `ExternalRef` and `Chain::Block`.
friend void RiegeliDumpStructure(
ABSL_ATTRIBUTE_UNUSED const FlatCordBlock* self, std::ostream& out) {
out << "[cord] { }";
ABSL_ATTRIBUTE_UNUSED const FlatCordBlock* self, std::ostream& dest) {
dest << "[cord] { }";
}

// Support `MemoryEstimator`.
Expand Down Expand Up @@ -128,12 +128,12 @@ inline FlatCordBlock::operator absl::string_view() const {

namespace chain_internal {

void DumpStructureDefault(std::ostream& out) { out << "[external] { }"; }
void DumpStructureDefault(std::ostream& dest) { dest << "[external] { }"; }

} // namespace chain_internal

void RiegeliDumpStructure(const std::string* self, std::ostream& out) {
out << "[string] { capacity: " << self->capacity() << " }";
void RiegeliDumpStructure(const std::string* self, std::ostream& dest) {
dest << "[string] { capacity: " << self->capacity() << " }";
}

inline IntrusiveSharedPtr<Chain::RawBlock> Chain::RawBlock::NewInternal(
Expand Down Expand Up @@ -210,19 +210,19 @@ inline bool Chain::RawBlock::wasteful(size_t extra_size) const {
return Wasteful(kInternalAllocatedOffset() + capacity(), size() + extra_size);
}

inline void Chain::RawBlock::DumpStructure(std::ostream& out) const {
out << "block {";
inline void Chain::RawBlock::DumpStructure(std::ostream& dest) const {
dest << "block {";
const size_t ref_count = ref_count_.GetCount();
if (ref_count != 1) out << " ref_count: " << ref_count;
out << " size: " << size();
if (ref_count != 1) dest << " ref_count: " << ref_count;
dest << " size: " << size();
if (is_internal()) {
if (space_before() > 0) out << " space_before: " << space_before();
out << " space_after: " << space_after();
if (space_before() > 0) dest << " space_before: " << space_before();
dest << " space_after: " << space_after();
} else {
out << " ";
external_.methods->dump_structure(*this, out);
dest << " ";
external_.methods->dump_structure(*this, dest);
}
out << " }";
dest << " }";
}

size_t Chain::RawBlock::DynamicSizeOf() const {
Expand Down Expand Up @@ -419,11 +419,11 @@ absl::Cord Chain::Block::ToCord(absl::string_view substr) const& {
}

void Chain::Block::DumpStructure(absl::string_view substr,
std::ostream& out) const {
out << "[block] { offset: "
<< PtrDistance(block_->data_begin(), substr.data()) << " ";
block_->DumpStructure(out);
out << " }";
std::ostream& dest) const {
dest << "[block] { offset: "
<< PtrDistance(block_->data_begin(), substr.data()) << " ";
block_->DumpStructure(dest);
dest << " }";
}

Chain::Chain(const absl::Cord& src) { Initialize(src); }
Expand Down Expand Up @@ -813,13 +813,13 @@ Chain::BlockAndChar Chain::BlockAndCharIndex(size_t char_index_in_chain) const {
}
}

void Chain::DumpStructure(std::ostream& out) const {
out << "chain {\n size: " << size_ << " memory: " << EstimateMemory();
void Chain::DumpStructure(std::ostream& dest) const {
dest << "chain {\n size: " << size_ << " memory: " << EstimateMemory();
for (const BlockPtr* iter = begin_; iter != end_; ++iter) {
out << "\n ";
iter->block_ptr->DumpStructure(out);
dest << "\n ";
iter->block_ptr->DumpStructure(dest);
}
out << "\n}\n";
dest << "\n}\n";
}

size_t Chain::EstimateMemory() const {
Expand Down Expand Up @@ -2111,25 +2111,25 @@ StrongOrdering Chain::Compare(const Chain& a, absl::string_view b) {
return that_pos == b.size() ? StrongOrdering::equal : StrongOrdering::less;
}

void Chain::Output(std::ostream& out) const {
std::ostream::sentry sentry(out);
void Chain::Output(std::ostream& dest) const {
std::ostream::sentry sentry(dest);
if (sentry) {
size_t lpad = 0;
size_t rpad = 0;
if (IntCast<size_t>(out.width()) > size()) {
const size_t pad = IntCast<size_t>(out.width()) - size();
if ((out.flags() & out.adjustfield) == out.left) {
rpad = pad;
size_t left_pad = 0;
size_t right_pad = 0;
if (IntCast<size_t>(dest.width()) > size()) {
const size_t pad = IntCast<size_t>(dest.width()) - size();
if ((dest.flags() & dest.adjustfield) == dest.left) {
right_pad = pad;
} else {
lpad = pad;
left_pad = pad;
}
}
if (lpad > 0) WritePadding(out, lpad);
if (left_pad > 0) WritePadding(dest, left_pad);
for (const absl::string_view fragment : blocks()) {
out.write(fragment.data(), IntCast<std::streamsize>(fragment.size()));
dest.write(fragment.data(), IntCast<std::streamsize>(fragment.size()));
}
if (rpad > 0) WritePadding(out, rpad);
out.width(0);
if (right_pad > 0) WritePadding(dest, right_pad);
dest.width(0);
}
}

Expand Down
26 changes: 13 additions & 13 deletions riegeli/base/chain_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class Chain : public WithCompare<Chain> {
BlockAndChar BlockAndCharIndex(size_t char_index_in_chain) const;

// Shows internal structure in a human-readable way, for debugging.
void DumpStructure(std::ostream& out) const;
void DumpStructure(std::ostream& dest) const;
// Estimates the amount of memory used by this `Chain`.
size_t EstimateMemory() const;
// Support `MemoryEstimator`.
Expand Down Expand Up @@ -409,13 +409,13 @@ class Chain : public WithCompare<Chain> {

// Default stringification by `absl::StrCat()` etc.
template <typename Sink>
friend void AbslStringify(Sink& sink, const Chain& self) {
self.Stringify(sink);
friend void AbslStringify(Sink& dest, const Chain& src) {
src.Stringify(dest);
}

friend std::ostream& operator<<(std::ostream& out, const Chain& self) {
self.Output(out);
return out;
friend std::ostream& operator<<(std::ostream& dest, const Chain& src) {
src.Output(dest);
return dest;
}

// Support `absl::Format(&chain, format, args...)`.
Expand Down Expand Up @@ -592,8 +592,8 @@ class Chain : public WithCompare<Chain> {
template <typename HashState>
HashState HashValue(HashState hash_state) const;
template <typename Sink>
void Stringify(Sink& sink) const;
void Output(std::ostream& out) const;
void Stringify(Sink& dest) const;
void Output(std::ostream& dest) const;

BlockPtrs block_ptrs_;

Expand Down Expand Up @@ -706,7 +706,7 @@ class Chain::RawBlock {
bool wasteful(size_t extra_size = 0) const;

// Shows internal structure in a human-readable way, for debugging.
void DumpStructure(std::ostream& out) const;
void DumpStructure(std::ostream& dest) const;

// Support `MemoryEstimator`.
friend size_t RiegeliDynamicSizeOf(const RawBlock* self) {
Expand Down Expand Up @@ -842,7 +842,7 @@ class Chain::Block {
//
// // Shows internal structure in a human-readable way, for debugging.
// friend void RiegeliDumpStructure(const T* self, absl::string_view substr,
// std::ostream& out) {
// std::ostream& dest) {
// out << "[external] { }";
// }
//
Expand Down Expand Up @@ -921,8 +921,8 @@ class Chain::Block {

// Support `ExternalRef` and `Chain::Block`.
friend void RiegeliDumpStructure(const Block* self, absl::string_view substr,
std::ostream& out) {
self->DumpStructure(substr, out);
std::ostream& dest) {
self->DumpStructure(substr, dest);
}

// Support `MemoryEstimator`.
Expand All @@ -947,7 +947,7 @@ class Chain::Block {
absl::Cord ToCord(absl::string_view substr) &&;
absl::Cord ToCord(absl::string_view substr) const&;
ExternalStorage ToExternalStorage() &&;
void DumpStructure(absl::string_view substr, std::ostream& out) const;
void DumpStructure(absl::string_view substr, std::ostream& dest) const;

IntrusiveSharedPtr<RawBlock> block_;
};
Expand Down
Loading

0 comments on commit 23438c3

Please sign in to comment.