OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 /** | 5 /** |
6 * Defines functions for translating between JavaScript strings and UTF8 strings | 6 * Defines functions for translating between JavaScript strings and UTF8 strings |
7 * stored in ArrayBuffers. There is much room for optimization in this code if | 7 * stored in ArrayBuffers. There is much room for optimization in this code if |
8 * it proves necessary. | 8 * it proves necessary. |
9 */ | 9 */ |
10 (function() { | 10 define("mojo/public/js/unicode", function() { |
11 var internal = mojoBindings.internal; | |
12 | |
13 /** | 11 /** |
14 * Decodes the UTF8 string from the given buffer. | 12 * Decodes the UTF8 string from the given buffer. |
15 * @param {ArrayBufferView} buffer The buffer containing UTF8 string data. | 13 * @param {ArrayBufferView} buffer The buffer containing UTF8 string data. |
16 * @return {string} The corresponding JavaScript string. | 14 * @return {string} The corresponding JavaScript string. |
17 */ | 15 */ |
18 function decodeUtf8String(buffer) { | 16 function decodeUtf8String(buffer) { |
19 return decodeURIComponent(escape(String.fromCharCode.apply(null, buffer))); | 17 return decodeURIComponent(escape(String.fromCharCode.apply(null, buffer))); |
20 } | 18 } |
21 | 19 |
22 /** | 20 /** |
(...skipping 15 matching lines...) Expand all Loading... |
38 | 36 |
39 /** | 37 /** |
40 * Returns the number of bytes that a UTF8 encoding of the JavaScript string | 38 * Returns the number of bytes that a UTF8 encoding of the JavaScript string |
41 * |str| would occupy. | 39 * |str| would occupy. |
42 */ | 40 */ |
43 function utf8Length(str) { | 41 function utf8Length(str) { |
44 var utf8String = unescape(encodeURIComponent(str)); | 42 var utf8String = unescape(encodeURIComponent(str)); |
45 return utf8String.length; | 43 return utf8String.length; |
46 } | 44 } |
47 | 45 |
48 internal.decodeUtf8String = decodeUtf8String; | 46 var exports = {}; |
49 internal.encodeUtf8String = encodeUtf8String; | 47 exports.decodeUtf8String = decodeUtf8String; |
50 internal.utf8Length = utf8Length; | 48 exports.encodeUtf8String = encodeUtf8String; |
51 })(); | 49 exports.utf8Length = utf8Length; |
| 50 return exports; |
| 51 }); |
OLD | NEW |