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

Jericho / Picton / 259

03 Feb 2025 08:26PM UTC coverage: 48.469% (+2.5%) from 45.968%
259

push

appveyor

Jericho
Add multiple messages to a queue as efficiently as possible

190 of 392 relevant lines covered (48.47%)

5002.64 hits per line

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

86.67
/Source/Picton/Extensions/StringExtensions.cs
1
using System;
2
using System.Linq;
3
using System.Security.Cryptography;
4
using System.Text;
5

6
namespace Picton
7
{
8
        /// <summary>
9
        /// Contains extension methods for the <see cref="string"/> data type.
10
        /// </summary>
11
        public static class StringExtensions
12
        {
13
                #region PUBLIC EXTENSION METHODS
14

15
                /// <summary>
16
                /// Trim all occurrences of the exactly matching substring at the start of a given string.
17
                /// </summary>
18
                /// <remarks>
19
                /// From: http://stackoverflow.com/questions/4335878/c-sharp-trimstart-with-string-parameter .
20
                /// </remarks>
21
                /// <param name="target">The string to be trimmed.</param>
22
                /// <param name="trimString">The substring to be removed.</param>
23
                /// <param name="comparisonType">Comparison settings.</param>
24
                /// <returns>The trimmed string.</returns>
25
                public static string TrimStart(this string target, string trimString, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
26
                {
27
                        if (target == null) throw new ArgumentNullException(nameof(target));
10✔
28
                        if (trimString == null) throw new ArgumentNullException(nameof(trimString));
11✔
29

30
                        int startIndex = 0;
9✔
31
                        while (target.IndexOf(trimString, startIndex, comparisonType) == startIndex)
13✔
32
                        {
33
                                startIndex += trimString.Length;
4✔
34
                        }
35

36
                        return target.Substring(startIndex);
9✔
37
                }
38

39
                /// <summary>
40
                /// Trim all occurrences of the exactly matching substring at the end of a given string.
41
                /// </summary>
42
                /// <remarks>
43
                /// From: http://stackoverflow.com/questions/4335878/c-sharp-trimstart-with-string-parameter .
44
                /// </remarks>
45
                /// <param name="target">The string to be trimmed.</param>
46
                /// <param name="trimString">The substring to be removed.</param>
47
                /// <param name="comparisonType">Comparison settings.</param>
48
                /// <returns>The trimmed string.</returns>
49
                public static string TrimEnd(this string target, string trimString, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
50
                {
51
                        if (target == null) throw new ArgumentNullException(nameof(target));
4✔
52
                        if (trimString == null) throw new ArgumentNullException(nameof(trimString));
5✔
53

54
                        int sourceLength = target.Length;
3✔
55
                        int count = sourceLength;
3✔
56
                        while (target.LastIndexOf(trimString, count - 1, comparisonType) == count - trimString.Length)
7✔
57
                        {
58
                                count -= trimString.Length;
4✔
59
                        }
60

61
                        return target.Substring(0, count);
3✔
62
                }
63

64
                /// <summary>
65
                /// Converts the string to a byte-array using the supplied encoding.
66
                /// </summary>
67
                /// <param name="value">The input string.</param>
68
                /// <param name="encoding">The encoding to be used.</param>
69
                /// <returns>The created byte array.</returns>
70
                /// <example><code>
71
                /// var value = "Hello World";
72
                /// var ansiBytes = value.ToBytes(Encoding.GetEncoding(1252)); // 1252 = ANSI
73
                /// var utf8Bytes = value.ToBytes(Encoding.UTF8);
74
                /// </code></example>
75
                /// <remarks>From the .NET Extensions project: http://dnpextensions.codeplex.com/ .</remarks>
76
                public static byte[] ToBytes(this string value, Encoding encoding = null)
77
                {
78
                        if (value == null) throw new ArgumentNullException(nameof(value));
86✔
79

80
                        return (encoding ?? Encoding.UTF8).GetBytes(value);
84✔
81
                }
82

83
                /// <summary>
84
                /// Calculates the MD5 hash for a given string.
85
                /// </summary>
86
                /// <param name="value">The string.</param>
87
                /// <returns>The hash.</returns>
88
                public static byte[] ToMD5Hash(this string value)
89
                {
90
                        using (var md5 = MD5.Create())
26✔
91
                        {
92
                                // Convert the input string to a byte array.
93
                                byte[] data = value.ToBytes(Encoding.UTF8);
26✔
94

95
                                // Compute the hash.
96
                                byte[] hash = md5.ComputeHash(data);
26✔
97

98
                                // Return the byte array.
99
                                return hash;
26✔
100
                        }
101
                }
26✔
102

103
                /// <summary>
104
                /// Calculates the MD5 hash for a given string.
105
                /// </summary>
106
                /// <param name="value">The string.</param>
107
                /// <returns>The hash.</returns>
108
                public static string ToMD5HashString(this string value)
109
                {
110
                        // Calculate the hash.
111
                        var hash = value.ToMD5Hash();
×
112

113
                        // Create a new Stringbuilder to collect the bytes and create a string.
114
                        var sb = new StringBuilder();
×
115

116
                        // Loop through each byte of the hashed data and format each one as a hexadecimal string.
117
                        for (int i = 0; i < hash.Length; i++)
×
118
                        {
119
                                sb.Append(hash[i].ToString("x2"));
×
120
                        }
121

122
                        // Return the hexadecimal string.
123
                        return sb.ToString();
×
124
                }
125

126
                /// <summary>
127
                /// Determines if a given string is base64 encoded.
128
                /// </summary>
129
                /// <param name="value">The string.</param>
130
                /// <returns>true if the string contains only valid base64 characters, false otherwise.</returns>
131
                public static bool IsBase64Encoded(this string value)
132
                {
133
                        if (value == null ||
10✔
134
                                value.Length == 0 ||
10✔
135
                                value.Length % 4 != 0 ||
10✔
136
                                value.Contains(' ') ||
10✔
137
                                value.Contains('\t') ||
10✔
138
                                value.Contains('\r') ||
10✔
139
                                value.Contains('\n'))
10✔
140
                                return false;
1✔
141

142
                        var index = value.Length - 1;
9✔
143
                        if (value[index] == '=') index--;
16✔
144

145
                        if (value[index] == '=') index--;
9✔
146

147
                        for (var i = 0; i <= index; i++)
354,532✔
148
                        {
149
                                if (!IsValidBase64Char(value[i])) return false;
177,257✔
150
                        }
151

152
                        return true;
9✔
153
                }
154

155
                private static bool IsValidBase64Char(char value)
156
                {
157
                        var intValue = (int)value;
177,257✔
158
                        if (intValue >= 48 && intValue <= 57) return true; // 1 - 9
204,189✔
159
                        if (intValue >= 65 && intValue <= 90) return true; // A - Z
219,359✔
160
                        if (intValue >= 97 && intValue <= 122) return true; // a - z
161,897✔
161
                        if (intValue == 43 || intValue == 47) return true; // + or /
1,370✔
162
                        return false;
×
163
                }
164

165
                #endregion
166
        }
167
}
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