From 933f260db2f2a8732e096efc9808977ff90c95d6 Mon Sep 17 00:00:00 2001 From: Michael Krisper <michael.krisper@tugraz.at> Date: Mon, 6 Jul 2015 15:48:01 +0200 Subject: [PATCH] added slice helper method for strings and modulo for integers --- VectoCore/Utils/IntExtensionMethods.cs | 14 +++++++++++--- VectoCore/Utils/StringExtensionMethods.cs | 10 ++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/VectoCore/Utils/IntExtensionMethods.cs b/VectoCore/Utils/IntExtensionMethods.cs index 2381c3bdd3..265ed5e69e 100644 --- a/VectoCore/Utils/IntExtensionMethods.cs +++ b/VectoCore/Utils/IntExtensionMethods.cs @@ -1,6 +1,3 @@ -using System; -using System.Diagnostics.Contracts; - namespace TUGraz.VectoCore.Utils { public static class IntExtensionMethods @@ -34,5 +31,16 @@ namespace TUGraz.VectoCore.Utils { return SIBase<T>.Create(d); } + + /// <summary> + /// Modulo functions which also works on negative Numbers (not like the built-in %-operator which just returns the remainder). + /// </summary> + /// <param name="a"></param> + /// <param name="b"></param> + /// <returns></returns> + public static int Mod(this int a, int b) + { + return (a %= b) < 0 ? a + b : a; + } } } \ No newline at end of file diff --git a/VectoCore/Utils/StringExtensionMethods.cs b/VectoCore/Utils/StringExtensionMethods.cs index c72e97e05a..d0eeff2c14 100644 --- a/VectoCore/Utils/StringExtensionMethods.cs +++ b/VectoCore/Utils/StringExtensionMethods.cs @@ -1,3 +1,4 @@ +using System; using System.Globalization; namespace TUGraz.VectoCore.Utils @@ -8,5 +9,14 @@ namespace TUGraz.VectoCore.Utils { return double.Parse(self, CultureInfo.InvariantCulture); } + + public static string Slice(this string s, int from = 0, int to = int.MaxValue) + { + from = Math.Min(Math.Max(from, -s.Length), s.Length); + from = from < 0 ? from + s.Length : from; + to = Math.Min(Math.Max(to, -s.Length), s.Length); + to = to < 0 ? to + s.Length : to; + return s.Substring(from, Math.Max(to - from, 0)); + } } } \ No newline at end of file -- GitLab