diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-07 03:59:06 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-07 03:59:06 +0000 |
commit | a3f72189102fc85a7f9cfe60c61e124a2677f0ea (patch) | |
tree | fa5699e92f87a628740fd9b88960b6449541055d /base/strings/utf_offset_string_conversions.h | |
parent | 3d350326fd4def19e343890c55c5388e370c7c2f (diff) | |
download | chromium_src-a3f72189102fc85a7f9cfe60c61e124a2677f0ea.zip chromium_src-a3f72189102fc85a7f9cfe60c61e124a2677f0ea.tar.gz chromium_src-a3f72189102fc85a7f9cfe60c61e124a2677f0ea.tar.bz2 |
Move utf_offset_string_conversions and utf_string_conversion_utils to strings.
Review URL: https://codereview.chromium.org/12087115
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@181183 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/strings/utf_offset_string_conversions.h')
-rw-r--r-- | base/strings/utf_offset_string_conversions.h | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/base/strings/utf_offset_string_conversions.h b/base/strings/utf_offset_string_conversions.h new file mode 100644 index 0000000..98f29b9 --- /dev/null +++ b/base/strings/utf_offset_string_conversions.h @@ -0,0 +1,93 @@ +// 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. + +#ifndef BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ +#define BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ + +#include <string> +#include <vector> + +#include "base/base_export.h" +#include "base/string16.h" +#include "base/string_piece.h" + +namespace base { + +// 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 string16::npos. |offset[s]_for_adjustment| may be NULL. +BASE_EXPORT bool UTF8ToUTF16AndAdjustOffset(const char* src, + size_t src_len, + string16* output, + size_t* offset_for_adjustment); +BASE_EXPORT bool UTF8ToUTF16AndAdjustOffsets( + const char* src, + size_t src_len, + string16* output, + std::vector<size_t>* offsets_for_adjustment); + +BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffset(const base::StringPiece& utf8, + size_t* offset_for_adjustment); +BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffsets( + const base::StringPiece& utf8, + std::vector<size_t>* offsets_for_adjustment); + +BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffset( + const base::StringPiece16& utf16, + size_t* offset_for_adjustment); +BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffsets( + const base::StringPiece16& 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_; +}; + +// Stack object which, on destruction, will update a vector of offsets based on +// any supplied adjustments. To use, declare one of these, providing the +// address of the offset vector to adjust. Then Add() any number of Adjustments +// (each Adjustment gives the |original_offset| of a substring and the lengths +// of the substring before and after transforming). When the OffsetAdjuster +// goes out of scope, all the offsets in the provided vector will be updated. +class BASE_EXPORT OffsetAdjuster { + public: + struct BASE_EXPORT Adjustment { + Adjustment(size_t original_offset, + size_t original_length, + size_t output_length); + + size_t original_offset; + size_t original_length; + size_t output_length; + }; + + explicit OffsetAdjuster(std::vector<size_t>* offsets_for_adjustment); + ~OffsetAdjuster(); + + void Add(const Adjustment& adjustment); + + private: + void AdjustOffset(std::vector<size_t>::iterator offset); + + std::vector<size_t>* offsets_for_adjustment_; + std::vector<Adjustment> adjustments_; +}; + +} // namespace base + +#endif // BASE_STRINGS_UTF_OFFSET_STRING_CONVERSIONS_H_ |