| 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> | 7 #include <string> |
| 8 | 8 |
| 9 using std::string; | 9 using std::string; |
| 10 | 10 |
| 11 namespace browser_sync { | 11 namespace browser_sync { |
| 12 | 12 |
| 13 // Converts input_string to UTF8 and appends the result into output_string. | 13 // Returns UTF8 string from the given FilePath. |
| 14 void AppendPathStringToUTF8(const PathChar *wide, int size, | 14 std::string FilePathToUTF8(const FilePath& file_path) { |
| 15 string* output_string) { | 15 return file_path.value(); |
| 16 output_string->append(wide, size); | |
| 17 } | 16 } |
| 18 | 17 |
| 19 bool AppendUTF8ToPathString(const char* utf8, size_t size, | 18 // Returns FilePath from the given UTF8 string. |
| 20 PathString* output_string) { | 19 FilePath UTF8ToFilePath(const std::string& utf8) { |
| 21 output_string->append(utf8, size); | 20 return FilePath(utf8); |
| 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 size_t 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 } | |
| 47 if ((c & 0x0f0) == 0xc0) { // 3-byte encoded char. | |
| 48 if (2 == partial_enc_bytes) | |
| 49 return; | |
| 50 else | |
| 51 break; | |
| 52 } | |
| 53 if ((c & 0x0f8) == 0x0f0) { // 4-byte encoded char. | |
| 54 if (3 == partial_enc_bytes) | |
| 55 return; | |
| 56 else | |
| 57 break; | |
| 58 } | |
| 59 } | |
| 60 string->resize(string->length() - 1 - partial_enc_bytes); | |
| 61 } | 21 } |
| 62 | 22 |
| 63 } // namespace browser_sync | 23 } // namespace browser_sync |
| OLD | NEW |