c++ - SAFE[] 在这里是什么意思?

标签 c++ visual-studio char

<分区>

所以我查看了 here 中的 UrlEncode 函数:

std::string UriEncode(const std::string & sSrc)
{
   const char DEC2HEX[16 + 1] = "0123456789ABCDEF";
   const unsigned char * pSrc = (const unsigned char *)sSrc.c_str();
   const int SRC_LEN = sSrc.length();
   unsigned char * const pStart = new unsigned char[SRC_LEN * 3];
   unsigned char * pEnd = pStart;
   const unsigned char * const SRC_END = pSrc + SRC_LEN;

   for (; pSrc < SRC_END; ++pSrc)
   {
      if (SAFE[*pSrc]) 
         *pEnd++ = *pSrc;
      else
      {
         // escape this char
         *pEnd++ = '%';
         *pEnd++ = DEC2HEX[*pSrc >> 4];
         *pEnd++ = DEC2HEX[*pSrc & 0x0F];
      }
   }

   std::string sResult((char *)pStart, (char *)pEnd);
   delete [] pStart;
   return sResult;
}

它不能在我的 Visual Studio 2008 下编译。如何让它工作(最好不要使用任何特殊的 Windows 函数),SAFE[ const unsigned char ] 到底是什么意思?

更新: 查看源代码下的 zip 文件,我找到了

const char SAFE[256] =
{
    /*      0 1 2 3  4 5 6 7  8 9 A B  C D E F */
    /* 0 */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
    /* 1 */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
    /* 2 */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
    /* 3 */ 1,1,1,1, 1,1,1,1, 1,1,0,0, 0,0,0,0,

    /* 4 */ 0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1,
    /* 5 */ 1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,0,
    /* 6 */ 0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1,
    /* 7 */ 1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,0,

    /* 8 */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
    /* 9 */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
    /* A */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
    /* B */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,

    /* C */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
    /* D */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
    /* E */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
    /* F */ 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
};

吓到我了...如何让它在 C++ 类中可用?

最佳答案

SAFE 看起来只是一个数组,标记了 URI 中哪些字符是有效的。它甚至具有行和列注释,可以更轻松地查看哪个条目。

您会看到第一个有效字符是 0x30,或 0。 0x41-0x5A为大写字母等

这样,当字符在 URI 中有效时,SAFE[*pSrc] 只是非零(“真”),否则为零(“假”)。

关于c++ - SAFE[] 在这里是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6832916/

相关文章:

c++ - 在静态库段错误中绑定(bind)lua

c++ - 在 vector 中添加整数

c++ - FillConsoleOutputCharacter/WriteConsoleOutput 和特殊字符

c++ - 无法在 Visual Studio 2013 中正确创建 C++ 项目

c++ - 比较 C++ 中的许多 char 变量

java - 在 Java 中将二维整数数组转换为字符和字符串

c++ - Effective C++,第三版 : Overloading const function

c# - 如何在xamarin中禁用/取消撤消按钮

C++ 2 字节数组计算(字符数组、字符串和整数)

c++ - 派生类中基类的指针作为成员变量