External/OxyPlot/OxyPlot/Reporting/ReportWriters/StringExtensions.cs
changeset 391 5be8f2773237
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/OxyPlot/OxyPlot/Reporting/ReportWriters/StringExtensions.cs	Sat Jun 08 16:53:22 2013 +0000
     1.3 @@ -0,0 +1,130 @@
     1.4 +// --------------------------------------------------------------------------------------------------------------------
     1.5 +// <copyright file="StringExtensions.cs" company="OxyPlot">
     1.6 +//   The MIT License (MIT)
     1.7 +//
     1.8 +//   Copyright (c) 2012 Oystein Bjorke
     1.9 +//
    1.10 +//   Permission is hereby granted, free of charge, to any person obtaining a
    1.11 +//   copy of this software and associated documentation files (the
    1.12 +//   "Software"), to deal in the Software without restriction, including
    1.13 +//   without limitation the rights to use, copy, modify, merge, publish,
    1.14 +//   distribute, sublicense, and/or sell copies of the Software, and to
    1.15 +//   permit persons to whom the Software is furnished to do so, subject to
    1.16 +//   the following conditions:
    1.17 +//
    1.18 +//   The above copyright notice and this permission notice shall be included
    1.19 +//   in all copies or substantial portions of the Software.
    1.20 +//
    1.21 +//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    1.22 +//   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    1.23 +//   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    1.24 +//   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    1.25 +//   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    1.26 +//   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    1.27 +//   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    1.28 +// </copyright>
    1.29 +// <summary>
    1.30 +//   The string extensions.
    1.31 +// </summary>
    1.32 +// --------------------------------------------------------------------------------------------------------------------
    1.33 +namespace OxyPlot.Reporting
    1.34 +{
    1.35 +    using System.Collections.Generic;
    1.36 +    using System.Text;
    1.37 +
    1.38 +    /// <summary>
    1.39 +    /// The string extensions.
    1.40 +    /// </summary>
    1.41 +    public static class StringExtensions
    1.42 +    {
    1.43 +        /// <summary>
    1.44 +        /// The repeat.
    1.45 +        /// </summary>
    1.46 +        /// <param name="source">
    1.47 +        /// The source.
    1.48 +        /// </param>
    1.49 +        /// <param name="n">
    1.50 +        /// The n.
    1.51 +        /// </param>
    1.52 +        /// <returns>
    1.53 +        /// The repeat.
    1.54 +        /// </returns>
    1.55 +        public static string Repeat(this string source, int n)
    1.56 +        {
    1.57 +            var sb = new StringBuilder(n * source.Length);
    1.58 +            for (int i = 0; i < n; i++)
    1.59 +            {
    1.60 +                sb.Append(source);
    1.61 +            }
    1.62 +
    1.63 +            return sb.ToString();
    1.64 +        }
    1.65 +
    1.66 +        /// <summary>
    1.67 +        /// The split lines.
    1.68 +        /// </summary>
    1.69 +        /// <param name="s">
    1.70 +        /// The s.
    1.71 +        /// </param>
    1.72 +        /// <param name="lineLength">
    1.73 +        /// The line length.
    1.74 +        /// </param>
    1.75 +        /// <returns>
    1.76 +        /// </returns>
    1.77 +        public static string[] SplitLines(this string s, int lineLength = 80)
    1.78 +        {
    1.79 +            var lines = new List<string>();
    1.80 +
    1.81 +            int i = 0;
    1.82 +            while (i < s.Length)
    1.83 +            {
    1.84 +                int len = FindLineLength(s, i, lineLength);
    1.85 +                lines.Add(len == 0 ? s.Substring(i).Trim() : s.Substring(i, len).Trim());
    1.86 +                i += len;
    1.87 +                if (len == 0)
    1.88 +                {
    1.89 +                    break;
    1.90 +                }
    1.91 +            }
    1.92 +
    1.93 +            return lines.ToArray();
    1.94 +        }
    1.95 +
    1.96 +        /// <summary>
    1.97 +        /// The find line length.
    1.98 +        /// </summary>
    1.99 +        /// <param name="text">
   1.100 +        /// The text.
   1.101 +        /// </param>
   1.102 +        /// <param name="i">
   1.103 +        /// The i.
   1.104 +        /// </param>
   1.105 +        /// <param name="maxLineLength">
   1.106 +        /// The max line length.
   1.107 +        /// </param>
   1.108 +        /// <returns>
   1.109 +        /// The find line length.
   1.110 +        /// </returns>
   1.111 +        private static int FindLineLength(string text, int i, int maxLineLength)
   1.112 +        {
   1.113 +            int i2 = i + 1;
   1.114 +            int len = 0;
   1.115 +            while (i2 < i + maxLineLength && i2 < text.Length)
   1.116 +            {
   1.117 +                i2 = text.IndexOfAny(" \n\r".ToCharArray(), i2 + 1);
   1.118 +                if (i2 == -1)
   1.119 +                {
   1.120 +                    i2 = text.Length;
   1.121 +                }
   1.122 +
   1.123 +                if (i2 - i < maxLineLength)
   1.124 +                {
   1.125 +                    len = i2 - i;
   1.126 +                }
   1.127 +            }
   1.128 +
   1.129 +            return len;
   1.130 +        }
   1.131 +
   1.132 +    }
   1.133 +}
   1.134 \ No newline at end of file