Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Unified Diff: pdf/chunk_stream.cc

Issue 2911853002: Refactor PDF ChunkStream and DocumentLoader code. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pdf/chunk_stream.h ('k') | pdf/document_loader.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pdf/chunk_stream.cc
diff --git a/pdf/chunk_stream.cc b/pdf/chunk_stream.cc
index d9dae1e20af0a048f83be43386eaec5eaa589b75..4eae4a958db9fbc45c22ac6b16a5683f9b12faee 100644
--- a/pdf/chunk_stream.cc
+++ b/pdf/chunk_stream.cc
@@ -7,14 +7,8 @@
#include <stddef.h>
#include <string.h>
-#define __STDC_LIMIT_MACROS
-#ifdef _WIN32
-#include <limits.h>
-#else
-#include <stdint.h>
-#endif
-
#include <algorithm>
+#include <limits>
namespace chrome_pdf {
@@ -38,7 +32,7 @@ size_t ChunkStream::GetSize() const {
}
bool ChunkStream::WriteData(size_t offset, void* buffer, size_t size) {
- if (SIZE_MAX - size < offset)
+ if (std::numeric_limits<size_t>::max() - size < offset)
return false;
if (data_.size() < offset + size)
@@ -51,28 +45,22 @@ bool ChunkStream::WriteData(size_t offset, void* buffer, size_t size) {
return true;
}
- std::map<size_t, size_t>::iterator start = chunks_.upper_bound(offset);
- if (start != chunks_.begin())
- --start; // start now points to the key equal or lower than offset.
- if (start->first + start->second < offset)
- ++start; // start element is entirely before current chunk, skip it.
-
- std::map<size_t, size_t>::iterator end = chunks_.upper_bound(offset + size);
+ auto start = GetStartChunk(offset);
+ auto end = chunks_.upper_bound(offset + size);
if (start == end) { // No chunks to merge.
chunks_[offset] = size;
return true;
}
- --end;
+ auto prev = end;
+ --prev;
+ size_t prev_size = prev->first + prev->second;
size_t new_offset = std::min<size_t>(start->first, offset);
- size_t new_size =
- std::max<size_t>(end->first + end->second, offset + size) - new_offset;
-
- chunks_.erase(start, ++end);
+ size_t new_size = std::max<size_t>(prev_size, offset + size) - new_offset;
+ chunks_.erase(start, end);
chunks_[new_offset] = new_size;
-
return true;
}
@@ -97,22 +85,15 @@ bool ChunkStream::GetMissedRanges(
return true;
}
- std::map<size_t, size_t>::const_iterator start = chunks_.upper_bound(offset);
- if (start != chunks_.begin())
- --start; // start now points to the key equal or lower than offset.
- if (start->first + start->second < offset)
- ++start; // start element is entirely before current chunk, skip it.
-
- std::map<size_t, size_t>::const_iterator end =
- chunks_.upper_bound(offset + size);
+ auto start = GetStartChunk(offset);
+ auto end = chunks_.upper_bound(offset + size);
if (start == end) { // No data in the current range available.
ranges->push_back(std::pair<size_t, size_t>(offset, size));
return true;
}
size_t cur_offset = offset;
- std::map<size_t, size_t>::const_iterator it;
- for (it = start; it != end; ++it) {
+ for (auto it = start; it != end; ++it) {
if (cur_offset < it->first) {
size_t new_size = it->first - cur_offset;
ranges->push_back(std::pair<size_t, size_t>(cur_offset, new_size));
@@ -123,9 +104,10 @@ bool ChunkStream::GetMissedRanges(
}
// Add last chunk.
- if (cur_offset < offset + size)
+ if (cur_offset < offset + size) {
ranges->push_back(
std::pair<size_t, size_t>(cur_offset, offset + size - cur_offset));
+ }
return true;
}
@@ -134,28 +116,28 @@ bool ChunkStream::IsRangeAvailable(size_t offset, size_t size) const {
if (chunks_.empty())
return false;
- if (SIZE_MAX - size < offset)
+ if (std::numeric_limits<size_t>::max() - size < offset)
return false;
- std::map<size_t, size_t>::const_iterator it = chunks_.upper_bound(offset);
+ auto it = chunks_.upper_bound(offset);
if (it == chunks_.begin())
return false; // No chunks includes offset byte.
--it; // Now it starts equal or before offset.
- return (it->first + it->second) >= (offset + size);
+ return it->first + it->second >= offset + size;
}
size_t ChunkStream::GetFirstMissingByte() const {
if (chunks_.empty())
return 0;
- std::map<size_t, size_t>::const_iterator begin = chunks_.begin();
+ auto begin = chunks_.begin();
return begin->first > 0 ? 0 : begin->second;
}
size_t ChunkStream::GetFirstMissingByteInInterval(size_t offset) const {
if (chunks_.empty())
return 0;
- std::map<size_t, size_t>::const_iterator it = chunks_.upper_bound(offset);
+ auto it = chunks_.upper_bound(offset);
if (it == chunks_.begin())
return 0;
--it;
@@ -165,10 +147,20 @@ size_t ChunkStream::GetFirstMissingByteInInterval(size_t offset) const {
size_t ChunkStream::GetLastMissingByteInInterval(size_t offset) const {
if (chunks_.empty())
return stream_size_ - 1;
- std::map<size_t, size_t>::const_iterator it = chunks_.upper_bound(offset);
+ auto it = chunks_.upper_bound(offset);
if (it == chunks_.end())
return stream_size_ - 1;
return it->first - 1;
}
+std::map<size_t, size_t>::const_iterator ChunkStream::GetStartChunk(
+ size_t offset) const {
+ auto start = chunks_.upper_bound(offset);
+ if (start != chunks_.begin())
+ --start; // start now points to the key equal or lower than offset.
+ if (start->first + start->second < offset)
+ ++start; // start element is entirely before current chunk, skip it.
+ return start;
+}
+
} // namespace chrome_pdf
« no previous file with comments | « pdf/chunk_stream.h ('k') | pdf/document_loader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698