OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 "mojo/common/common_type_converters.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/strings/utf_string_conversions.h" | |
10 | |
11 namespace mojo { | |
12 | |
13 // static | |
14 String TypeConverter<String, base::StringPiece>::Convert( | |
15 const base::StringPiece& input) { | |
16 if (input.empty()) { | |
17 char c = 0; | |
18 return String(&c, 0); | |
19 } | |
20 return String(input.data(), input.size()); | |
21 } | |
22 // static | |
23 base::StringPiece TypeConverter<base::StringPiece, String>::Convert( | |
24 const String& input) { | |
25 return input.get(); | |
26 } | |
27 | |
28 // static | |
29 String TypeConverter<String, base::string16>::Convert( | |
30 const base::string16& input) { | |
31 return TypeConverter<String, base::StringPiece>::Convert( | |
32 base::UTF16ToUTF8(input)); | |
33 } | |
34 // static | |
35 base::string16 TypeConverter<base::string16, String>::Convert( | |
36 const String& input) { | |
37 return base::UTF8ToUTF16(input.To<base::StringPiece>()); | |
38 } | |
39 | |
40 std::string TypeConverter<std::string, Array<uint8_t> >::Convert( | |
41 const Array<uint8_t>& input) { | |
42 if (input.is_null()) | |
43 return std::string(); | |
44 | |
45 return std::string(reinterpret_cast<const char*>(&input.front()), | |
46 input.size()); | |
47 } | |
48 | |
49 Array<uint8_t> TypeConverter<Array<uint8_t>, std::string>::Convert( | |
50 const std::string& input) { | |
51 Array<uint8_t> result(input.size()); | |
52 memcpy(&result.front(), input.c_str(), input.size()); | |
53 return result.Pass(); | |
54 } | |
55 | |
56 } // namespace mojo | |
OLD | NEW |