示例#1
0
        public bool TryReadTextString(Span <char> destination, out int charsWritten)
        {
            CborInitialByte header = PeekInitialByte(expectedType: CborMajorType.TextString);

            if (header.AdditionalInfo == CborAdditionalInfo.IndefiniteLength)
            {
                return(TryReadChunkedTextStringConcatenated(destination, out charsWritten));
            }

            int byteLength = checked ((int)ReadUnsignedInteger(_buffer.Span, header, out int additionalBytes));

            EnsureBuffer(1 + additionalBytes + byteLength);

            ReadOnlySpan <byte> encodedSlice = _buffer.Span.Slice(1 + additionalBytes, byteLength);
            int charLength = s_utf8Encoding.GetCharCount(encodedSlice);

            if (charLength > destination.Length)
            {
                charsWritten = 0;
                return(false);
            }

            s_utf8Encoding.GetChars(encodedSlice, destination);
            AdvanceBuffer(1 + additionalBytes + byteLength);
            AdvanceDataItemCounters();
            charsWritten = charLength;
            return(true);
        }
 public unsafe static int GetCharCount(this System.Text.Encoding encoding, ReadOnlySpan <byte> bytes)
 {
     fixed(byte *p = bytes)
     {
         return(encoding.GetCharCount(p, bytes.Length));
     }
 }
示例#3
0
        static int _m_GetCharCount(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                System.Text.Encoding gen_to_be_invoked = (System.Text.Encoding)translator.FastGetCSObj(L, 1);


                int gen_param_count = LuaAPI.lua_gettop(L);

                if (gen_param_count == 2 && (LuaAPI.lua_isnil(L, 2) || LuaAPI.lua_type(L, 2) == LuaTypes.LUA_TSTRING))
                {
                    byte[] _bytes = LuaAPI.lua_tobytes(L, 2);

                    int gen_ret = gen_to_be_invoked.GetCharCount(
                        _bytes);
                    LuaAPI.xlua_pushinteger(L, gen_ret);



                    return(1);
                }
                if (gen_param_count == 4 && (LuaAPI.lua_isnil(L, 2) || LuaAPI.lua_type(L, 2) == LuaTypes.LUA_TSTRING) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 3) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 4))
                {
                    byte[] _bytes = LuaAPI.lua_tobytes(L, 2);
                    int    _index = LuaAPI.xlua_tointeger(L, 3);
                    int    _count = LuaAPI.xlua_tointeger(L, 4);

                    int gen_ret = gen_to_be_invoked.GetCharCount(
                        _bytes,
                        _index,
                        _count);
                    LuaAPI.xlua_pushinteger(L, gen_ret);



                    return(1);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }

            return(LuaAPI.luaL_error(L, "invalid arguments to System.Text.Encoding.GetCharCount!"));
        }
示例#4
0
        public void GetCharCount_InvalidLength()
        {
            var strBytes = new[]
            {
                (byte)0
            };

            System.Text.Encoding enc = GetEncoding();
            Assert.Throws <ArgumentException>(() => enc.GetCharCount(strBytes));
        }
示例#5
0
        public void GetCharCount_ValidLength_MSB()
        {
            var strBytes = new[]
            {
                (byte)255,
                (byte)255,
            };

            System.Text.Encoding enc = GetEncoding();
            var count = enc.GetCharCount(strBytes);

            Assert.Equal <int>(65535, count);
        }
示例#6
0
        /// <summary>
        /// GB2312转换成UTF-8
        /// </summary>
        /// <param name="gbInfo"></param>
        /// <returns></returns>
        public static string ToUTF8(this string gbInfo)
        {
            string Info = string.Empty;

            System.Text.Encoding gb2312 = System.Text.Encoding.GetEncoding("gb2312");
            System.Text.Encoding utf_8  = System.Text.Encoding.GetEncoding("UTF-8");
            byte[] unicodeBytes         = gb2312.GetBytes(gbInfo);
            byte[] asciiBytes           = System.Text.Encoding.Convert(gb2312, utf_8, unicodeBytes);
            char[] asciiChars           = new char[utf_8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
            utf_8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
            string info = new string(asciiChars);

            return(info);
        }
示例#7
0
        public void GetCharCount_ValidLength_LSB()
        {
            var strBytes = new[]
            {
                (byte)0,
                (byte)3,
                (byte)'a',
                (byte)'b',
                (byte)'c'
            };

            System.Text.Encoding enc = GetEncoding();
            var count = enc.GetCharCount(strBytes);

            Assert.Equal <int>(3, count);
        }
示例#8
0
 private string GB2312ToUTF8(string str)
 {
     try
     {
         System.Text.Encoding utf8   = System.Text.Encoding.UTF8;
         System.Text.Encoding gb2312 = System.Text.Encoding.GetEncoding("GB2312");
         byte[] unicodeBytes         = gb2312.GetBytes(str);
         byte[] asciiBytes           = System.Text.Encoding.Convert(gb2312, utf8, unicodeBytes);
         char[] asciiChars           = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
         utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
         string result = new string(asciiChars);
         return(result);
     }
     catch
     {
         return("");
     }
 }
示例#9
0
        /// <summary>
        /// 处理表格编码
        /// </summary>
        /// <param name="tableData"></param>
        protected string CheckFileEncoding(TableToolDatas.TableToolsData tableData)
        {
            string tablePath = AssetDatabase.GetAssetPath(tableData.Table);

            System.IO.FileStream   fs     = new System.IO.FileStream(tablePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader br     = new System.IO.BinaryReader(fs);
            Byte[] buffer                 = br.ReadBytes(2);
            System.Text.Encoding encoding = null;
            if (buffer[0] >= 0xEF)
            {
                if (buffer[0] == 0xEF && buffer[1] == 0xBB)
                {
                    encoding = System.Text.Encoding.UTF8;
                }
            }

            if (encoding != System.Text.Encoding.UTF8)
            {
                //	强转	//
                fs     = new System.IO.FileStream(tablePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                br     = new System.IO.BinaryReader(fs);
                buffer = br.ReadBytes((int)fs.Length);
                //buffer = new byte[(int)fs.Length];
                //br.Read(buffer, 0, (int)fs.Length);
                System.Text.Encoding utf8   = System.Text.Encoding.UTF8;
                System.Text.Encoding gb2312 = System.Text.Encoding.GetEncoding("GB2312");
                byte[] asciiBytes           = System.Text.Encoding.Convert(gb2312, utf8, buffer);
                char[] asciiChars           = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
                utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
                string result = new string(asciiChars);

                br.Close();
                fs.Close();
                return(result);
            }
            return(null);
        }
示例#10
0
 public override int GetCharCount(byte[] bytes)
 {
     return(enc.GetCharCount(bytes));
 }