Esempio n. 1
0
        /// <summary>
        ///     Automatically detects the Encoding type of a given byte buffer.
        /// </summary>
        /// <param name="buffer">The byte buffer.</param>
        /// <param name="size">The size of the byte buffer.</param>
        /// <returns>The Encoding type or Encoding.None if unknown.</returns>
        public TextEncode DetectWithoutBom(byte[] buffer, int size)
        {
            // Now check for valid UTF8
            TextEncode encoding = CheckUtf8(buffer, size);

            if (encoding != TextEncode.None)
            {
                return(encoding);
            }

            // ANSI or None (binary) then 一个零都没有情况。
            if (!ContainsZero(buffer, size))
            {
                CheckChinese(buffer, size);
                return(TextEncode.Ansi);
            }

            // Now try UTF16  按寻找换行字符先进行判断
            encoding = CheckByNewLineChar(buffer, size);
            if (encoding != TextEncode.None)
            {
                return(encoding);
            }

            // 没办法了,只能按0出现的次数比率,做大体的预判
            encoding = CheckByZeroNumPercent(buffer, size);
            if (encoding != TextEncode.None)
            {
                return(encoding);
            }

            // Found a null, return based on the preference in null_suggests_binary_
            return(TextEncode.None);
        }
Esempio n. 2
0
        private void btBat_Click(object sender, EventArgs e)
        {
            lab.Text    = "";
            labCur.Text = "";
            Application.DoEvents();
            var bat = _batches[tComboBox.SelectedIndex];

            EnumFiles(tbDir.Text, tbPattern.Text, file =>
            {
                labCur.Text    = file;
                var textEnc    = new TextEncode();
                string content = textEnc.ReadText(file);
                string result  = bat.ReplaceBat(content);
                if (content != result)
                {
                    textEnc.WriteBySameEncoding(file, result);
                    lab.AppendText(file + "\r\n");
                }
                Application.DoEvents();
            });
            labCur.Text = "";
            lab.AppendText("Done!");
        }
Esempio n. 3
0
 public virtual String ProvideString(byte[] bytArr, int nIndex, int nCount)
 {
     return(TextEncode.GetString(bytArr, nIndex, nCount));
 }
Esempio n. 4
0
 public byte[] ProvideBytes(String str)
 {
     return(TextEncode.GetBytes(str));
 }