Index: chrome/common/string_index.cc |
diff --git a/chrome/common/string_index.cc b/chrome/common/string_index.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5cd90076601974eb3953bd62cee51a0c7fa1761a |
--- /dev/null |
+++ b/chrome/common/string_index.cc |
@@ -0,0 +1,71 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/common/string_index.h" |
+ |
+StringIndex::StringIndex(): kMaxDigitValue(kMaxDigit - kZeroDigit), |
akalin
2011/10/11 19:34:39
surely you don't need these to be initialized for
|
+ kMidDigitValue(kMidDigit - kZeroDigit) { |
+} |
+ |
+std::string StringIndex::RemoveTrailingZeros(const std::string& value) { |
+ int end_position = value.length() - 1; |
akalin
2011/10/11 19:34:39
assert that value is non-empty
|
+ while (value[end_position] == kZeroDigit) { |
+ --end_position; |
+ } |
+ |
+ return value.substr(0, end_position + 1); |
+} |
+ |
+void StringIndex::AddHalf(int position, std::string& value) { |
+ value[position] += kMidDigitValue; |
+ |
+ if (value[position] > kMaxDigit) { |
+ value[position] -= kMaxDigitValue; |
+ ++value[position - 1]; |
akalin
2011/10/11 19:34:39
what if this also overflows? consider (.99999 + .
csharp1
2011/10/11 21:46:03
I'm fairly certain it can't overflow.
It could on
csharp1
2011/10/11 21:46:03
I'm fairly certain it can't overflow.
It could on
akalin
2011/10/11 22:00:24
Ah, it can't overflow because mid digit value is s
csharp1
2011/10/12 15:31:12
I fixed the middle digit bug, but I still don't se
akalin
2011/10/12 18:13:07
I did, in my previous two comments about it. :P To
csharp1
2011/10/12 18:52:46
Ok, I made a unit test with the example you gave a
|
+ } |
+} |
+ |
+int StringIndex::GetPositionValue(const std::string& value, size_t index) { |
akalin
2011/10/11 19:34:39
this can be a static function, or a free function
|
+ return index < value.length() ? value[index] - kZeroDigit : 0; |
+} |
+ |
+std::string StringIndex::CreateStringBetween(const std::string& start, |
+ const std::string& end) { |
+ // If we have no end value, we create one that has the maximum value |
+ // to use as our end point. |
+ if (end.length() == 0) { |
akalin
2011/10/11 19:34:39
end.empty()
|
+ return CreateStringBetween(start, |
akalin
2011/10/11 19:34:39
this fails the precondition below
csharp1
2011/10/11 21:46:03
I'm not sure what you mean here. This does fail st
akalin
2011/10/11 22:00:24
Seems better to go the extra step to make the prec
|
+ std::string(start.length(), kMaxDigit)); |
+ } |
akalin
2011/10/11 19:34:39
assert that start < end || start.empty()
assert t
|
+ |
+ size_t max_size = std::max(start.length(), end.length()); |
+ std::string middle_string(max_size, kZeroDigit); |
+ |
+ bool add_half = false; |
+ for (size_t i = 0; i < max_size; ++i) { |
+ int char_value = GetPositionValue(start, i); |
+ char_value += GetPositionValue(end, i); |
+ |
+ middle_string[i] += (char_value / 2); |
+ if (add_half) { |
+ AddHalf(i, middle_string); |
+ } |
+ add_half = (char_value % 2 == 1); |
+ } |
+ |
+ std::string no_trailing_zeros = RemoveTrailingZeros(middle_string); |
+ if (no_trailing_zeros.length() == 0) { |
+ middle_string += kMidDigit; |
akalin
2011/10/11 19:34:39
pretty sure this is only needed because you don't
|
+ } else { |
+ middle_string = no_trailing_zeros; |
+ } |
+ |
+ // We only add an extra digit if it is required for uniqueness, otherwise |
+ // we accept a close to middle placement |
+ if (start == middle_string) { |
+ middle_string += kMidDigit; |
+ } |
+ |
+ return middle_string; |
akalin
2011/10/11 19:34:39
assert that start < middle < end
|
+} |