blob: 97b641edda8b49a9a9797ec4cd08966f2400c277 [file] [log] [blame]
rvargas@google.com4891e7d2011-03-26 03:46:38 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
pkasting@chromium.org046cd5a2009-11-14 04:27:48 +09002// 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_
thakis@chromium.org01d14522010-07-27 08:08:24 +09007#pragma once
pkasting@chromium.org046cd5a2009-11-14 04:27:48 +09008
9#include <string>
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090010#include <vector>
pkasting@chromium.org046cd5a2009-11-14 04:27:48 +090011
darin@chromium.orge585bed2011-08-06 00:34:00 +090012#include "base/base_export.h"
pkasting@chromium.org046cd5a2009-11-14 04:27:48 +090013#include "base/string16.h"
erikwright@chromium.orgbd9f7722011-12-23 06:54:49 +090014#include "base/string_piece.h"
pkasting@chromium.org046cd5a2009-11-14 04:27:48 +090015
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090016// 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
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090021// be set to string16::npos. |offset[s]_for_adjustment| may be NULL.
darin@chromium.orge585bed2011-08-06 00:34:00 +090022BASE_EXPORT bool UTF8ToUTF16AndAdjustOffset(const char* src,
23 size_t src_len,
24 string16* output,
25 size_t* offset_for_adjustment);
26BASE_EXPORT bool UTF8ToUTF16AndAdjustOffsets(
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090027 const char* src,
28 size_t src_len,
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090029 string16* output,
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090030 std::vector<size_t>* offsets_for_adjustment);
31
darin@chromium.orge585bed2011-08-06 00:34:00 +090032BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffset(const base::StringPiece& utf8,
33 size_t* offset_for_adjustment);
34BASE_EXPORT string16 UTF8ToUTF16AndAdjustOffsets(
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090035 const base::StringPiece& utf8,
36 std::vector<size_t>* offsets_for_adjustment);
rvargas@google.com4891e7d2011-03-26 03:46:38 +090037
kinaba@chromium.orgfb4d5292011-09-08 11:18:10 +090038BASE_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
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090045// 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
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090060// 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.
darin@chromium.orge585bed2011-08-06 00:34:00 +090066class BASE_EXPORT OffsetAdjuster {
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090067 public:
darin@chromium.orge585bed2011-08-06 00:34:00 +090068 struct BASE_EXPORT Adjustment {
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090069 Adjustment(size_t original_offset,
70 size_t original_length,
71 size_t output_length);
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090072
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090073 size_t original_offset;
74 size_t original_length;
75 size_t output_length;
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090076 };
77
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090078 explicit OffsetAdjuster(std::vector<size_t>* offsets_for_adjustment);
79 ~OffsetAdjuster();
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090080
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090081 void Add(const Adjustment& adjustment);
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090082
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090083 private:
84 void AdjustOffset(std::vector<size_t>::iterator offset);
85
86 std::vector<size_t>* offsets_for_adjustment_;
87 std::vector<Adjustment> adjustments_;
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090088};
pkasting@chromium.org046cd5a2009-11-14 04:27:48 +090089
90#endif // BASE_UTF_OFFSET_STRING_CONVERSIONS_H_