This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new d4b096f905 [Web] Fix string to uint8 array for special characters 
(#17031)
d4b096f905 is described below

commit d4b096f905ad32be448c3a188ecf93a14c5734d5
Author: Charlie Ruan <53290280+charliefr...@users.noreply.github.com>
AuthorDate: Tue May 28 10:35:06 2024 -0700

    [Web] Fix string to uint8 array for special characters (#17031)
---
 web/src/memory.ts  |  5 +++--
 web/src/support.ts | 11 ++++++-----
 2 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/web/src/memory.ts b/web/src/memory.ts
index dbbb449a0b..b0d4ff3bf1 100644
--- a/web/src/memory.ts
+++ b/web/src/memory.ts
@@ -375,8 +375,9 @@ export class CachedCallStack implements Disposable {
    * @param data The string content.
    */
   allocThenSetArgString(offset: PtrOffset, data: string): void {
-    const strOffset = this.allocRawBytes(data.length + 1);
-    this.storeRawBytes(strOffset, StringToUint8Array(data));
+    const dataUint8: Uint8Array = StringToUint8Array(data);
+    const strOffset = this.allocRawBytes(dataUint8.length);
+    this.storeRawBytes(strOffset, dataUint8);
     this.addressToSetTargetValue.push([offset, strOffset]);
   }
   /**
diff --git a/web/src/support.ts b/web/src/support.ts
index 2fa87ed291..be85e85b7b 100644
--- a/web/src/support.ts
+++ b/web/src/support.ts
@@ -35,12 +35,13 @@ export function isPromise(value: any): boolean {
  * @returns The corresponding Uint8Array.
  */
 export function StringToUint8Array(str: string): Uint8Array {
-  const arr = new Uint8Array(str.length + 1);
-  for (let i = 0; i < str.length; ++i) {
-    arr[i] = str.charCodeAt(i);
+  const arr: Uint8Array = new TextEncoder().encode(str);
+  const resArr = new Uint8Array(arr.length + 1);
+  for (let i = 0; i < arr.length; ++i) {
+    resArr[i] = arr[i];
   }
-  arr[str.length] = 0;
-  return arr;
+  resArr[arr.length] = 0;
+  return resArr;
 }
 
 /**

Reply via email to