mirror of
https://git.um-react.app/um/um-react.git
synced 2025-11-28 11:33:02 +00:00
refactor: move text encoder/decoder to util file
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import type { StagingKWMv2Key } from '~/features/settings/keyFormats';
|
||||
import { bytesToUTF8String } from '~/decrypt-worker/util/utf8Encoder';
|
||||
import { formatHex } from './formatHex';
|
||||
|
||||
const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
|
||||
|
||||
export class MMKVParser {
|
||||
private offset = 4;
|
||||
private length: number;
|
||||
@@ -67,7 +66,7 @@ export class MMKVParser {
|
||||
// ]
|
||||
const strByteLen = this.readInt();
|
||||
const data = this.readBytes(strByteLen);
|
||||
return textDecoder.decode(data).normalize();
|
||||
return bytesToUTF8String(data).normalize();
|
||||
}
|
||||
|
||||
public readVariantString() {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const textEncoder = new TextEncoder();
|
||||
|
||||
// translation of pseudocode from Wikipedia:
|
||||
import { stringToUTF8Bytes } from '~/decrypt-worker/util/utf8Encoder';
|
||||
|
||||
// https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_two_matrix_rows
|
||||
export function levenshtein(str1: string, str2: string) {
|
||||
if (str1 === str2) {
|
||||
@@ -14,8 +14,8 @@ export function levenshtein(str1: string, str2: string) {
|
||||
}
|
||||
|
||||
// Convert them to Uint8Array to avoid expensive string APIs.
|
||||
const s = textEncoder.encode(str1.toLowerCase());
|
||||
const t = textEncoder.encode(str2.toLowerCase());
|
||||
const s = stringToUTF8Bytes(str1.normalize().toLowerCase());
|
||||
const t = stringToUTF8Bytes(str2.normalize().toLowerCase());
|
||||
const m = s.byteLength;
|
||||
const n = t.byteLength;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user