将c字符串中的字符转换为其转义序列 [英] Convert characters in a c string to their escape sequences

查看:243
本文介绍了将c字符串中的字符转换为其转义序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个像 string ToLiteral(string input) -to-a-escaped-string-literal>这篇文章。这样

I need a function like string ToLiteral(string input) from this post. Such that

char *literal = to_literal("asdf\r\n");

会产生 literal ==>asdf\\\\\

Would yield literal ==> "asdf\\r\\n".

我已经搜索了,但无法找到任何东西(猜测我一定是使用错误的术语)。但是,我认为一个具有此功能的图书馆必须出现在某处...

I've googled around, but not been able to find anything (guess that I've must be using the wrong terms). However, I assume that a library with this functionality must be out there somewhere...

感谢您的interresting答案。 Googlingc string escape function似乎是获取更多示例的关键,GLIB提供了似乎正是我需要的g_strescape()。

Thank you for the interresting answers. Googling "c string escape function" by the way seems to be the key to obtaining even more examples and GLIB provides g_strescape () which seems to be exactly what I need.

推荐答案

这个没有内置的功能,但是你可以一个人打一个:

There's no built-in function for this, but you could whip one up:

/* Expands escape sequences within a C-string
 *
 * src must be a C-string with a NUL terminator
 *
 * dest should be long enough to store the resulting expanded
 * string. A string of size 2 * strlen(src) + 1 will always be sufficient
 *
 * NUL characters are not expanded to \0 (otherwise how would we know when
 * the input string ends?)
 */

void expand_escapes(char* dest, const char* src) 
{
  char c;

  while (c = *(src++)) {
    switch(c) {
      case '\a': 
        *(dest++) = '\\';
        *(dest++) = 'a';
        break;
      case '\b': 
        *(dest++) = '\\';
        *(dest++) = 'b';
        break;
      case '\t': 
        *(dest++) = '\\';
        *(dest++) = 't';
        break;
      case '\n': 
        *(dest++) = '\\';
        *(dest++) = 'n';
        break;
      case '\v': 
        *(dest++) = '\\';
        *(dest++) = 'v';
        break;
      case '\f': 
        *(dest++) = '\\';
        *(dest++) = 'f';
        break;
      case '\r': 
        *(dest++) = '\\';
        *(dest++) = 'r';
        break;
      case '\\': 
        *(dest++) = '\\';
        *(dest++) = '\\';
        break;
      case '\"': 
        *(dest++) = '\\';
        *(dest++) = '\"';
        break;
      default:
        *(dest++) = c;
     }
  }

  *dest = '\0'; /* Ensure nul terminator */
}

请注意,我没有翻译一个逃脱字符的转义序列,因为这在C中没有标准化(一些编译器使用 \e 而其他使用 \x )。您可以添加以适用于您的方式。

Note that I've left out translation of an escape sequence for the "escape" character, since this isn't standardized in C (some compilers use \e and others use \x). You can add in whichever applies to you.

如果您需要为您分配目的地缓冲区的功能:

If you want a function that allocates your destination buffer for you:

/* Returned buffer may be up to twice as large as necessary */
char* expand_escapes_alloc(const char* src)
{
   char* dest = malloc(2 * strlen(src) + 1);
   expand_escapes(dest, src);
   return dest;
}

这篇关于将c字符串中的字符转换为其转义序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆