Files
archived-discord-bot/CompatApiClient/Utils/Utils.cs
13xforever 7fd7d09973 RPCS3 Compatibility Bot reimplemented in C# for .NET Core
RPCS3 Compatibility Bot reimplemented in C# for .NET Core

Current status of this PR:
* tested and targeted for .NET Core 2.1
* all functionality is either on par or improved compared to the python version
* compatibility with current bot.db should be preserved in all upgrade scenarios
* some bot management commands were changed (now under !sudo bot)
* standard help generator for the new discord client is ... different;
  compatibility with old format could be restored through custom formatter if needed
* everything has been split in more loosely tied components for easier extensibility and maintenance
* log parsing has been rewritten and should work ~2x as fast
2018-07-20 09:22:28 +02:00

45 lines
1.2 KiB
C#

using System;
namespace CompatApiClient
{
public static class Utils
{
public static string Trim(this string str, int maxLength)
{
const int minSaneLimit = 4;
if (maxLength < minSaneLimit)
throw new ArgumentException("Argument cannot be less than " + minSaneLimit, nameof(maxLength));
if (string.IsNullOrEmpty(str))
return str;
if (str.Length > maxLength)
return str.Substring(0, maxLength - 3) + "...";
return str;
}
public static string Truncate(this string str, int maxLength)
{
if (maxLength < 1)
throw new ArgumentException("Argument must be positive, but was " + maxLength, nameof(maxLength));
if (string.IsNullOrEmpty(str) || str.Length <= maxLength)
return str;
return str.Substring(0, maxLength);
}
public static string Sanitize(this string str)
{
return str?.Replace("`", "`\u200d").Replace("@", "@\u200d");
}
public static int Clamp(this int amount, int low, int high)
{
return Math.Min(high, Math.Max(amount, low));
}
}
}