OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/common/string_index.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 std::string StringIndex::RemoveTrailingZeros(const std::string& value) { | |
10 DCHECK(!value.empty()); | |
11 | |
12 int end_position = value.length() - 1; | |
13 while (value[end_position] == kZeroDigit) { | |
14 --end_position; | |
15 } | |
16 | |
17 return value.substr(0, end_position + 1); | |
18 } | |
19 | |
20 void StringIndex::AddHalf(size_t position, std::string& value) { | |
21 DCHECK(position > 0); | |
22 value[position] += kMidDigitValue; | |
23 | |
24 if (value[position] > kMaxDigit) { | |
25 value[position] -= kFullDigitValue; | |
26 ++value[position - 1]; | |
27 DCHECK(value[position - 1] <= kMaxDigit); | |
akalin
2011/10/12 18:50:14
as I mentioned previously, this won't work. you'l
| |
28 } | |
29 } | |
30 | |
31 void StringIndex::ValidateString(const std::string& value) { | |
32 if (value.empty()) { | |
33 return; | |
34 } | |
35 | |
36 DCHECK(value[0] >= kZeroDigit); | |
akalin
2011/10/12 18:50:14
use DCHECK_GE, DCHECK_LE, etc.
| |
37 DCHECK(value[0] <= kMaxDigit); | |
38 | |
39 for (size_t i = 1; i < value.length(); ++i) { | |
40 DCHECK(value[i] >= kMinDigit); | |
41 DCHECK(value[i] <= kMaxDigit); | |
42 } | |
43 } | |
44 | |
45 std::string StringIndex::CreateStringBetween(const std::string& start, | |
46 const std::string& end) { | |
47 ValidateString(start); | |
48 ValidateString(end); | |
49 std::string start_expanded = start; | |
50 std::string end_expanded = end; | |
51 | |
52 // If we have no end value, we create one that has the maximum value | |
53 // to use as our end point. | |
54 if (end_expanded.empty()) { | |
55 int length = start_expanded.length(); | |
56 if (start_expanded.find_first_not_of(kMaxDigit) == std::string::npos) { | |
57 //Ensure we are bigger than start if it is Z* | |
58 ++length; | |
59 } | |
60 | |
61 end_expanded = std::string(length, kMaxDigit); | |
62 } | |
63 | |
64 // Pad the shorter string with zeros so they are the same length | |
65 size_t max_size = std::max(start_expanded.length(), end_expanded.length()); | |
66 start_expanded.resize(max_size, kZeroDigit); | |
67 end_expanded.resize(max_size, kZeroDigit); | |
68 CHECK(start_expanded < end_expanded); | |
akalin
2011/10/12 18:50:14
CHECK_LT
| |
69 | |
70 std::string middle_string(max_size, kZeroDigit); | |
71 bool add_half = false; | |
72 for (size_t i = 0; i < max_size; ++i) { | |
73 int char_value = start_expanded[i] - kZeroDigit; | |
74 char_value += end_expanded[i] - kZeroDigit; | |
75 | |
76 middle_string[i] += (char_value / 2); | |
77 if (add_half) { | |
78 AddHalf(i, middle_string); | |
79 } | |
80 add_half = (char_value % 2 == 1); | |
81 } | |
akalin
2011/10/12 18:50:14
this should be decomped out into a helper function
| |
82 | |
83 // We only add an extra digit if it is required for uniqueness, otherwise | |
84 // we accept a close to middle placement | |
85 if (add_half && middle_string == start_expanded) { | |
86 middle_string += kMidDigit; | |
87 } | |
88 | |
89 DCHECK(middle_string > start_expanded); | |
90 DCHECK(middle_string < end_expanded); | |
91 return RemoveTrailingZeros(middle_string); | |
92 } | |
OLD | NEW |