javascript 字符串中的空字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13698677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:31:23  来源:igfitidea点击:

Null character in strings

javascriptstringgoogle-chromeunicodenull-terminated

提问by Denys Séguret

Consider this string:

考虑这个字符串:

var s = "A##代码##Z";

Its length is 3, as given by s.length. Using console.logyou can see the string isn't cut and that s[1]is ""and s.charCodeAt(1)is 0.

它的长度为 3,由 给出s.length。使用console.log您可以看到该字符串不会削减,并且s[1]""s.charCodeAt(1)0

When you alert it in Firefox, you see AZ. When you alert it in Chrome/Linux using alert(s), the \0terminates the string and you see A.

当您在 Firefox 中提醒它时,您会看到AZ。当您在 Chrome/Linux 中使用 警告它时alert(s)\0该字符串会终止,您会看到A.

My question is: what shouldbrowsers and Javascript engines do? Is Chrome buggy here? Is there a document defining what should happen?

我的问题是:浏览器和 Javascript 引擎该做什么?Chrome 有问题吗?是否有文件定义应该发生什么?

As this is a question about standard, a reference is needed.

由于这是关于标准的问题,因此需要参考。

采纳答案by Gung Foo

What the browser shoulddo is keep track of the string and its length separately since there are no null terminators present in the standard. (A string is just an object with a length).

浏览器应该做的是分别跟踪字符串及其长度,因为标准中不存在空终止符。(字符串只是一个有长度的对象)。

What Chrome seemsto do (I am taking your word for this) is use the standard C string functions which terminate at a \0. To answer one of your questions: Yes this to me constitutes a bug in Chrome's handling of the alert()function.

Chrome似乎做的(我相信你的话)是使用以 \0 终止的标准 C 字符串函数。回答您的一个问题:是的,这对我来说构成了 Chrome 处理该alert()功能的错误。

Formally the spec says:

规范正式说:

A string literal is zero or more characters enclosed in single or double quotes. Each character may be represented by an escape sequence. All characters may appear literally in a string literal except for the closing quote character, backslash, carriage return, line separator, paragraph separator, and line feed. Any character may appear in the form of an escape sequence.

字符串文字是用单引号或双引号括起来的零个或多个字符。每个字符都可以由一个转义序列表示。除了结束引号字符、反斜杠、回车符、行分隔符、段落分隔符和换行符之外,所有字符都可以字面出现在字符串文字中。任何字符都可能以转义序列的形式出现。

Also:

还:

A string literal stands for a value of the String type. The String value (SV) of the literal is described in terms of character values (CV) contributed by the various parts of the string literal.

字符串文字代表 String 类型的值。文字的字符串值 (SV) 是根据字符串文字的各个部分贡献的字符值 (CV) 来描述的。

And regarding the NUL byte:

关于 NUL 字节:

The CV [Character Value] of EscapeSequence :: 0 [lookahead ? DecimalDigit] is a <NUL> character (Unicode value 0000).

EscapeSequence 的 CV [字符值] :: 0 [lookahead ? DecimalDigit] 是一个 <NUL> 字符(Unicode 值 0000)。

Therefore, a NUL byte should simply be "yet another character value" and have no special meaning, as opposed to other languages where it might end a SV (String value).

因此,NUL 字节应该只是“另一个字符值”并且没有特殊含义,这与它可能以 SV(字符串值)结尾的其他语言相反。

For Reference of (valid) "String Single Character Escape Sequences" have a look at the ECMAScript Language spec section 7.8.4. There is a table at the end of the paragraph listing the aforementioned escape sequences.

有关(有效)“字符串单字符转义序列”的参考,请查看ECMAScript 语言规范第 7.8.4 节。段落末尾有一个表格,列出了上述转义序列。

What someone aiming to write a Javascript engine could probably learn from this: Don't use C/C++ string functions. :)

打算编写 Javascript 引擎的人可能会从中学到什么:不要使用 C/C++ 字符串函数。:)

回答by dencey

Javascript treat null character just like any other character, your question is how to display it in cosole or in a alert, it vary in different browsers, no standard about this, so chrome is OK.

Javascript 像对待任何其他字符一样对待空字符,您的问题是如何在 cosole 或警报中显示它,它在不同的浏览器中有所不同,对此没有标准,所以 chrome 是可以的。

回答by Nelson

You are asking about a non uniform (across browsers) behaviour of alert()method, so it has nothing to do with the Script object and the ECMAscript spec as is, it's about how alert()shows an String object.

您问的是方法的非统一(跨浏览器)行为alert(),因此它与 Script 对象和 ECMAscript 规范无关,而是关于如何alert()显示 String 对象。

alert()is a method of the Windowobject and ECMAscript does not define it (it only tells the host environment may provide global objects as the window object).

alert()Window对象的一个方法,ECMAscript 没有定义它(它只是告诉宿主环境可以提供全局对象作为窗口对象)。

But it happens to be a w3c spec that defines alert()behaviour, unfortunately it's very scarse and doesn't provide any hint about how messages with embedded null characters should be shown.

但它恰好是一个定义alert()行为的w3c 规范,不幸的是,它非常稀缺,并且没有提供任何关于如何显示带有嵌入空字符的消息的提示。

So this behaviour is, as with any other detail not specified in the spec, left out for the browsers own implementations.

因此,与规范中未指定的任何其他细节一样,这种行为被浏览器自己的实现排除在外。