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 <windows.h> | |
8 | |
9 #include <string> | |
10 | |
11 using std::string; | |
12 | |
13 namespace browser_sync { | |
14 | |
15 // Converts input_string to UTF8 and appends the result into to output_string. | |
16 void AppendPathStringToUTF8(const PathChar* wide, int size, | |
17 string* output_string) { | |
18 CHECK(output_string); | |
19 if (0 == size) | |
20 return; | |
21 | |
22 int needed_space = ::WideCharToMultiByte(CP_UTF8, 0, wide, size, 0, 0, 0, 0); | |
23 // TODO(sync): This should flag an error when we move to an api that can let | |
24 // utf-16 -> utf-8 fail. | |
25 CHECK(0 != needed_space); | |
26 string::size_type current_size = output_string->size(); | |
27 output_string->resize(current_size + needed_space); | |
28 CHECK(0 != ::WideCharToMultiByte(CP_UTF8, 0, wide, size, | |
29 &(*output_string)[current_size], needed_space, 0, 0)); | |
30 } | |
31 | |
32 bool AppendUTF8ToPathString(const char* utf8, size_t size, | |
33 PathString* output_string) { | |
34 CHECK(output_string); | |
35 if (0 == size) | |
36 return true; | |
37 // TODO(sync): Do we want to force precomposed characters here? | |
38 int needed_wide_chars = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, | |
39 utf8, size, 0, 0); | |
40 if (0 == needed_wide_chars) { | |
41 DWORD err = ::GetLastError(); | |
42 if (MB_ERR_INVALID_CHARS == err) | |
43 return false; | |
44 CHECK(0 == err); | |
45 } | |
46 PathString::size_type current_length = output_string->size(); | |
47 output_string->resize(current_length + needed_wide_chars); | |
48 CHECK(0 != ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8, size, | |
49 &(*output_string)[current_length], needed_wide_chars)); | |
50 return true; | |
51 } | |
52 | |
53 void TrimPathStringToValidCharacter(PathString* string) { | |
54 CHECK(string); | |
55 // Constants from http://en.wikipedia.org/wiki/UTF-16 | |
56 if (string->empty()) | |
57 return; | |
58 if (0x0dc00 == (string->at(string->length() - 1) & 0x0fc00)) | |
59 string->resize(string->length() - 1); | |
60 } | |
61 | |
62 } // namespace browser_sync | |
OLD | NEW |