v8.isStringOneByteRepresentation(content)


新增于: v23.10.0, v22.15.0

V8 仅支持 Latin-1/ISO-8859-1UTF16 作为字符串的底层表示。如果 content 使用 Latin-1/ISO-8859-1 作为底层表示,则此函数将返回 true;否则返回 false。

\V8 only supports Latin-1/ISO-8859-1 and UTF16 as the underlying representation of a string. If the content uses Latin-1/ISO-8859-1 as the underlying representation, this function will return true; otherwise, it returns false.

如果此方法返回 false,这并不意味着字符串包含 Latin-1/ISO-8859-1 中没有的某些字符。有时 Latin-1 字符串也可以表示为 UTF16

\If this method returns false, that does not mean that the string contains some characters not in Latin-1/ISO-8859-1. Sometimes a Latin-1 string may also be represented as UTF16.

const { isStringOneByteRepresentation } = require('node:v8');
const Encoding = {
 latin1: 1,
 utf16le: 2,
};
const buffer = Buffer.alloc(100);
function writeString(input) {
 if (isStringOneByteRepresentation(input)) {
 buffer.writeUint8(Encoding.latin1);
 buffer.writeUint32LE(input.length, 1);
 buffer.write(input, 5, 'latin1');
 } else {
 buffer.writeUint8(Encoding.utf16le);
 buffer.writeUint32LE(input.length * 2, 1);
 buffer.write(input, 5, 'utf16le');
 }
}
writeString('hello');
writeString('你好'); 

AltStyle によって変換されたページ (->オリジナル) /