| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="StringExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | |
| | | 10 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 11 | | namespace MyNet.Primitives; |
| | | 12 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 13 | | |
| | | 14 | | public static class StringExtensions |
| | | 15 | | { |
| | | 16 | | extension(string? value) |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Returns the original string if it is not null, or an empty string if it is null. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <returns>The original string or an empty string.</returns> |
| | 426 | 22 | | public string OrEmpty() => value ?? string.Empty; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Returns the original string if it is not null or whitespace, or the specified placeholder string if it is nu |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="placeholder">The placeholder string to return if the original string is null or whitespace.</pa |
| | | 28 | | /// <returns>The original string or the placeholder string.</returns> |
| | 9 | 29 | | public string Or(string placeholder) => string.IsNullOrWhiteSpace(value) ? placeholder : value; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Add a prefix before value, separated by period. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="prefix">Prefix to add.</param> |
| | | 35 | | /// <param name="separator">Separator to use between prefix and value.</param> |
| | | 36 | | /// <returns>prefix.value.</returns> |
| | | 37 | | public string? WithPrefix(string? prefix = null, string? separator = "-") |
| | 0 | 38 | | => !string.IsNullOrWhiteSpace(prefix) && !string.IsNullOrWhiteSpace(value) ? $"{prefix}{separator}{value}" : |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Determines whether the original string contains any of the specified values, using a case-insensitive compar |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="values">The values to check for in the original string.</param> |
| | | 44 | | /// <returns>True if the original string contains any of the specified values; otherwise, false.</returns> |
| | | 45 | | public bool ContainsAny(params ReadOnlySpan<string> values) |
| | | 46 | | { |
| | 9 | 47 | | if (string.IsNullOrEmpty(value)) |
| | 3 | 48 | | return false; |
| | | 49 | | |
| | 21 | 50 | | foreach (var item in values) |
| | | 51 | | { |
| | 6 | 52 | | if (!string.IsNullOrEmpty(item) && value.Contains(item, StringComparison.OrdinalIgnoreCase)) |
| | | 53 | | { |
| | 3 | 54 | | return true; |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | 3 | 58 | | return false; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Determines whether the original string does not contain any of the specified values, using a case-insensitiv |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="values">The values to check for in the original string.</param> |
| | | 65 | | /// <returns>True if the original string does not contain any of the specified values; otherwise, false.</return |
| | | 66 | | public bool NotContainsAny(params ReadOnlySpan<string> values) |
| | | 67 | | { |
| | 9 | 68 | | if (string.IsNullOrEmpty(value)) |
| | 3 | 69 | | return true; |
| | | 70 | | |
| | 21 | 71 | | foreach (var item in values) |
| | | 72 | | { |
| | 6 | 73 | | if (!string.IsNullOrEmpty(item) && value.Contains(item, StringComparison.OrdinalIgnoreCase)) |
| | | 74 | | { |
| | 3 | 75 | | return false; |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | 3 | 79 | | return true; |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | extension(string value) |
| | | 84 | | { |
| | | 85 | | /// <summary> |
| | | 86 | | /// Extension method to format string with passed arguments. Current thread's current culture is used. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="args">arguments.</param> |
| | 0 | 89 | | public string FormatWith(params object?[] args) => string.Format(CultureInfo.CurrentCulture, value, args); |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Extension method to format string with passed arguments using specified format provider (i.e. CultureInfo). |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="provider">An object that supplies culture-specific formatting information.</param> |
| | | 95 | | /// <param name="args">arguments.</param> |
| | 105 | 96 | | public string FormatWith(IFormatProvider provider, params object?[] args) => string.Format(provider, value, args |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Extension method to format string with passed arguments using the invariant culture. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="args">arguments.</param> |
| | 3 | 102 | | public string FormatWithInvariant(params object?[] args) => string.Format(CultureInfo.InvariantCulture, value, a |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Converts the string to a <see cref="Version"/>. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <returns>The parsed <see cref="Version"/> instance.</returns> |
| | | 108 | | /// <exception cref="ArgumentException">Thrown when the input is empty.</exception> |
| | | 109 | | /// <exception cref="ArgumentNullException">Thrown when the input is null.</exception> |
| | | 110 | | /// <exception cref="FormatException">Thrown when the input format is invalid.</exception> |
| | | 111 | | /// <exception cref="OverflowException">Thrown when a numeric component is too large.</exception> |
| | 3 | 112 | | public Version ToVersion() => new(value); |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Attempts to convert the string to a <see cref="Version"/>. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <param name="version">When this method returns, contains the parsed version if successful; otherwise <see la |
| | | 118 | | /// <returns><see langword="true"/> when parsing succeeds; otherwise <see langword="false"/>.</returns> |
| | | 119 | | public bool TryToVersion(out Version? version) |
| | | 120 | | { |
| | | 121 | | try |
| | | 122 | | { |
| | 6 | 123 | | version = new(value); |
| | 3 | 124 | | return true; |
| | | 125 | | } |
| | 3 | 126 | | catch (ArgumentException) |
| | | 127 | | { |
| | 3 | 128 | | version = null; |
| | 3 | 129 | | return false; |
| | | 130 | | } |
| | 0 | 131 | | catch (FormatException) |
| | | 132 | | { |
| | 0 | 133 | | version = null; |
| | 0 | 134 | | return false; |
| | | 135 | | } |
| | 0 | 136 | | catch (OverflowException) |
| | | 137 | | { |
| | 0 | 138 | | version = null; |
| | 0 | 139 | | return false; |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |