| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/common/common_type_converters.h" | 5 #include "mojo/common/common_type_converters.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 | |
| 13 namespace mojo { | 11 namespace mojo { |
| 14 | 12 |
| 15 // static | |
| 16 String TypeConverter<String, base::StringPiece>::Convert( | |
| 17 const base::StringPiece& input) { | |
| 18 if (input.empty()) { | |
| 19 char c = 0; | |
| 20 return String(&c, 0); | |
| 21 } | |
| 22 return String(input.data(), input.size()); | |
| 23 } | |
| 24 // static | |
| 25 base::StringPiece TypeConverter<base::StringPiece, String>::Convert( | |
| 26 const String& input) { | |
| 27 return input.get(); | |
| 28 } | |
| 29 | |
| 30 // static | |
| 31 String TypeConverter<String, base::string16>::Convert( | |
| 32 const base::string16& input) { | |
| 33 return TypeConverter<String, base::StringPiece>::Convert( | |
| 34 base::UTF16ToUTF8(input)); | |
| 35 } | |
| 36 // static | |
| 37 base::string16 TypeConverter<base::string16, String>::Convert( | |
| 38 const String& input) { | |
| 39 return base::UTF8ToUTF16(input.To<base::StringPiece>()); | |
| 40 } | |
| 41 | |
| 42 std::string TypeConverter<std::string, Array<uint8_t>>::Convert( | 13 std::string TypeConverter<std::string, Array<uint8_t>>::Convert( |
| 43 const Array<uint8_t>& input) { | 14 const Array<uint8_t>& input) { |
| 44 if (input.is_null() || input.empty()) | 15 if (input.is_null() || input.empty()) |
| 45 return std::string(); | 16 return std::string(); |
| 46 | 17 |
| 47 return std::string(reinterpret_cast<const char*>(&input.front()), | 18 return std::string(reinterpret_cast<const char*>(&input.front()), |
| 48 input.size()); | 19 input.size()); |
| 49 } | 20 } |
| 50 | 21 |
| 51 Array<uint8_t> TypeConverter<Array<uint8_t>, std::string>::Convert( | 22 Array<uint8_t> TypeConverter<Array<uint8_t>, std::string>::Convert( |
| (...skipping 23 matching lines...) Expand all Loading... |
| 75 | 46 |
| 76 Array<uint8_t> TypeConverter<Array<uint8_t>, base::string16>::Convert( | 47 Array<uint8_t> TypeConverter<Array<uint8_t>, base::string16>::Convert( |
| 77 const base::string16& input) { | 48 const base::string16& input) { |
| 78 Array<uint8_t> result(input.size() * sizeof(base::char16)); | 49 Array<uint8_t> result(input.size() * sizeof(base::char16)); |
| 79 if (!input.empty()) | 50 if (!input.empty()) |
| 80 memcpy(&result.front(), input.c_str(), input.size() * sizeof(base::char16)); | 51 memcpy(&result.front(), input.c_str(), input.size() * sizeof(base::char16)); |
| 81 return result; | 52 return result; |
| 82 } | 53 } |
| 83 | 54 |
| 84 } // namespace mojo | 55 } // namespace mojo |
| OLD | NEW |