如何将文字转义转义为 "real" 转义,以便打印 "correctly"?

How do I turn literal escapes into "real" escapes, so that they are printed "correctly"?

下面的 C++ 代码应该很容易理解。
在前 2 "cout" 的评论中有实际打印。
在第 3 个 "cout" 评论中有我希望它打印的内容。
"restore_escapes" 函数应该是什么样子的?没有现成的东西吗?

std::string a("abc\"\tdef\n");
std::string b(R"(abc\"\tdef\n)");

std::cout << a; //abc"    def
std::cout << b; //abc\"\tdef\n
std::cout << restore_escapes(b);  //abc"    def

查看 glib g_strcompress function,转载于此:

gchar *
g_strcompress (const gchar *source)
{
  const gchar *p = source, *octal;
  gchar *dest;
  gchar *q;

  g_return_val_if_fail (source != NULL, NULL);

  dest = g_malloc (strlen (source) + 1);
  q = dest;

  while (*p)
    {
      if (*p == '\')
        {
          p++;
          switch (*p)
            {
            case '[=10=]':
              g_warning ("g_strcompress: trailing \");
              goto out;
            case '0':  case '1':  case '2':  case '3':  case '4':
            case '5':  case '6':  case '7':
              *q = 0;
              octal = p;
              while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
                {
                  *q = (*q * 8) + (*p - '0');
                  p++;
                }
              q++;
              p--;
              break;
            case 'b':
              *q++ = '\b';
              break;
            case 'f':
              *q++ = '\f';
              break;
            case 'n':
              *q++ = '\n';
              break;
            case 'r':
              *q++ = '\r';
              break;
            case 't':
              *q++ = '\t';
              break;
            case 'v':
              *q++ = '\v';
              break;
            default:            /* Also handles \" and \ */
              *q++ = *p;
              break;
            }
        }
      else
        *q++ = *p;
      p++;
    }
out:
  *q = 0;

  return dest;
}