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 // Module "mojo/public/js/bindings/unicode" | 5 /** |
6 // | 6 * Defines functions for translating between JavaScript strings and UTF8 strings |
7 // Note: This file is for documentation purposes only. The code here is not | 7 * stored in ArrayBuffers. There is much room for optimization in this code if |
8 // actually executed. The real module is implemented natively in Mojo. | 8 * it proves necessary. |
| 9 */ |
| 10 define("mojo/public/js/bindings/unicode", function() { |
| 11 /** |
| 12 * Decodes the UTF8 string from the given buffer. |
| 13 * @param {ArrayBufferView} buffer The buffer containing UTF8 string data. |
| 14 * @return {string} The corresponding JavaScript string. |
| 15 */ |
| 16 function decodeUtf8String(buffer) { |
| 17 return decodeURIComponent(escape(String.fromCharCode.apply(null, buffer))); |
| 18 } |
9 | 19 |
10 while (1); | 20 /** |
| 21 * Encodes the given JavaScript string into UTF8. |
| 22 * @param {string} str The string to encode. |
| 23 * @param {ArrayBufferView} outputBuffer The buffer to contain the result. |
| 24 * Should be pre-allocated to hold enough space. Use |utf8Length| to determine |
| 25 * how much space is required. |
| 26 * @return {number} The number of bytes written to |outputBuffer|. |
| 27 */ |
| 28 function encodeUtf8String(str, outputBuffer) { |
| 29 var utf8String = unescape(encodeURIComponent(str)); |
| 30 if (outputBuffer.length < utf8String.length) |
| 31 throw new Error("Buffer too small for encodeUtf8String"); |
| 32 for (var i = 0; i < outputBuffer.length && i < utf8String.length; i++) |
| 33 outputBuffer[i] = utf8String.charCodeAt(i); |
| 34 return i; |
| 35 } |
11 | 36 |
12 /** | 37 /** |
13 * Decodes the UTF8 string from the given buffer. | 38 * Returns the number of bytes that a UTF8 encoding of the JavaScript string |
14 * @param {ArrayBufferView} buffer The buffer containing UTF8 string data. | 39 * |str| would occupy. |
15 * @return {string} The corresponding JavaScript string. | 40 */ |
16 */ | 41 function utf8Length(str) { |
17 function decodeUtf8String(buffer) { [native code] } | 42 var utf8String = unescape(encodeURIComponent(str)); |
| 43 return utf8String.length; |
| 44 } |
18 | 45 |
19 /** | 46 var exports = {}; |
20 * Encodes the given JavaScript string into UTF8. | 47 exports.decodeUtf8String = decodeUtf8String; |
21 * @param {string} str The string to encode. | 48 exports.encodeUtf8String = encodeUtf8String; |
22 * @param {ArrayBufferView} outputBuffer The buffer to contain the result. | 49 exports.utf8Length = utf8Length; |
23 * Should be pre-allocated to hold enough space. Use |utf8Length| to determine | 50 return exports; |
24 * how much space is required. | 51 }); |
25 * @return {number} The number of bytes written to |outputBuffer|. | |
26 */ | |
27 function encodeUtf8String(str, outputBuffer) { [native code] } | |
28 | |
29 /** | |
30 * Returns the number of bytes that a UTF8 encoding of the JavaScript string | |
31 * |str| would occupy. | |
32 */ | |
33 function utf8Length(str) { [native code] } | |
OLD | NEW |