OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/util/character_set_converters.h" | 5 #include "chrome/browser/sync/util/character_set_converters.h" |
6 | 6 |
7 #include <string> | |
8 | |
9 using std::string; | |
10 | |
11 namespace browser_sync { | 7 namespace browser_sync { |
12 | 8 |
13 void PathStringToUTF8(const PathChar* wide, int size, | 9 void TrimPathStringToValidCharacter(PathString* string) { |
14 std::string* output_string) { | 10 // Constants from http://en.wikipedia.org/wiki/UTF-8 |
15 CHECK(output_string); | 11 CHECK(string); |
16 output_string->clear(); | 12 if (string->empty()) |
17 AppendPathStringToUTF8(wide, size, output_string); | 13 return; |
18 } | 14 if (0 == (string->at(string->length() - 1) & 0x080)) |
19 | 15 return; |
20 bool UTF8ToPathString(const char* utf8, size_t size, | 16 size_t partial_enc_bytes = 0; |
21 PathString* output_string) { | 17 for (partial_enc_bytes = 0 ; true ; ++partial_enc_bytes) { |
22 CHECK(output_string); | 18 if (4 == partial_enc_bytes || partial_enc_bytes == string->length()) { |
23 output_string->clear(); | 19 // original string was broken, garbage in, garbage out. |
24 return AppendUTF8ToPathString(utf8, size, output_string); | 20 return; |
25 }; | 21 } |
26 | 22 PathChar c = string->at(string->length() - 1 - partial_enc_bytes); |
27 ToUTF8::ToUTF8(const PathChar* wide, size_t size) { | 23 if ((c & 0x0c0) == 0x080) // utf continuation char; |
28 PathStringToUTF8(wide, size, &result_); | 24 continue; |
29 } | 25 if ((c & 0x0e0) == 0x0e0) { // 2-byte encoded char. |
30 | 26 if (1 == partial_enc_bytes) |
31 ToUTF8::ToUTF8(const PathString& wide) { | 27 return; |
32 PathStringToUTF8(wide.data(), wide.length(), &result_); | 28 else |
33 } | 29 break; |
34 | 30 } |
35 ToUTF8::ToUTF8(const PathChar* wide) { | 31 if ((c & 0x0f0) == 0xc0) { // 3-byte encoded char. |
36 PathStringToUTF8(wide, PathLen(wide), &result_); | 32 if (2 == partial_enc_bytes) |
37 } | 33 return; |
38 | 34 else |
39 ToPathString::ToPathString(const char* utf8, size_t size) { | 35 break; |
40 good_ = UTF8ToPathString(utf8, size, &result_); | 36 } |
41 good_checked_ = false; | 37 if ((c & 0x0f8) == 0x0f0) { // 4-byte encoded char. |
42 } | 38 if (3 == partial_enc_bytes) |
43 | 39 return; |
44 ToPathString::ToPathString(const std::string& utf8) { | 40 else |
45 good_ = UTF8ToPathString(utf8.data(), utf8.length(), &result_); | 41 break; |
46 good_checked_ = false; | 42 } |
47 } | 43 } |
48 | 44 string->resize(string->length() - 1 - partial_enc_bytes); |
49 ToPathString::ToPathString(const char* utf8) { | |
50 good_ = UTF8ToPathString(utf8, strlen(utf8), &result_); | |
51 good_checked_ = false; | |
52 } | 45 } |
53 | 46 |
54 } // namespace browser_sync | 47 } // namespace browser_sync |
OLD | NEW |