为什么 Atom 代码段需要在其正文中同时使用四个反斜杠才能打印单个反斜杠
Why do Atom snippets need four backslashes at once in their body in order to print a single backslash
我在设置代码片段时才意识到这一点。
'.source':
'shrug':
'prefix': 'shrug'
'body': '¯\\_(ツ)_/¯'
为了打印典型的 ¯\_(ツ)_/¯
耸肩,您需要 4 个反斜杠。使用 2 个反斜杠不会导致任何错误,但不会打印反斜杠。如果您需要 2 个,我会理解,但为什么需要 4 个?
atom 片段中的四个反斜杠是由于片段使用通用 CSON 表示法(Coffeescript 风格 JSON)。
在this comment on an issue from the atom-snippets repo
中有详细描述
I think that four backslashes makes sense, however notationally
inconvenient.
It has to do with the levels of interpretation a snippet goes through
before it ends up in your text buffer:
- The snippet is declared in a CSON file, the parsing of string elements
in this format is "backslash sensitive" i.e. \n represents the newline
character and a \ is represented as .
The snippet then has to be
parsed by the snippet body parser. The parser uses one \ to escape the
following character, e.g. \ becomes . So the process goes as follows:
\ --CSON--> \ --BodyParser--> \
The reason two backslashes used to work, was because the snippet body
parser never really handled escaped characters (the escape cases were
handled explicitly rather than in a generic way) this was why we had
bug #60.
The process could be made more notationally friendly if the snippets
were stored in a custom format. Then we would have more control over
how it is parsed, such as not interpreting backslashes before they are
being fed to the body parser.
我在设置代码片段时才意识到这一点。
'.source':
'shrug':
'prefix': 'shrug'
'body': '¯\\_(ツ)_/¯'
为了打印典型的 ¯\_(ツ)_/¯
耸肩,您需要 4 个反斜杠。使用 2 个反斜杠不会导致任何错误,但不会打印反斜杠。如果您需要 2 个,我会理解,但为什么需要 4 个?
atom 片段中的四个反斜杠是由于片段使用通用 CSON 表示法(Coffeescript 风格 JSON)。
在this comment on an issue from the atom-snippets repo
中有详细描述I think that four backslashes makes sense, however notationally inconvenient.
It has to do with the levels of interpretation a snippet goes through before it ends up in your text buffer:
- The snippet is declared in a CSON file, the parsing of string elements in this format is "backslash sensitive" i.e. \n represents the newline character and a \ is represented as .
The snippet then has to be parsed by the snippet body parser. The parser uses one \ to escape the following character, e.g. \ becomes . So the process goes as follows:
\ --CSON--> \ --BodyParser--> \
The reason two backslashes used to work, was because the snippet body parser never really handled escaped characters (the escape cases were handled explicitly rather than in a generic way) this was why we had bug #60.
The process could be made more notationally friendly if the snippets were stored in a custom format. Then we would have more control over how it is parsed, such as not interpreting backslashes before they are being fed to the body parser.