zoukankan      html  css  js  c++  java
  • URL格式编码与解码


    char
    * urlencode(const void* buf, size_t size) { _assert_(buf && size <= MEMMAXSIZ); const unsigned char* rp = (const unsigned char*)buf; char* zbuf = new char[size*3+1]; char* wp = zbuf; for (const unsigned char* ep = rp + size; rp < ep; rp++) { int32_t c = *rp; if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c != '' && std::strchr("_-.~", c))) { *(wp++) = c; } else { *(wp++) = '%'; int32_t num = c >> 4; if (num < 10) { *(wp++) = '0' + num; } else { *(wp++) = 'a' + num - 10; } num = c & 0x0f; if (num < 10) { *(wp++) = '0' + num; } else { *(wp++) = 'a' + num - 10; } } } *wp = ''; return zbuf; }

    decode

    char* urldecode(const char* str, size_t* sp) {
      _assert_(str && sp);
      size_t zsiz = std::strlen(str);
      char* zbuf = new char[zsiz+1];
      char* wp = zbuf;
      const char* ep = str + zsiz;
      while (str < ep) {
        int32_t c = *str;
        if (c == '%') {
          int32_t num = 0;
          if (++str >= ep) break;
          c = *str;
          if (c >= '0' && c <= '9') {
            num = c - '0';
          } else if (c >= 'a' && c <= 'f') {
            num = c - 'a' + 10;
          } else if (c >= 'A' && c <= 'F') {
            num = c - 'A' + 10;
          }
          if (++str >= ep) break;
          c = *str;
          if (c >= '0' && c <= '9') {
            num = num * 0x10 + c - '0';
          } else if (c >= 'a' && c <= 'f') {
            num = num * 0x10 + c - 'a' + 10;
          } else if (c >= 'A' && c <= 'F') {
            num = num * 0x10 + c - 'A' + 10;
          }
          *(wp++) = num;
          str++;
        } else if (c == '+') {
          *(wp++) = ' ';
          str++;
        } else if (c <= ' ' || c == 0x7f) {
          str++;
        } else {
          *(wp++) = c;
          str++;
        }
      }
      *wp = '';
      *sp = wp - zbuf;
      return zbuf;
    }
  • 相关阅读:
    功能强大表格控件Spread for Windows Forms 中文版发布
    charles 证书在 andriod 7 及更高版本手机上的安装
    Hibernate 开发中问题
    Silverlight中DataForm对数据进行校验
    LightSpeed ORM .NET简单运用
    Hibernate manytomany实现
    C# _lopen判断文件是否正在被使用
    SQL Join
    Castle与Mixin
    Catalysis 的构成部分与框架
  • 原文地址:https://www.cnblogs.com/feika/p/3575123.html
Copyright © 2011-2022 走看看