blob: 4c87f83993e619990011a54b39dc698c8d095b30 [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
rvargas@google.com4891e7d2011-03-26 03:46:38 +090012#include "base/base_api.h"
pkasting@chromium.org046cd5a2009-11-14 04:27:48 +090013#include "base/string16.h"
14
15namespace base {
16class StringPiece;
17}
18
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090019// Like the conversions in utf_string_conversions.h, but also takes one or more
20// offsets (|offset[s]_for_adjustment|) into the source strings, each offset
21// will be adjusted to point at the same logical place in the result strings.
22// If this isn't possible because an offset points past the end of the source
23// strings or into the middle of a multibyte sequence, the offending offset will
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090024// be set to string16::npos. |offset[s]_for_adjustment| may be NULL.
25BASE_API bool UTF8ToUTF16AndAdjustOffset(const char* src,
26 size_t src_len,
27 string16* output,
28 size_t* offset_for_adjustment);
29BASE_API bool UTF8ToUTF16AndAdjustOffsets(
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090030 const char* src,
31 size_t src_len,
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090032 string16* output,
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090033 std::vector<size_t>* offsets_for_adjustment);
34
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090035BASE_API string16 UTF8ToUTF16AndAdjustOffset(const base::StringPiece& utf8,
36 size_t* offset_for_adjustment);
37BASE_API string16 UTF8ToUTF16AndAdjustOffsets(
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090038 const base::StringPiece& utf8,
39 std::vector<size_t>* offsets_for_adjustment);
rvargas@google.com4891e7d2011-03-26 03:46:38 +090040
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090041// Limiting function callable by std::for_each which will replace any value
42// which is equal to or greater than |limit| with npos.
43template <typename T>
44struct LimitOffset {
45 explicit LimitOffset(size_t limit)
46 : limit_(limit) {}
47
48 void operator()(size_t& offset) {
49 if (offset >= limit_)
50 offset = T::npos;
51 }
52
53 size_t limit_;
54};
55
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090056// Stack object which, on destruction, will update a vector of offsets based on
57// any supplied adjustments. To use, declare one of these, providing the
58// address of the offset vector to adjust. Then Add() any number of Adjustments
59// (each Adjustment gives the |original_offset| of a substring and the lengths
60// of the substring before and after transforming). When the OffsetAdjuster
61// goes out of scope, all the offsets in the provided vector will be updated.
62class BASE_API OffsetAdjuster {
63 public:
rvargas@google.com97a20ec2011-04-22 07:22:10 +090064 struct BASE_API Adjustment {
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090065 Adjustment(size_t original_offset,
66 size_t original_length,
67 size_t output_length);
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090068
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090069 size_t original_offset;
70 size_t original_length;
71 size_t output_length;
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090072 };
73
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090074 explicit OffsetAdjuster(std::vector<size_t>* offsets_for_adjustment);
75 ~OffsetAdjuster();
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090076
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090077 void Add(const Adjustment& adjustment);
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090078
pkasting@chromium.orge6b5c202011-05-04 05:03:50 +090079 private:
80 void AdjustOffset(std::vector<size_t>::iterator offset);
81
82 std::vector<size_t>* offsets_for_adjustment_;
83 std::vector<Adjustment> adjustments_;
mrossetti@chromium.org9422b222011-04-14 03:43:05 +090084};
pkasting@chromium.org046cd5a2009-11-14 04:27:48 +090085
86#endif // BASE_UTF_OFFSET_STRING_CONVERSIONS_H_