| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace Pozitron.Convert; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Conversion extensions to string |
| | | 7 | | /// </summary> |
| | | 8 | | public 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 | | { |
| | 59 | 23 | | if (value is null) return defaultValue; |
| | | 24 | | |
| | 39 | 25 | | 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 | | { |
| | 36 | 37 | | if (value is null) return null; |
| | | 38 | | |
| | 24 | 39 | | 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 | | } |