2020-02-12 15:16:19 +01:00
using System.Security.Cryptography ;
2020-07-07 20:57:22 +02:00
using System.Text ;
2020-02-20 22:57:37 +01:00
using System.Text.RegularExpressions ;
2020-02-12 15:16:19 +01:00
2021-11-26 21:10:56 -05:00
namespace PluralKit.Core ;
public static class StringUtils
2020-02-12 15:16:19 +01:00
{
2021-11-26 21:10:56 -05:00
public static string GenerateToken ( )
2020-02-12 15:16:19 +01:00
{
2021-11-26 21:10:56 -05:00
// Results in a 64-byte Base64 string (no padding)
var buf = RandomNumberGenerator . GetBytes ( 48 ) ;
return Convert . ToBase64String ( buf ) ;
}
2021-08-27 11:03:47 -04:00
2021-11-26 21:10:56 -05:00
public static bool IsLongerThan ( this string str , int length )
{
if ( str ! = null ) return str . Length > length ;
return false ;
}
2021-08-27 11:03:47 -04:00
2021-11-26 21:10:56 -05:00
public static string ExtractCountryFlag ( string flag )
{
if ( flag . Length ! = 4 ) return null ;
try
2020-02-12 15:16:19 +01:00
{
2021-11-26 21:10:56 -05:00
var cp1 = char . ConvertToUtf32 ( flag , 0 ) ;
var cp2 = char . ConvertToUtf32 ( flag , 2 ) ;
if ( cp1 < 0x1F1E6 | | cp1 > 0x1F1FF ) return null ;
if ( cp2 < 0x1F1E6 | | cp2 > 0x1F1FF ) return null ;
return $"{(char)(cp1 - 0x1F1E6 + 'A')}{(char)(cp2 - 0x1F1E6 + 'A')}" ;
2020-02-12 15:16:19 +01:00
}
2021-11-26 21:10:56 -05:00
catch ( ArgumentException )
2020-02-12 23:18:31 +01:00
{
2021-11-26 21:10:56 -05:00
return null ;
2020-02-12 23:18:31 +01:00
}
2021-11-26 21:10:56 -05:00
}
2020-02-20 22:57:37 +01:00
2021-11-26 21:10:56 -05:00
public static string NullIfEmpty ( this string input )
{
if ( input = = null ) return null ;
if ( input . Trim ( ) . Length = = 0 ) return null ;
return input ;
}
2020-07-07 20:57:22 +02:00
2021-11-26 21:10:56 -05:00
public static bool EmptyOrNull ( this string input )
{
if ( input = = null ) return true ;
if ( input . Trim ( ) . Length = = 0 ) return true ;
return false ;
}
2020-07-07 20:57:22 +02:00
2021-11-26 21:10:56 -05:00
public static string NormalizeLineEndSpacing ( this string input ) = >
// iOS has a weird issue on embeds rendering newlines when there are spaces *just before* it
2023-02-05 01:20:52 +13:00
// so we remove 'em - except in the case where there's a blockquote line that contains only
// spaces (if we removed all the spaces there, the blockquote breaks horribly)
Regex . Replace ( input , "(?<!\\>) *\n" , "\n" ) ;
2020-07-07 20:57:22 +02:00
2021-11-26 21:10:56 -05:00
public static IReadOnlyList < string > JoinPages ( IEnumerable < string > input , int characterLimit ) = >
JoinPages ( input , _ = > characterLimit ) ;
2021-08-27 11:03:47 -04:00
2021-11-26 21:10:56 -05:00
public static IReadOnlyList < string > JoinPages ( IEnumerable < string > input , Func < int , int > characterLimitByPage )
{
var output = new List < string > ( ) ;
2020-07-07 20:57:22 +02:00
2021-11-26 21:10:56 -05:00
var buf = new StringBuilder ( ) ;
foreach ( var s in input )
{
var limit = characterLimitByPage . Invoke ( output . Count ) ;
2021-08-27 11:03:47 -04:00
2021-11-26 21:10:56 -05:00
// Would adding this string put us over the limit?
// (note: don't roll over if the buffer's already empty; this means an individual section is above the character limit. todo: truncate, then?)
if ( buf . Length > 0 & & buf . Length + s . Length > limit )
{
// If so, "roll over" (before adding the string to the buffer)
2020-07-07 20:57:22 +02:00
output . Add ( buf . ToString ( ) ) ;
2021-11-26 21:10:56 -05:00
buf . Clear ( ) ;
}
2020-07-07 20:57:22 +02:00
2021-11-26 21:10:56 -05:00
buf . Append ( s ) ;
2021-08-27 11:03:47 -04:00
}
2021-11-26 21:10:56 -05:00
// We most likely have something left over, so add that in too
if ( buf . Length > 0 )
output . Add ( buf . ToString ( ) ) ;
return output ;
2020-02-12 15:16:19 +01:00
}
2024-10-22 03:05:32 -04:00
// Lightweight formatting that intentionally is very basic to not have silly things like in-template for loops like other templating engines seem to have
// Currently doesn't handle escapes which might cause problems
public static string SafeFormat ( string template , ( string pattern , string arg ) [ ] args ) = >
args
. Aggregate ( template , ( acc , x ) = > acc . Replace ( x . pattern , x . arg ) ) ;
2020-02-12 15:16:19 +01:00
}