Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2011 The LevelDB Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file. See the AUTHORS file for names of contributors.5#include "leveldb/comparator.h"7#include <algorithm>8#include <cstdint>9#include <string>10#include <type_traits>12#include "leveldb/slice.h"13#include "util/logging.h"14#include "util/no_destructor.h"16namespace leveldb {18Comparator::~Comparator() = default;20namespace {21class BytewiseComparatorImpl : public Comparator {22public:23BytewiseComparatorImpl() = default;25const char* Name() const override { return "leveldb.BytewiseComparator"; }27int Compare(const Slice& a, const Slice& b) const override {28return a.compare(b);29}31void FindShortestSeparator(std::string* start,32const Slice& limit) const override {33// Find length of common prefix34size_t min_length = std::min(start->size(), limit.size());35size_t diff_index = 0;36while ((diff_index < min_length) &&37((*start)[diff_index] == limit[diff_index])) {38diff_index++;39}41if (diff_index >= min_length) {42// Do not shorten if one string is a prefix of the other43} else {44uint8_t diff_byte = static_cast<uint8_t>((*start)[diff_index]);45if (diff_byte < static_cast<uint8_t>(0xff) &&46diff_byte + 1 < static_cast<uint8_t>(limit[diff_index])) {47(*start)[diff_index]++;48start->resize(diff_index + 1);49assert(Compare(*start, limit) < 0);50}51}52}54void FindShortSuccessor(std::string* key) const override {55// Find first character that can be incremented56size_t n = key->size();57for (size_t i = 0; i < n; i++) {58const uint8_t byte = (*key)[i];59if (byte != static_cast<uint8_t>(0xff)) {60(*key)[i] = byte + 1;61key->resize(i + 1);62return;63}64}65// *key is a run of 0xffs. Leave it alone.66}67};68} // namespace70const Comparator* BytewiseComparator() {71static NoDestructor<BytewiseComparatorImpl> singleton;72return singleton.get();73}75} // namespace leveldb
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.