// <copyright file="SplitStringExtensions.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

/*
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.
 */

// TODO: remove when analyzer is fixed: https://github.com/dotnet/roslyn-analyzers/issues/5158
#pragma warning disable CA1034 // Nested types should not be visible
#pragma warning disable SA1309 // Variables should not begin with underscore

namespace Jellyfin.Extensions
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Runtime.InteropServices;

    /// <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 Enumerator 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 Enumerator Split(this ReadOnlySpan<char> str, char separator) => new(str, separator);

        /// <summary>
        /// Provides an enumerator for the substrings separated by the separator.
        /// </summary>
        [StructLayout(LayoutKind.Auto)]
        public ref struct Enumerator
        {
            private readonly char _separator;
            private ReadOnlySpan<char> _str;

            /// <summary>
            /// Initializes a new instance of the <see cref="Enumerator"/> struct.
            /// </summary>
            /// <param name="str">The span to split.</param>
            /// <param name="separator">The separator to split on.</param>
            public Enumerator(ReadOnlySpan<char> str, char separator)
            {
                this._str = str;
                this._separator = separator;
                this.Current = default;
            }

            /// <summary>
            /// Gets a reference to the item at the current position of the enumerator.
            /// </summary>
            public ReadOnlySpan<char> Current { get; private set; }

            /// <summary>
            /// Returns <c>this</c>.
            /// </summary>
            /// <returns><c>this</c>.</returns>
            public readonly Enumerator GetEnumerator() => this;

            /// <summary>
            /// Advances the enumerator to the next item.
            /// </summary>
            /// <returns><c>true</c> if there is a next element; otherwise <c>false</c>.</returns>
            public bool MoveNext()
            {
                if (this._str.Length == 0)
                {
                    return false;
                }

                var span = this._str;
                var index = span.IndexOf(this._separator);
                if (index == -1)
                {
                    this._str = ReadOnlySpan<char>.Empty;
                    this.Current = span;
                    return true;
                }

                this.Current = span.Slice(0, index);
                this._str = span[(index + 1)..];
                return true;
            }
        }
    }
}
