From e1a90a48a49ba134050475fe61f5f30e3ff215c6 Mon Sep 17 00:00:00 2001 From: Gregor Mohorko Date: Sun, 28 Jun 2020 12:44:31 +0200 Subject: [PATCH] Added ParseUtility, DateTimeUtility.IsDateTime, StringUtility.TakeS. --- LICENSE.md | 2 +- src/GM.Utility/GM.Utility/DateTimeUtility.cs | 13 +++- src/GM.Utility/GM.Utility/DecimalUtility.cs | 7 +- src/GM.Utility/GM.Utility/DoubleUtility.cs | 9 ++- src/GM.Utility/GM.Utility/GM.Utility.csproj | 10 +-- .../GM.Utility/IEnumerableUtility.cs | 3 + src/GM.Utility/GM.Utility/ParseUtility.cs | 73 +++++++++++++++++++ .../GM.Utility/ReflectionUtility.cs | 3 +- src/GM.Utility/GM.Utility/StringUtility.cs | 40 ++++++++++ 9 files changed, 144 insertions(+), 16 deletions(-) create mode 100644 src/GM.Utility/GM.Utility/ParseUtility.cs diff --git a/LICENSE.md b/LICENSE.md index b731fbc..8c96c45 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 Gregor Mohorko +Copyright (c) 2020 Gregor Mohorko Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/GM.Utility/GM.Utility/DateTimeUtility.cs b/src/GM.Utility/GM.Utility/DateTimeUtility.cs index 065d82f..150ff31 100644 --- a/src/GM.Utility/GM.Utility/DateTimeUtility.cs +++ b/src/GM.Utility/GM.Utility/DateTimeUtility.cs @@ -1,7 +1,7 @@ /* MIT License -Copyright (c) 2017 Grega Mohorko +Copyright (c) 2020 Gregor Mohorko Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,7 +23,7 @@ MIT License Project: GM.Utility Created: 2017-10-27 -Author: Grega Mohorko +Author: Gregor Mohorko */ using System; @@ -117,6 +117,15 @@ public static bool IsCurrentMonth(this DateTime date) return DateTime.Today.Month == date.Month; } + /// + /// Determines whether the provided text represents a valid . + /// + /// A string, possibly representing a valid date and time. + public static bool IsDateTime(string text) + { + return DateTime.TryParse(text, out _); + } + /// /// Determines whether this date is in the future. Ignores time. /// diff --git a/src/GM.Utility/GM.Utility/DecimalUtility.cs b/src/GM.Utility/GM.Utility/DecimalUtility.cs index 2af1e7d..9d29efd 100644 --- a/src/GM.Utility/GM.Utility/DecimalUtility.cs +++ b/src/GM.Utility/GM.Utility/DecimalUtility.cs @@ -77,10 +77,11 @@ public static decimal GetDecimals(this decimal value, int decimalCount) /// /// The decimal value. /// Number of decimals to get. - [Obsolete("This method is obsolete and will be removed in the next release, please use GetDecimalPart(decimal value, int decimalCount, bool round).", false)] + [Obsolete("This method is obsolete and will be removed in the next releases, please use GetDecimalPart(this decimal, int, bool).", false)] public static int GetDecimalPart(this decimal value, int decimalCount) { - // FIXME obsolete 2019-06-15 + // FIXME obsolete v1.3.0.0 + // 2020-06-28 return GetDecimalPart(value, decimalCount, false); } @@ -137,7 +138,7 @@ public static int GetWholePart(this decimal value) /// The text. public static bool IsDecimal(string text) { - return decimal.TryParse(text, out decimal value); + return decimal.TryParse(text, out _); } /// diff --git a/src/GM.Utility/GM.Utility/DoubleUtility.cs b/src/GM.Utility/GM.Utility/DoubleUtility.cs index c07785d..6d659b7 100644 --- a/src/GM.Utility/GM.Utility/DoubleUtility.cs +++ b/src/GM.Utility/GM.Utility/DoubleUtility.cs @@ -1,7 +1,7 @@ /* MIT License -Copyright (c) 2017 Grega Mohorko +Copyright (c) 2020 Gregor Mohorko Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,7 +23,7 @@ MIT License Project: GM.Utility Created: 2017-10-27 -Author: Grega Mohorko +Author: Gregor Mohorko */ using System; @@ -79,10 +79,11 @@ public static double GetDecimals(this double value, int decimalCount) /// /// The double value. /// Number of decimals to get. - [Obsolete("This method is obsolete and will be removed in next releases, please use GetDecimalPart(double value, int decimalCount, bool round).", false)] + [Obsolete("This method is obsolete and will be removed in next releases, please use GetDecimalPart(this double, int, bool).", false)] public static int GetDecimalPart(this double value, int decimalCount) { - // FIXMe obsolete 2019-06-15 + // FIXMe obsolete v1.3.0.0 + // 2020-06-28 return GetDecimalPart(value, decimalCount, false); } diff --git a/src/GM.Utility/GM.Utility/GM.Utility.csproj b/src/GM.Utility/GM.Utility/GM.Utility.csproj index b719258..13f88cc 100644 --- a/src/GM.Utility/GM.Utility/GM.Utility.csproj +++ b/src/GM.Utility/GM.Utility/GM.Utility.csproj @@ -5,7 +5,7 @@ true false GM.StrongNameKey.snk - 1.2.9.0 + 1.3.0.0 GM.Utility Gregor Mohorko Gregor Mohorko @@ -14,11 +14,11 @@ icon.png utility extensions software design pattern Library with various static classes and tools that provide universally useful functions, extensions and utilities. - Copyright © Gregor Mohorko 2019 + Copyright © Gregor Mohorko 2020 true - Added methods: DictionaryUtility.GetOrCreate; IEnumerableUtility.ForEach; ReflectionUtility.GetProperyValueFromPath; StatisticUtility.CalculateCV, CalculateRSD. - 1.2.9.0 - 1.2.9.0 + Enabled SourceLink. Added utility classes: ExcelUtility, ExceptionUtility, ListUtility, ParseUtility, TaskUtility. Added utility methods: ColorUtility.ScaleLinear; CsvUtility.SurroundWithDoubleQuotes; DateTimeUtility.IsDateTime; DecimalUtility.GetDecimals(decimalCount), ToStringFixedLength, ToStringShortest; DictionaryUtility.AddOrCreate; IEnumerableUtility.AllSame, MaxOrZero, ToObservableCollection; IOUtility.SetAs(Not)ReadOnly; PathUtility.IsNetworkDrive; StatisticUtility.DifferencePercentage, RSD; ReflectionUtility.GetValue, SetValue; StringUtility.ContainsWhitespace, OccurencesOf, TakeS, ToPascalCase, ToSentenceCase, ToSingleLine, ToTitleCase, ToUpperCaseFirstLetterOnly; XMLUtility.(De)Serialize(To/From File). + 1.3.0.0 + 1.3.0.0 https://github.com/GregaMohorko/GM.Utility Git LICENSE.md diff --git a/src/GM.Utility/GM.Utility/IEnumerableUtility.cs b/src/GM.Utility/GM.Utility/IEnumerableUtility.cs index a2d0a3e..d2d1d7c 100644 --- a/src/GM.Utility/GM.Utility/IEnumerableUtility.cs +++ b/src/GM.Utility/GM.Utility/IEnumerableUtility.cs @@ -45,6 +45,7 @@ public static class IEnumerableUtility { /// /// Determines whether all elements in this collection are equal to each other. Compares using the method. + /// This method returns true if the collection is empty or it contains only one element. /// /// The type of the elements. /// The collection. @@ -62,6 +63,7 @@ public static bool AllSame(this IEnumerable enumerable) /// /// Determines whether all elements in this collection produce the same value with the provided value selector. Compares using the method. + /// This method returns true if the collection is empty or it contains only one element. /// /// The type of the elements. /// The type of the values to compare. @@ -84,6 +86,7 @@ public static bool AllSame(this IEnumerable enumerable) /// /// Determines whether all elements in this collection produce the same values with the provided value selectors. Compares using the method. + /// This method returns true if the collection is empty or it contains only one element. /// /// The type of the elements. /// The type of the first values to compare. diff --git a/src/GM.Utility/GM.Utility/ParseUtility.cs b/src/GM.Utility/GM.Utility/ParseUtility.cs new file mode 100644 index 0000000..723bfbe --- /dev/null +++ b/src/GM.Utility/GM.Utility/ParseUtility.cs @@ -0,0 +1,73 @@ +/* +MIT License + +Copyright (c) 2020 Gregor Mohorko + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Project: GM.Utility +Created: 2020-6-28 +Author: Gregor Mohorko +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace GM.Utility +{ + /// + /// Utilities for parsing string. + /// + public static class ParseUtility + { + /// + /// Converts the string representation of to its equivalent. + /// Uses . + /// + /// The type of object to return. + /// A string containing the value to convert. + /// + /// + public static T ParseTo(this string value) + { + return (T)Convert.ChangeType(value, typeof(T)); + } + + /// + /// Tries to convert the specified string representation of to its equivalent. A return value indicates whether the conversion succeeded or failed. + /// + /// The type of object to return. + /// A string containing the value to convert. + /// The result of the parse operation. + public static bool TryParseTo(this string value, out T result) + { + try { + result = ParseTo(value); + } catch(Exception e) { + if(e is InvalidCastException || e is FormatException) { + result = default; + return false; + } + throw; + } + return true; + } + } +} diff --git a/src/GM.Utility/GM.Utility/ReflectionUtility.cs b/src/GM.Utility/GM.Utility/ReflectionUtility.cs index edaf629..95ab556 100644 --- a/src/GM.Utility/GM.Utility/ReflectionUtility.cs +++ b/src/GM.Utility/GM.Utility/ReflectionUtility.cs @@ -421,7 +421,8 @@ public static object GetFieldValue(this object obj, string fieldName) [Obsolete("This method is obsolete and will be removed in next releases, please use GetValueFromPath.", false)] public static object GetPropertyValueFromPath(object obj, string propertyPath) { - // FIXME obsolete 2019-10-08 + // FIXME obsolete v1.3.0.0 + // 2019-10-08 return GetValueFromPath(obj, propertyPath); } diff --git a/src/GM.Utility/GM.Utility/StringUtility.cs b/src/GM.Utility/GM.Utility/StringUtility.cs index af60f7a..2fdbbba 100644 --- a/src/GM.Utility/GM.Utility/StringUtility.cs +++ b/src/GM.Utility/GM.Utility/StringUtility.cs @@ -142,6 +142,9 @@ public static string RemoveFirstOf(this string text, string value) /// The text. public static string RemoveWhitespace(this string text) { + // TESTME + // what is faster? + //return new string(text.Where(c => !char.IsWhiteSpace(c)).ToArray()); return RegexWhitespace.Replace(text, ""); } @@ -222,6 +225,20 @@ public static string ShortenWith3Dots(this string text, int maxLength) return text.Substring(0, maxLength) + "..."; } + /// + /// Retrieves a substring from this instance. Starts at the beginnning and takes the specified number of characters. + /// If is bigger than the length of this string, a copy of this instance is returned. + /// + /// The text. + /// The number of characters to take. + public static string TakeS(this string text, int count) + { + if(text == null) { + throw new ArgumentNullException(nameof(text)); + } + return new string(text.Take(count).ToArray()); + } + private static readonly Lazy REGEX_SENTENCEORTITLE = new Lazy(() => new Regex("(^| )[A-z]", RegexOptions.Compiled)); /// @@ -297,6 +314,29 @@ public static string ToTitleCase(this string text) } TextInfo textInfo = CultureInfo.InvariantCulture.TextInfo; return textInfo.ToTitleCase(text); + + // old implementation + /* + var sb = new StringBuilder(); + var word = new StringBuilder(); + foreach(char c in text) { + if(!char.IsWhiteSpace(c)) { + word.Append(c); + continue; + } + if(word.Length > 0) { + string titleCaseWord = word.ToString().ToLowerInvariant().ToUpperFirstLetter(); + sb.Append(titleCaseWord); + } + sb.Append(c); + word.Clear(); + } + if(word.Length > 0) { + string titleCaseWord = word.ToString().ToLowerInvariant().ToUpperFirstLetter(); + sb.Append(titleCaseWord); + } + return sb.ToString(); + //*/ } ///