• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / EntityChange / 13417967502

19 Feb 2025 04:51PM UTC coverage: 45.43% (+5.7%) from 39.697%
13417967502

push

github

pwelter34
fix failing test

287 of 726 branches covered (39.53%)

Branch coverage included in aggregate %.

563 of 1145 relevant lines covered (49.17%)

73.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.54
/src/EntityChange/Extensions/StringExtensions.cs
1
using System.Diagnostics.CodeAnalysis;
2
using System.Text;
3
using System.Text.RegularExpressions;
4

5
namespace EntityChange.Extensions;
6

7
/// <summary>
8
/// Extension methods for <see cref="string"/> type.
9
/// </summary>
10
public static partial class StringExtensions
11
{
12
#if NET7_0_OR_GREATER
13
    [GeneratedRegex("([A-Z][a-z]*)|([0-9]+)")]
14
    private static partial Regex WordsExpression();
15
#else
16
    private static readonly Lazy<Regex> _wordsRegex = new(() => new("([A-Z][a-z]*)|([0-9]+)"));
17
    private static Regex WordsExpression() => _wordsRegex.Value;
18
#endif
19

20
    /// <summary>
21
    /// Truncates the specified text.
22
    /// </summary>
23
    /// <param name="text">The text to truncate.</param>
24
    /// <param name="keep">The number of characters to keep.</param>
25
    /// <param name="ellipsis">The ellipsis string to use when truncating. (Default ...)</param>
26
    /// <returns>
27
    /// A truncate string.
28
    /// </returns>
29
    [return: NotNullIfNotNull(nameof(text))]
30
    public static string? Truncate(this string? text, int keep, string ellipsis = "...")
31
    {
32
        if (string.IsNullOrEmpty(text))
×
33
            return text;
×
34

35
        if (text!.Length <= keep)
×
36
            return text;
×
37

38
        ellipsis ??= string.Empty;
×
39

40
        if (text.Length <= keep + ellipsis.Length || keep < ellipsis.Length)
×
41
            return text.Substring(0, keep);
×
42

43
        int prefix = keep - ellipsis.Length;
×
44
        return string.Concat(text.Substring(0, prefix), ellipsis);
×
45
    }
46

47
    /// <summary>
48
    /// Indicates whether the specified String object is null or an empty string
49
    /// </summary>
50
    /// <param name="item">A String reference</param>
51
    /// <returns>
52
    ///     <c>true</c> if is null or empty; otherwise, <c>false</c>.
53
    /// </returns>
54
    public static bool IsNullOrEmpty([NotNullWhen(false)] this string? item)
55
    {
56
        return string.IsNullOrEmpty(item);
291✔
57
    }
58

59
    /// <summary>
60
    /// Indicates whether a specified string is null, empty, or consists only of white-space characters
61
    /// </summary>
62
    /// <param name="item">A String reference</param>
63
    /// <returns>
64
    ///      <c>true</c> if is null or empty; otherwise, <c>false</c>.
65
    /// </returns>
66
    public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? item)
67
    {
68
        if (item == null)
×
69
            return true;
×
70

71
        for (int i = 0; i < item.Length; i++)
×
72
            if (!char.IsWhiteSpace(item[i]))
×
73
                return false;
×
74

75
        return true;
×
76
    }
77

78
    /// <summary>
79
    /// Determines whether the specified string is not <see cref="IsNullOrEmpty"/>.
80
    /// </summary>
81
    /// <param name="value">The value to check.</param>
82
    /// <returns>
83
    ///   <c>true</c> if the specified <paramref name="value"/> is not <see cref="IsNullOrEmpty"/>; otherwise, <c>false</c>.
84
    /// </returns>
85
    public static bool HasValue([NotNullWhen(true)] this string? value)
86
    {
87
        return !string.IsNullOrEmpty(value);
291✔
88
    }
89

90
    /// <summary>
91
    /// Replaces the format item in a specified string with the string representation of a corresponding object in a specified array.
92
    /// </summary>
93
    /// <param name="format">A composite format string</param>
94
    /// <param name="args">An object array that contains zero or more objects to format</param>
95
    /// <returns>A copy of format in which the format items have been replaced by the string representation of the corresponding objects in args</returns>
96
    public static string FormatWith(this string format, params object?[] args)
97
    {
98
        return string.Format(format, args);
×
99
    }
100

101
    /// <summary>
102
    /// Replaces the format item in a specified string with the string representation of a corresponding object in a specified array.
103
    /// </summary>
104
    /// <param name="format">A composite format string</param>
105
    /// <param name="provider">Format provider</param>
106
    /// <param name="args">An object array that contains zero or more objects to format</param>
107
    /// <returns>A copy of format in which the format items have been replaced by the string representation of the corresponding objects in args</returns>
108
    public static string FormatWith(this string format, IFormatProvider? provider, params object?[] args)
109
    {
110
        return string.Format(provider, format, args);
×
111
    }
112

113
    /// <summary>
114
    /// Converts a NameIdentifier and spaces it out into words "Name Identifier".
115
    /// </summary>
116
    /// <param name="text">The text value to convert.</param>
117
    /// <returns>The text converted</returns>
118
    [return: NotNullIfNotNull(nameof(text))]
119
    public static string? ToTitle(this string? text)
120
    {
121
        if (text.IsNullOrEmpty() || text.Length < 2)
291✔
122
            return text;
1✔
123

124
        var words = WordsExpression().Matches(text);
290✔
125

126
        var builder = StringBuilderCache.Acquire();
290✔
127
        foreach (Match word in words)
1,424✔
128
        {
129
            if (builder.Length > 0)
422✔
130
                builder.Append(' ');
133✔
131

132
            builder.Append(word.Value);
422✔
133
        }
134

135
        return StringBuilderCache.ToString(builder);
290✔
136
    }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc