• 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;
    }
  • 相关阅读:
    JSP_内置对象_out
    POJ_2186_Popular Cows_强连通分量
    POJ_2411_Mondriaan's Dream_状态压缩dp
    POJ_2115_扩展欧几里德
    HDU_5690_快速幂,同余的性质
    POJ_1061_扩展欧几里德
    HDU_5833_高斯消元
    Delphi 使用 Datasnap 的几种三层应用技术总结
    BIM 开发商 --广州
    Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台
  • 原文地址:https://www.cnblogs.com/feika/p/3575123.html
Copyright © 2020-2023  润新知