1 Star 1 Fork 0

ChangweiZhang / chardet-arkts

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

@changwei/chardet

Chardet 是一个检测文本编码的库,移植自npm两千多万下载量的同名包。

  • 非常轻量
  • 性能优秀
  • 无外部依赖
  • 纯TS/ArkTS编写

安装

ohpm install @changwei/chardet

使用

返回最可能的字符编码

import * as chardet from '@changwei/chardet';
import fs from '@ohos.file.fs'

let file=fs.openSync('/path/to/file',fs.OpenMode.READ_ONLY);
let arrayBuffer=new ArrayBuffer(1024);//读取文本文件内容
let readLen = fs.readSync(file.fd, arrayBuffer,{
    offset: 0,
    length: arrayBuffer.length
});
//提取前512个字节内容以提高性能,长文本建议这样做
let charset = chardet.detect(new Uint8Array(arrayBuffer, 0, 512));
// or
const encoding = await chardet.detectFile('/path/to/file');
// or
const encoding = chardet.detectFileSync('/path/to/file');

使用analyse方法分析所有字符编码的可能性,结果返回列表

chardet.analyse(new Uint8Array(arrayBuffer, 0, 512));

返回值格式如下:

[
  { confidence: 90, name: 'UTF-8' },
  { confidence: 20, name: 'windows-1252', lang: 'fr' },
];

支持的字符编码:

  • UTF-8
  • UTF-16 LE
  • UTF-16 BE
  • UTF-32 LE
  • UTF-32 BE
  • ISO-2022-JP
  • ISO-2022-KR
  • ISO-2022-CN
  • Shift_JIS
  • Big5
  • EUC-JP
  • EUC-KR
  • GB18030
  • ISO-8859-1
  • ISO-8859-2
  • ISO-8859-5
  • ISO-8859-6
  • ISO-8859-7
  • ISO-8859-8
  • ISO-8859-9
  • windows-1250
  • windows-1251
  • windows-1252
  • windows-1253
  • windows-1254
  • windows-1255
  • windows-1256
  • KOI8-R

目前支持以上编码,注意gb18030兼容gbk和gb2312.

引用项目

开源协议

本项目基于 MIT ,请自由地享受和参与开源。

联系我

B站 @Changwei同学

抖音/西瓜 @梦断代码

求点赞 求关注 求分享

Copyright (C) 2023 Changwei Zhang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

鸿蒙原生文本编码检测库 展开 收起
TypeScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
TypeScript
1
https://gitee.com/changweizhang/chardet-arkts.git
git@gitee.com:changweizhang/chardet-arkts.git
changweizhang
chardet-arkts
chardet-arkts
master

搜索帮助