| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Implementation of ChunkRange class. | 5 // Implementation of ChunkRange class. |
| 6 | 6 |
| 7 #include "chrome/browser/safe_browsing/chunk_range.h" | 7 #include "chrome/browser/safe_browsing/chunk_range.h" |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "base/string_split.h" | 11 #include "base/string_split.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 | 13 |
| 14 ChunkRange::ChunkRange(int start) : start_(start), stop_(start) { | 14 ChunkRange::ChunkRange(int start) : start_(start), stop_(start) { |
| 15 } | 15 } |
| 16 | 16 |
| 17 ChunkRange::ChunkRange(int start, int stop) : start_(start), stop_(stop) { | 17 ChunkRange::ChunkRange(int start, int stop) : start_(start), stop_(stop) { |
| 18 } | 18 } |
| 19 | 19 |
| 20 ChunkRange::ChunkRange(const ChunkRange& rhs) | 20 ChunkRange::ChunkRange(const ChunkRange& rhs) |
| 21 : start_(rhs.start()), stop_(rhs.stop()) { | 21 : start_(rhs.start()), stop_(rhs.stop()) { |
| 22 } | 22 } |
| 23 | 23 |
| 24 // Helper functions ----------------------------------------------------------- | 24 // Helper functions ----------------------------------------------------------- |
| 25 | 25 |
| 26 // Traverse the chunks vector looking for contiguous integers. | 26 void ChunksToRangeString(const std::vector<int>& chunks, std::string* result) { |
| 27 void ChunksToRanges(const std::vector<int>& chunks, | 27 // The following code requires the range to be sorted. |
| 28 std::vector<ChunkRange>* ranges) { | 28 std::vector<int> sorted_chunks(chunks); |
| 29 DCHECK(ranges); | 29 std::sort(sorted_chunks.begin(), sorted_chunks.end()); |
| 30 for (size_t i = 0; i < chunks.size(); ++i) { | 30 |
| 31 int start = static_cast<int>(i); | 31 DCHECK(result); |
| 32 int next = start + 1; | 32 result->clear(); |
| 33 while (next < static_cast<int>(chunks.size()) && | 33 std::vector<int>::const_iterator iter = sorted_chunks.begin(); |
| 34 (chunks[start] == chunks[next] - 1 || | 34 while (iter != sorted_chunks.end()) { |
| 35 chunks[start] == chunks[next])) { | 35 const int range_begin = *iter; |
| 36 ++start; | 36 int range_end = *iter; |
| 37 ++next; | 37 |
| 38 // Extend the range forward across duplicates and increments. |
| 39 for (; iter != sorted_chunks.end() && *iter <= range_end + 1; ++iter) { |
| 40 range_end = *iter; |
| 38 } | 41 } |
| 39 ranges->push_back(ChunkRange(chunks[i], chunks[start])); | 42 |
| 40 if (next >= static_cast<int>(chunks.size())) | 43 if (!result->empty()) |
| 41 break; | 44 result->append(","); |
| 42 i = start; | 45 result->append(base::IntToString(range_begin)); |
| 46 if (range_end > range_begin) { |
| 47 result->append("-"); |
| 48 result->append(base::IntToString(range_end)); |
| 49 } |
| 43 } | 50 } |
| 44 } | 51 } |
| 45 | 52 |
| 46 void RangesToChunks(const std::vector<ChunkRange>& ranges, | 53 void RangesToChunks(const std::vector<ChunkRange>& ranges, |
| 47 std::vector<int>* chunks) { | 54 std::vector<int>* chunks) { |
| 48 DCHECK(chunks); | 55 DCHECK(chunks); |
| 49 for (size_t i = 0; i < ranges.size(); ++i) { | 56 for (size_t i = 0; i < ranges.size(); ++i) { |
| 50 const ChunkRange& range = ranges[i]; | 57 const ChunkRange& range = ranges[i]; |
| 51 for (int chunk = range.start(); chunk <= range.stop(); ++chunk) { | 58 for (int chunk = range.start(); chunk <= range.stop(); ++chunk) { |
| 52 chunks->push_back(chunk); | 59 chunks->push_back(chunk); |
| 53 } | 60 } |
| 54 } | 61 } |
| 55 } | 62 } |
| 56 | 63 |
| 57 void RangesToString(const std::vector<ChunkRange>& ranges, | |
| 58 std::string* result) { | |
| 59 DCHECK(result); | |
| 60 result->clear(); | |
| 61 std::vector<ChunkRange>::const_iterator it = ranges.begin(); | |
| 62 for (; it != ranges.end(); ++it) { | |
| 63 if (!result->empty()) | |
| 64 result->append(","); | |
| 65 if (it->start() == it->stop()) { | |
| 66 std::string num_buf = base::IntToString(it->start()); | |
| 67 result->append(num_buf); | |
| 68 } else { | |
| 69 result->append(StringPrintf("%d-%d", it->start(), it->stop())); | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 bool StringToRanges(const std::string& input, | 64 bool StringToRanges(const std::string& input, |
| 75 std::vector<ChunkRange>* ranges) { | 65 std::vector<ChunkRange>* ranges) { |
| 76 DCHECK(ranges); | 66 DCHECK(ranges); |
| 77 | 67 |
| 78 // Crack the string into chunk parts, then crack each part looking for a | 68 // Crack the string into chunk parts, then crack each part looking for a |
| 79 // range. | 69 // range. |
| 80 std::vector<std::string> chunk_parts; | 70 std::vector<std::string> chunk_parts; |
| 81 base::SplitString(input, ',', &chunk_parts); | 71 base::SplitString(input, ',', &chunk_parts); |
| 82 | 72 |
| 83 for (size_t i = 0; i < chunk_parts.size(); ++i) { | 73 for (size_t i = 0; i < chunk_parts.size(); ++i) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 105 |
| 116 // Adjust our mid point. | 106 // Adjust our mid point. |
| 117 if (chunk.stop() < chunk_number) | 107 if (chunk.stop() < chunk_number) |
| 118 low = mid + 1; | 108 low = mid + 1; |
| 119 else | 109 else |
| 120 high = mid - 1; | 110 high = mid - 1; |
| 121 } | 111 } |
| 122 | 112 |
| 123 return false; | 113 return false; |
| 124 } | 114 } |
| OLD | NEW |