forked from oxyplot/oxyplot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringHelper.cs
More file actions
133 lines (120 loc) · 5.2 KB
/
StringHelper.cs
File metadata and controls
133 lines (120 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StringHelper.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Provides extended string formatting functionality.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
#nullable enable
namespace OxyPlot
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
/// <summary>
/// Provides extended string formatting functionality.
/// </summary>
public static class StringHelper
{
/// <summary>
/// The formatting expression.
/// </summary>
private static readonly Regex FormattingExpression = new Regex("{(?<Property>.+?)(?<Format>\\:.*?)?}");
/// <summary>
/// Replaces the format items in the specified string.
/// </summary>
/// <param name="provider">The culture specific format provider.</param>
/// <param name="formatString">The format string.</param>
/// <param name="item">The item.</param>
/// <param name="values">The values.</param>
/// <returns>The formatted string.</returns>
/// <remarks>The format string and values works as in <c>String.Format</c>.
/// In addition, you can format properties of the item object by using the syntax
/// <c>{PropertyName:Formatstring}</c>.
/// E.g. if you have a "Value" property in your item's class, use <c>"{Value:0.00}"</c> to output the value with two digits.
/// Note that this formatting is using reflection and does not have the same performance as string.Format.</remarks>
public static string Format(IFormatProvider provider, string formatString, object item, params object[] values)
{
// Replace items on the format {Property[:Formatstring]}
var s = FormattingExpression.Replace(
formatString,
delegate (Match match)
{
var property = match.Groups["Property"].Value;
if (property.Length > 0 && char.IsDigit(property[0]))
{
return match.Value;
}
var pi = item.GetType().GetRuntimeProperty(property);
if (pi == null)
{
return string.Empty;
}
var v = pi.GetValue(item, null);
var format = match.Groups["Format"].Value;
var fs = "{0" + format + "}";
return string.Format(provider, fs, v);
});
// Also apply the standard formatting
s = string.Format(provider, s, values);
return s;
}
/// <summary>
/// Creates a valid format string on the form "{0:###}".
/// </summary>
/// <param name="input">The input format string.</param>
/// <returns>The corrected format string.</returns>
public static string CreateValidFormatString(string input)
{
if (string.IsNullOrEmpty(input))
{
return "{0}";
}
if (input.Contains("{"))
{
return input;
}
return string.Concat("{0:", input, "}");
}
/// <summary>
/// Formats each item in a sequence by the specified format string and property.
/// </summary>
/// <param name="source">The source target.</param>
/// <param name="propertyName">The property name.</param>
/// <param name="formatString">The format string. The format argument {0} can be used for the value of the property in each element of the sequence.</param>
/// <param name="provider">The format provider.</param>
/// <exception cref="System.InvalidOperationException">Could not find property.</exception>
public static IEnumerable<string> Format(this IEnumerable source, string propertyName, string formatString, IFormatProvider provider)
{
var fs = CreateValidFormatString(formatString);
if (string.IsNullOrEmpty(propertyName))
{
foreach (var element in source)
{
yield return string.Format(provider, fs, element);
}
}
else
{
var reflectionPath = new ReflectionPath(propertyName);
foreach (var element in source)
{
var value = reflectionPath.GetValue(element);
yield return string.Format(provider, fs, value);
}
}
}
/// <summary>
/// Splits the given text into separate lines.
/// </summary>
/// <param name="text">The text to split.</param>
/// <returns>An array of the individual lines.</returns>
public static string[] SplitLines(string text)
{
return Regex.Split(text, "\r?\n");
}
}
}