SplitStringExtensions.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. MIT License
  3. Copyright (c) 2019 Gérald Barré
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #pragma warning disable CS1591
  21. #pragma warning disable CA1034
  22. using System;
  23. using System.Diagnostics.Contracts;
  24. using System.Runtime.InteropServices;
  25. namespace MediaBrowser.Common.Extensions
  26. {
  27. /// <summary>
  28. /// Extension class for splitting lines without unnecessary allocations.
  29. /// </summary>
  30. public static class SplitStringExtensions
  31. {
  32. /// <summary>
  33. /// Creates a new string split enumerator.
  34. /// </summary>
  35. /// <param name="str">The string to split.</param>
  36. /// <param name="separator">The separator to split on.</param>
  37. /// <returns>The enumerator struct.</returns>
  38. [Pure]
  39. public static SplitEnumerator SpanSplit(this string str, char separator) => new (str.AsSpan(), separator);
  40. /// <summary>
  41. /// Creates a new span split enumerator.
  42. /// </summary>
  43. /// <param name="str">The span to split.</param>
  44. /// <param name="separator">The separator to split on.</param>
  45. /// <returns>The enumerator struct.</returns>
  46. [Pure]
  47. public static SplitEnumerator Split(this ReadOnlySpan<char> str, char separator) => new (str, separator);
  48. [StructLayout(LayoutKind.Auto)]
  49. public ref struct SplitEnumerator
  50. {
  51. private readonly char _separator;
  52. private ReadOnlySpan<char> _str;
  53. public SplitEnumerator(ReadOnlySpan<char> str, char separator)
  54. {
  55. _str = str;
  56. _separator = separator;
  57. Current = default;
  58. }
  59. public ReadOnlySpan<char> Current { get; private set; }
  60. public readonly SplitEnumerator GetEnumerator() => this;
  61. public bool MoveNext()
  62. {
  63. if (_str.Length == 0)
  64. {
  65. return false;
  66. }
  67. var span = _str;
  68. var index = span.IndexOf(_separator);
  69. if (index == -1)
  70. {
  71. _str = ReadOnlySpan<char>.Empty;
  72. Current = span;
  73. return true;
  74. }
  75. Current = span.Slice(0, index);
  76. _str = span[(index + 1)..];
  77. return true;
  78. }
  79. }
  80. }
  81. }