< Summary

Information
Class: MyNet.Primitives.StringExtensions
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Extensions/StringExtensions.cs
Tag: 323_28699572109
Line coverage
73%
Covered lines: 22
Uncovered lines: 8
Coverable lines: 30
Total lines: 144
Line coverage: 73.3%
Branch coverage
83%
Covered branches: 20
Total branches: 24
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
OrEmpty(...)100%22100%
Or(...)100%22100%
WithPrefix(...)0%2040%
ContainsAny(...)100%88100%
NotContainsAny(...)100%88100%
FormatWith(...)100%210%
FormatWith(...)100%11100%
FormatWithInvariant(...)100%11100%
ToVersion(...)100%11100%
TryToVersion(...)100%1145.45%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Extensions/StringExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="StringExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Globalization;
 9
 10#pragma warning disable IDE0130 // Namespace does not match folder structure
 11namespace MyNet.Primitives;
 12#pragma warning restore IDE0130 // Namespace does not match folder structure
 13
 14public 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>
 42622        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>
 929        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 = "-")
 038            => !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        {
 947            if (string.IsNullOrEmpty(value))
 348                return false;
 49
 2150            foreach (var item in values)
 51            {
 652                if (!string.IsNullOrEmpty(item) && value.Contains(item, StringComparison.OrdinalIgnoreCase))
 53                {
 354                    return true;
 55                }
 56            }
 57
 358            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        {
 968            if (string.IsNullOrEmpty(value))
 369                return true;
 70
 2171            foreach (var item in values)
 72            {
 673                if (!string.IsNullOrEmpty(item) && value.Contains(item, StringComparison.OrdinalIgnoreCase))
 74                {
 375                    return false;
 76                }
 77            }
 78
 379            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>
 089        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>
 10596        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>
 3102        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>
 3112        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            {
 6123                version = new(value);
 3124                return true;
 125            }
 3126            catch (ArgumentException)
 127            {
 3128                version = null;
 3129                return false;
 130            }
 0131            catch (FormatException)
 132            {
 0133                version = null;
 0134                return false;
 135            }
 0136            catch (OverflowException)
 137            {
 0138                version = null;
 0139                return false;
 140            }
 141        }
 142    }
 143}
 144