lvzixun / sproto-Csharp

A pure C# implementation of sproto.
140 stars 53 forks source link

这个sz是否有问题 #7

Closed dpull closed 8 years ago

dpull commented 8 years ago

随意看代码,隐约感觉有问题,这个sz是v.Length,而非utf8编码后的length

    public void write_string(List<string> str_list, int tag) {
        if (str_list == null || str_list.Count <= 0)
            return;

        // write size length
        int sz = 0;
        foreach (string v in str_list) {
            sz += SprotoTypeSize.sizeof_length + v.Length;
        }
        this.fill_size (sz);

        // write stirng
        foreach (string v in str_list) {
            this.encode_string (v);
        }

        this.write_tag (tag, 0);
    }
lvzixun commented 8 years ago

恩,确实是,之前改过read_stringwrite_string。貌似把这个给漏掉了。 thanks~ ;)

dpull commented 8 years ago

btw:string类型 在C#表示为byte[]是否会更好,可以增加两个属性来兼容以前的代码,目前utf8类型的string和lua中的string类型表意不同

举个例子

        private byte[] _Data; // tag 2
        public byte[] Data_Bin {
            get { return _Data; }
            set { base.has_field.set_field (2, true); _Data = value; }
        }
        public string Data {
            get { return System.Text.Encoding.UTF8.GetString (_Data); }
            set { base.has_field.set_field (2, true); _Data = System.Text.Encoding.UTF8.GetBytes (value); }
        }            
lvzixun commented 8 years ago

可以, 你提交下PR,我这边merge下。

在 2016年1月5日 下午2:08,Acai notifications@github.com写道:

btw:string类型 在C#表示为byte[]是否会更好,可以增加两个属性来兼容以前的代码(通过get;set将string转化为byte[]),目前utf8类型的string和lua中的string类型表意不同

— Reply to this email directly or view it on GitHub https://github.com/lvzixun/sproto-Csharp/issues/7#issuecomment-168909636 .