| 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 namespace browser_sync { | |
| 8 | |
| 9 void TrimPathStringToValidCharacter(std::string* string) { | |
| 10 // Constants from http://en.wikipedia.org/wiki/UTF-8 | |
| 11 CHECK(string); | |
| 12 if (string->empty()) | |
| 13 return; | |
| 14 if (0 == (string->at(string->length() - 1) & 0x080)) | |
| 15 return; | |
| 16 size_t partial_enc_bytes = 0; | |
| 17 for (partial_enc_bytes = 0 ; true ; ++partial_enc_bytes) { | |
| 18 if (4 == partial_enc_bytes || partial_enc_bytes == string->length()) { | |
| 19 // original string was broken, garbage in, garbage out. | |
| 20 return; | |
| 21 } | |
| 22 PathChar c = string->at(string->length() - 1 - partial_enc_bytes); | |
| 23 if ((c & 0x0c0) == 0x080) // utf continuation char; | |
| 24 continue; | |
| 25 if ((c & 0x0e0) == 0x0e0) { // 2-byte encoded char. | |
| 26 if (1 == partial_enc_bytes) | |
| 27 return; | |
| 28 else | |
| 29 break; | |
| 30 } | |
| 31 if ((c & 0x0f0) == 0xc0) { // 3-byte encoded char. | |
| 32 if (2 == partial_enc_bytes) | |
| 33 return; | |
| 34 else | |
| 35 break; | |
| 36 } | |
| 37 if ((c & 0x0f8) == 0x0f0) { // 4-byte encoded char. | |
| 38 if (3 == partial_enc_bytes) | |
| 39 return; | |
| 40 else | |
| 41 break; | |
| 42 } | |
| 43 } | |
| 44 string->resize(string->length() - 1 - partial_enc_bytes); | |
| 45 } | |
| 46 | |
| 47 } // namespace browser_sync | |
| OLD | NEW |