|
| 1 | +// Copyright (c) Ubiquity.NET Contributors. All rights reserved. |
| 2 | +// Licensed under the Apache-2.0 WITH LLVM-exception license. See the LICENSE.md file in the project root for full license information. |
| 3 | + |
| 4 | +namespace Ubiquity.NET.Extensions |
| 5 | +{ |
| 6 | + /// <summary>Utility class to provide extensions for <see cref="string"/></summary> |
| 7 | + public static partial class StringExtensions |
| 8 | + { |
| 9 | + /// <summary>Tests if a string contains any line endings</summary> |
| 10 | + /// <param name="self">String to test</param> |
| 11 | + /// <param name="skipUnicodeLineEndigs">Indicates whether additional Unicode line endings are considered [default: false]</param> |
| 12 | + /// <returns><see langword="true"/> if the string contains any line endings</returns> |
| 13 | + public static bool HasLineEndings( this string self, bool skipUnicodeLineEndigs = false ) |
| 14 | + { |
| 15 | +#if NETSTANDARD2_0 |
| 16 | + PolyFillExceptionValidators.ThrowIfNull(self); |
| 17 | + return skipUnicodeLineEndigs |
| 18 | + ? PolyFillStringExtensions.SystemNewLinesRegex.IsMatch(self) |
| 19 | + : PolyFillStringExtensions.UnicodeNewLinesRegex.IsMatch(self); |
| 20 | +#else |
| 21 | + ArgumentNullException.ThrowIfNull(self); |
| 22 | + return skipUnicodeLineEndigs |
| 23 | + ? SystemNewLinesRegex.IsMatch(self) |
| 24 | + : UnicodeNewLinesRegex.IsMatch(self); |
| 25 | +#endif |
| 26 | + } |
| 27 | + |
| 28 | + // Since the poly fills are only applied for .NET Standard 2.0 this has to |
| 29 | + // replicate the new line Regular expressions. |
| 30 | +#if !NETSTANDARD2_0 |
| 31 | + // The Unicode Standard, Sec. 5.8, Recommendation R4 and Table 5-2 state that the CR, LF, |
| 32 | + // CRLF, NEL, LS, FF, and PS sequences are considered newline functions. That section |
| 33 | + // also specifically excludes VT from the list of newline functions, so we do not include |
| 34 | + // it in the regular expression match. |
| 35 | + |
| 36 | + // language=regex |
| 37 | + private const string UnicodeNewLinesRegExPattern = @"(\r\n|\r|\n|\f|\u0085|\u2028|\u2029)"; |
| 38 | + |
| 39 | + // language=regex |
| 40 | + private const string SystemNewLinesRegExPattern = @"(\r\n|\r|\n)"; |
| 41 | + |
| 42 | + #if NET9_0_OR_GREATER |
| 43 | + // Source generator for .NET 9+ understands properties directly |
| 44 | + [GeneratedRegex( UnicodeNewLinesRegExPattern )] |
| 45 | + internal static partial Regex UnicodeNewLinesRegEx { get; } |
| 46 | + |
| 47 | + // Source generator for .NET 9+ understands properties directly |
| 48 | + [GeneratedRegex( SystemNewLinesRegExPattern )] |
| 49 | + internal static partial Regex SystemNewLinesRegEx { get; } |
| 50 | + #else |
| 51 | + // The generated method uses a static instance already, so |
| 52 | + // this syntactic sugar should be aggressively inlined to |
| 53 | + // avoid even tail-call overhead. |
| 54 | + internal static Regex UnicodeNewLinesRegex |
| 55 | + { |
| 56 | + [MethodImpl( MethodImplOptions.AggressiveInlining )] |
| 57 | + get => GetUnicodeNewLinesRegex(); |
| 58 | + } |
| 59 | + |
| 60 | + [GeneratedRegex( UnicodeNewLinesRegExPattern )] |
| 61 | + private static partial Regex GetUnicodeNewLinesRegex(); |
| 62 | + |
| 63 | + internal static Regex SystemNewLinesRegex |
| 64 | + { |
| 65 | + [MethodImpl( MethodImplOptions.AggressiveInlining )] |
| 66 | + get => GetSystemNewLinesRegex(); |
| 67 | + } |
| 68 | + |
| 69 | + [GeneratedRegex( SystemNewLinesRegExPattern )] |
| 70 | + private static partial Regex GetSystemNewLinesRegex(); |
| 71 | + #endif |
| 72 | +#endif |
| 73 | + } |
| 74 | +} |
0 commit comments