Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 933f260d authored by Michael KRISPER's avatar Michael KRISPER
Browse files

added slice helper method for strings and modulo for integers

parent 1e74af8b
No related branches found
No related tags found
No related merge requests found
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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment