< Summary - Jellyfin

Information
Class: Jellyfin.Extensions.SplitStringExtensions
Assembly: Jellyfin.Extensions
File(s): /srv/git/jellyfin/src/Jellyfin.Extensions/SplitStringExtensions.cs
Line coverage
100%
Covered lines: 2
Uncovered lines: 0
Coverable lines: 2
Total lines: 115
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SpanSplit(...)100%11100%
Split(...)100%11100%

File(s)

/srv/git/jellyfin/src/Jellyfin.Extensions/SplitStringExtensions.cs

#LineLine coverage
 1/*
 2MIT License
 3
 4Copyright (c) 2019 Gérald Barré
 5
 6Permission is hereby granted, free of charge, to any person obtaining a copy
 7of this software and associated documentation files (the "Software"), to deal
 8in the Software without restriction, including without limitation the rights
 9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10copies of the Software, and to permit persons to whom the Software is
 11furnished to do so, subject to the following conditions:
 12
 13The above copyright notice and this permission notice shall be included in all
 14copies or substantial portions of the Software.
 15
 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 22SOFTWARE.
 23 */
 24
 25// TODO: remove when analyzer is fixed: https://github.com/dotnet/roslyn-analyzers/issues/5158
 26#pragma warning disable CA1034 // Nested types should not be visible
 27
 28using System;
 29using System.Diagnostics.Contracts;
 30using System.Runtime.InteropServices;
 31
 32namespace Jellyfin.Extensions
 33{
 34    /// <summary>
 35    /// Extension class for splitting lines without unnecessary allocations.
 36    /// </summary>
 37    public static class SplitStringExtensions
 38    {
 39        /// <summary>
 40        /// Creates a new string split enumerator.
 41        /// </summary>
 42        /// <param name="str">The string to split.</param>
 43        /// <param name="separator">The separator to split on.</param>
 44        /// <returns>The enumerator struct.</returns>
 45        [Pure]
 711246        public static Enumerator SpanSplit(this string str, char separator) => new(str.AsSpan(), separator);
 47
 48        /// <summary>
 49        /// Creates a new span split enumerator.
 50        /// </summary>
 51        /// <param name="str">The span to split.</param>
 52        /// <param name="separator">The separator to split on.</param>
 53        /// <returns>The enumerator struct.</returns>
 54        [Pure]
 752755        public static Enumerator Split(this ReadOnlySpan<char> str, char separator) => new(str, separator);
 56
 57        /// <summary>
 58        /// Provides an enumerator for the substrings separated by the separator.
 59        /// </summary>
 60        [StructLayout(LayoutKind.Auto)]
 61        public ref struct Enumerator
 62        {
 63            private readonly char _separator;
 64            private ReadOnlySpan<char> _str;
 65
 66            /// <summary>
 67            /// Initializes a new instance of the <see cref="Enumerator"/> struct.
 68            /// </summary>
 69            /// <param name="str">The span to split.</param>
 70            /// <param name="separator">The separator to split on.</param>
 71            public Enumerator(ReadOnlySpan<char> str, char separator)
 72            {
 73                _str = str;
 74                _separator = separator;
 75                Current = default;
 76            }
 77
 78            /// <summary>
 79            /// Gets a reference to the item at the current position of the enumerator.
 80            /// </summary>
 81            public ReadOnlySpan<char> Current { get; private set; }
 82
 83            /// <summary>
 84            /// Returns <c>this</c>.
 85            /// </summary>
 86            /// <returns><c>this</c>.</returns>
 87            public readonly Enumerator GetEnumerator() => this;
 88
 89            /// <summary>
 90            /// Advances the enumerator to the next item.
 91            /// </summary>
 92            /// <returns><c>true</c> if there is a next element; otherwise <c>false</c>.</returns>
 93            public bool MoveNext()
 94            {
 95                if (_str.Length == 0)
 96                {
 97                    return false;
 98                }
 99
 100                var span = _str;
 101                var index = span.IndexOf(_separator);
 102                if (index == -1)
 103                {
 104                    _str = ReadOnlySpan<char>.Empty;
 105                    Current = span;
 106                    return true;
 107                }
 108
 109                Current = span.Slice(0, index);
 110                _str = span[(index + 1)..];
 111                return true;
 112            }
 113        }
 114    }
 115}