|
| 1 | +/* |
| 2 | + * Copyright © 2018 Jesse Nicholson |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Text; |
| 11 | + |
| 12 | +namespace DistillNET.Extensions |
| 13 | +{ |
| 14 | + internal static class MemoryExtensions |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Determines if this ReadOnlyMemory begins with the same character sequence as the supplied string. |
| 18 | + /// </summary> |
| 19 | + /// <param name="str"> |
| 20 | + /// This string. |
| 21 | + /// </param> |
| 22 | + /// <param name="other"> |
| 23 | + /// The ReadOnlyMemory to compare the beginning of this |
| 24 | + /// </param> |
| 25 | + /// <returns> |
| 26 | + /// </returns> |
| 27 | + /// <remarks> |
| 28 | + /// This method is used rather than the built in .net equivalent for the sake of speed. The |
| 29 | + /// built in method invokes various internationalization methods inside the .net framework |
| 30 | + /// that simply are not necessary for our purposes. |
| 31 | + /// </remarks> |
| 32 | + public static bool StartsWithQuick(this ReadOnlyMemory<char> str, ReadOnlyMemory<char> other) |
| 33 | + { |
| 34 | + return str.Span.StartsWithQuick(other.Span); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Gets the next position of a character that would indicate the end of an address or domain |
| 39 | + /// anchor string, if any. |
| 40 | + /// </summary> |
| 41 | + /// <param name="str"> |
| 42 | + /// This string. |
| 43 | + /// </param> |
| 44 | + /// <param name="startIndex"> |
| 45 | + /// The index to begin searching at. |
| 46 | + /// </param> |
| 47 | + /// <returns> |
| 48 | + /// A positive value indicating the index of a matched character in the event that one is |
| 49 | + /// found, a negative number in the event that no such character is found. |
| 50 | + /// </returns> |
| 51 | + public static int IndexOfAnchorEnd(this ReadOnlyMemory<char> str, int startIndex = 0) |
| 52 | + { |
| 53 | + return str.Span.IndexOfAnchorEnd(startIndex); |
| 54 | + } |
| 55 | + |
| 56 | + public static int IndexOfQuick(this ReadOnlyMemory<char> str, char what, int startIndex = 0) |
| 57 | + { |
| 58 | + return str.Span.IndexOfQuick(what, startIndex); |
| 59 | + } |
| 60 | + |
| 61 | + public static int IndexOfQuickICase(this ReadOnlyMemory<char> str, char what, int startIndex = 0) |
| 62 | + { |
| 63 | + return str.Span.IndexOfQuickICase(what, startIndex); |
| 64 | + } |
| 65 | + |
| 66 | + public static int IndexOfQuick(this ReadOnlyMemory<char> str, ReadOnlyMemory<char> what, int startIndex = 0) |
| 67 | + { |
| 68 | + return str.Span.IndexOfQuick(what.Span, startIndex); |
| 69 | + } |
| 70 | + |
| 71 | + public static int IndexOfQuickICase(this ReadOnlyMemory<char> str, ReadOnlyMemory<char> what, int startIndex = 0) |
| 72 | + { |
| 73 | + return str.Span.IndexOfQuickICase(what.Span, startIndex); |
| 74 | + } |
| 75 | + |
| 76 | + public static bool EqualsAt(this ReadOnlyMemory<char> str, ReadOnlyMemory<char> what, int index) |
| 77 | + { |
| 78 | + return str.Span.EqualsAt(what.Span, index); |
| 79 | + } |
| 80 | + |
| 81 | + public static bool EqualsAtICase(this ReadOnlyMemory<char> str, ReadOnlyMemory<char> what, int index) |
| 82 | + { |
| 83 | + return str.Span.EqualsAtICase(what.Span, index); |
| 84 | + } |
| 85 | + |
| 86 | + public static ReadOnlyMemory<char> TrimQuick(this ReadOnlyMemory<char> str) |
| 87 | + { |
| 88 | + bool trimming = true; |
| 89 | + while (trimming && str.Length > 0) |
| 90 | + { |
| 91 | + if (char.IsWhiteSpace(str.Span[0])) |
| 92 | + { |
| 93 | + str = str.Slice(1); |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + if (char.IsWhiteSpace(str.Span[str.Length - 1])) |
| 98 | + { |
| 99 | + str = str.Slice(0, str.Length - 1); |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + trimming = false; |
| 104 | + } |
| 105 | + return str; |
| 106 | + } |
| 107 | + |
| 108 | + public static int LastIndexOfQuick(this ReadOnlyMemory<char> str, ReadOnlyMemory<char> what) |
| 109 | + { |
| 110 | + return str.Span.LastIndexOfQuick(what.Span); |
| 111 | + } |
| 112 | + |
| 113 | + public static int LastIndexOfQuickICase(this ReadOnlyMemory<char> str, ReadOnlyMemory<char> what) |
| 114 | + { |
| 115 | + return str.Span.LastIndexOfQuickICase(what.Span); |
| 116 | + } |
| 117 | + |
| 118 | + public static List<ReadOnlyMemory<char>> Split(this ReadOnlyMemory<char> str, ReadOnlyMemory<char> what) |
| 119 | + { |
| 120 | + var retVal = new List<ReadOnlyMemory<char>>(); |
| 121 | + |
| 122 | + var firstIndex = str.IndexOfQuick(what, 0); |
| 123 | + |
| 124 | + if (firstIndex > -1) |
| 125 | + { |
| 126 | + do |
| 127 | + { |
| 128 | + retVal.Add(str.Slice(0, firstIndex)); |
| 129 | + str = str.Slice(firstIndex + what.Length); |
| 130 | + firstIndex = str.IndexOfQuick(what, 0); |
| 131 | + } while (firstIndex > -1); |
| 132 | + |
| 133 | + if (str.Length > 0) |
| 134 | + { |
| 135 | + retVal.Add(str); |
| 136 | + } |
| 137 | + } |
| 138 | + else |
| 139 | + { |
| 140 | + retVal.Add(str); |
| 141 | + } |
| 142 | + |
| 143 | + return retVal; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments