using System;
using System.Collections.Generic;
namespace JiaZhiQuan.Common
{
public static class Extensions
{
private static Random rng = new Random();
///
/// 随机排列
///
public static void Shuffle(this IList list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
///
/// 如:通过110101,可获得 110101,1101,11
///
///
///
public static List Regions(this string relatedRegionCodes)
{
List list = new List();
if (string.IsNullOrEmpty(relatedRegionCodes))
{
return list;
}
var codes = relatedRegionCodes.Split(',', StringSplitOptions.RemoveEmptyEntries);
foreach (var code in codes)
{
if (code.Length == 6)
{
list.Add(code);
list.Add(code.Substring(0, 4));
list.Add(code.Substring(0, 2));
}
}
return list;
}
public static byte[] FromBase64(this string base64WithoutPadding)
{
var base64 = base64WithoutPadding.Length % 4 == 0
? base64WithoutPadding
: base64WithoutPadding + new string('=', 4 - base64WithoutPadding.Length % 4);
return Convert.FromBase64String(base64.Replace("-", "+").Replace('_', '/'));
}
}
}