blob: 97b641edda8b49a9a9797ec4cd08966f2400c277 [file] [log] [blame]
[email protected]90509cb2011-03-25 18:46:381// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]b9f93832009-11-13 19:27:482// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_UTF_OFFSET_STRING_CONVERSIONS_H_
6#define BASE_UTF_OFFSET_STRING_CONVERSIONS_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]b9f93832009-11-13 19:27:488
9#include <string>
[email protected]421de2ab2011-04-13 18:43:0510#include <vector>
[email protected]b9f93832009-11-13 19:27:4811
[email protected]0bea7252011-08-05 15:34:0012#include "base/base_export.h"
[email protected]b9f93832009-11-13 19:27:4813#include "base/string16.h"
[email protected]b1ecd2fc2011-12-22 21:54:4914#include "base/string_piece.h"
[email protected]b9f93832009-11-13 19:27:4815
[email protected]421de2ab2011-04-13 18:43:0516// Like the conversions in utf_string_conversions.h, but also takes one or more
17// offsets (|offset[s]_for_adjustment|) into the source strings, each offset
18// will be adjusted to point at the same logical place in the result strings.
19// If this isn't possible because an offset points past the end of the source
20// strings or into the middle of a multibyte sequence, the offending offset will
[email protected]04866c42011-05-03 20:03:5021// be set to string16::npos. |offset[s]_for_adjustment| may be NULL.
[email protected]0bea7252011-08-05 15:34:0022BASE_EXPORT bool UTF8ToUTF16AndAdjustOffset(const char* src,
23 size_t src_len,
24 string16* output,
25 size_t* offset_for_adjustment);
26BASE_EXPORT bool UTF8ToUTF16AndAdjustOffsets(
[email protected]421de2ab2011-04-13 18:43:0527 const char* src,
28 size_t src_len,
[email protected]04866c42011-05-03 20:03:5029 string16* output,
[email protected]421de2ab2011-04-13 18:43:0530 std::vector<size_t>* offsets_for_adjustment);
31
[email protected]0bea7252011-08-05 15:34:0032BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffset(const base::StringPiece& utf8,
33 size_t* offset_for_adjustment);
34BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffsets(
[email protected]421de2ab2011-04-13 18:43:0535 const base::StringPiece& utf8,
36 std::vector<size_t>* offsets_for_adjustment);
[email protected]90509cb2011-03-25 18:46:3837
[email protected]cbf35e172011-09-08 02:18:1038BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffset(
39 const base::StringPiece16& utf16,
40 size_t* offset_for_adjustment);
41BASE_EXPORT std::string UTF16ToUTF8AndAdjustOffsets(
42 const base::StringPiece16& utf16,
43 std::vector<size_t>* offsets_for_adjustment);
44
[email protected]421de2ab2011-04-13 18:43:0545// Limiting function callable by std::for_each which will replace any value
46// which is equal to or greater than |limit| with npos.
47template <typename T>
48struct LimitOffset {
49 explicit LimitOffset(size_t limit)
50 : limit_(limit) {}
51
52 void operator()(size_t& offset) {
53 if (offset >= limit_)
54 offset = T::npos;
55 }
56
57 size_t limit_;
58};
59
[email protected]04866c42011-05-03 20:03:5060// Stack object which, on destruction, will update a vector of offsets based on
61// any supplied adjustments. To use, declare one of these, providing the
62// address of the offset vector to adjust. Then Add() any number of Adjustments
63// (each Adjustment gives the |original_offset| of a substring and the lengths
64// of the substring before and after transforming). When the OffsetAdjuster
65// goes out of scope, all the offsets in the provided vector will be updated.
[email protected]0bea7252011-08-05 15:34:0066class BASE_EXPORT OffsetAdjuster {
[email protected]04866c42011-05-03 20:03:5067 public:
[email protected]0bea7252011-08-05 15:34:0068 struct BASE_EXPORT Adjustment {
[email protected]04866c42011-05-03 20:03:5069 Adjustment(size_t original_offset,
70 size_t original_length,
71 size_t output_length);
[email protected]421de2ab2011-04-13 18:43:0572
[email protected]04866c42011-05-03 20:03:5073 size_t original_offset;
74 size_t original_length;
75 size_t output_length;
[email protected]421de2ab2011-04-13 18:43:0576 };
77
[email protected]04866c42011-05-03 20:03:5078 explicit OffsetAdjuster(std::vector<size_t>* offsets_for_adjustment);
79 ~OffsetAdjuster();
[email protected]421de2ab2011-04-13 18:43:0580
[email protected]04866c42011-05-03 20:03:5081 void Add(const Adjustment& adjustment);
[email protected]421de2ab2011-04-13 18:43:0582
[email protected]04866c42011-05-03 20:03:5083 private:
84 void AdjustOffset(std::vector<size_t>::iterator offset);
85
86 std::vector<size_t>* offsets_for_adjustment_;
87 std::vector<Adjustment> adjustments_;
[email protected]421de2ab2011-04-13 18:43:0588};
[email protected]b9f93832009-11-13 19:27:4889
90#endif // BASE_UTF_OFFSET_STRING_CONVERSIONS_H_