Skip to content

Commit

Permalink
Add chrome_pdf::ChunkStream::GetChunkEnd().
Browse files Browse the repository at this point in the history
So several callers don't have to repeat the same calculation.
Fix some nits too.

Change-Id: I36e0b8d1c724b0b393a7b51bdf599177c375a68e
Reviewed-on: https://chromium-review.googlesource.com/982538
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#546534}
  • Loading branch information
leizleiz authored and Commit Bot committed Mar 28, 2018
1 parent e15e055 commit fc4640d
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions pdf/chunk_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <algorithm>
#include <array>
#include <memory>
#include <utility>
#include <vector>

#include "pdf/range_set.h"
Expand All @@ -31,20 +32,21 @@ class ChunkStream {
void SetChunkData(uint32_t chunk_index, std::unique_ptr<ChunkData> data) {
if (!data)
return;
if (chunk_index >= data_.size()) {

if (chunk_index >= data_.size())
data_.resize(chunk_index + 1);
}
if (!data_[chunk_index]) {

if (!data_[chunk_index])
++filled_chunks_count_;
}

data_[chunk_index] = std::move(data);
filled_chunks_.Union(gfx::Range(chunk_index, chunk_index + 1));
}

bool ReadData(const gfx::Range& range, void* buffer) const {
if (!IsRangeAvailable(range)) {
if (!IsRangeAvailable(range))
return false;
}

unsigned char* data_buffer = static_cast<unsigned char*>(buffer);
uint32_t start = range.start();
while (start != range.end()) {
Expand All @@ -62,18 +64,20 @@ class ChunkStream {
uint32_t GetChunkIndex(uint32_t offset) const { return offset / kChunkSize; }

gfx::Range GetChunksRange(uint32_t offset, uint32_t size) const {
return gfx::Range(GetChunkIndex(offset),
GetChunkIndex(offset + size + kChunkSize - 1));
return gfx::Range(GetChunkIndex(offset), GetChunkEnd(offset + size));
}

bool IsRangeAvailable(const gfx::Range& range) const {
if (!range.IsValid() || range.is_reversed() ||
(eof_pos_ > 0 && eof_pos_ < range.end()))
(eof_pos_ > 0 && eof_pos_ < range.end())) {
return false;
}

if (range.is_empty())
return true;

const gfx::Range chunks_range(GetChunkIndex(range.start()),
GetChunkIndex(range.end() + kChunkSize - 1));
GetChunkEnd(range.end()));
return filled_chunks_.Contains(chunks_range);
}

Expand All @@ -94,11 +98,13 @@ class ChunkStream {
}

uint32_t filled_chunks_count() const { return filled_chunks_count_; }
uint32_t total_chunks_count() const {
return GetChunkIndex(eof_pos_ + kChunkSize - 1);
}
uint32_t total_chunks_count() const { return GetChunkEnd(eof_pos_); }

private:
uint32_t GetChunkEnd(uint32_t offset) const {
return (offset + kChunkSize - 1) / kChunkSize;
}

std::vector<std::unique_ptr<ChunkData>> data_;
uint32_t eof_pos_ = 0;
RangeSet filled_chunks_;
Expand Down

0 comments on commit fc4640d

Please sign in to comment.