C String Literal必需的转义字符

时间:2022-05-22 22:29:19

I have a zero terminated string:

我有一个零终止字符串:

char* s = ...;

and I am generating C source code (at runtime) and I want to output a string literal representing s that will produce an identical string to s in the generated C program.

我正在生成C源代码(在运行时),我想输出一个表示s的字符串文字,它将在生成的C程序中生成与s相同的字符串。

The algorithm I am using is:

我使用的算法是:

Output "

Foreach char c in s
    if c == " output \"
    else if c == \ output \\
    else output c

Output "

Are there any other characters that I need to give special treatment other than " and \?

除了“和\”之外,还有其他任何需要特殊处理的角色吗?

2 个解决方案

#1


7  

  • You must encode ", \, \r and \n and \0 (and \? as Michael Burr mentions). Failure to do this will break your code.
  • 你必须编码“,\,\ r \ n和\ n以及\ 0(和Michael Burr提到的\?)。如果不这样做会破坏你的代码。
  • You should encode non-ASCII characters using the hexadecimal escape code, e.g. \x80. It is implementation defined if you have non-ASCII characters in your source code. Failure to encode these characters will work on some compilers but it could break on others.
  • 您应该使用十六进制转义码对非ASCII字符进行编码,例如: \ X80。如果源代码中包含非ASCII字符,则为实现定义。不对这些字符进行编码将对某些编译器起作用,但它可能会破坏其他编译器。
  • You can encode ASCII non-printable characters. It would improve the readability of the generated source code if you used the escape codes for characters like \t, \b, \x05, etc. If you don't do this your code will still work but it might be hard to read.
  • 您可以编码ASCII不可打印的字符。如果您使用\ t,\ t,\ x05等字符的转义码,它将提高生成的源代码的可读性。如果您不这样做,您的代码仍然可以工作,但可能很难阅读。
  • You don't need to escape ' inside a double-quoted string. It's legal, but it's unnecessary and it doesn't make the source code more readable.
  • 你不需要在双引号字符串内转义。这是合法的,但它是不必要的,它不会使源代码更具可读性。

#2


4  

the set of escape sequences in standard C include the following:

标准C中的转义序列集包括以下内容:

\' 
\" 
\? 
\\ 
\a  (alert - usually Ctrl-G)
\b  (backspace)
\f  (form feed)
\n  
\r
\t
\v  (vertical tab)

Note that the \? is in there so the question mark can be escaped so a sequence like "??!" can be encoded as `"\?\?!" to prevent it from being interpreted as a dreaded trigraph.

注意\?在那里所以问号可以被转义所以像“??!”这样的序列可以编码为“”\?\?!“防止它被解释为可怕的三角形。

For completeness, I would consider handling each of these (though some of them like \a and \v I might escape using a \x escape sequence instead - that may depend on your needs). Also, for any other non-printable character, I'd convert to its hex equivalent using the \x escape sequence.

为了完整性,我会考虑处理其中的每一个(虽然它们中的一些像\ a和\ v我可能会使用\ x转义序列转义 - 这可能取决于您的需要)。此外,对于任何其他不可打印的字符,我将使用\ x转义序列转换为其十六进制等效值。

#1


7  

  • You must encode ", \, \r and \n and \0 (and \? as Michael Burr mentions). Failure to do this will break your code.
  • 你必须编码“,\,\ r \ n和\ n以及\ 0(和Michael Burr提到的\?)。如果不这样做会破坏你的代码。
  • You should encode non-ASCII characters using the hexadecimal escape code, e.g. \x80. It is implementation defined if you have non-ASCII characters in your source code. Failure to encode these characters will work on some compilers but it could break on others.
  • 您应该使用十六进制转义码对非ASCII字符进行编码,例如: \ X80。如果源代码中包含非ASCII字符,则为实现定义。不对这些字符进行编码将对某些编译器起作用,但它可能会破坏其他编译器。
  • You can encode ASCII non-printable characters. It would improve the readability of the generated source code if you used the escape codes for characters like \t, \b, \x05, etc. If you don't do this your code will still work but it might be hard to read.
  • 您可以编码ASCII不可打印的字符。如果您使用\ t,\ t,\ x05等字符的转义码,它将提高生成的源代码的可读性。如果您不这样做,您的代码仍然可以工作,但可能很难阅读。
  • You don't need to escape ' inside a double-quoted string. It's legal, but it's unnecessary and it doesn't make the source code more readable.
  • 你不需要在双引号字符串内转义。这是合法的,但它是不必要的,它不会使源代码更具可读性。

#2


4  

the set of escape sequences in standard C include the following:

标准C中的转义序列集包括以下内容:

\' 
\" 
\? 
\\ 
\a  (alert - usually Ctrl-G)
\b  (backspace)
\f  (form feed)
\n  
\r
\t
\v  (vertical tab)

Note that the \? is in there so the question mark can be escaped so a sequence like "??!" can be encoded as `"\?\?!" to prevent it from being interpreted as a dreaded trigraph.

注意\?在那里所以问号可以被转义所以像“??!”这样的序列可以编码为“”\?\?!“防止它被解释为可怕的三角形。

For completeness, I would consider handling each of these (though some of them like \a and \v I might escape using a \x escape sequence instead - that may depend on your needs). Also, for any other non-printable character, I'd convert to its hex equivalent using the \x escape sequence.

为了完整性,我会考虑处理其中的每一个(虽然它们中的一些像\ a和\ v我可能会使用\ x转义序列转义 - 这可能取决于您的需要)。此外,对于任何其他不可打印的字符,我将使用\ x转义序列转换为其十六进制等效值。