• 2016-02-25

    Objective-C 对二进制数据 NSData 进行 URL 编码

    Views: 10095 | No Comments

    Objective-C 和其它所谓的 Unicode 友好型编程语言, 大多对内存不友好, 这些语言一提到"二进制", 好像就当机了一样.

    所以, 我认为 PHP 确实是最好的编程语言, 对于 PHP 来说, 字符串就是二进制, 二进制就是字符串, 不管你什么字符集. 这并不是说 PHP 支持 Unicode, 事实上, PHP 对 Unicode 的支持是最友好最高级的. 例如, 拿到一段内存, 你想把它当作为 UTF-8 或者 UTF-16, 随你意, 只要你认为它是什么, 它就是什么, 然后, PHP 提供了对应的函数来处理.

    回到题目, 在 Objective-C 里, 要将一段二进制数据(也就是一段内存)进行 urlencode(URL 编码), 应该怎么做? 很不幸, CFURLCreateStringByAddingPercentEscapes() 函数只处理字符串. 没错, 又是字符串! 这就是不把字符串当二进制的坏处!

    所以, 只能自己写一个函数.

    static int is_safe_char(char c){
        if(c == '.' || c == '-' || c == '_'){
            return 1;
        }else if(c >= '0' && c <= '9'){
            return 1;
        }else if(c >= 'A' && c <= 'Z'){
            return 1;
        }else if(c >= 'a' && c <= 'z'){
            return 1;
        }
        return 0;
    }
    
    static NSString *urlencode_data(NSData *data){
        NSMutableString *ret = [[NSMutableString alloc] init];
        char *ptr = (char *)data.bytes;
        int len = (int)data.length;
        for(int i=0; i<len; i++){
            char c = ptr[i];
            if(is_safe_char(c)){
                [ret appendFormat:@"%c", c];
            }else{
                [ret appendFormat:@"%%%02X", c];
            }
        }
        return ret;
    }
    
    Posted by ideawu at 2016-02-25 16:20:03
  • 2016-02-16

    iphp 框架增加 lazyload 特性

    Views: 15521 | No Comments

    基类中为了方便原则, 一般会加载所有可能用到的属性到 Context 中, 但这会导致性能问题, 所以需要 lazyload 机制. 例如, 某些属性只有在子类中被用到时, 才会真正地去查询数据库, 如果子类中不使用这些属性, 则不会发生数据库请求.

    原型:

    Context::lazyload($name, callable $callback_func);
    

    示例:

    class AppController extends Controller
    {
        function init($ctx){
            parent::init($ctx);
            $ctx->lazyload('account', array($this, 'ctx_lazyload'));
        }
    
        private function ctx_lazyload($name, $ctx){
            if($name == 'account'){
                return Account::get($ctx->user->id);
            }
        }
    }
    
    Posted by ideawu at 2016-02-16 11:41:54
  • 2016-01-29

    mtr 查看网络丢包率

    Views: 10815 | No Comments
    mtr 8.8.8.8
    

    统计网卡流量:http://www.ideawu.net/blog/archives/919.html

    Posted by ideawu at 2016-01-29 22:38:31
  • 2016-01-29

    出现大量TCP连接状态CLOSE_WAIT的原因

    Views: 8861 | No Comments

    1. 程序bug, 没有关闭socket.
    2. 带宽不足.

    Posted by ideawu at 14:38:27
  • 2016-01-25

    获取当前shell脚本的路径, 支持软链接

    Views: 9263 | No Comments

    获取当前shell脚本所在的路径(目录), 支持软链接.

    DIR=`S=\`readlink "$0"\`; [ -z "$S" ] && S=$0; dirname $S`
    
    Posted by ideawu at 2016-01-25 14:53:46
  • 2016-01-15

    Linux统计网卡IP流量

    Views: 11713 | No Comments
    iftop -n -N -B -i eth1 -t -P
    
    • -n 不要反查主机名dns
    • -N 不要将端口解析为文字描述
    • -B 显示流量单位为字节, 而不是位
    • -i 指定网卡
    • -t 不要显示为字符窗口形式(类似top命令), 而是滚动输出
    • -P 按端口统计

    mtr 查看网络丢包率

    mtr 8.8.8.8
    
    Posted by ideawu at 2016-01-15 20:33:41
|<<<212223242526272829>>>| 25/138 Pages, 825 Results.