| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | // Copied from https://github.com/aspnet/Extensions/tree/master/src/Primitives/src |
| | 5 | | using System; |
| | 6 | | using System.Runtime.CompilerServices; |
| | 7 | |
|
| | 8 | | #pragma warning disable CA1307 // Equals Locale |
| | 9 | | #pragma warning disable IDE0041 // Null check can be simplified |
| | 10 | |
|
| | 11 | | namespace Azure.Core.Http.Multipart |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// An optimized representation of a substring. |
| | 15 | | /// </summary> |
| | 16 | | internal readonly struct StringSegment : IEquatable<StringSegment>, IEquatable<string> |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// A <see cref="StringSegment"/> for <see cref="string.Empty"/>. |
| | 20 | | /// </summary> |
| 0 | 21 | | public static readonly StringSegment Empty = string.Empty; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Initializes an instance of the <see cref="StringSegment"/> struct. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="buffer"> |
| | 27 | | /// The original <see cref="string"/>. The <see cref="StringSegment"/> includes the whole <see cref="string"/>. |
| | 28 | | /// </param> |
| | 29 | | public StringSegment(string buffer) |
| | 30 | | { |
| 0 | 31 | | Buffer = buffer; |
| 0 | 32 | | Offset = 0; |
| 0 | 33 | | Length = buffer?.Length ?? 0; |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Initializes an instance of the <see cref="StringSegment"/> struct. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="buffer">The original <see cref="string"/> used as buffer.</param> |
| | 40 | | /// <param name="offset">The offset of the segment within the <paramref name="buffer"/>.</param> |
| | 41 | | /// <param name="length">The length of the segment.</param> |
| | 42 | | /// <exception cref="ArgumentNullException"> |
| | 43 | | /// <paramref name="buffer"/> is <code>null</code>. |
| | 44 | | /// </exception> |
| | 45 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 46 | | /// <paramref name="offset"/> or <paramref name="length"/> is less than zero, or <paramref name="offset"/> + |
| | 47 | | /// <paramref name="length"/> is greater than the number of characters in <paramref name="buffer"/>. |
| | 48 | | /// </exception> |
| | 49 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 50 | | public StringSegment(string buffer, int offset, int length) |
| | 51 | | { |
| | 52 | | // Validate arguments, check is minimal instructions with reduced branching for inlinable fast-path |
| | 53 | | // Negative values discovered though conversion to high values when converted to unsigned |
| | 54 | | // Failure should be rare and location determination and message is delegated to failure functions |
| 0 | 55 | | if (buffer == null || (uint)offset > (uint)buffer.Length || (uint)length > (uint)(buffer.Length - offset)) |
| | 56 | | { |
| 0 | 57 | | ThrowInvalidArguments(buffer, offset, length); |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | Buffer = buffer; |
| 0 | 61 | | Offset = offset; |
| 0 | 62 | | Length = length; |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Gets the <see cref="string"/> buffer for this <see cref="StringSegment"/>. |
| | 67 | | /// </summary> |
| 0 | 68 | | public string Buffer { get; } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Gets the offset within the buffer for this <see cref="StringSegment"/>. |
| | 72 | | /// </summary> |
| 0 | 73 | | public int Offset { get; } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Gets the length of this <see cref="StringSegment"/>. |
| | 77 | | /// </summary> |
| 0 | 78 | | public int Length { get; } |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Gets the value of this segment as a <see cref="string"/>. |
| | 82 | | /// </summary> |
| | 83 | | public string Value |
| | 84 | | { |
| | 85 | | get |
| | 86 | | { |
| 0 | 87 | | if (HasValue) |
| | 88 | | { |
| 0 | 89 | | return Buffer.Substring(Offset, Length); |
| | 90 | | } |
| | 91 | | else |
| | 92 | | { |
| 0 | 93 | | return null; |
| | 94 | | } |
| | 95 | | } |
| | 96 | | } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Gets whether this <see cref="StringSegment"/> contains a valid value. |
| | 100 | | /// </summary> |
| | 101 | | public bool HasValue |
| | 102 | | { |
| 0 | 103 | | get { return Buffer != null; } |
| | 104 | | } |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// Gets the <see cref="char"/> at a specified position in the current <see cref="StringSegment"/>. |
| | 108 | | /// </summary> |
| | 109 | | /// <param name="index">The offset into the <see cref="StringSegment"/></param> |
| | 110 | | /// <returns>The <see cref="char"/> at a specified position.</returns> |
| | 111 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 112 | | /// <paramref name="index"/> is greater than or equal to <see cref="Length"/> or less than zero. |
| | 113 | | /// </exception> |
| | 114 | | public char this[int index] |
| | 115 | | { |
| | 116 | | get |
| | 117 | | { |
| 0 | 118 | | if ((uint)index >= (uint)Length) |
| | 119 | | { |
| 0 | 120 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index); |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | return Buffer[Offset + index]; |
| | 124 | | } |
| | 125 | | } |
| | 126 | |
|
| | 127 | | /// <summary> |
| | 128 | | /// Gets a <see cref="ReadOnlySpan{T}"/> from the current <see cref="StringSegment"/>. |
| | 129 | | /// </summary> |
| | 130 | | /// <returns>The <see cref="ReadOnlySpan{T}"/> from this <see cref="StringSegment"/>.</returns> |
| 0 | 131 | | public ReadOnlySpan<char> AsSpan() => Buffer.AsSpan(Offset, Length); |
| | 132 | |
|
| | 133 | | /// <summary> |
| | 134 | | /// Gets a <see cref="ReadOnlyMemory{T}"/> from the current <see cref="StringSegment"/>. |
| | 135 | | /// </summary> |
| | 136 | | /// <returns>The <see cref="ReadOnlyMemory{T}"/> from this <see cref="StringSegment"/>.</returns> |
| 0 | 137 | | public ReadOnlyMemory<char> AsMemory() => Buffer.AsMemory(Offset, Length); |
| | 138 | |
|
| | 139 | | /// <summary> |
| | 140 | | /// Compares substrings of two specified <see cref="StringSegment"/> objects using the specified rules, |
| | 141 | | /// and returns an integer that indicates their relative position in the sort order. |
| | 142 | | /// </summary> |
| | 143 | | /// <param name="a">The first <see cref="StringSegment"/> to compare.</param> |
| | 144 | | /// <param name="b">The second <see cref="StringSegment"/> to compare.</param> |
| | 145 | | /// <param name="comparisonType">One of the enumeration values that specifies the rules for the comparison.</par |
| | 146 | | /// <returns> |
| | 147 | | /// A 32-bit signed integer indicating the lexical relationship between the two comparands. |
| | 148 | | /// The value is negative if <paramref name="a"/> is less than <paramref name="b"/>, 0 if the two comparands are |
| | 149 | | /// and positive if <paramref name="a"/> is greater than <paramref name="b"/>. |
| | 150 | | /// </returns> |
| | 151 | | public static int Compare(StringSegment a, StringSegment b, StringComparison comparisonType) |
| | 152 | | { |
| 0 | 153 | | var minLength = Math.Min(a.Length, b.Length); |
| 0 | 154 | | var diff = string.Compare(a.Buffer, a.Offset, b.Buffer, b.Offset, minLength, comparisonType); |
| 0 | 155 | | if (diff == 0) |
| | 156 | | { |
| 0 | 157 | | diff = a.Length - b.Length; |
| | 158 | | } |
| | 159 | |
|
| 0 | 160 | | return diff; |
| | 161 | | } |
| | 162 | |
|
| | 163 | | /// <inheritdoc /> |
| | 164 | | public override bool Equals(object obj) |
| | 165 | | { |
| 0 | 166 | | if (ReferenceEquals(null, obj)) |
| | 167 | | { |
| 0 | 168 | | return false; |
| | 169 | | } |
| | 170 | |
|
| 0 | 171 | | return obj is StringSegment segment && Equals(segment); |
| | 172 | | } |
| | 173 | |
|
| | 174 | | /// <summary> |
| | 175 | | /// Indicates whether the current object is equal to another object of the same type. |
| | 176 | | /// </summary> |
| | 177 | | /// <param name="other">An object to compare with this object.</param> |
| | 178 | | /// <returns><code>true</code> if the current object is equal to the other parameter; otherwise, <code>false</co |
| 0 | 179 | | public bool Equals(StringSegment other) => Equals(other, StringComparison.Ordinal); |
| | 180 | |
|
| | 181 | | /// <summary> |
| | 182 | | /// Indicates whether the current object is equal to another object of the same type. |
| | 183 | | /// </summary> |
| | 184 | | /// <param name="other">An object to compare with this object.</param> |
| | 185 | | /// <param name="comparisonType">One of the enumeration values that specifies the rules to use in the comparison |
| | 186 | | /// <returns><code>true</code> if the current object is equal to the other parameter; otherwise, <code>false</co |
| | 187 | | public bool Equals(StringSegment other, StringComparison comparisonType) |
| | 188 | | { |
| 0 | 189 | | if (Length != other.Length) |
| | 190 | | { |
| 0 | 191 | | return false; |
| | 192 | | } |
| | 193 | |
|
| 0 | 194 | | return string.Compare(Buffer, Offset, other.Buffer, other.Offset, other.Length, comparisonType) == 0; |
| | 195 | | } |
| | 196 | |
|
| | 197 | | // This handles StringSegment.Equals(string, StringSegment, StringComparison) and StringSegment.Equals(StringSeg |
| | 198 | | // via the implicit type converter |
| | 199 | | /// <summary> |
| | 200 | | /// Determines whether two specified <see cref="StringSegment"/> objects have the same value. A parameter specif |
| | 201 | | /// sort rules used in the comparison. |
| | 202 | | /// </summary> |
| | 203 | | /// <param name="a">The first <see cref="StringSegment"/> to compare.</param> |
| | 204 | | /// <param name="b">The second <see cref="StringSegment"/> to compare.</param> |
| | 205 | | /// <param name="comparisonType">One of the enumeration values that specifies the rules for the comparison.</par |
| | 206 | | /// <returns><code>true</code> if the objects are equal; otherwise, <code>false</code>.</returns> |
| | 207 | | public static bool Equals(StringSegment a, StringSegment b, StringComparison comparisonType) |
| | 208 | | { |
| 0 | 209 | | return a.Equals(b, comparisonType); |
| | 210 | | } |
| | 211 | |
|
| | 212 | | /// <summary> |
| | 213 | | /// Checks if the specified <see cref="string"/> is equal to the current <see cref="StringSegment"/>. |
| | 214 | | /// </summary> |
| | 215 | | /// <param name="text">The <see cref="string"/> to compare with the current <see cref="StringSegment"/>.</param> |
| | 216 | | /// <returns><code>true</code> if the specified <see cref="string"/> is equal to the current <see cref="StringSe |
| | 217 | | public bool Equals(string text) |
| | 218 | | { |
| 0 | 219 | | return Equals(text, StringComparison.Ordinal); |
| | 220 | | } |
| | 221 | |
|
| | 222 | | /// <summary> |
| | 223 | | /// Checks if the specified <see cref="string"/> is equal to the current <see cref="StringSegment"/>. |
| | 224 | | /// </summary> |
| | 225 | | /// <param name="text">The <see cref="string"/> to compare with the current <see cref="StringSegment"/>.</param> |
| | 226 | | /// <param name="comparisonType">One of the enumeration values that specifies the rules to use in the comparison |
| | 227 | | /// <returns><code>true</code> if the specified <see cref="string"/> is equal to the current <see cref="StringSe |
| | 228 | | /// <exception cref="ArgumentNullException"> |
| | 229 | | /// <paramref name="text"/> is <code>null</code>. |
| | 230 | | /// </exception> |
| | 231 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 232 | | public bool Equals(string text, StringComparison comparisonType) |
| | 233 | | { |
| 0 | 234 | | if (text == null) |
| | 235 | | { |
| 0 | 236 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); |
| | 237 | | } |
| | 238 | |
|
| 0 | 239 | | var textLength = text.Length; |
| 0 | 240 | | if (!HasValue || Length != textLength) |
| | 241 | | { |
| 0 | 242 | | return false; |
| | 243 | | } |
| | 244 | |
|
| 0 | 245 | | return string.Compare(Buffer, Offset, text, 0, textLength, comparisonType) == 0; |
| | 246 | | } |
| | 247 | |
|
| | 248 | | /// <inheritdoc /> |
| | 249 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 250 | | public override int GetHashCode() |
| | 251 | | { |
| | 252 | | #if NETCOREAPP |
| | 253 | | return string.GetHashCode(AsSpan()); |
| | 254 | | #elif NETSTANDARD |
| | 255 | | // This GetHashCode is expensive since it allocates on every call. |
| | 256 | | // However this is required to ensure we retain any behavior (such as hash code randomization) that |
| | 257 | | // string.GetHashCode has. |
| 0 | 258 | | return Value?.GetHashCode() ?? 0; |
| | 259 | | #else |
| | 260 | | #error Target frameworks need to be updated. |
| | 261 | | #endif |
| | 262 | |
|
| | 263 | | } |
| | 264 | |
|
| | 265 | | /// <summary> |
| | 266 | | /// Checks if two specified <see cref="StringSegment"/> have the same value. |
| | 267 | | /// </summary> |
| | 268 | | /// <param name="left">The first <see cref="StringSegment"/> to compare, or <code>null</code>.</param> |
| | 269 | | /// <param name="right">The second <see cref="StringSegment"/> to compare, or <code>null</code>.</param> |
| | 270 | | /// <returns><code>true</code> if the value of <paramref name="left"/> is the same as the value of <paramref nam |
| 0 | 271 | | public static bool operator ==(StringSegment left, StringSegment right) => left.Equals(right); |
| | 272 | |
|
| | 273 | | /// <summary> |
| | 274 | | /// Checks if two specified <see cref="StringSegment"/> have different values. |
| | 275 | | /// </summary> |
| | 276 | | /// <param name="left">The first <see cref="StringSegment"/> to compare, or <code>null</code>.</param> |
| | 277 | | /// <param name="right">The second <see cref="StringSegment"/> to compare, or <code>null</code>.</param> |
| | 278 | | /// <returns><code>true</code> if the value of <paramref name="left"/> is different from the value of <paramref |
| 0 | 279 | | public static bool operator !=(StringSegment left, StringSegment right) => !left.Equals(right); |
| | 280 | |
|
| | 281 | | // PERF: Do NOT add a implicit converter from StringSegment to String. That would negate most of the perf safety |
| | 282 | | /// <summary> |
| | 283 | | /// Creates a new <see cref="StringSegment"/> from the given <see cref="string"/>. |
| | 284 | | /// </summary> |
| | 285 | | /// <param name="value">The <see cref="string"/> to convert to a <see cref="StringSegment"/></param> |
| 0 | 286 | | public static implicit operator StringSegment(string value) => new StringSegment(value); |
| | 287 | |
|
| | 288 | | /// <summary> |
| | 289 | | /// Creates a see <see cref="ReadOnlySpan{T}"/> from the given <see cref="StringSegment"/>. |
| | 290 | | /// </summary> |
| | 291 | | /// <param name="segment">The <see cref="StringSegment"/> to convert to a <see cref="ReadOnlySpan{T}"/>.</param> |
| 0 | 292 | | public static implicit operator ReadOnlySpan<char>(StringSegment segment) => segment.AsSpan(); |
| | 293 | |
|
| | 294 | | /// <summary> |
| | 295 | | /// Creates a see <see cref="ReadOnlyMemory{T}"/> from the given <see cref="StringSegment"/>. |
| | 296 | | /// </summary> |
| | 297 | | /// <param name="segment">The <see cref="StringSegment"/> to convert to a <see cref="ReadOnlyMemory{T}"/>.</para |
| 0 | 298 | | public static implicit operator ReadOnlyMemory<char>(StringSegment segment) => segment.AsMemory(); |
| | 299 | |
|
| | 300 | | /// <summary> |
| | 301 | | /// Checks if the beginning of this <see cref="StringSegment"/> matches the specified <see cref="string"/> when |
| | 302 | | /// </summary> |
| | 303 | | /// <param name="text">The <see cref="string"/>to compare.</param> |
| | 304 | | /// <param name="comparisonType">One of the enumeration values that specifies the rules to use in the comparison |
| | 305 | | /// <returns><code>true</code> if <paramref name="text"/> matches the beginning of this <see cref="StringSegment |
| | 306 | | /// <exception cref="ArgumentNullException"> |
| | 307 | | /// <paramref name="text"/> is <code>null</code>. |
| | 308 | | /// </exception> |
| | 309 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 310 | | public bool StartsWith(string text, StringComparison comparisonType) |
| | 311 | | { |
| 0 | 312 | | if (text == null) |
| | 313 | | { |
| 0 | 314 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); |
| | 315 | | } |
| | 316 | |
|
| 0 | 317 | | var result = false; |
| 0 | 318 | | var textLength = text.Length; |
| | 319 | |
|
| 0 | 320 | | if (HasValue && Length >= textLength) |
| | 321 | | { |
| 0 | 322 | | result = string.Compare(Buffer, Offset, text, 0, textLength, comparisonType) == 0; |
| | 323 | | } |
| | 324 | |
|
| 0 | 325 | | return result; |
| | 326 | | } |
| | 327 | |
|
| | 328 | | /// <summary> |
| | 329 | | /// Checks if the end of this <see cref="StringSegment"/> matches the specified <see cref="string"/> when compar |
| | 330 | | /// </summary> |
| | 331 | | /// <param name="text">The <see cref="string"/>to compare.</param> |
| | 332 | | /// <param name="comparisonType">One of the enumeration values that specifies the rules to use in the comparison |
| | 333 | | /// <returns><code>true</code> if <paramref name="text"/> matches the end of this <see cref="StringSegment"/>; o |
| | 334 | | /// <exception cref="ArgumentNullException"> |
| | 335 | | /// <paramref name="text"/> is <code>null</code>. |
| | 336 | | /// </exception> |
| | 337 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 338 | | public bool EndsWith(string text, StringComparison comparisonType) |
| | 339 | | { |
| 0 | 340 | | if (text == null) |
| | 341 | | { |
| 0 | 342 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); |
| | 343 | | } |
| | 344 | |
|
| 0 | 345 | | var result = false; |
| 0 | 346 | | var textLength = text.Length; |
| 0 | 347 | | var comparisonLength = Offset + Length - textLength; |
| | 348 | |
|
| 0 | 349 | | if (HasValue && comparisonLength > 0) |
| | 350 | | { |
| 0 | 351 | | result = string.Compare(Buffer, comparisonLength, text, 0, textLength, comparisonType) == 0; |
| | 352 | | } |
| | 353 | |
|
| 0 | 354 | | return result; |
| | 355 | | } |
| | 356 | |
|
| | 357 | | /// <summary> |
| | 358 | | /// Retrieves a substring from this <see cref="StringSegment"/>. |
| | 359 | | /// The substring starts at the position specified by <paramref name="offset"/> and has the remaining length. |
| | 360 | | /// </summary> |
| | 361 | | /// <param name="offset">The zero-based starting character position of a substring in this <see cref="StringSegm |
| | 362 | | /// <returns>A <see cref="string"/> that is equivalent to the substring of remaining length that begins at |
| | 363 | | /// <paramref name="offset"/> in this <see cref="StringSegment"/></returns> |
| | 364 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 365 | | /// <paramref name="offset"/> is greater than or equal to <see cref="Length"/> or less than zero. |
| | 366 | | /// </exception> |
| 0 | 367 | | public string Substring(int offset) => Substring(offset, Length - offset); |
| | 368 | |
|
| | 369 | | /// <summary> |
| | 370 | | /// Retrieves a substring from this <see cref="StringSegment"/>. |
| | 371 | | /// The substring starts at the position specified by <paramref name="offset"/> and has the specified <paramref |
| | 372 | | /// </summary> |
| | 373 | | /// <param name="offset">The zero-based starting character position of a substring in this <see cref="StringSegm |
| | 374 | | /// <param name="length">The number of characters in the substring.</param> |
| | 375 | | /// <returns>A <see cref="string"/> that is equivalent to the substring of length <paramref name="length"/> that |
| | 376 | | /// <paramref name="offset"/> in this <see cref="StringSegment"/></returns> |
| | 377 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 378 | | /// <paramref name="offset"/> or <paramref name="length"/> is less than zero, or <paramref name="offset"/> + <pa |
| | 379 | | /// greater than <see cref="Length"/>. |
| | 380 | | /// </exception> |
| | 381 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 382 | | public string Substring(int offset, int length) |
| | 383 | | { |
| 0 | 384 | | if (!HasValue || offset < 0 || length < 0 || (uint)(offset + length) > (uint)Length) |
| | 385 | | { |
| 0 | 386 | | ThrowInvalidArguments(offset, length); |
| | 387 | | } |
| | 388 | |
|
| 0 | 389 | | return Buffer.Substring(Offset + offset, length); |
| | 390 | | } |
| | 391 | |
|
| | 392 | | /// <summary> |
| | 393 | | /// Retrieves a <see cref="StringSegment"/> that represents a substring from this <see cref="StringSegment"/>. |
| | 394 | | /// The <see cref="StringSegment"/> starts at the position specified by <paramref name="offset"/>. |
| | 395 | | /// </summary> |
| | 396 | | /// <param name="offset">The zero-based starting character position of a substring in this <see cref="StringSegm |
| | 397 | | /// <returns>A <see cref="StringSegment"/> that begins at <paramref name="offset"/> in this <see cref="StringSeg |
| | 398 | | /// whose length is the remainder.</returns> |
| | 399 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 400 | | /// <paramref name="offset"/> is greater than or equal to <see cref="Length"/> or less than zero. |
| | 401 | | /// </exception> |
| 0 | 402 | | public StringSegment Subsegment(int offset) => Subsegment(offset, Length - offset); |
| | 403 | |
|
| | 404 | | /// <summary> |
| | 405 | | /// Retrieves a <see cref="StringSegment"/> that represents a substring from this <see cref="StringSegment"/>. |
| | 406 | | /// The <see cref="StringSegment"/> starts at the position specified by <paramref name="offset"/> and has the sp |
| | 407 | | /// </summary> |
| | 408 | | /// <param name="offset">The zero-based starting character position of a substring in this <see cref="StringSegm |
| | 409 | | /// <param name="length">The number of characters in the substring.</param> |
| | 410 | | /// <returns>A <see cref="StringSegment"/> that is equivalent to the substring of length <paramref name="length" |
| | 411 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 412 | | /// <paramref name="offset"/> or <paramref name="length"/> is less than zero, or <paramref name="offset"/> + <pa |
| | 413 | | /// greater than <see cref="Length"/>. |
| | 414 | | /// </exception> |
| | 415 | | public StringSegment Subsegment(int offset, int length) |
| | 416 | | { |
| 0 | 417 | | if (!HasValue || offset < 0 || length < 0 || (uint)(offset + length) > (uint)Length) |
| | 418 | | { |
| 0 | 419 | | ThrowInvalidArguments(offset, length); |
| | 420 | | } |
| | 421 | |
|
| 0 | 422 | | return new StringSegment(Buffer, Offset + offset, length); |
| | 423 | | } |
| | 424 | |
|
| | 425 | | /// <summary> |
| | 426 | | /// Gets the zero-based index of the first occurrence of the character <paramref name="c"/> in this <see cref="S |
| | 427 | | /// The search starts at <paramref name="start"/> and examines a specified number of <paramref name="count"/> ch |
| | 428 | | /// </summary> |
| | 429 | | /// <param name="c">The Unicode character to seek.</param> |
| | 430 | | /// <param name="start">The zero-based index position at which the search starts. </param> |
| | 431 | | /// <param name="count">The number of characters to examine.</param> |
| | 432 | | /// <returns>The zero-based index position of <paramref name="c"/> from the beginning of the <see cref="StringSe |
| | 433 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 434 | | /// <paramref name="start"/> or <paramref name="count"/> is less than zero, or <paramref name="start"/> + <param |
| | 435 | | /// greater than <see cref="Length"/>. |
| | 436 | | /// </exception> |
| | 437 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 438 | | public int IndexOf(char c, int start, int count) |
| | 439 | | { |
| 0 | 440 | | var offset = Offset + start; |
| | 441 | |
|
| 0 | 442 | | if (!HasValue || start < 0 || (uint)offset > (uint)Buffer.Length) |
| | 443 | | { |
| 0 | 444 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); |
| | 445 | | } |
| | 446 | |
|
| 0 | 447 | | if (count < 0) |
| | 448 | | { |
| 0 | 449 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count); |
| | 450 | | } |
| | 451 | |
|
| 0 | 452 | | var index = Buffer.IndexOf(c, offset, count); |
| 0 | 453 | | if (index != -1) |
| | 454 | | { |
| 0 | 455 | | index -= Offset; |
| | 456 | | } |
| | 457 | |
|
| 0 | 458 | | return index; |
| | 459 | | } |
| | 460 | |
|
| | 461 | | /// <summary> |
| | 462 | | /// Gets the zero-based index of the first occurrence of the character <paramref name="c"/> in this <see cref="S |
| | 463 | | /// The search starts at <paramref name="start"/>. |
| | 464 | | /// </summary> |
| | 465 | | /// <param name="c">The Unicode character to seek.</param> |
| | 466 | | /// <param name="start">The zero-based index position at which the search starts. </param> |
| | 467 | | /// <returns>The zero-based index position of <paramref name="c"/> from the beginning of the <see cref="StringSe |
| | 468 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 469 | | /// <paramref name="start"/> is greater than or equal to <see cref="Length"/> or less than zero. |
| | 470 | | /// </exception> |
| 0 | 471 | | public int IndexOf(char c, int start) => IndexOf(c, start, Length - start); |
| | 472 | |
|
| | 473 | | /// <summary> |
| | 474 | | /// Gets the zero-based index of the first occurrence of the character <paramref name="c"/> in this <see cref="S |
| | 475 | | /// </summary> |
| | 476 | | /// <param name="c">The Unicode character to seek.</param> |
| | 477 | | /// <returns>The zero-based index position of <paramref name="c"/> from the beginning of the <see cref="StringSe |
| 0 | 478 | | public int IndexOf(char c) => IndexOf(c, 0, Length); |
| | 479 | |
|
| | 480 | | /// <summary> |
| | 481 | | /// Reports the zero-based index of the first occurrence in this instance of any character in a specified array |
| | 482 | | /// of Unicode characters. The search starts at a specified character position and examines a specified number |
| | 483 | | /// of character positions. |
| | 484 | | /// </summary> |
| | 485 | | /// <param name="anyOf">A Unicode character array containing one or more characters to seek.</param> |
| | 486 | | /// <param name="startIndex">The search starting position.</param> |
| | 487 | | /// <param name="count">The number of character positions to examine.</param> |
| | 488 | | /// <returns>The zero-based index position of the first occurrence in this instance where any character in <para |
| | 489 | | /// was found; -1 if no character in <paramref name="anyOf"/> was found.</returns> |
| | 490 | | /// <exception cref="ArgumentNullException"> |
| | 491 | | /// <paramref name="anyOf"/> is <code>null</code>. |
| | 492 | | /// </exception> |
| | 493 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 494 | | /// <paramref name="startIndex"/> or <paramref name="count"/> is less than zero, or <paramref name="startIndex"/ |
| | 495 | | /// greater than <see cref="Length"/>. |
| | 496 | | /// </exception> |
| | 497 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 498 | | public int IndexOfAny(char[] anyOf, int startIndex, int count) |
| | 499 | | { |
| 0 | 500 | | var index = -1; |
| | 501 | |
|
| 0 | 502 | | if (HasValue) |
| | 503 | | { |
| 0 | 504 | | if (startIndex < 0 || Offset + startIndex > Buffer.Length) |
| | 505 | | { |
| 0 | 506 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.start); |
| | 507 | | } |
| | 508 | |
|
| 0 | 509 | | if (count < 0 || Offset + startIndex + count > Buffer.Length) |
| | 510 | | { |
| 0 | 511 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count); |
| | 512 | | } |
| | 513 | |
|
| 0 | 514 | | index = Buffer.IndexOfAny(anyOf, Offset + startIndex, count); |
| 0 | 515 | | if (index != -1) |
| | 516 | | { |
| 0 | 517 | | index -= Offset; |
| | 518 | | } |
| | 519 | | } |
| | 520 | |
|
| 0 | 521 | | return index; |
| | 522 | | } |
| | 523 | |
|
| | 524 | | /// <summary> |
| | 525 | | /// Reports the zero-based index of the first occurrence in this instance of any character in a specified array |
| | 526 | | /// of Unicode characters. The search starts at a specified character position. |
| | 527 | | /// </summary> |
| | 528 | | /// <param name="anyOf">A Unicode character array containing one or more characters to seek.</param> |
| | 529 | | /// <param name="startIndex">The search starting position.</param> |
| | 530 | | /// <returns>The zero-based index position of the first occurrence in this instance where any character in <para |
| | 531 | | /// was found; -1 if no character in <paramref name="anyOf"/> was found.</returns> |
| | 532 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | 533 | | /// <paramref name="startIndex"/> is greater than or equal to <see cref="Length"/> or less than zero. |
| | 534 | | /// </exception> |
| | 535 | | public int IndexOfAny(char[] anyOf, int startIndex) |
| | 536 | | { |
| 0 | 537 | | return IndexOfAny(anyOf, startIndex, Length - startIndex); |
| | 538 | | } |
| | 539 | |
|
| | 540 | | /// <summary> |
| | 541 | | /// Reports the zero-based index of the first occurrence in this instance of any character in a specified array |
| | 542 | | /// of Unicode characters. |
| | 543 | | /// </summary> |
| | 544 | | /// <param name="anyOf">A Unicode character array containing one or more characters to seek.</param> |
| | 545 | | /// <returns>The zero-based index position of the first occurrence in this instance where any character in <para |
| | 546 | | /// was found; -1 if no character in <paramref name="anyOf"/> was found.</returns> |
| | 547 | | public int IndexOfAny(char[] anyOf) |
| | 548 | | { |
| 0 | 549 | | return IndexOfAny(anyOf, 0, Length); |
| | 550 | | } |
| | 551 | |
|
| | 552 | | /// <summary> |
| | 553 | | /// Reports the zero-based index position of the last occurrence of a specified Unicode character within this in |
| | 554 | | /// </summary> |
| | 555 | | /// <param name="value">The Unicode character to seek.</param> |
| | 556 | | /// <returns>The zero-based index position of value if that character is found, or -1 if it is not.</returns> |
| | 557 | | public int LastIndexOf(char value) |
| | 558 | | { |
| 0 | 559 | | var index = -1; |
| | 560 | |
|
| 0 | 561 | | if (HasValue) |
| | 562 | | { |
| 0 | 563 | | index = Buffer.LastIndexOf(value, Offset + Length - 1, Length); |
| 0 | 564 | | if (index != -1) |
| | 565 | | { |
| 0 | 566 | | index -= Offset; |
| | 567 | | } |
| | 568 | | } |
| | 569 | |
|
| 0 | 570 | | return index; |
| | 571 | | } |
| | 572 | |
|
| | 573 | | /// <summary> |
| | 574 | | /// Removes all leading and trailing whitespaces. |
| | 575 | | /// </summary> |
| | 576 | | /// <returns>The trimmed <see cref="StringSegment"/>.</returns> |
| 0 | 577 | | public StringSegment Trim() => TrimStart().TrimEnd(); |
| | 578 | |
|
| | 579 | | /// <summary> |
| | 580 | | /// Removes all leading whitespaces. |
| | 581 | | /// </summary> |
| | 582 | | /// <returns>The trimmed <see cref="StringSegment"/>.</returns> |
| | 583 | | public unsafe StringSegment TrimStart() |
| | 584 | | { |
| 0 | 585 | | var trimmedStart = Offset; |
| 0 | 586 | | var length = Offset + Length; |
| | 587 | |
|
| 0 | 588 | | fixed (char* p = Buffer) |
| | 589 | | { |
| 0 | 590 | | while (trimmedStart < length) |
| | 591 | | { |
| 0 | 592 | | var c = p[trimmedStart]; |
| | 593 | |
|
| 0 | 594 | | if (!char.IsWhiteSpace(c)) |
| | 595 | | { |
| | 596 | | break; |
| | 597 | | } |
| | 598 | |
|
| 0 | 599 | | trimmedStart++; |
| | 600 | | } |
| | 601 | | } |
| | 602 | |
|
| 0 | 603 | | return new StringSegment(Buffer, trimmedStart, length - trimmedStart); |
| | 604 | | } |
| | 605 | |
|
| | 606 | | /// <summary> |
| | 607 | | /// Removes all trailing whitespaces. |
| | 608 | | /// </summary> |
| | 609 | | /// <returns>The trimmed <see cref="StringSegment"/>.</returns> |
| | 610 | | public unsafe StringSegment TrimEnd() |
| | 611 | | { |
| 0 | 612 | | var offset = Offset; |
| 0 | 613 | | var trimmedEnd = offset + Length - 1; |
| | 614 | |
|
| 0 | 615 | | fixed (char* p = Buffer) |
| | 616 | | { |
| 0 | 617 | | while (trimmedEnd >= offset) |
| | 618 | | { |
| 0 | 619 | | var c = p[trimmedEnd]; |
| | 620 | |
|
| 0 | 621 | | if (!char.IsWhiteSpace(c)) |
| | 622 | | { |
| | 623 | | break; |
| | 624 | | } |
| | 625 | |
|
| 0 | 626 | | trimmedEnd--; |
| | 627 | | } |
| | 628 | | } |
| | 629 | |
|
| 0 | 630 | | return new StringSegment(Buffer, offset, trimmedEnd - offset + 1); |
| | 631 | | } |
| | 632 | |
|
| | 633 | | /// <summary> |
| | 634 | | /// Splits a string into <see cref="StringSegment"/>s that are based on the characters in an array. |
| | 635 | | /// </summary> |
| | 636 | | /// <param name="chars">A character array that delimits the substrings in this string, an empty array that |
| | 637 | | /// contains no delimiters, or null.</param> |
| | 638 | | /// <returns>An <see cref="StringTokenizer"/> whose elements contain the <see cref="StringSegment"/>s from this |
| | 639 | | /// that are delimited by one or more characters in <paramref name="chars"/>.</returns> |
| | 640 | | public StringTokenizer Split(char[] chars) |
| | 641 | | { |
| 0 | 642 | | return new StringTokenizer(this, chars); |
| | 643 | | } |
| | 644 | |
|
| | 645 | | /// <summary> |
| | 646 | | /// Indicates whether the specified <see cref="StringSegment"/> is null or an Empty string. |
| | 647 | | /// </summary> |
| | 648 | | /// <param name="value">The <see cref="StringSegment"/> to test.</param> |
| | 649 | | /// <returns></returns> |
| | 650 | | public static bool IsNullOrEmpty(StringSegment value) |
| | 651 | | { |
| 0 | 652 | | var res = false; |
| | 653 | |
|
| 0 | 654 | | if (!value.HasValue || value.Length == 0) |
| | 655 | | { |
| 0 | 656 | | res = true; |
| | 657 | | } |
| | 658 | |
|
| 0 | 659 | | return res; |
| | 660 | | } |
| | 661 | |
|
| | 662 | | /// <summary> |
| | 663 | | /// Returns the <see cref="string"/> represented by this <see cref="StringSegment"/> or <code>String.Empty</code |
| | 664 | | /// </summary> |
| | 665 | | /// <returns>The <see cref="string"/> represented by this <see cref="StringSegment"/> or <code>String.Empty</cod |
| | 666 | | public override string ToString() |
| | 667 | | { |
| 0 | 668 | | return Value ?? string.Empty; |
| | 669 | | } |
| | 670 | |
|
| | 671 | | // Methods that do no return (i.e. throw) are not inlined |
| | 672 | | // https://github.com/dotnet/coreclr/pull/6103 |
| | 673 | | private static void ThrowInvalidArguments(string buffer, int offset, int length) |
| | 674 | | { |
| | 675 | | // Only have single throw in method so is marked as "does not return" and isn't inlined to caller |
| 0 | 676 | | throw GetInvalidArgumentsException(); |
| | 677 | |
|
| | 678 | | Exception GetInvalidArgumentsException() |
| | 679 | | { |
| 0 | 680 | | if (buffer == null) |
| | 681 | | { |
| 0 | 682 | | return ThrowHelper.GetArgumentNullException(ExceptionArgument.buffer); |
| | 683 | | } |
| | 684 | |
|
| 0 | 685 | | if (offset < 0) |
| | 686 | | { |
| 0 | 687 | | return ThrowHelper.GetArgumentOutOfRangeException(ExceptionArgument.offset); |
| | 688 | | } |
| | 689 | |
|
| 0 | 690 | | if (length < 0) |
| | 691 | | { |
| 0 | 692 | | return ThrowHelper.GetArgumentOutOfRangeException(ExceptionArgument.length); |
| | 693 | | } |
| | 694 | |
|
| 0 | 695 | | return ThrowHelper.GetArgumentException(ExceptionResource.Argument_InvalidOffsetLength); |
| | 696 | | } |
| | 697 | | } |
| | 698 | |
|
| | 699 | | private void ThrowInvalidArguments(int offset, int length) |
| | 700 | | { |
| 0 | 701 | | throw GetInvalidArgumentsException(HasValue); |
| | 702 | |
|
| | 703 | | Exception GetInvalidArgumentsException(bool hasValue) |
| | 704 | | { |
| 0 | 705 | | if (!hasValue) |
| | 706 | | { |
| 0 | 707 | | return ThrowHelper.GetArgumentOutOfRangeException(ExceptionArgument.offset); |
| | 708 | | } |
| | 709 | |
|
| 0 | 710 | | if (offset < 0) |
| | 711 | | { |
| 0 | 712 | | return ThrowHelper.GetArgumentOutOfRangeException(ExceptionArgument.offset); |
| | 713 | | } |
| | 714 | |
|
| 0 | 715 | | if (length < 0) |
| | 716 | | { |
| 0 | 717 | | return ThrowHelper.GetArgumentOutOfRangeException(ExceptionArgument.length); |
| | 718 | | } |
| | 719 | |
|
| 0 | 720 | | return ThrowHelper.GetArgumentException(ExceptionResource.Argument_InvalidOffsetLengthStringSegment); |
| | 721 | | } |
| | 722 | | } |
| | 723 | | } |
| | 724 | | } |