Esempio n. 1
0
        /// <summary>
        /// Manipulates the specified text value.
        /// </summary>
        /// <param name="value">The value to manipulate.</param>
        /// <param name="stringComparison">The type of string comparison.</param>
        /// <param name="fileLineTypes">The type of the line ending in the <see cref="Scintilla"/> control.</param>
        /// <returns>A string containing the manipulated text.</returns>
        public static string Manipulate(string value, StringComparison stringComparison, FileLineTypes fileLineTypes)
        {
            try
            {
                var lines = new List <string>();
                using var reader = new StringReader(value);

                string readLine;

                while ((readLine = reader.ReadLine()) != null)
                {
                    lines.Add(readLine);
                }

                var linesNew = new List <string>();


                foreach (var line in lines)
                {
                    if (!linesNew.Exists(f => f.Equals(line, stringComparison)))
                    {
                        linesNew.Add(line);
                    }
                }

                string lineSeparator = Environment.NewLine;

                if (fileLineTypes.HasFlag(FileLineTypes.CRLF))
                {
                    lineSeparator = "\r\n";
                }
                else if (fileLineTypes.HasFlag(FileLineTypes.LF))
                {
                    lineSeparator = "\n";
                }
                else if (fileLineTypes.HasFlag(FileLineTypes.CR))
                {
                    lineSeparator = "\r";
                }

                return(string.Join(lineSeparator, linesNew));
            }
            catch (Exception ex)
            {
                ErrorHandlingBase.ExceptionLogAction?.Invoke(ex);
                return(value);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the file line types of a given memory stream.
        /// </summary>
        /// <param name="stream">The memory stream to be used to check for the line endings.</param>
        /// <param name="fileLineTypes">The types of line endings wanted to be included in the search.</param>
        /// <returns>A collection of line ending key value pairs with their enumeration values and their names.</returns>
        public static IEnumerable <KeyValuePair <FileLineTypes, string> > GetFileLineTypes(MemoryStream stream,
                                                                                           FileLineTypes fileLineTypes = // the default set..
                                                                                           FileLineTypes.CR |
                                                                                           FileLineTypes.CRLF |
                                                                                           FileLineTypes.LF |
                                                                                           FileLineTypes.LFCR |
                                                                                           FileLineTypes.UCR |
                                                                                           FileLineTypes.UCRLF |
                                                                                           FileLineTypes.ULF |
                                                                                           FileLineTypes.ULFCR)
        {
            // do note that this is the "master" method of the overloads, so the logic is going to be weird..
            List <KeyValuePair <FileLineTypes, string> > result = new List <KeyValuePair <FileLineTypes, string> >();

            // the contents of the given memory stream is requires as a byte array..
            byte[] contentsBytes = stream.ToArray();

            // indicators if a negative or positive index for the byte array
            // search was given..
            bool eof1, eof2, eof3;

            // initialize the loop variable..
            int i = 0;

            // loop through the bytes in the array..
            while (i < contentsBytes.Length)
            {
                // comparison of CR+LF without Unicode..
                if (ContainsBytes(i, contentsBytes, new byte[] { 0x0D, 0x0A }, out eof1))
                {
                    if (eof1) // check for an overflow of the byte array..
                    {
                        // the index was outside the byte array so do
                        // increase the loop variable by one and continue..
                        i++;
                        continue;
                    }

                    i += 2; // increase the loop variable by the amount of bytes used in the comparison..

                    // check that the result value doesn't already contain the given enumeration and string pair..
                    if (!result.Exists(f => f.Key == FileLineTypes.CRLF) && fileLineTypes.HasFlag(FileLineTypes.CRLF))
                    {
                        result.Add(
                            new KeyValuePair <FileLineTypes, string>(FileLineTypes.CRLF, GetLineEndingDescriptionByEnumeration(FileLineTypes.CRLF, UseEnumNames)));
                    }
                    continue; // match found so do continue the loop..
                }

                // comparison of LF+CR without Unicode..
                if (ContainsBytes(i, contentsBytes, new byte[] { 0x0A, 0x0D }, out eof1))
                {
                    if (eof1) // check for an overflow of the byte array..
                    {
                        // the index was outside the byte array so do
                        // increase the loop variable by one and continue..
                        i++;
                        continue;
                    }

                    i += 2; // increase the loop variable by the amount of bytes used in the comparison..

                    // check that the result value doesn't already contain the given enumeration and string pair..
                    if (!result.Exists(f => f.Key == FileLineTypes.LFCR) && fileLineTypes.HasFlag(FileLineTypes.LFCR))
                    {
                        result.Add(
                            new KeyValuePair <FileLineTypes, string>(FileLineTypes.LFCR, GetLineEndingDescriptionByEnumeration(FileLineTypes.LFCR, UseEnumNames)));
                    }

                    continue; // match found so do continue the loop..
                }

                // comparison of LF without Unicode..
                if (ContainsBytes(i, contentsBytes, new byte[] { 0x0A }, out eof1) &&
                    !ContainsBytes(i + 1, contentsBytes, new byte[] { 0x0D }, out eof2) &&
                    !ContainsBytes(i - 1, contentsBytes, new byte[] { 0x0D }, out eof3))
                {
                    if (eof1 || eof2 || eof3) // check for an overflow of the byte array..
                    {
                        // one or more of the indexes was outside the byte array so do
                        // increase the loop variable by one and continue..
                        i++;
                        continue;
                    }

                    i++; // increase the loop variable by the amount of bytes used in the comparison..

                    // check that the result value doesn't already contain the given enumeration and string pair..
                    if (!result.Exists(f => f.Key == FileLineTypes.LF) && fileLineTypes.HasFlag(FileLineTypes.LF))
                    {
                        result.Add(
                            new KeyValuePair <FileLineTypes, string>(FileLineTypes.LF, GetLineEndingDescriptionByEnumeration(FileLineTypes.LF, UseEnumNames)));
                    }

                    continue; // match found so do continue the loop..
                }

                // comparison of CR without Unicode..
                if (ContainsBytes(i, contentsBytes, new byte[] { 0x0D }, out eof1) &&
                    !ContainsBytes(i + 1, contentsBytes, new byte[] { 0x0A }, out eof2) &&
                    !ContainsBytes(i - 1, contentsBytes, new byte[] { 0x0A }, out eof3))
                {
                    if (eof1 || eof2 || eof3) // check for an overflow of the byte array..
                    {
                        // one or more of the indexes was outside the byte array so do
                        // increase the loop variable by one and continue..
                        i++;
                        continue;
                    }

                    i++; // increase the loop variable by the amount of bytes used in the comparison..

                    // check that the result value doesn't already contain the given enumeration and string pair..
                    if (!result.Exists(f => f.Key == FileLineTypes.CR) && fileLineTypes.HasFlag(FileLineTypes.CR))
                    {
                        result.Add(
                            new KeyValuePair <FileLineTypes, string>(FileLineTypes.CR, GetLineEndingDescriptionByEnumeration(FileLineTypes.CR, UseEnumNames)));
                    }
                    continue; // match found so do continue the loop..
                }

                // comparison of CR+LF with Unicode..
                if (ContainsBytes(i, contentsBytes, new byte[] { 0x00, 0x0D, 0x00, 0x0A }, out eof1))
                {
                    if (eof1) // check for an overflow of the byte array..
                    {
                        // the index was outside the byte array so do
                        // increase the loop variable by one and continue..
                        i++;
                        continue;
                    }

                    i += 4; // increase the loop variable by the amount of bytes used in the comparison..

                    // check that the result value doesn't already contain the given enumeration and string pair..
                    if (!result.Exists(f => f.Key == FileLineTypes.UCRLF) && fileLineTypes.HasFlag(FileLineTypes.UCRLF))
                    {
                        result.Add(
                            new KeyValuePair <FileLineTypes, string>(FileLineTypes.UCRLF, GetLineEndingDescriptionByEnumeration(FileLineTypes.UCRLF, UseEnumNames)));
                    }
                    continue; // match found so do continue the loop..
                }

                // comparison of LF+CR with Unicode..
                if (ContainsBytes(i, contentsBytes, new byte[] { 0x00, 0x0A, 0x00, 0x0D }, out eof1))
                {
                    if (eof1) // check for an overflow of the byte array..
                    {
                        // the index was outside the byte array so do
                        // increase the loop variable by one and continue..
                        i++;
                        continue;
                    }

                    i += 4; // increase the loop variable by the amount of bytes used in the comparison..

                    // check that the result value doesn't already contain the given enumeration and string pair..
                    if (!result.Exists(f => f.Key == FileLineTypes.ULFCR) && fileLineTypes.HasFlag(FileLineTypes.ULFCR))
                    {
                        result.Add(
                            new KeyValuePair <FileLineTypes, string>(FileLineTypes.ULFCR, GetLineEndingDescriptionByEnumeration(FileLineTypes.ULFCR, UseEnumNames)));
                    }
                    continue; // match found so do continue the loop..
                }

                // comparison of LF with Unicode..
                if (ContainsBytes(i, contentsBytes, new byte[] { 0x00, 0x0A }, out eof1) &&
                    !ContainsBytes(i + 2, contentsBytes, new byte[] { 0x00, 0x0D }, out eof2) &&
                    !ContainsBytes(i - 2, contentsBytes, new byte[] { 0x00, 0x0D }, out eof3))
                {
                    if (eof1 || eof2 || eof3) // check for an overflow of the byte array..
                    {
                        // one or more of the indexes was outside the byte array so do
                        // increase the loop variable by one and continue..
                        i++;
                        continue;
                    }

                    i += 2; // increase the loop variable by the amount of bytes used in the comparison..

                    // check that the result value doesn't already contain the given enumeration and string pair..
                    if (!result.Exists(f => f.Key == FileLineTypes.ULF) && fileLineTypes.HasFlag(FileLineTypes.ULF))
                    {
                        result.Add(
                            new KeyValuePair <FileLineTypes, string>(FileLineTypes.ULF, GetLineEndingDescriptionByEnumeration(FileLineTypes.ULF, UseEnumNames)));
                    }

                    continue; // match found so do continue the loop..
                }

                // comparison of CR with Unicode..
                if (ContainsBytes(i, contentsBytes, new byte[] { 0x00, 0x0D }, out eof1) &&
                    !ContainsBytes(i + 2, contentsBytes, new byte[] { 0x00, 0x0A }, out eof2) &&
                    !ContainsBytes(i - 2, contentsBytes, new byte[] { 0x00, 0x0A }, out eof3))
                {
                    if (eof1 || eof2 || eof3) // check for an overflow of the byte array..
                    {
                        // one or more of the indexes was outside the byte array so do
                        // increase the loop variable by one and continue..
                        i++;
                        continue;
                    }

                    i += 2; // increase the loop variable by the amount of bytes used in the comparison..

                    // check that the result value doesn't already contain the given enumeration and string pair..
                    if (!result.Exists(f => f.Key == FileLineTypes.UCR) && fileLineTypes.HasFlag(FileLineTypes.UCR))
                    {
                        result.Add(
                            new KeyValuePair <FileLineTypes, string>(FileLineTypes.UCR, GetLineEndingDescriptionByEnumeration(FileLineTypes.UCR, UseEnumNames)));
                    }
                    continue; // match found so do continue the loop..
                }

                // comparison of RS (see: FileLineTypes.RS description..)..
                if (ContainsBytes(i, contentsBytes, new byte[] { 0x1E }, out eof1))
                {
                    if (eof1) // check for an overflow of the byte array..
                    {
                        // the index was outside the byte array so do
                        // increase the loop variable by one and continue..
                        i++;
                        continue;
                    }

                    i++; // increase the loop variable by the amount of bytes used in the comparison..

                    // check that the result value doesn't already contain the given enumeration and string pair..
                    if (!result.Exists(f => f.Key == FileLineTypes.RS) && fileLineTypes.HasFlag(FileLineTypes.RS))
                    {
                        result.Add(
                            new KeyValuePair <FileLineTypes, string>(FileLineTypes.RS, GetLineEndingDescriptionByEnumeration(FileLineTypes.RS, UseEnumNames)));
                    }
                    continue; // match found so do continue the loop..
                }

                // comparison of NL (see: FileLineTypes.NL description..)..
                if (ContainsBytes(i, contentsBytes, new byte[] { 0x15 }, out eof1))
                {
                    if (eof1) // check for an overflow of the byte array..
                    {
                        // the index was outside the byte array so do
                        // increase the loop variable by one and continue..
                        i++;
                        continue;
                    }

                    i++; // increase the loop variable by the amount of bytes used in the comparison..

                    // check that the result value doesn't already contain the given enumeration and string pair..
                    if (!result.Exists(f => f.Key == FileLineTypes.NL) && fileLineTypes.HasFlag(FileLineTypes.NL))
                    {
                        result.Add(
                            new KeyValuePair <FileLineTypes, string>(FileLineTypes.NL, GetLineEndingDescriptionByEnumeration(FileLineTypes.NL, UseEnumNames)));
                    }
                    continue; // match found so do continue the loop..
                }

                // comparison of ATASCII (see: FileLineTypes.ATASCII description..)..
                if (ContainsBytes(i, contentsBytes, new byte[] { 0x9B }, out eof1))
                {
                    if (eof1) // check for an overflow of the byte array..
                    {
                        // the index was outside the byte array so do
                        // increase the loop variable by one and continue..
                        i++;
                        continue;
                    }

                    i++; // increase the loop variable by the amount of bytes used in the comparison..

                    // check that the result value doesn't already contain the given enumeration and string pair..
                    if (!result.Exists(f => f.Key == FileLineTypes.ATASCII) && fileLineTypes.HasFlag(FileLineTypes.ATASCII))
                    {
                        result.Add(
                            new KeyValuePair <FileLineTypes, string>(FileLineTypes.ATASCII, GetLineEndingDescriptionByEnumeration(FileLineTypes.ATASCII, UseEnumNames)));
                    }
                    continue; // match found so do continue the loop..
                }

                // comparison of NEWLINE (see: FileLineTypes.NEWLINE description..)..
                if (ContainsBytes(i, contentsBytes, new byte[] { 0x76 }, out eof1))
                {
                    if (eof1) // check for an overflow of the byte array..
                    {
                        // the index was outside the byte array so do
                        // increase the loop variable by one and continue..
                        i++;
                        continue;
                    }

                    i++; // increase the loop variable by the amount of bytes used in the comparison..

                    // check that the result value doesn't already contain the given enumeration and string pair..
                    if (!result.Exists(f => f.Key == FileLineTypes.NEWLINE) && fileLineTypes.HasFlag(FileLineTypes.NEWLINE))
                    {
                        result.Add(
                            new KeyValuePair <FileLineTypes, string>(FileLineTypes.NEWLINE, GetLineEndingDescriptionByEnumeration(FileLineTypes.NEWLINE, UseEnumNames)));
                    }
                    continue; // match found so do continue the loop..
                }
                i++;          // the loop must not be infinite so increase the loop variable by one..
            }

            // if nothing was found assume CR+LF..
            if (result.Count == 0)
            {
                result.Add(
                    new KeyValuePair <FileLineTypes, string>(FileLineTypes.CRLF, GetLineEndingDescriptionByEnumeration(FileLineTypes.CRLF, UseEnumNames)));
            }

            // if multiple items was found, assume mixed line endings..
            if (result.Count() > 1)
            {
                result.Add(
                    new KeyValuePair <FileLineTypes, string>(FileLineTypes.Mixed, GetLineEndingDescriptionByEnumeration(FileLineTypes.Mixed, UseEnumNames)));
            }

            // return the result..
            return(result);
        }