xref: /aosp_15_r20/external/libchrome/mojo/public/js/lib/unicode.js (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker// Copyright 2014 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker// Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker// found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker/**
6*635a8641SAndroid Build Coastguard Worker * Defines functions for translating between JavaScript strings and UTF8 strings
7*635a8641SAndroid Build Coastguard Worker * stored in ArrayBuffers. There is much room for optimization in this code if
8*635a8641SAndroid Build Coastguard Worker * it proves necessary.
9*635a8641SAndroid Build Coastguard Worker */
10*635a8641SAndroid Build Coastguard Worker(function() {
11*635a8641SAndroid Build Coastguard Worker  var internal = mojo.internal;
12*635a8641SAndroid Build Coastguard Worker  var textDecoder = new TextDecoder('utf-8');
13*635a8641SAndroid Build Coastguard Worker  var textEncoder = new TextEncoder('utf-8');
14*635a8641SAndroid Build Coastguard Worker
15*635a8641SAndroid Build Coastguard Worker  /**
16*635a8641SAndroid Build Coastguard Worker   * Decodes the UTF8 string from the given buffer.
17*635a8641SAndroid Build Coastguard Worker   * @param {ArrayBufferView} buffer The buffer containing UTF8 string data.
18*635a8641SAndroid Build Coastguard Worker   * @return {string} The corresponding JavaScript string.
19*635a8641SAndroid Build Coastguard Worker   */
20*635a8641SAndroid Build Coastguard Worker  function decodeUtf8String(buffer) {
21*635a8641SAndroid Build Coastguard Worker    return textDecoder.decode(buffer);
22*635a8641SAndroid Build Coastguard Worker  }
23*635a8641SAndroid Build Coastguard Worker
24*635a8641SAndroid Build Coastguard Worker  /**
25*635a8641SAndroid Build Coastguard Worker   * Encodes the given JavaScript string into UTF8.
26*635a8641SAndroid Build Coastguard Worker   * @param {string} str The string to encode.
27*635a8641SAndroid Build Coastguard Worker   * @param {ArrayBufferView} outputBuffer The buffer to contain the result.
28*635a8641SAndroid Build Coastguard Worker   * Should be pre-allocated to hold enough space. Use |utf8Length| to determine
29*635a8641SAndroid Build Coastguard Worker   * how much space is required.
30*635a8641SAndroid Build Coastguard Worker   * @return {number} The number of bytes written to |outputBuffer|.
31*635a8641SAndroid Build Coastguard Worker   */
32*635a8641SAndroid Build Coastguard Worker  function encodeUtf8String(str, outputBuffer) {
33*635a8641SAndroid Build Coastguard Worker    const utf8Buffer = textEncoder.encode(str);
34*635a8641SAndroid Build Coastguard Worker    if (outputBuffer.length < utf8Buffer.length)
35*635a8641SAndroid Build Coastguard Worker      throw new Error("Buffer too small for encodeUtf8String");
36*635a8641SAndroid Build Coastguard Worker    outputBuffer.set(utf8Buffer);
37*635a8641SAndroid Build Coastguard Worker    return utf8Buffer.length;
38*635a8641SAndroid Build Coastguard Worker  }
39*635a8641SAndroid Build Coastguard Worker
40*635a8641SAndroid Build Coastguard Worker  /**
41*635a8641SAndroid Build Coastguard Worker   * Returns the number of bytes that a UTF8 encoding of the JavaScript string
42*635a8641SAndroid Build Coastguard Worker   * |str| would occupy.
43*635a8641SAndroid Build Coastguard Worker   */
44*635a8641SAndroid Build Coastguard Worker  function utf8Length(str) {
45*635a8641SAndroid Build Coastguard Worker    return textEncoder.encode(str).length;
46*635a8641SAndroid Build Coastguard Worker  }
47*635a8641SAndroid Build Coastguard Worker
48*635a8641SAndroid Build Coastguard Worker  internal.decodeUtf8String = decodeUtf8String;
49*635a8641SAndroid Build Coastguard Worker  internal.encodeUtf8String = encodeUtf8String;
50*635a8641SAndroid Build Coastguard Worker  internal.utf8Length = utf8Length;
51*635a8641SAndroid Build Coastguard Worker})();
52