コード例 #1
0
 public FileEncoding(Encoding type, bool bom, NewlineMode newline, bool endOfFileNewline)
 {
     this.Type             = type;
     this.Bom              = bom;
     this.Newline          = newline;
     this.EndOfFileNewline = endOfFileNewline;
 }
コード例 #2
0
        private static string GetNewline(NewlineMode newlineMode)
        {
            switch (newlineMode)
            {
            case NewlineMode.Unix: return("\n");

            case NewlineMode.Windows: return("\r\n");
            }

            throw new Exception($"Unrecognized newline-mode: '{newlineMode}'");
        }
コード例 #3
0
ファイル: Unicode.cs プロジェクト: leonluffy/FileDiff
        public static FileEncoding GetEncoding(string path)
        {
            Encoding    encoding    = Encoding.Default;
            bool        bom         = false;
            NewlineMode newlineMode = NewlineMode.Windows;

            var bytes     = new byte[10000];
            int bytesRead = 0;

            using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                bytesRead = fileStream.Read(bytes, 0, bytes.Length);
            }

            // Check if the file has a BOM
            if (bytes[0] == 0x2B && bytes[1] == 0x2F && bytes[2] == 0x76)
            {
                encoding = Encoding.UTF7;
                bom      = true;
            }
            else if (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
            {
                encoding = Encoding.UTF8;
                bom      = true;
            }
            else if (bytes[0] == 0xFF && bytes[1] == 0xFE)
            {
                encoding = Encoding.Unicode;
                bom      = true;
            }
            else if (bytes[0] == 0xFE && bytes[1] == 0xFF)
            {
                encoding = Encoding.BigEndianUnicode;
                bom      = true;
            }
            else if (bytes[0] == 0x00 && bytes[1] == 0x00 && bytes[2] == 0xFE && bytes[3] == 0xFF)
            {
                encoding = new UTF32Encoding(true, true);
                bom      = true;
            }

            // No bom found, check if data passes as a bom-less UTF-8 file
            else if (ValidUtf8(bytes, bytesRead))
            {
                encoding = Encoding.UTF8;
            }

            // Check if the file has null bytes, if so we assume it's a bom-less UTF-16
            else
            {
                for (int i = 0; i < bytesRead; i++)
                {
                    if (bytes[i] == 0)
                    {
                        encoding = Encoding.Unicode;
                        break;
                    }
                }
            }


            // Check what newline characters are used
            for (int i = 0; i < bytesRead; i++)
            {
                if (bytes[i] == '\n')
                {
                    newlineMode = NewlineMode.Unix;
                    break;
                }
                else if (bytes[i] == '\r')
                {
                    int newLineBytes = encoding == Encoding.Unicode || encoding == Encoding.BigEndianUnicode ? 2 : 1;
                    if (i < bytesRead - newLineBytes && bytes[i + newLineBytes] == '\n')
                    {
                        newlineMode = NewlineMode.Windows;
                        break;
                    }
                    newlineMode = NewlineMode.Mac;
                    break;
                }
            }

            return(new FileEncoding(encoding, bom, newlineMode));
        }
コード例 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CodeBuilder"/> class.
 /// </summary>
 /// <param name="indentMode">Mode to use for indenting</param>
 /// <param name="spaceIndentSize">When indenting with spaces this controls how many</param>
 /// <param name="newlineMode">Mode to use for ending lines</param>
 public CodeBuilder(IndentMode indentMode, int spaceIndentSize, NewlineMode newlineMode)
 {
     this.indent  = GetIndent(indentMode, spaceIndentSize);
     this.newline = GetNewline(newlineMode);
 }