Don't crash when SimpleCache index is corrupt.
[chromium-blink-merge.git] / ui / gfx / text_utils.cc
bloba31ef3d3cc999cd844fb4042b17628b9db976bcf
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/text_utils.h"
7 #include "base/i18n/char_iterator.h"
9 namespace gfx {
11 base::string16 RemoveAcceleratorChar(const base::string16& s,
12 base::char16 accelerator_char,
13 int* accelerated_char_pos,
14 int* accelerated_char_span) {
15 bool escaped = false;
16 ptrdiff_t last_char_pos = -1;
17 int last_char_span = 0;
18 base::i18n::UTF16CharIterator chars(&s);
19 base::string16 accelerator_removed;
21 accelerator_removed.reserve(s.size());
22 while (!chars.end()) {
23 int32 c = chars.get();
24 int array_pos = chars.array_pos();
25 chars.Advance();
27 if (c != accelerator_char || escaped) {
28 int span = chars.array_pos() - array_pos;
29 if (escaped && c != accelerator_char) {
30 last_char_pos = accelerator_removed.size();
31 last_char_span = span;
33 for (int i = 0; i < span; i++)
34 accelerator_removed.push_back(s[array_pos + i]);
35 escaped = false;
36 } else {
37 escaped = true;
41 if (accelerated_char_pos)
42 *accelerated_char_pos = last_char_pos;
43 if (accelerated_char_span)
44 *accelerated_char_span = last_char_span;
46 return accelerator_removed;
49 } // namespace gfx