OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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. |
| 4 |
| 5 #include "chrome/browser/sync/util/character_set_converters.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 using std::string; |
| 10 |
| 11 namespace browser_sync { |
| 12 |
| 13 // Converts input_string to UTF8 and appends the result into output_string. |
| 14 void AppendPathStringToUTF8(const PathChar *wide, int size, |
| 15 string* output_string) { |
| 16 output_string->append(wide, size); |
| 17 } |
| 18 |
| 19 bool AppendUTF8ToPathString(const char* utf8, size_t size, |
| 20 PathString* output_string) { |
| 21 output_string->append(utf8, size); |
| 22 return true; |
| 23 } |
| 24 |
| 25 void TrimPathStringToValidCharacter(PathString* string) { |
| 26 // Constants from http://en.wikipedia.org/wiki/UTF-8 |
| 27 CHECK(string); |
| 28 if (string->empty()) |
| 29 return; |
| 30 if (0 == (string->at(string->length() - 1) & 0x080)) |
| 31 return; |
| 32 int partial_enc_bytes = 0; |
| 33 for (partial_enc_bytes = 0 ; true ; ++partial_enc_bytes) { |
| 34 if (4 == partial_enc_bytes || partial_enc_bytes == string->length()) { |
| 35 // original string was broken, garbage in, garbage out. |
| 36 return; |
| 37 } |
| 38 PathChar c = string->at(string->length() - 1 - partial_enc_bytes); |
| 39 if ((c & 0x0c0) == 0x080) // utf continuation char; |
| 40 continue; |
| 41 if ((c & 0x0e0) == 0x0e0) // 2-byte encoded char. |
| 42 if (1 == partial_enc_bytes) |
| 43 return; |
| 44 else |
| 45 break; |
| 46 if ((c & 0x0f0) == 0xc0) // 3-byte encoded char. |
| 47 if (2 == partial_enc_bytes) |
| 48 return; |
| 49 else |
| 50 break; |
| 51 if ((c & 0x0f8) == 0x0f0) // 4-byte encoded char. |
| 52 if (3 == partial_enc_bytes) |
| 53 return; |
| 54 else |
| 55 break; |
| 56 } |
| 57 string->resize(string->length() - 1 - partial_enc_bytes); |
| 58 } |
| 59 |
| 60 } // namespace browser_sync |
OLD | NEW |