summaryrefslogtreecommitdiffstats
path: root/base/utf_offset_string_conversions.h
diff options
context:
space:
mode:
authormrossetti@chromium.org <mrossetti@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-13 18:43:05 +0000
committermrossetti@chromium.org <mrossetti@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-13 18:43:05 +0000
commit421de2ab1eb9eb65b4b8531608be5f8889492b5f (patch)
tree5f209e09c6577c297b204dc6cec0830318eb540d /base/utf_offset_string_conversions.h
parent87938386e8e3c0fa83d3a8a3c7ced8268c7f6c86 (diff)
downloadchromium_src-421de2ab1eb9eb65b4b8531608be5f8889492b5f.zip
chromium_src-421de2ab1eb9eb65b4b8531608be5f8889492b5f.tar.gz
chromium_src-421de2ab1eb9eb65b4b8531608be5f8889492b5f.tar.bz2
Add multiple-offset versions of the various URL reformatting functions. Fixed a couple of erroneous unit tests of offsets into username/password.
Note: This does not complete the work required for 78153 -- tis but the first 2/3rds. Previously reviewed as: http://codereview.chromium.org/6822038/. BUG=78153 TEST=Many unit tests updated and added. Review URL: http://codereview.chromium.org/6839007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@81448 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/utf_offset_string_conversions.h')
-rw-r--r--base/utf_offset_string_conversions.h71
1 files changed, 66 insertions, 5 deletions
diff --git a/base/utf_offset_string_conversions.h b/base/utf_offset_string_conversions.h
index 13df1b4..152eda3 100644
--- a/base/utf_offset_string_conversions.h
+++ b/base/utf_offset_string_conversions.h
@@ -7,6 +7,7 @@
#pragma once
#include <string>
+#include <vector>
#include "base/base_api.h"
#include "base/string16.h"
@@ -15,23 +16,83 @@ namespace base {
class StringPiece;
}
-// Like the conversions in utf_string_conversions.h, but also take offsets into
-// the source strings, which will be adjusted to point at the same logical place
-// in the result strings. If this isn't possible because the offsets point past
-// the end of the source strings or into the middle of multibyte sequences, they
-// will be set to std::wstring::npos. |offset_for_adjustment| may be NULL.
+// Like the conversions in utf_string_conversions.h, but also takes one or more
+// offsets (|offset[s]_for_adjustment|) into the source strings, each offset
+// will be adjusted to point at the same logical place in the result strings.
+// If this isn't possible because an offset points past the end of the source
+// strings or into the middle of a multibyte sequence, the offending offset will
+// be set to std::wstring::npos. |offset[s]_for_adjustment| may be NULL.
BASE_API bool UTF8ToWideAndAdjustOffset(const char* src,
size_t src_len,
std::wstring* output,
size_t* offset_for_adjustment);
+BASE_API bool UTF8ToWideAndAdjustOffsets(
+ const char* src,
+ size_t src_len,
+ std::wstring* output,
+ std::vector<size_t>* offsets_for_adjustment);
+
BASE_API std::wstring UTF8ToWideAndAdjustOffset(const base::StringPiece& utf8,
size_t* offset_for_adjustment);
+BASE_API std::wstring UTF8ToWideAndAdjustOffsets(
+ const base::StringPiece& utf8,
+ std::vector<size_t>* offsets_for_adjustment);
BASE_API bool UTF16ToWideAndAdjustOffset(const char16* src,
size_t src_len,
std::wstring* output,
size_t* offset_for_adjustment);
+BASE_API bool UTF16ToWideAndAdjustOffsets(
+ const char16* src,
+ size_t src_len,
+ std::wstring* output,
+ std::vector<size_t>* offsets_for_adjustment);
+
BASE_API std::wstring UTF16ToWideAndAdjustOffset(const string16& utf16,
size_t* offset_for_adjustment);
+BASE_API std::wstring UTF16ToWideAndAdjustOffsets(
+ const string16& utf16,
+ std::vector<size_t>* offsets_for_adjustment);
+
+// Limiting function callable by std::for_each which will replace any value
+// which is equal to or greater than |limit| with npos.
+template <typename T>
+struct LimitOffset {
+ explicit LimitOffset(size_t limit)
+ : limit_(limit) {}
+
+ void operator()(size_t& offset) {
+ if (offset >= limit_)
+ offset = T::npos;
+ }
+
+ size_t limit_;
+};
+
+// Adjustment function called by std::transform which will adjust any offset
+// that occurs after one or more modified substrings. To use, create any
+// number of AdjustOffset::Adjustments, drop them into a vector, then call
+// std::transform with the transform function being something similar to
+// AdjustOffset(adjustments). Each Adjustment gives the original |location|
+// of the encoded section and the |old_length| and |new_length| of the section
+// before and after decoding.
+struct AdjustOffset {
+ // Helper structure which indicates where an encoded character occurred
+ // and how long that encoding was.
+ struct Adjustment {
+ Adjustment(size_t location, size_t old_length, size_t new_length);
+
+ size_t location;
+ size_t old_length;
+ size_t new_length;
+ };
+
+ typedef std::vector<Adjustment> Adjustments;
+
+ explicit AdjustOffset(const Adjustments& adjustments);
+ void operator()(size_t& offset);
+
+ const Adjustments& adjustments_;
+};
#endif // BASE_UTF_OFFSET_STRING_CONVERSIONS_H_