Exemple #1
0
        private void IdentifyLineEndings(string canonicalName, string fullText, BomCheckEOLOptions localOptions, CheckEOLStats cs)
        {
            StringBuilder sb = new StringBuilder();

            int[] leTypes = new int[3];

            BomClean.LineEndingCursor cursor = new BomClean.LineEndingCursor(fullText);

            bool specific = (localOptions.CRLFOnly || localOptions.CROnly || localOptions.LFOnly);

            while (cursor.NextLine())
            {
                var leType = cursor.LineEndingType;

                if (leType != BomClean.LineEndingType.EOF)
                {
                    leTypes[(int)leType]++;
                }
            }
            if (leTypes.Where(x => x > 0).Count() > 1)
            {
                if (!specific)
                {
                    Printer.PrintMessage("#b#{0}##: #w#Mixed## #q#({1})##", canonicalName, BomClean.LETypesToString(leTypes));
                }
                lock (cs)
                    cs.MixedFiles++;
            }
            else if (leTypes[0] > 0)
            {
                if (localOptions.CRLFOnly || (!specific && !localOptions.Ambigious))
                {
                    Printer.PrintMessage("#b#{0}##: #s#CRLF##", canonicalName);
                }
                lock (cs)
                    cs.CRLFFiles++;
            }
            else if (leTypes[1] > 0)
            {
                if (localOptions.LFOnly || (!specific && !localOptions.Ambigious))
                {
                    Printer.PrintMessage("#b#{0}##: #s#LF##", canonicalName);
                }
                lock (cs)
                    cs.LFFiles++;
            }
            else if (leTypes[2] > 0)
            {
                if (localOptions.CROnly || (!specific && !localOptions.Ambigious))
                {
                    Printer.PrintMessage("#b#{0}##: #w#CR##", canonicalName);
                }
                lock (cs)
                    cs.CRFiles++;
            }
            else if (!specific && !localOptions.Ambigious)
            {
                Printer.PrintMessage("#b#{0}##: #c#(No line endings)##", canonicalName);
            }
            return;
        }
Exemple #2
0
        private bool UnifyLineEndings(string fullText, BomSetEOLOptions localOptions, out string resultString, out BomClean.LineEndingType let)
        {
            resultString = null;
            StringBuilder sb = new StringBuilder();

            int[] leTypes = new int[3];

            bool updatedLines = false;

            BomClean.LineEndingCursor cursor = new BomClean.LineEndingCursor(fullText);

            if (localOptions.CRLF)
            {
                let = BomClean.LineEndingType.CRLF;
            }
            else if (localOptions.CR)
            {
                let = BomClean.LineEndingType.CR;
            }
            else if (localOptions.LF)
            {
                let = BomClean.LineEndingType.LF;
            }
            else
            {
                while (cursor.NextLine())
                {
                    var leType = cursor.LineEndingType;

                    if (leType != BomClean.LineEndingType.EOF)
                    {
                        leTypes[(int)leType]++;
                    }
                }
                if (leTypes[0] > leTypes[1])
                {
                    if (leTypes[0] > leTypes[2])
                    {
                        let = (BomClean.LineEndingType) 0;
                    }
                    else
                    {
                        let = (BomClean.LineEndingType) 2;
                    }
                }
                else
                {
                    if (leTypes[1] > leTypes[2])
                    {
                        let = (BomClean.LineEndingType) 1;
                    }
                    else
                    {
                        let = (BomClean.LineEndingType) 2;
                    }
                }
                if (leTypes.Where(x => x > 0).Count() < 2)
                {
                    return(false);
                }
                cursor = new BomClean.LineEndingCursor(fullText);
            }

            while (cursor.NextLine())
            {
                var le = cursor.LineEndingType;

                var line = cursor.Line;

                sb.Append(line.Array, line.Offset, line.Count);
                if (le == BomClean.LineEndingType.EOF)
                {
                    break;
                }

                if (le != let)
                {
                    updatedLines = true;
                }
                if (let == BomClean.LineEndingType.CR)
                {
                    sb.Append('\x0D');
                }
                else if (let == BomClean.LineEndingType.CRLF)
                {
                    sb.Append('\x0D');
                    sb.Append('\x0A');
                }
                else if (let == BomClean.LineEndingType.LF)
                {
                    sb.Append('\x0A');
                }
            }
            if (updatedLines)
            {
                resultString = sb.ToString();
                return(true);
            }
            return(false);
        }