Yea.Localization.Utils.NormalizeLineBreaks C# (CSharp) Method

NormalizeLineBreaks() public static method

public static NormalizeLineBreaks ( string input ) : string
input string
return string
        public static string NormalizeLineBreaks(string input)
        {
            //Written by Jon Skeet at http://stackoverflow.com/a/841453/17027

            // Allow 10% as a rough guess of how much the string may grow.
            // If we're wrong we'll either waste space or have extra copies -
            // it will still work
            var builder = new StringBuilder((int)(input.Length * 1.1));

            bool lastWasCR = false;

            foreach (char c in input)
            {
                if (lastWasCR)
                {
                    lastWasCR = false;
                    if (c == '\n')
                    {
                        continue; // Already written \r\n
                    }
                }
                switch (c)
                {
                    case '\r':
                        builder.Append("\r\n");
                        lastWasCR = true;
                        break;
                    case '\n':
                        builder.Append("\r\n");
                        break;
                    default:
                        builder.Append(c);
                        break;
                }
            }
            return builder.ToString();
        }

Usage Example

Beispiel #1
0
        public void Save(string xmlPath)
        {
            var xmlDocument = new XmlDocument();

            var xmlDeclaration = xmlDocument.CreateXmlDeclaration(@"1.0", @"utf-8", null);

            xmlDocument.InsertBefore(xmlDeclaration, xmlDocument.DocumentElement);

            var xmlNode = xmlDocument.AppendChild(xmlDocument.CreateElement(@"localization"));

            xmlNode = xmlNode.AppendChild(xmlDocument.CreateElement(@"strings"));

            foreach (var entry in _strings.Values)
            {
                var stringNode = xmlDocument.CreateElement(@"string");

                stringNode.SetAttribute(@"key", entry.Key);

                if (entry.AliasedKey)
                {
                    stringNode.SetAttribute(@"clone", entry.CloneOf);
                }
                else if (entry.DeriveFromParent)
                {
                    stringNode.SetAttribute(@"derive", @"true");
                }
                else if (!string.IsNullOrEmpty(entry.Value))
                {
                    stringNode.SetAttribute(@"value", Utils.NormalizeLineBreaks(entry.Value));

                    if (entry.BumpVersion)
                    {
                        ++entry.Version;
                    }

                    if (entry.Version != 0)
                    {
                        stringNode.SetAttribute(@"version", entry.Version.ToString());
                    }
                }
                else
                {
                    continue;
                }

                xmlNode.AppendChild(stringNode);
            }

            xmlDocument.Save(xmlPath);
        }