From 1f2147ef72dc8e818228beeec352059fbaa60f56 Mon Sep 17 00:00:00 2001 From: nyne Date: Mon, 5 May 2025 12:51:36 +0800 Subject: [PATCH] Add support for gbk. Close #354 --- assets/init.js | 26 ++++++++++++++++++++++++++ lib/foundation/js_engine.dart | 6 ++++++ pubspec.lock | 8 ++++++++ pubspec.yaml | 1 + 4 files changed, 41 insertions(+) diff --git a/assets/init.js b/assets/init.js index 7a016e3..b061bbd 100644 --- a/assets/init.js +++ b/assets/init.js @@ -39,6 +39,32 @@ let Convert = { }); }, + /** + * @param str {string} + * @returns {ArrayBuffer} + */ + encodeGbk: (str) => { + return sendMessage({ + method: "convert", + type: "gbk", + value: str, + isEncode: true + }); + }, + + /** + * @param value {ArrayBuffer} + * @returns {string} + */ + decodeGbk: (value) => { + return sendMessage({ + method: "convert", + type: "gbk", + value: value, + isEncode: false + }); + }, + /** * @param {ArrayBuffer} value * @returns {string} diff --git a/lib/foundation/js_engine.dart b/lib/foundation/js_engine.dart index 40fde70..633cd92 100644 --- a/lib/foundation/js_engine.dart +++ b/lib/foundation/js_engine.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'dart:math' as math; import 'package:crypto/crypto.dart'; import 'package:dio/io.dart'; +import 'package:enough_convert/enough_convert.dart'; import 'package:flutter/foundation.dart' show protected; import 'package:flutter/services.dart'; import 'package:html/parser.dart' as html; @@ -372,6 +373,11 @@ mixin class _JSEngineApi { switch (type) { case "utf8": return isEncode ? utf8.encode(value) : utf8.decode(value); + case "gbk": + final codec = const GbkCodec(); + return isEncode + ? Uint8List.fromList(codec.encode(value)) + : codec.decode(value); case "base64": return isEncode ? base64Encode(value) : base64Decode(value); case "md5": diff --git a/pubspec.lock b/pubspec.lock index 811ca1c..1d4a5da 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -178,6 +178,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.7.0" + enough_convert: + dependency: "direct main" + description: + name: enough_convert + sha256: c67d85ca21aaa0648f155907362430701db41f7ec8e6501a58ad9cd9d8569d01 + url: "https://pub.dev" + source: hosted + version: "1.6.0" fake_async: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 708f5b7..9655157 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -85,6 +85,7 @@ dependencies: flutter_localizations: sdk: flutter yaml: ^3.1.3 + enough_convert: ^1.6.0 dev_dependencies: flutter_test: