mirror of
https://github.com/openharmony/js_util_module.git
synced 2026-07-20 21:58:22 -04:00
-159
@@ -1,159 +0,0 @@
|
||||
# js_util_module
|
||||
|
||||
#### 1、Introduction to TextEncoder
|
||||
|
||||
The TextEncoder represents a text encoder, accepts character strings as input, encodes in UTF-8 format, and outputs UTF-8 byte stream.
|
||||
|
||||
Interface introduction:
|
||||
|
||||
1.readonly encoding : string
|
||||
|
||||
Get the encoding format, only UTF-8 is supported.
|
||||
|
||||
2.encode(input : string) : Uint8Array
|
||||
|
||||
Input stirng string, encode and output UTF-8 byte stream.
|
||||
|
||||
3.encodeInto(input : string, dest : Uint8Array) : {read : number, written : number}
|
||||
|
||||
Enter the stirng string, dest represents the storage location after encoding, and returns an object, read represents the number of characters that have been encoded,
|
||||
and written represents the size of bytes occupied by the encoded characters.
|
||||
|
||||
Method of use:
|
||||
|
||||
import util from '@ohos.util'
|
||||
|
||||
var textEncoder = new util.TextEncoder();
|
||||
|
||||
var result = textEncoder.encode('abc');
|
||||
|
||||
var dest = new Uint8Array(6);
|
||||
|
||||
var obj = textEncoder.encodeInto('abc', dest);
|
||||
|
||||
var getEncoding = textEncoder.encoding();
|
||||
|
||||
#### 2、Introduction to TextDecoder
|
||||
|
||||
The TextDecoder interface represents a text decoder. The decoder takes a byte stream as input and outputs a stirng string.
|
||||
|
||||
Interface introduction:
|
||||
|
||||
1.constructor(encoding? : string, options? : {fatal? : boolean, ignoreBOM? : boolean})
|
||||
|
||||
Constructor, the first parameter encoding indicates the format of decoding.
|
||||
|
||||
The second parameter represents some attributes.
|
||||
|
||||
Fatal in the attribute indicates whether an exception is thrown, and ignoreBOM indicates whether to ignore the bom flag.
|
||||
|
||||
2.readonly encoding : string
|
||||
|
||||
Get the set decoding format
|
||||
|
||||
3.readonly fatal : boolean
|
||||
|
||||
Get the setting that throws the exception
|
||||
|
||||
4.readonly ignoreBOM : boolean
|
||||
|
||||
Get whether to ignore the setting of the bom flag
|
||||
|
||||
5.decode(input : ArrayBuffer | ArrayBufferView, options? : {stream? : false}) : string
|
||||
|
||||
Input the data to be decoded, and solve the corresponding string character string.
|
||||
|
||||
The first parameter input represents the data to be decoded, and the second parameter options represents a bool flag, which means that additional data will be followed. The default is false.
|
||||
|
||||
Method of use:
|
||||
|
||||
import util from '@ohos.util'
|
||||
|
||||
var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
|
||||
|
||||
var getEncoding = textDecoder.encoding();
|
||||
|
||||
var fatalStr = textDecoder.fatal();
|
||||
|
||||
var ignoreBom = textDecoder.ignoreBOM();
|
||||
|
||||
var input = new Uint8Array([96, 97, 98]);
|
||||
|
||||
var result = textDecoder.decode(input, {stream : true});
|
||||
|
||||
#### 3、 Introduction to helpfunction
|
||||
|
||||
It is mainly used to callback and promise functions, output error code information, and format a printf-like string.
|
||||
|
||||
The helpfunction module involves four interfaces.
|
||||
|
||||
Interface introduction:
|
||||
|
||||
1.function printf(format: string, ...args: Object[]): string;
|
||||
|
||||
The util.format() method returns a formatted string using the first argument as a printf-like format string which can contain zero or more format specifiers. Each specifier is replaced with the converted value from the corresponding argument. Supported specifiers are:
|
||||
Each specifier is replaced with a converted value from the corresponding parameter. Supported specifiers are:
|
||||
%s: String will be used to convert all values except BigInt, Object and -0. BigInt values will be represented with an n and Objects that have no user defined toString function are inspected using util.inspect() with options { depth: 0, colors: false, compact: 3 }.
|
||||
%d: Number will be used to convert all values except BigInt and Symbol.
|
||||
%i: parseInt(value, 10) is used for all values except BigInt and Symbol.
|
||||
%f: parseFloat(value) is used for all values expect Symbol.
|
||||
%j: JSON. Replaced with the string '[Circular]' if the argument contains circular references.
|
||||
%o: Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() with options { showHidden: true, showProxy: true }. This will show the full object including non-enumerable properties and proxies.
|
||||
%O: Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() without options. This will show the full object not including non-enumerable properties and proxies.
|
||||
%c: CSS. This specifier is ignored and will skip any CSS passed in.
|
||||
%%: single percent sign ('%'). This does not consume an argument.
|
||||
Returns: <string> The formatted string
|
||||
If a specifier does not have a corresponding argument, it is not replaced:
|
||||
util.format('%s:%s', 'foo');
|
||||
// Returns: 'foo:%s'
|
||||
|
||||
Values that are not part of the format string are formatted using util.inspect() if their type is not string.
|
||||
If there are more arguments passed to the util.format() method than the number of specifiers, the extra arguments are concatenated to the returned string, separated by spaces:
|
||||
util.format('%s:%s', 'foo', 'bar', 'baz');
|
||||
// Returns: 'foo:bar baz'
|
||||
|
||||
If the first argument does not contain a valid format specifier, util.format() returns a string that is the concatenation of all arguments separated by spaces:
|
||||
util.format(1, 2, 3);
|
||||
// Returns: '1 2 3'
|
||||
|
||||
If only one argument is passed to util.format(), it is returned as it is without any formatting:
|
||||
util.format('%% %s');
|
||||
// Returns: '%% %s'
|
||||
|
||||
2.function getErrorString(errno: number): string;
|
||||
|
||||
The geterrorstring () method uses a system error number as a parameter to return system error information.
|
||||
|
||||
3.function callbackWrapper(original: Function): (err: Object, value: Object) => void;
|
||||
|
||||
Takes an async function (or a function that returns a Promise) and returns a function following the error-first callback style, i.e. taking an (err, value) => ... callback as the last argument. In the callback, the first argument will be the rejection reason (or null if the Promise resolved), and the second argument will be the resolved value.
|
||||
|
||||
4.function promiseWrapper(original: (err: Object, value: Object) => void): Object;
|
||||
|
||||
Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
|
||||
|
||||
####2、 Method of use
|
||||
|
||||
Take printf and geterrorstring as examples:
|
||||
|
||||
1.printf()
|
||||
|
||||
{
|
||||
var format = "%%%o%%%i%s";
|
||||
|
||||
var value = function aa(){};
|
||||
|
||||
var value1 = 1.5;
|
||||
|
||||
var value2 = "qwer";
|
||||
|
||||
var result = util.printf(format,value,value1,value2);
|
||||
}
|
||||
|
||||
2.geterrorstring()
|
||||
|
||||
{
|
||||
var errnum = 13;
|
||||
|
||||
var result = util.geterrorstring(errnum);
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
# js_util_module
|
||||
|
||||
#### 一、TextEncoder介绍
|
||||
|
||||
TextEncoder表示一个文本编码器,接受字符串作为输入,以UTF-8格式进行编码,输出UTF-8字节流。
|
||||
|
||||
接口介绍
|
||||
|
||||
1.readonly encoding : string
|
||||
|
||||
获取编码的格式,只支持UTF-8。
|
||||
|
||||
2.encode(input : string) : Uint8Array
|
||||
|
||||
输入stirng字符串,编码并输出UTF-8字节流。
|
||||
|
||||
3.encodeInto(input : string, dest : Uint8Array) : {read : number, written : number}
|
||||
|
||||
输入stirng字符串,dest表示编码后存放位置,返回一个对象,read表示已经编码的字符的个数,written表示已编码字符所占字节的大小。
|
||||
|
||||
使用方法:
|
||||
|
||||
import util from '@ohos.util'
|
||||
|
||||
var textEncoder = new util.TextEncoder();
|
||||
|
||||
var result = textEncoder.encode('abc');
|
||||
|
||||
var dest = new Uint8Array(6);
|
||||
|
||||
var obj = textEncoder.encodeInto('abc', dest);
|
||||
|
||||
var getEncoding = textEncoder.encoding();
|
||||
|
||||
#### 二、TextDecoder介绍
|
||||
|
||||
TextDecoder接口表示一个文本解码器,解码器将字节流作为输入,输出stirng字符串。
|
||||
|
||||
接口介绍
|
||||
|
||||
1.constructor(encoding? : string, options? : {fatal? : boolean, ignoreBOM? : boolean})
|
||||
构造函数,第一个参数encoding表示解码的格式。
|
||||
|
||||
第二个参数表示一些属性。
|
||||
|
||||
属性中fatal表示是否抛出异常,ignoreBOM表示是否忽略bom标志。
|
||||
|
||||
2.readonly encoding : string
|
||||
|
||||
获取设置的解码格式
|
||||
|
||||
3.readonly fatal : boolean
|
||||
|
||||
获取抛出异常的设置
|
||||
|
||||
4.readonly ignoreBOM : boolean
|
||||
|
||||
获取是否忽略bom标志的设置
|
||||
|
||||
5.decode(input : ArrayBuffer | ArrayBufferView, options? : {stream? : false}) : string
|
||||
|
||||
输入要解码的数据,解出对应的string字符串。
|
||||
|
||||
第一个参数input表示要解码的数据,第二个参数options表示一个bool标志,表示将跟随附加数据,默认为false。
|
||||
|
||||
使用方法:
|
||||
|
||||
import util from '@ohos.util'
|
||||
|
||||
var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
|
||||
|
||||
var getEncoding = textDecoder.encoding();
|
||||
|
||||
var fatalStr = textDecoder.fatal();
|
||||
|
||||
var ignoreBom = textDecoder.ignoreBOM();
|
||||
|
||||
var input = new Uint8Array([96, 97, 98]);
|
||||
|
||||
var result = textDecoder.decode(input, {stream : true});
|
||||
|
||||
#### 三、helpfunction介绍
|
||||
|
||||
主要是对函数做callback化、promise化以及对错误码进行编写输出,及类字符串的格式化输出。
|
||||
|
||||
helpfunction模块,涉及4个接口。
|
||||
|
||||
接口介绍
|
||||
|
||||
1.function printf(format: string, ...args: Object[]): string;
|
||||
|
||||
printf()方法使用第一个参数作为格式字符串(其可以包含零个或多个格式说明符)来返回格式化的字符串。
|
||||
|
||||
每个说明符都替换为来自相应参数的转换后的值。 支持的说明符有:
|
||||
|
||||
%s: String 将用于转换除 BigInt、Object 和 -0 之外的所有值。
|
||||
|
||||
%d: Number 将用于转换除 BigInt 和 Symbol 之外的所有值。
|
||||
|
||||
%i: parseInt(value, 10) 用于除 BigInt 和 Symbol 之外的所有值。
|
||||
|
||||
%f: parseFloat(value) 用于除 Symbol 之外的所有值。
|
||||
|
||||
%j: JSON。 如果参数包含循环引用,则替换为字符串 '[Circular]'。
|
||||
|
||||
%o: Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。
|
||||
|
||||
类似于具有选项 { showHidden: true, showProxy: true } 的 util.inspect()。
|
||||
|
||||
这将显示完整的对象,包括不可枚举的属性和代理。
|
||||
|
||||
%O: Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。
|
||||
|
||||
类似于没有选项的 util.inspect()。 这将显示完整的对象,但不包括不可枚举的属性和代理。
|
||||
|
||||
%c: CSS. 此说明符被忽略,将跳过任何传入的 CSS。
|
||||
|
||||
%%: 单个百分号 ('%')。 这不消费参数。
|
||||
|
||||
返回: <string> 格式化的字符串
|
||||
|
||||
如果说明符没有相应的参数,则不会替换它:
|
||||
|
||||
printf('%s:%s', 'foo');
|
||||
|
||||
// 返回: 'foo:%s'
|
||||
|
||||
如果其类型不是 string,则不属于格式字符串的值将进行%o类型的格式化。
|
||||
|
||||
如果传给 printf() 方法的参数多于说明符的数量,则额外的参数将以空格分隔串联到返回的字符串:
|
||||
|
||||
printf('%s:%s', 'foo', 'bar', 'baz');
|
||||
|
||||
// 返回: 'foo:bar baz'
|
||||
|
||||
如果第一个参数不包含有效的格式说明符,则printf()返回以空格分隔的所有参数的串联的字符串:
|
||||
|
||||
printf(1, 2, 3);
|
||||
|
||||
// 返回: '1 2 3'
|
||||
|
||||
如果只有一个参数传给printf(),则它会按原样返回,不进行任何格式化:
|
||||
|
||||
util.format('%% %s');
|
||||
|
||||
// Returns: '%% %s'
|
||||
|
||||
2.function getErrorString(errno: number): string;
|
||||
|
||||
getErrorString()方法使用一个系统的错误数字作为参数,用来返回系统的错误信息。
|
||||
|
||||
3.function callbackWrapper(original: Function): (err: Object, value: Object) => void;
|
||||
|
||||
参数为一个采用 async 函数(或返回 Promise 的函数)并返回遵循错误优先回调风格的函数,
|
||||
|
||||
即将 (err, value) => ... 回调作为最后一个参数。 在回调中,第一个参数将是拒绝原因
|
||||
|
||||
(如果 Promise 已解决,则为 null),第二个参数将是已解决的值。
|
||||
|
||||
4.function promiseWrapper(original: (err: Object, value: Object) => void): Object;
|
||||
|
||||
参数为采用遵循常见的错误优先的回调风格的函数
|
||||
|
||||
(也就是将 (err, value) => ... 回调作为最后一个参数),并返回一个返回 promise 的版本。
|
||||
|
||||
使用方法:
|
||||
|
||||
以printf、geterrorstring为例:
|
||||
|
||||
import util from '@ohos.util'
|
||||
|
||||
1.printf()
|
||||
|
||||
{
|
||||
var format = "%%%o%%%i%s";
|
||||
|
||||
var value = function aa(){};
|
||||
|
||||
var value1 = 1.5;
|
||||
|
||||
var value2 = "qwer";
|
||||
|
||||
var result = util.printf(format,value,value1,value2);
|
||||
}
|
||||
|
||||
2.geterrorstring()
|
||||
|
||||
{
|
||||
var errnum = 13;
|
||||
|
||||
var result = util.geterrorstring(errnum);
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
# js_ util_ module
|
||||
|
||||
####1、 Introduction to helpfunction
|
||||
|
||||
It is mainly used to callback and promise functions, output error code information, and format a printf-like string.
|
||||
|
||||
The helpfunction module involves four interfaces.
|
||||
|
||||
Interface introduction:
|
||||
|
||||
1.function printf(format: string, ...args: Object[]): string;
|
||||
|
||||
The util.format() method returns a formatted string using the first argument as a printf-like format string which can contain zero or more format specifiers. Each specifier is replaced with the converted value from the corresponding argument. Supported specifiers are:
|
||||
Each specifier is replaced with a converted value from the corresponding parameter. Supported specifiers are:
|
||||
%s: String will be used to convert all values except BigInt, Object and -0. BigInt values will be represented with an n and Objects that have no user defined toString function are inspected using util.inspect() with options { depth: 0, colors: false, compact: 3 }.
|
||||
%d: Number will be used to convert all values except BigInt and Symbol.
|
||||
%i: parseInt(value, 10) is used for all values except BigInt and Symbol.
|
||||
%f: parseFloat(value) is used for all values expect Symbol.
|
||||
%j: JSON. Replaced with the string '[Circular]' if the argument contains circular references.
|
||||
%o: Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() with options { showHidden: true, showProxy: true }. This will show the full object including non-enumerable properties and proxies.
|
||||
%O: Object. A string representation of an object with generic JavaScript object formatting. Similar to util.inspect() without options. This will show the full object not including non-enumerable properties and proxies.
|
||||
%c: CSS. This specifier is ignored and will skip any CSS passed in.
|
||||
%%: single percent sign ('%'). This does not consume an argument.
|
||||
Returns: <string> The formatted string
|
||||
If a specifier does not have a corresponding argument, it is not replaced:
|
||||
util.format('%s:%s', 'foo');
|
||||
// Returns: 'foo:%s'
|
||||
|
||||
Values that are not part of the format string are formatted using util.inspect() if their type is not string.
|
||||
If there are more arguments passed to the util.format() method than the number of specifiers, the extra arguments are concatenated to the returned string, separated by spaces:
|
||||
util.format('%s:%s', 'foo', 'bar', 'baz');
|
||||
// Returns: 'foo:bar baz'
|
||||
|
||||
If the first argument does not contain a valid format specifier, util.format() returns a string that is the concatenation of all arguments separated by spaces:
|
||||
util.format(1, 2, 3);
|
||||
// Returns: '1 2 3'
|
||||
|
||||
If only one argument is passed to util.format(), it is returned as it is without any formatting:
|
||||
util.format('%% %s');
|
||||
// Returns: '%% %s'
|
||||
|
||||
2.function getErrorString(errno: number): string;
|
||||
|
||||
The geterrorstring () method uses a system error number as a parameter to return system error information.
|
||||
|
||||
3.function callbackWrapper(original: Function): (err: Object, value: Object) => void;
|
||||
|
||||
Takes an async function (or a function that returns a Promise) and returns a function following the error-first callback style, i.e. taking an (err, value) => ... callback as the last argument. In the callback, the first argument will be the rejection reason (or null if the Promise resolved), and the second argument will be the resolved value.
|
||||
|
||||
4.function promiseWrapper(original: (err: Object, value: Object) => void): Object;
|
||||
|
||||
Takes a function following the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument, and returns a version that returns promises.
|
||||
|
||||
####2、 Method of use
|
||||
|
||||
Take printf and geterrorstring as examples:
|
||||
|
||||
1.printf()
|
||||
|
||||
{
|
||||
|
||||
var format = "%%%o%%%i%s";
|
||||
|
||||
var value = function aa(){};
|
||||
|
||||
var value1 = 1.5;
|
||||
|
||||
var value2 = "qwer";
|
||||
|
||||
var result = util.printf(format,value,value1,value2);
|
||||
|
||||
console.log("-----SK-----printf---result---+[" +result +"]");
|
||||
|
||||
}
|
||||
|
||||
2.geterrorstring()
|
||||
|
||||
{
|
||||
|
||||
var errnum = 13;
|
||||
|
||||
var result = util.geterrorstring(errnum);
|
||||
|
||||
console.log("-----SK------ = " + result);
|
||||
|
||||
}
|
||||
Executable
+165
@@ -0,0 +1,165 @@
|
||||
# js_util_module子系统/组件
|
||||
|
||||
- [简介](#简介)
|
||||
- [目录](#目录)
|
||||
- [说明](#说明)
|
||||
- [接口说明](#接口说明)
|
||||
- [使用说明](#使用说明)
|
||||
|
||||
- [相关仓](#相关仓)
|
||||
|
||||
## 简介
|
||||
|
||||
UTIL接口用于字符编码TextEncoder、解码TextDecoder和帮助函数HelpFunction。TextEncoder表示一个文本编码器,接受字符串作为输入,以UTF-8格式进行编码,输出UTF-8字节流。TextDecoder接口表示一个文本解码器,解码器将字节流作为输入,输出stirng字符串。HelpFunction主要是对函数做callback化、promise化以及对错误码进行编写输出,及类字符串的格式化输出。
|
||||
## 目录
|
||||
|
||||
```
|
||||
base/compileruntime/js_util_module/
|
||||
├── Class:TextEncoder # TextEncoder类
|
||||
│ ├── new TextEncoder() # 创建TextEncoder对象
|
||||
│ ├── encode() # encode方法
|
||||
│ ├── encoding # encoding属性
|
||||
│ └── encodeInto() # encodeInto方法
|
||||
├── Class:TextDecoder # TextDecoder类
|
||||
│ ├── new TextDecoder() # 创建TextDecoder对象
|
||||
│ ├── decode() # decode方法
|
||||
│ ├── encoding # encoding属性
|
||||
│ ├── fatal # fatal属性
|
||||
│ └── ignoreBOM # ignoreBOM属性
|
||||
├── printf() # printf方法
|
||||
├── getErrorString() # getErrorString方法
|
||||
├── callbackWrapper() # callbackWrapper方法
|
||||
└── promiseWrapper() # promiseWrapper方法
|
||||
```
|
||||
|
||||
## 说明
|
||||
|
||||
### 接口说明
|
||||
|
||||
|
||||
| 接口名 | 说明 |
|
||||
| -------- | -------- |
|
||||
| readonly encoding : string | 获取编码的格式,只支持UTF-8。 |
|
||||
| encode(input : string) : Uint8Array | 输入stirng字符串,编码并输出UTF-8字节流。 |
|
||||
| encodeInto(input : string, dest : Uint8Array) : {read : number, written : number} | 输入stirng字符串,dest表示编码后存放位置,返回一个对象,read表示已经编码的字符的个数,written表示已编码字符所占字节的大小。 |
|
||||
| constructor(encoding? : string, options? : {fatal? : boolean, ignoreBOM? : boolean}) | 构造函数,第一个参数encoding表示解码的格式。第二个参数表示一些属性。属性中fatal表示是否抛出异常,ignoreBOM表示是否忽略bom标志。 |
|
||||
| readonly encoding : string | 获取设置的解码格式。 |
|
||||
| readonly fatal : boolean | 获取抛出异常的设置 |
|
||||
| readonly ignoreBOM : boolean | 获取是否忽略bom标志的设置 |
|
||||
| decode(input : ArrayBuffer | 输入要解码的数据,解出对应的string字符串。第一个参数input表示要解码的数据,第二个参数options表示一个bool标志,表示将跟随附加数据,默认为false。 |
|
||||
| function printf(format: string, ...args: Object[]): string | printf()方法使用第一个参数作为格式字符串(其可以包含零个或多个格式说明符)来返回格式化的字符串。 |
|
||||
| function getErrorString(errno: number): string | getErrorString()方法使用一个系统的错误数字作为参数,用来返回系统的错误信息。 |
|
||||
| function callbackWrapper(original: Function): (err: Object, value: Object) => void | 参数为一个采用 async 函数(或返回 Promise 的函数)并返回遵循错误优先回调风格的函数,即将 (err, value) => ... 回调作为最后一个参数。 在回调中,第一个参数将是拒绝原因(如果 Promise 已解决,则为 null),第二个参数将是已解决的值。 |
|
||||
| function promiseWrapper(original: (err: Object, value: Object) => void): Object | 参数为采用遵循常见的错误优先的回调风格的函数(也就是将 (err, value) => ... 回调作为最后一个参数),并返回一个返回 promise 的版本。 |
|
||||
|
||||
printf中每个说明符都替换为来自相应参数的转换后的值。 支持的说明符有:
|
||||
| 式样化字符 | 式样要求 |
|
||||
| -------- | -------- |
|
||||
| %s: | String 将用于转换除 BigInt、Object 和 -0 之外的所有值。|
|
||||
| %d: |Number 将用于转换除 BigInt 和 Symbol 之外的所有值。|
|
||||
| %i: |parseInt(value, 10) 用于除 BigInt 和 Symbol 之外的所有值。|
|
||||
| %f: |parseFloat(value) 用于除 Symbol 之外的所有值。|
|
||||
| %j: |JSON。 如果参数包含循环引用,则替换为字符串 '[Circular]'。|
|
||||
| %o: |Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。类似于具有选项 { showHidden: true, showProxy: true } 的 util.inspect()。这将显示完整的对象,包括不可枚举的属性和代理。|
|
||||
| %O: |Object. 具有通用 JavaScript 对象格式的对象的字符串表示形式。类似于没有选项的 util.inspect()。 这将显示完整的对象,但不包括不可枚举的属性和代理。|
|
||||
| %c: | 此说明符被忽略,将跳过任何传入的 CSS。|
|
||||
| %%: |单个百分号 ('%')。 这不消耗待式样化参数。|
|
||||
|
||||
### 使用说明
|
||||
|
||||
各接口使用方法如下:
|
||||
|
||||
1.readonly encoding()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var textEncoder = new util.TextEncoder();
|
||||
var getEncoding = textEncoder.encoding();
|
||||
```
|
||||
2.encode()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var textEncoder = new util.TextEncoder();
|
||||
var result = textEncoder.encode('abc');
|
||||
```
|
||||
3.encodeInto()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var textEncoder = new util.TextEncoder();
|
||||
var obj = textEncoder.encodeInto('abc', dest);
|
||||
```
|
||||
4.textDecoder()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
|
||||
```
|
||||
5.readonly encoding()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
|
||||
var getEncoding = textDecoder.encoding();
|
||||
```
|
||||
6.readonly fatal()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
|
||||
var fatalStr = textDecoder.fatal();
|
||||
```
|
||||
7.readonly ignoreBOM()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
|
||||
var ignoreBom = textDecoder.ignoreBOM();
|
||||
```
|
||||
8.decode()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var textDecoder = new util.textDecoder("utf-16be", {fatal : ture, ignoreBOM : false});
|
||||
var result = textDecoder.decode(input, {stream : true});
|
||||
```
|
||||
9.printf()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var format = "%%%o%%%i%s";
|
||||
var value = function aa(){};
|
||||
var value1 = 1.5;
|
||||
var value2 = "qwer";
|
||||
var result = util.printf(format,value,value1,value2);
|
||||
```
|
||||
10.getErrorString()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
var errnum = 13;
|
||||
var result = util.getErrorString(errnum);
|
||||
```
|
||||
11.callbackWrapper()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
async function promiseFn() {
|
||||
return Promise.resolve('value');
|
||||
};
|
||||
var cb = util.callbackWrapper(promiseFn);
|
||||
cb((err, ret) => {
|
||||
expect(err).strictEqual(null);
|
||||
expect(ret).strictEqual('value');
|
||||
})
|
||||
```
|
||||
12.promiseWrapper()
|
||||
```
|
||||
import util from '@ohos.util'
|
||||
function aysnFun(str1, str2, callback) {
|
||||
if (typeof str1 === 'string' && typeof str1 === 'string') {
|
||||
callback(null, str1 + str2);
|
||||
} else {
|
||||
callback('type err');
|
||||
}
|
||||
}
|
||||
let newPromiseObj = util.promiseWrapper(aysnFun)("Hello", 'World');
|
||||
newPromiseObj.then(res => {
|
||||
expect(res).strictEqual('HelloWorld');
|
||||
})
|
||||
```
|
||||
## 相关仓
|
||||
|
||||
[js_util_module子系统](https://gitee.com/OHOS_STD/js_util_module)
|
||||
|
||||
[base/compileruntime/js_util_module/](base/compileruntime/js_util_module-readme.md)
|
||||
@@ -12,6 +12,7 @@
|
||||
"inner_kits": [
|
||||
],
|
||||
"test_list": [
|
||||
"//base/compileruntime/js_util_module/test/unittest:unittest"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
+5
-5
@@ -22,7 +22,7 @@ gen_js_obj("util_js") {
|
||||
output = util_js_obj_path
|
||||
}
|
||||
|
||||
ohos_shared_library("utilapi") {
|
||||
ohos_shared_library("util") {
|
||||
include_dirs = [
|
||||
"//foundation/ace/napi",
|
||||
"//foundation/ace/napi/native_engine",
|
||||
@@ -33,11 +33,11 @@ ohos_shared_library("utilapi") {
|
||||
]
|
||||
|
||||
sources = [
|
||||
"js_base64.cpp",
|
||||
"js_rational.cpp",
|
||||
"js_textdecoder.cpp",
|
||||
"js_textencoder.cpp",
|
||||
"js_rational.cpp",
|
||||
"native_module_util.cpp",
|
||||
"js_base64.cpp",
|
||||
]
|
||||
|
||||
deps = [
|
||||
@@ -61,5 +61,5 @@ ohos_shared_library("utilapi") {
|
||||
}
|
||||
|
||||
group("util_packages") {
|
||||
deps = [ ":utilapi" ]
|
||||
}
|
||||
deps = [ ":util" ]
|
||||
}
|
||||
|
||||
+33
-26
@@ -58,32 +58,28 @@ namespace OHOS::Util {
|
||||
tranTool_ = std::move(tempTranTool);
|
||||
}
|
||||
|
||||
|
||||
napi_value TextDecoder::Decode(napi_value src, bool iflag)
|
||||
{
|
||||
HILOG_INFO("textcoder Decode start");
|
||||
uint32_t flags = 0;
|
||||
flags |= iflag ? 0 : FLUSH_FLG;
|
||||
UBool flush = ((flags & FLUSH_FLG)) == FLUSH_FLG;
|
||||
napi_typedarray_type type;
|
||||
size_t length = 0;
|
||||
void* data1 = nullptr;
|
||||
napi_value arrayBuffer = nullptr;
|
||||
size_t byteOffset = 0;
|
||||
napi_value arrayBuffer = nullptr;
|
||||
NAPI_CALL(env_, napi_get_typedarray_info(env_, src, &type, &length, &data1, &arrayBuffer, &byteOffset));
|
||||
const char* source = static_cast<char*>(data1);
|
||||
size_t sourceLength = length;
|
||||
UErrorCode codeflag = U_ZERO_ERROR;
|
||||
size_t limit = GetMinByteSize() * sourceLength;
|
||||
UErrorCode codeFlag = U_ZERO_ERROR;
|
||||
size_t limit = GetMinByteSize() * length;
|
||||
size_t len = limit * sizeof(UChar);
|
||||
UChar* arr = nullptr;
|
||||
if (limit > 0) {
|
||||
arr = new UChar[limit + 1];
|
||||
if (memset_s(arr, len + sizeof(UChar), 0, len + sizeof(UChar)) != 0) {
|
||||
HILOG_ERROR("decode arr memset_s failed");
|
||||
if (arr != nullptr) {
|
||||
delete[] arr;
|
||||
arr = nullptr;
|
||||
}
|
||||
FreedMemory(arr);
|
||||
return nullptr;
|
||||
}
|
||||
} else {
|
||||
@@ -92,21 +88,11 @@ namespace OHOS::Util {
|
||||
}
|
||||
UChar* target = arr;
|
||||
size_t tarStartPos = (intptr_t)arr;
|
||||
ucnv_toUnicode(GetConverterPtr(), &target, target + len, &source,
|
||||
source + sourceLength, nullptr, flush, &codeflag);
|
||||
ucnv_toUnicode(GetConverterPtr(), &target, target + len, &source, source + length, nullptr, flush, &codeFlag);
|
||||
size_t resultLength = 0;
|
||||
bool omitInitialBom = false;
|
||||
if (U_SUCCESS(codeflag)) {
|
||||
if (limit > 0) {
|
||||
resultLength = (intptr_t)target - tarStartPos;
|
||||
if (resultLength > 0 && IsUnicode() && !IsIgnoreBom() && !IsBomFlag()) {
|
||||
if (arr[0] == 0xFEFF) {
|
||||
omitInitialBom = true;
|
||||
}
|
||||
label_ |= BOM_SEEN_FLG;
|
||||
}
|
||||
}
|
||||
}
|
||||
DecodeArr decArr(target, tarStartPos, limit);
|
||||
SetBomFlag(arr, codeFlag, decArr, resultLength, omitInitialBom);
|
||||
UChar* arrDat = arr;
|
||||
if (omitInitialBom && resultLength > 0) {
|
||||
arrDat = &arr[2]; // 2: Obtains the 2 value of the array.
|
||||
@@ -117,10 +103,7 @@ namespace OHOS::Util {
|
||||
char* rstCh = const_cast<char*>(tempCh);
|
||||
napi_value resultStr = nullptr;
|
||||
NAPI_CALL(env_, napi_create_string_utf8(env_, rstCh, strlen(rstCh), &resultStr));
|
||||
if (arr != nullptr) {
|
||||
delete[] arr;
|
||||
arr = nullptr;
|
||||
}
|
||||
FreedMemory(arr);
|
||||
if (flush) {
|
||||
label_ &= BOM_SEEN_FLG;
|
||||
Reset();
|
||||
@@ -180,4 +163,28 @@ namespace OHOS::Util {
|
||||
}
|
||||
ucnv_reset(tranTool_.get());
|
||||
}
|
||||
|
||||
void TextDecoder::FreedMemory(UChar *pData)
|
||||
{
|
||||
if (pData != nullptr) {
|
||||
delete[] pData;
|
||||
pData = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void TextDecoder::SetBomFlag(const UChar* arr, const UErrorCode codeFlag, const DecodeArr decArr , size_t &rstLen, bool &bomFlag)
|
||||
{
|
||||
if (arr == nullptr ) {
|
||||
return;
|
||||
}
|
||||
if (U_SUCCESS(codeFlag)) {
|
||||
if (decArr.limitLen > 0) {
|
||||
rstLen = (intptr_t)decArr.target - decArr.tarStartPos;
|
||||
if (rstLen > 0 && IsUnicode() && !IsIgnoreBom() && !IsBomFlag()) {
|
||||
bomFlag = (arr[0] == 0xFEFF) ? true : false;
|
||||
label_ |= BOM_SEEN_FLG;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
-1
@@ -25,6 +25,17 @@
|
||||
|
||||
using TransformToolPointer = std::unique_ptr<UConverter, void(*)(UConverter*)>;
|
||||
namespace OHOS::Util {
|
||||
struct DecodeArr {
|
||||
DecodeArr(UChar* tarPos, size_t tarStaPos, size_t limLen) {
|
||||
this->target = tarPos;
|
||||
this->tarStartPos = tarStaPos;
|
||||
this->limitLen = limLen;
|
||||
}
|
||||
UChar* target = 0;
|
||||
size_t tarStartPos = 0;
|
||||
size_t limitLen = 0;
|
||||
};
|
||||
|
||||
class TextDecoder {
|
||||
public:
|
||||
enum ConverterFlags {
|
||||
@@ -79,7 +90,8 @@ namespace OHOS::Util {
|
||||
ucnv_close(pointer);
|
||||
}
|
||||
private:
|
||||
void FreedMemory(UChar *pData);
|
||||
void SetBomFlag(const UChar* arr, const UErrorCode codeFlag, const DecodeArr decArr, size_t& rstLen, bool& bomFlag);
|
||||
void FreedMemory(UChar* pData);
|
||||
napi_env env_;
|
||||
uint32_t label_;
|
||||
std::string encStr_;
|
||||
|
||||
Reference in New Issue
Block a user