aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Extensions
diff options
context:
space:
mode:
authorBond_009 <bond.009@outlook.com>2021-06-19 18:02:33 +0200
committerBond_009 <bond.009@outlook.com>2021-06-19 18:04:46 +0200
commit6f8ccab788e85e025eaa44b67a1487bf419afb53 (patch)
treef8895fae8ec17e922daab473180c9de9a702a02a /MediaBrowser.Common/Extensions
parent0c3dcdf77b0d124517bffa608bfddf7d8f7682db (diff)
Move non-jellyfin extensions to separate project
Diffstat (limited to 'MediaBrowser.Common/Extensions')
-rw-r--r--MediaBrowser.Common/Extensions/CopyToExtensions.cs26
-rw-r--r--MediaBrowser.Common/Extensions/EnumerableExtensions.cs51
-rw-r--r--MediaBrowser.Common/Extensions/ShuffleExtensions.cs41
-rw-r--r--MediaBrowser.Common/Extensions/SplitStringExtensions.cs95
-rw-r--r--MediaBrowser.Common/Extensions/StreamExtensions.cs63
-rw-r--r--MediaBrowser.Common/Extensions/StringBuilderExtensions.cs35
6 files changed, 0 insertions, 311 deletions
diff --git a/MediaBrowser.Common/Extensions/CopyToExtensions.cs b/MediaBrowser.Common/Extensions/CopyToExtensions.cs
deleted file mode 100644
index 2ecbc6539..000000000
--- a/MediaBrowser.Common/Extensions/CopyToExtensions.cs
+++ /dev/null
@@ -1,26 +0,0 @@
-using System.Collections.Generic;
-
-namespace MediaBrowser.Common.Extensions
-{
- /// <summary>
- /// Provides <c>CopyTo</c> extensions methods for <see cref="IReadOnlyList{T}" />.
- /// </summary>
- public static class CopyToExtensions
- {
- /// <summary>
- /// Copies all the elements of the current collection to the specified list
- /// starting at the specified destination array index. The index is specified as a 32-bit integer.
- /// </summary>
- /// <param name="source">The current collection that is the source of the elements.</param>
- /// <param name="destination">The list that is the destination of the elements copied from the current collection.</param>
- /// <param name="index">A 32-bit integer that represents the index in <c>destination</c> at which copying begins.</param>
- /// <typeparam name="T">The type of the array.</typeparam>
- public static void CopyTo<T>(this IReadOnlyList<T> source, IList<T> destination, int index = 0)
- {
- for (int i = 0; i < source.Count; i++)
- {
- destination[index + i] = source[i];
- }
- }
- }
-}
diff --git a/MediaBrowser.Common/Extensions/EnumerableExtensions.cs b/MediaBrowser.Common/Extensions/EnumerableExtensions.cs
deleted file mode 100644
index 2b8a6c395..000000000
--- a/MediaBrowser.Common/Extensions/EnumerableExtensions.cs
+++ /dev/null
@@ -1,51 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace MediaBrowser.Common.Extensions
-{
- /// <summary>
- /// Static extensions for the <see cref="IEnumerable{T}"/> interface.
- /// </summary>
- public static class EnumerableExtensions
- {
- /// <summary>
- /// Determines whether the value is contained in the source collection.
- /// </summary>
- /// <param name="source">An instance of the <see cref="IEnumerable{String}"/> interface.</param>
- /// <param name="value">The value to look for in the collection.</param>
- /// <param name="stringComparison">The string comparison.</param>
- /// <returns>A value indicating whether the value is contained in the collection.</returns>
- /// <exception cref="ArgumentNullException">The source is null.</exception>
- public static bool Contains(this IEnumerable<string> source, ReadOnlySpan<char> value, StringComparison stringComparison)
- {
- if (source == null)
- {
- throw new ArgumentNullException(nameof(source));
- }
-
- if (source is IList<string> list)
- {
- int len = list.Count;
- for (int i = 0; i < len; i++)
- {
- if (value.Equals(list[i], stringComparison))
- {
- return true;
- }
- }
-
- return false;
- }
-
- foreach (string element in source)
- {
- if (value.Equals(element, stringComparison))
- {
- return true;
- }
- }
-
- return false;
- }
- }
-}
diff --git a/MediaBrowser.Common/Extensions/ShuffleExtensions.cs b/MediaBrowser.Common/Extensions/ShuffleExtensions.cs
deleted file mode 100644
index 2604abf85..000000000
--- a/MediaBrowser.Common/Extensions/ShuffleExtensions.cs
+++ /dev/null
@@ -1,41 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace MediaBrowser.Common.Extensions
-{
- /// <summary>
- /// Provides <c>Shuffle</c> extensions methods for <see cref="IList{T}" />.
- /// </summary>
- public static class ShuffleExtensions
- {
- private static readonly Random _rng = new Random();
-
- /// <summary>
- /// Shuffles the items in a list.
- /// </summary>
- /// <param name="list">The list that should get shuffled.</param>
- /// <typeparam name="T">The type.</typeparam>
- public static void Shuffle<T>(this IList<T> list)
- {
- list.Shuffle(_rng);
- }
-
- /// <summary>
- /// Shuffles the items in a list.
- /// </summary>
- /// <param name="list">The list that should get shuffled.</param>
- /// <param name="rng">The random number generator to use.</param>
- /// <typeparam name="T">The type.</typeparam>
- public static void Shuffle<T>(this IList<T> list, Random rng)
- {
- int n = list.Count;
- while (n > 1)
- {
- int k = rng.Next(n--);
- T value = list[k];
- list[k] = list[n];
- list[n] = value;
- }
- }
- }
-}
diff --git a/MediaBrowser.Common/Extensions/SplitStringExtensions.cs b/MediaBrowser.Common/Extensions/SplitStringExtensions.cs
deleted file mode 100644
index 9c9108495..000000000
--- a/MediaBrowser.Common/Extensions/SplitStringExtensions.cs
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
-MIT License
-
-Copyright (c) 2019 Gérald Barré
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
- */
-
-#pragma warning disable CS1591
-#pragma warning disable CA1034
-using System;
-using System.Diagnostics.Contracts;
-using System.Runtime.InteropServices;
-
-namespace MediaBrowser.Common.Extensions
-{
- /// <summary>
- /// Extension class for splitting lines without unnecessary allocations.
- /// </summary>
- public static class SplitStringExtensions
- {
- /// <summary>
- /// Creates a new string split enumerator.
- /// </summary>
- /// <param name="str">The string to split.</param>
- /// <param name="separator">The separator to split on.</param>
- /// <returns>The enumerator struct.</returns>
- [Pure]
- public static SplitEnumerator SpanSplit(this string str, char separator) => new (str.AsSpan(), separator);
-
- /// <summary>
- /// Creates a new span split enumerator.
- /// </summary>
- /// <param name="str">The span to split.</param>
- /// <param name="separator">The separator to split on.</param>
- /// <returns>The enumerator struct.</returns>
- [Pure]
- public static SplitEnumerator Split(this ReadOnlySpan<char> str, char separator) => new (str, separator);
-
- [StructLayout(LayoutKind.Auto)]
- public ref struct SplitEnumerator
- {
- private readonly char _separator;
- private ReadOnlySpan<char> _str;
-
- public SplitEnumerator(ReadOnlySpan<char> str, char separator)
- {
- _str = str;
- _separator = separator;
- Current = default;
- }
-
- public ReadOnlySpan<char> Current { get; private set; }
-
- public readonly SplitEnumerator GetEnumerator() => this;
-
- public bool MoveNext()
- {
- if (_str.Length == 0)
- {
- return false;
- }
-
- var span = _str;
- var index = span.IndexOf(_separator);
- if (index == -1)
- {
- _str = ReadOnlySpan<char>.Empty;
- Current = span;
- return true;
- }
-
- Current = span.Slice(0, index);
- _str = span[(index + 1)..];
- return true;
- }
- }
- }
-}
diff --git a/MediaBrowser.Common/Extensions/StreamExtensions.cs b/MediaBrowser.Common/Extensions/StreamExtensions.cs
deleted file mode 100644
index 5cbf57d98..000000000
--- a/MediaBrowser.Common/Extensions/StreamExtensions.cs
+++ /dev/null
@@ -1,63 +0,0 @@
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Text;
-
-namespace MediaBrowser.Common.Extensions
-{
- /// <summary>
- /// Class BaseExtensions.
- /// </summary>
- public static class StreamExtensions
- {
- /// <summary>
- /// Reads all lines in the <see cref="Stream" />.
- /// </summary>
- /// <param name="stream">The <see cref="Stream" /> to read from.</param>
- /// <returns>All lines in the stream.</returns>
- public static string[] ReadAllLines(this Stream stream)
- => ReadAllLines(stream, Encoding.UTF8);
-
- /// <summary>
- /// Reads all lines in the <see cref="Stream" />.
- /// </summary>
- /// <param name="stream">The <see cref="Stream" /> to read from.</param>
- /// <param name="encoding">The character encoding to use.</param>
- /// <returns>All lines in the stream.</returns>
- public static string[] ReadAllLines(this Stream stream, Encoding encoding)
- {
- using (StreamReader reader = new StreamReader(stream, encoding))
- {
- return ReadAllLines(reader).ToArray();
- }
- }
-
- /// <summary>
- /// Reads all lines in the <see cref="TextReader" />.
- /// </summary>
- /// <param name="reader">The <see cref="TextReader" /> to read from.</param>
- /// <returns>All lines in the stream.</returns>
- public static IEnumerable<string> ReadAllLines(this TextReader reader)
- {
- string? line;
- while ((line = reader.ReadLine()) != null)
- {
- yield return line;
- }
- }
-
- /// <summary>
- /// Reads all lines in the <see cref="TextReader" />.
- /// </summary>
- /// <param name="reader">The <see cref="TextReader" /> to read from.</param>
- /// <returns>All lines in the stream.</returns>
- public static async IAsyncEnumerable<string> ReadAllLinesAsync(this TextReader reader)
- {
- string? line;
- while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
- {
- yield return line;
- }
- }
- }
-}
diff --git a/MediaBrowser.Common/Extensions/StringBuilderExtensions.cs b/MediaBrowser.Common/Extensions/StringBuilderExtensions.cs
deleted file mode 100644
index 75d654f23..000000000
--- a/MediaBrowser.Common/Extensions/StringBuilderExtensions.cs
+++ /dev/null
@@ -1,35 +0,0 @@
-using System.Collections.Generic;
-using System.Text;
-
-namespace MediaBrowser.Common.Extensions
-{
- /// <summary>
- /// Extension methods for the <see cref="StringBuilder"/> class.
- /// </summary>
- public static class StringBuilderExtensions
- {
- /// <summary>
- /// Concatenates and appends the members of a collection in single quotes using the specified delimiter.
- /// </summary>
- /// <param name="builder">The string builder.</param>
- /// <param name="delimiter">The character delimiter.</param>
- /// <param name="values">The collection of strings to concatenate.</param>
- /// <returns>The updated string builder.</returns>
- public static StringBuilder AppendJoinInSingleQuotes(this StringBuilder builder, char delimiter, IReadOnlyList<string> values)
- {
- var len = values.Count;
- for (var i = 0; i < len; i++)
- {
- builder.Append('\'')
- .Append(values[i])
- .Append('\'')
- .Append(delimiter);
- }
-
- // remove last ,
- builder.Length--;
-
- return builder;
- }
- }
-}