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

JaCraig / Archivist / 19457476719

18 Nov 2025 07:15AM UTC coverage: 77.414% (+0.06%) from 77.359%
19457476719

push

github

web-flow
Merge pull request #228 from JaCraig/dependabot/nuget/Archivist.Tests/dependencies-f4b6243359

chore: Bump the dependencies group with 1 update

2415 of 3412 branches covered (70.78%)

Branch coverage included in aggregate %.

3196 of 3836 relevant lines covered (83.32%)

5415.98 hits per line

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

95.28
/Archivist/ExtensionMethods/InternalExtensionMethods.cs
1
using System;
2
using System.Buffers;
3
using System.IO;
4
using System.Text;
5
using System.Threading.Tasks;
6

7
namespace Archivist.ExtensionMethods
8
{
9
    /// <summary>
10
    /// Internal extension methods.
11
    /// </summary>
12
    public static class InternalExtensionMethods
13
    {
14
        /// <summary>
15
        /// Adds spaces before each capital letter in the input string.
16
        /// </summary>
17
        /// <param name="input">Input string</param>
18
        /// <returns>String with spaces before each capital letter</returns>
19
        public static string AddSpaces(this string? input)
20
        {
21
            if (string.IsNullOrWhiteSpace(input))
213✔
22
            {
23
                return "";
3✔
24
            }
25

26
            StringBuilder NewText = new StringBuilder(input.Length * 2).Append(input[0]);
210✔
27
            for (int X = 1, InputLength = input.Length; X < InputLength; ++X)
4,940✔
28
            {
29
                var CurrentChar = input[X];
2,155✔
30
                var PreviousChar = input[X - 1];
2,155✔
31
                if (char.IsUpper(CurrentChar) && !(char.IsWhiteSpace(PreviousChar) || char.IsUpper(PreviousChar)))
2,155✔
32
                {
33
                    _ = NewText.Append(' ');
154✔
34
                }
35
                _ = NewText.Append(CurrentChar);
2,155✔
36
            }
37
            return NewText.ToString();
210✔
38
        }
39

40
        /// <summary>
41
        /// Formats a string using the specified format.
42
        /// </summary>
43
        /// <param name="input">Input string</param>
44
        /// <param name="format">Format to use</param>
45
        /// <returns>The formatted string</returns>
46
        public static string FormatString(this string? input, string format)
47
        {
48
            if (string.IsNullOrEmpty(format) || !InternalStringFormatter.IsValid(format))
49✔
49
            {
50
                throw new ArgumentException("Format is not valid.", nameof(format));
3✔
51
            }
52
            return InternalStringFormatter.Format(input, format);
46✔
53
        }
54

55
        /// <summary>
56
        /// Gets the first x number of characters from the left hand side
57
        /// </summary>
58
        /// <param name="input">Input string</param>
59
        /// <param name="length">x number of characters to return</param>
60
        /// <returns>The resulting string</returns>
61
        public static string Left(this string? input, int length)
62
        {
63
            return length <= 0
104✔
64
                ? ""
104✔
65
                : string.IsNullOrEmpty(input) ? "" : input[..(input.Length > length ? length : input.Length)];
104✔
66
        }
67

68
        /// <summary>
69
        /// Takes all of the data in the stream and returns it as a string
70
        /// </summary>
71
        /// <param name="input">Input stream</param>
72
        /// <param name="encodingUsing">The encoding to return as</param>
73
        /// <returns>The resulting string</returns>
74
        public static string ReadAll(this Stream? input, Encoding? encodingUsing = null)
75
        {
76
            if (input is null)
11✔
77
                return "";
2✔
78
            return input.ReadAllBinary().ToString(encodingUsing) ?? "";
9!
79
        }
80

81
        /// <summary>
82
        /// Takes all of the data in the stream and returns it as a string
83
        /// </summary>
84
        /// <param name="input">Input stream</param>
85
        /// <param name="encodingUsing">Encoding that the string should be in (defaults to UTF8)</param>
86
        /// <returns>A string containing the content of the stream</returns>
87
        public static async Task<string> ReadAllAsync(this Stream? input, Encoding? encodingUsing = null)
88
        {
89
            if (input is null)
33✔
90
                return "";
2✔
91
            return (await input.ReadAllBinaryAsync().ConfigureAwait(false)).ToString(encodingUsing) ?? "";
31!
92
        }
33✔
93

94
        /// <summary>
95
        /// Takes all of the data in the stream and returns it as an array of bytes
96
        /// </summary>
97
        /// <param name="input">Input stream</param>
98
        /// <returns>A byte array</returns>
99
        public static byte[] ReadAllBinary(this Stream? input)
100
        {
101
            if (input is null)
14✔
102
                return Array.Empty<byte>();
2✔
103

104
            if (input is MemoryStream TempInput)
12✔
105
                return TempInput.ToArray();
6✔
106

107
            ArrayPool<byte> Pool = ArrayPool<byte>.Shared;
6✔
108
            var Buffer = Pool.Rent(4096);
6✔
109
            using var Temp = new MemoryStream();
6✔
110
            while (true)
111
            {
112
                try
113
                {
114
                    var Count = input.Read(Buffer);
113✔
115
                    if (Count <= 0)
113✔
116
                        break;
6✔
117
                    Temp.Write(Buffer, 0, Count);
107✔
118
                }
107✔
119
                catch
×
120
                {
121
                    break;
×
122
                }
123
            }
124
            Pool.Return(Buffer);
6✔
125
            return Temp.ToArray();
6✔
126
        }
6✔
127

128
        /// <summary>
129
        /// Takes all of the data in the stream and returns it as an array of bytes
130
        /// </summary>
131
        /// <param name="input">Input stream</param>
132
        /// <returns>A byte array</returns>
133
        public static async Task<byte[]> ReadAllBinaryAsync(this Stream? input)
134
        {
135
            if (input is null)
34✔
136
                return Array.Empty<byte>();
2✔
137

138
            if (input is MemoryStream TempInput)
32✔
139
                return TempInput.ToArray();
19✔
140

141
            ArrayPool<byte> Pool = ArrayPool<byte>.Shared;
13✔
142
            var Buffer = Pool.Rent(4096);
13✔
143
            await using var Temp = new MemoryStream();
13✔
144
            while (true)
145
            {
146
                try
147
                {
148
                    var Count = await input.ReadAsync(Buffer.AsMemory(0, Buffer.Length)).ConfigureAwait(false);
59✔
149
                    if (Count <= 0)
59✔
150
                        break;
13✔
151
                    Temp.Write(Buffer, 0, Count);
46✔
152
                }
46✔
153
                catch
×
154
                {
155
                    break;
×
156
                }
157
            }
158
            Pool.Return(Buffer);
13✔
159
            return Temp.ToArray();
13✔
160
        }
34✔
161

162
        /// <summary>
163
        /// Strips illegal characters from RSS items
164
        /// </summary>
165
        /// <param name="original">Original text</param>
166
        /// <returns>string stripped of certain characters.</returns>
167
        public static string StripIllegalCharacters(this string? original)
168
        {
169
            return original?.Replace("&nbsp;", " ")
15,097✔
170
                .Replace("&#160;", string.Empty)
15,097✔
171
                .Trim()
15,097✔
172
                .Replace("&", "and") ?? "";
15,097✔
173
        }
174

175
        /// <summary>
176
        /// Converts a string to a byte array using the specified encoding.
177
        /// </summary>
178
        /// <param name="input">The input string</param>
179
        /// <param name="encodingUsing">The encoding to use (defaults to UTF8)</param>
180
        /// <returns>The resulting byte array</returns>
181
        public static byte[] ToByteArray(this string? input, Encoding? encodingUsing = null)
182
        {
183
            if (string.IsNullOrEmpty(input))
9✔
184
                return Array.Empty<byte>();
1✔
185

186
            encodingUsing ??= Encoding.UTF8;
8✔
187
            return encodingUsing.GetBytes(input);
8✔
188
        }
189

190
        /// <summary>
191
        /// Converts a byte array to a string
192
        /// </summary>
193
        /// <param name="input">Input byte array</param>
194
        /// <param name="encodingUsing">Encoding that the string should be in (defaults to UTF8)</param>
195
        /// <param name="index">Index to start at</param>
196
        /// <param name="count">Number of bytes to convert</param>
197
        /// <returns>A string containing the content of the byte array</returns>
198
        public static string ToString(this byte[]? input, Encoding? encodingUsing, int index = 0, int count = -1)
199
        {
200
            if (input is null)
43✔
201
                return "";
2✔
202

203
            if (count == -1)
41✔
204
                count = input.Length - index;
41✔
205

206
            encodingUsing ??= Encoding.UTF8;
41✔
207
            return encodingUsing.GetString(input, index, count);
41✔
208
        }
209
    }
210
}
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

© 2025 Coveralls, Inc