< Summary

Information
Class: Pozitron.Convert.StringExtensions
Assembly: Pozitron.Convert
File(s): /home/runner/work/Pozitron.Convert/Pozitron.Convert/src/Pozitron.Convert/StringExtensions.cs
Tag: 6_24670647575
Line coverage
100%
Covered lines: 4
Uncovered lines: 0
Coverable lines: 4
Total lines: 90
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
To(...)100%44100%
ToNullable(...)100%44100%

File(s)

/home/runner/work/Pozitron.Convert/Pozitron.Convert/src/Pozitron.Convert/StringExtensions.cs

#LineLine coverage
 1using System;
 2
 3namespace Pozitron.Convert;
 4
 5/// <summary>
 6/// Conversion extensions to string
 7/// </summary>
 8public static class StringExtensions
 9{
 10#if NET8_0_OR_GREATER
 11
 12    /// <summary>
 13    /// It converts the string value to various IParsable types.
 14    /// </summary>
 15    /// <typeparam name="T">The target IParsable type</typeparam>
 16    /// <param name="value">The string value to be converted.</param>
 17    /// <param name="defaultValue">The default to be returned if conversion is not successful. By default it's the defau
 18    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
 19    /// <returns>On success, it returns the converted value. On failure or null <see href="value" />, it returns the pro
 20    [return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull(nameof(defaultValue))]
 21    public static T? To<T>(this string? value, T? defaultValue = default, IFormatProvider? provider = null) where T : IP
 22    {
 5923        if (value is null) return defaultValue;
 24
 3925        return T.TryParse(value, provider, out var output) ? output : defaultValue;
 26    }
 27
 28    /// <summary>
 29    /// It converts the string value to various IParsable struct types.
 30    /// </summary>
 31    /// <typeparam name="T">The target IParsable type</typeparam>
 32    /// <param name="value">The string value to be converted.</param>
 33    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
 34    /// <returns>On success, it returns the converted value. On failure or null <see href="value" />, it returns null.</
 35    public static T? ToNullable<T>(this string? value, IFormatProvider? provider = null) where T : struct, IParsable<T>
 36    {
 3637        if (value is null) return null;
 38
 2439        return T.TryParse(value, provider, out var output) ? output : null;
 40    }
 41#else
 42
 43    /// <summary>
 44    /// It converts the string value to various struct types.
 45    /// </summary>
 46    /// <typeparam name="T">Supported types: int, decimal, double, float, DateTime, bool</typeparam>
 47    /// <param name="value">The string value to be converted.</param>
 48    /// <param name="defaultValue">The default to be returned if conversion is not successful. By default it's the defau
 49    /// <returns>On success, it returns the converted value. On failure or null <see href="value" />, it returns the pro
 50    public static T To<T>(this string? value, T defaultValue = default) where T : struct
 51    {
 52        if (value is null) return defaultValue;
 53
 54        // In NetFX 4.8 there is no IParsable interface, so we have to do this manually.
 55        return typeof(T) switch
 56        {
 57            Type type when type == typeof(int) => int.TryParse(value, out var output) && output is T x ? x : defaultValu
 58            Type type when type == typeof(decimal) => decimal.TryParse(value, out var output) && output is T x ? x : def
 59            Type type when type == typeof(double) => double.TryParse(value, out var output) && output is T x ? x : defau
 60            Type type when type == typeof(float) => float.TryParse(value, out var output) && output is T x ? x : default
 61            Type type when type == typeof(DateTime) => DateTime.TryParse(value, out var output) && output is T x ? x : d
 62            Type type when type == typeof(bool) => bool.TryParse(value, out var output) && output is T x ? x : defaultVa
 63            _ => defaultValue
 64        };
 65    }
 66
 67    /// <summary>
 68    /// It converts the string value to various struct types.
 69    /// </summary>
 70    /// <typeparam name="T">Supported types: int, decimal, double, float, DateTime, bool</typeparam>
 71    /// <param name="value">The string value to be converted.</param>
 72    /// <returns>On success, it returns the converted value. On failure or null <see href="value" />, it returns null.</
 73    public static T? ToNullable<T>(this string? value) where T : struct
 74    {
 75        if (value is null) return null;
 76
 77        // In NetFX 4.8 there is no IParsable interface, so we have to do this manually.
 78        return typeof(T) switch
 79        {
 80            Type type when type == typeof(int) => int.TryParse(value, out var output) && output is T x ? x : null,
 81            Type type when type == typeof(decimal) => decimal.TryParse(value, out var output) && output is T x ? x : nul
 82            Type type when type == typeof(double) => double.TryParse(value, out var output) && output is T x ? x : null,
 83            Type type when type == typeof(float) => float.TryParse(value, out var output) && output is T x ? x : null,
 84            Type type when type == typeof(DateTime) => DateTime.TryParse(value, out var output) && output is T x ? x : n
 85            Type type when type == typeof(bool) => bool.TryParse(value, out var output) && output is T x ? x : null,
 86            _ => null
 87        };
 88    }
 89#endif
 90}