Merge pull request #122 from 13xforever/vnext

No fun allowed
This commit is contained in:
Ilya 2018-11-06 22:27:13 +05:00 committed by GitHub
commit 110f16a902
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 1 deletions

View File

@ -36,9 +36,16 @@ namespace CompatBot.Commands
[Description("Searches the compatibility database, USE: !compat search term")]
public async Task Compat(CommandContext ctx, [RemainingText, Description("Game title to look up")] string title)
{
title = title?.TrimEager().Truncate(40);
if (string.IsNullOrEmpty(title))
{
await ctx.ReactWithAsync(Config.Reactions.Failure, "You should specify what you're looking for").ConfigureAwait(false);
return;
}
try
{
var requestBuilder = RequestBuilder.Start().SetSearch(title?.Truncate(40));
var requestBuilder = RequestBuilder.Start().SetSearch(title);
await DoRequestAndRespond(ctx, requestBuilder).ConfigureAwait(false);
}
catch (Exception e)

View File

@ -1,5 +1,6 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -12,6 +13,20 @@ namespace CompatBot.Utils
.GetEncoding()
?? Encoding.ASCII;
private static readonly Encoding Utf8 = new UTF8Encoding(false);
private static readonly HashSet<char> SpaceCharacters = new HashSet<char>
{
'\u00a0',
'\u2002', '\u2003', '\u2004', '\u2005', '\u2006',
'\u2007', '\u2008', '\u2009', '\u200a', '\u200b',
'\u200c', '\u200d', '\u200e', '\u200f',
'\u2028', '\u2029', '\u202a', '\u202b', '\u202c',
'\u202c', '\u202d', '\u202e', '\u202f',
'\u205f', '\u2060', '\u2061', '\u2062', '\u2063',
'\u2064', '\u2065', '\u2066', '\u2067', '\u2068',
'\u2069', '\u206a', '\u206b', '\u206c', '\u206d',
'\u206e', '\u206f',
'\u3000', '\u303f',
};
public static string StripQuotes(this string str)
{
@ -23,6 +38,24 @@ namespace CompatBot.Utils
return str;
}
public static string TrimEager(this string str)
{
if (string.IsNullOrEmpty(str))
return str;
int end = str.Length - 1;
int start = 0;
for (start = 0; start < str.Length; start++)
if (!char.IsWhiteSpace(str[start]) && !IsFormat(str[start]))
break;
for (end = str.Length - 1; end >= start; end--)
if (!char.IsWhiteSpace(str[end]) && !IsFormat(str[end]))
break;
return CreateTrimmedString(str, start, end);
}
public static string AsString(this ReadOnlySequence<byte> buffer, Encoding encoding = null)
{
encoding = encoding ?? Latin8BitEncoding;
@ -61,5 +94,19 @@ namespace CompatBot.Utils
public static string GetSuffix(long num) => num % 10 == 1 && num % 100 != 11 ? "" : "s";
public static string FixSpaces(this string text) => text?.Replace(" ", " \u200d \u200d");
private static bool IsFormat(char c) => SpaceCharacters.Contains(c);
private static string CreateTrimmedString(string str, int start, int end)
{
int len = end - start + 1;
if (len == str.Length)
return str;
if (len == 0)
return "";
return str.Substring(start, len);
}
}
}