<commit_before>// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RIEGELI_BYTES_PULLABLE_READER_H_
#define RIEGELI_BYTES_PULLABLE_READER_H_

#include <stddef.h>

#include <memory>
#include <utility>

#include "absl/base/attributes.h"
#include "absl/base/optimization.h"
#include "absl/strings/cord.h"
#include "riegeli/base/base.h"
#include "riegeli/base/chain.h"
#include "riegeli/base/object.h"
#include "riegeli/bytes/reader.h"

namespace riegeli {

class BackwardWriter;
class Writer;

// Abstract class `PullableReader` helps to implement
// `Reader::PullSlow(min_length, recommended_length)` with `min_length > 1`.
//
// `PullableReader` accumulates pulled data in a scratch buffer if needed.
class PullableReader : public Reader {
 protected:
  class BehindScratch;

  using Reader::Reader;

  PullableReader(PullableReader&& that) noexcept;
  PullableReader& operator=(PullableReader&& that) noexcept;

  // Makes `*this` equivalent to a newly constructed `PullableReader`. This
  // avoids constructing a temporary `PullableReader` and moving from it.
  // Derived classes which redefine `Reset()` should include a call to
  // `PullableReader::Reset()`.
  void Reset(Closed);
  void Reset();

  // Returns `true` if scratch is used, which means that buffer pointers are
  // temporarily unrelated to the source. This is exposed for assertions.
  bool scratch_used() const;

  // `PullableReader::{Done,SyncImpl}()` seek the source back to the current
  // position if scratch is used but not all data from scratch were read.
  // This is feasible only if `SupportsRandomAccess()`.
  //
  // Warning: if `!SupportsRandomAccess()`, the source will have an
  // unpredictable amount of extra data consumed because of buffering.
  //
  // For propagating `{Close,Sync}()` to dependencies, `{Done,SyncImpl}()`
  // should be overridden to call `PullableReader::{Done,SyncImpl}()` and then
  // close/sync the dependencies.

  // Implementation of `Done()`, called while scratch is not used. This is
  // called before buffer pointers are reset.
  //
  // If scratch was used but not all data from scratch were read and
  // `!SupportsRandomAccess()`, seeking back is not feasible and
  // `DoneBehindScratch()` is not called.
  //
  // By default calls `SyncBehindScratch(SyncType::kFromObject)`, which by
  // default does nothing.
  //
  // Precondition: `!scratch_used()`
  virtual void DoneBehindScratch();

  // Implementation of `PullSlow(1, recommended_length)`, called while scratch
  // is not used.
  //
  // Preconditions:
  //   `available() == 0`
  //   `!scratch_used()`
  virtual bool PullBehindScratch(size_t recommended_length) {
    return PullBehindScratch();
  }
  ABSL_DEPRECATED("Override PullBehindScratch(size_t) instead")
  virtual bool PullBehindScratch() {
    RIEGELI_ASSERT_UNREACHABLE()
        << "Either overload of PullBehindScratch() must be overridden";
  }

  // Implementation of `ReadSlow()`, `CopySlow()`, `ReadHintSlow()`,
  // `SyncImpl()`, and `SeekSlow()`, called while scratch is not used.
  //
  // Regarding `SyncBehindScratch()`, if scratch was used but not all data from
  // scratch were read and `!SupportsRandomAccess()`, seeking back is not
  // feasible and `SyncBehindScratch()` is not called.
  //
  // By default they are implemented analogously to the corresponding `Reader`
  // functions.
  //
  // Preconditions:
  //   like the corresponding `Reader` functions
  //   `!scratch_used()`
  virtual bool ReadBehindScratch(size_t length, char* dest);<commit_msg>Remove the overload of `PullableReader::PullBehindScratch()` with no parameters, which is no longer overridden anywhere.<commit_after>// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RIEGELI_BYTES_PULLABLE_READER_H_
#define RIEGELI_BYTES_PULLABLE_READER_H_

#include <stddef.h>

#include <memory>
#include <utility>

#include "absl/base/optimization.h"
#include "absl/strings/cord.h"
#include "riegeli/base/base.h"
#include "riegeli/base/chain.h"
#include "riegeli/base/object.h"
#include "riegeli/bytes/reader.h"

namespace riegeli {

class BackwardWriter;
class Writer;

// Abstract class `PullableReader` helps to implement
// `Reader::PullSlow(min_length, recommended_length)` with `min_length > 1`.
//
// `PullableReader` accumulates pulled data in a scratch buffer if needed.
class PullableReader : public Reader {
 protected:
  class BehindScratch;

  using Reader::Reader;

  PullableReader(PullableReader&& that) noexcept;
  PullableReader& operator=(PullableReader&& that) noexcept;

  // Makes `*this` equivalent to a newly constructed `PullableReader`. This
  // avoids constructing a temporary `PullableReader` and moving from it.
  // Derived classes which redefine `Reset()` should include a call to
  // `PullableReader::Reset()`.
  void Reset(Closed);
  void Reset();

  // Returns `true` if scratch is used, which means that buffer pointers are
  // temporarily unrelated to the source. This is exposed for assertions.
  bool scratch_used() const;

  // `PullableReader::{Done,SyncImpl}()` seek the source back to the current
  // position if scratch is used but not all data from scratch were read.
  // This is feasible only if `SupportsRandomAccess()`.
  //
  // Warning: if `!SupportsRandomAccess()`, the source will have an
  // unpredictable amount of extra data consumed because of buffering.
  //
  // For propagating `{Close,Sync}()` to dependencies, `{Done,SyncImpl}()`
  // should be overridden to call `PullableReader::{Done,SyncImpl}()` and then
  // close/sync the dependencies.

  // Implementation of `Done()`, called while scratch is not used. This is
  // called before buffer pointers are reset.
  //
  // If scratch was used but not all data from scratch were read and
  // `!SupportsRandomAccess()`, seeking back is not feasible and
  // `DoneBehindScratch()` is not called.
  //
  // By default calls `SyncBehindScratch(SyncType::kFromObject)`, which by
  // default does nothing.
  //
  // Precondition: `!scratch_used()`
  virtual void DoneBehindScratch();

  // Implementation of `PullSlow(1, recommended_length)`, called while scratch
  // is not used.
  //
  // Preconditions:
  //   `available() == 0`
  //   `!scratch_used()`
  virtual bool PullBehindScratch(size_t recommended_length) = 0;

  // Implementation of `ReadSlow()`, `CopySlow()`, `ReadHintSlow()`,
  // `SyncImpl()`, and `SeekSlow()`, called while scratch is not used.
  //
  // Regarding `SyncBehindScratch()`, if scratch was used but not all data from
  // scratch were read and `!SupportsRandomAccess()`, seeking back is not
  // feasible and `SyncBehindScratch()` is not called.
  //
  // By default they are implemented analogously to the corresponding `Reader`
  // functions.
  //
  // Preconditions:
  //   like the corresponding `Reader` functions
  //   `!scratch_used()`
  virtual bool ReadBehindScratch(size_t length, char* dest);
{'fraction_non_alphanumeric': 0.11442910915934755, 'fraction_numerical': 0.0028858218318695106, 'mean_word_length': 3.8514911746804628, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6178265', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2306, 'n_words': 839}