Browse Source

修改 TextUtility.Distinct 参数,在匹配时优先于 Linq,并提供扩展方法。

master
王厅 6 days ago
parent
commit
00a1941b40
  1. 146
      Apewer/TextUtility.cs

146
Apewer/TextUtility.cs

@ -2,7 +2,6 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -13,17 +12,27 @@ namespace Apewer
public static class TextUtility public static class TextUtility
{ {
const string LetterChars = LowerCase + UpperCase;
const string BlankChars = "  \n\r\t\f\b\a"; // 在 IsBlank 和 Trim 中视为空白的字符。 const string BlankChars = "  \n\r\t\f\b\a"; // 在 IsBlank 和 Trim 中视为空白的字符。
const string LineFeed = "\r\n"; // 换行符,由 ASCII 13 和 ASCII 10 组成。 const string LineFeed = "\r\n"; // 换行符,由 ASCII 13 和 ASCII 10 组成。
const string SpaceDbc = " ";
const string SpaceSbc = " "; /// <summary>半角空格。</summary>
const string LucidChars = "3456789acefhknpstwxyz"; public const string Space = " ";
const string KeyChars = "0123456789abcdefghijklmnopqrstuvwxyz";
const string HexChars = "0123456789abcdef"; /// <summary>全角空格。</summary>
const string NumericChars = "0123456789"; public const string SpaceSbc = " ";
const string LowerChars = "abcdefghijklmnopqrstuvwxyz";
const string UpperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; /// <summary>十进制字符。</summary>
const string LetterChars = LowerChars + UpperChars; public const string Decimal = "0123456789";
/// <summary>十六进制字符。</summary>
public const string Hexadecimal = "0123456789abcdef";
/// <summary>小写字母。</summary>
public const string LowerCase = "abcdefghijklmnopqrstuvwxyz";
/// <summary>大写字母。</summary>
public const string UpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/// <summary>UTF-8 BOM。</summary> /// <summary>UTF-8 BOM。</summary>
public static byte[] Bom { get => new byte[] { 0xEF, 0xBB, 0xBF }; } public static byte[] Bom { get => new byte[] { 0xEF, 0xBB, 0xBF }; }
@ -31,8 +40,11 @@ namespace Apewer
/// <summary>CRLF。</summary> /// <summary>CRLF。</summary>
public const string CRLF = "\r\n"; public const string CRLF = "\r\n";
/// <summary>CR。</summary>
public const char CR = '\r';
/// <summary>LF。</summary> /// <summary>LF。</summary>
public const string LF = "\n"; public const char LF = '\n';
/// <summary>长度为 0 的空字符串。</summary> /// <summary>长度为 0 的空字符串。</summary>
public const string Empty = ""; public const string Empty = "";
@ -220,9 +232,6 @@ namespace Apewer
return new string(output); return new string(output);
} }
/// <summary>获取指定长的的空格。</summary>
public static string Space(int length) => Duplicate(' ', length);
/// <summary>将文本以转换为字节数组。默认 Encoding 为 UTF-8。</summary> /// <summary>将文本以转换为字节数组。默认 Encoding 为 UTF-8。</summary>
public static byte[] Bytes(string text, Encoding encoding = null) public static byte[] Bytes(string text, Encoding encoding = null)
{ {
@ -650,70 +659,107 @@ namespace Apewer
public static string Random(int length, string pool = "0123456789abcdefghijklmnopqrstuvwxyz") public static string Random(int length, string pool = "0123456789abcdefghijklmnopqrstuvwxyz")
{ {
if (length < 1) return Empty; if (length < 1) return Empty;
if (IsEmpty(pool)) return Duplicate(SpaceDbc, length); if (IsEmpty(pool)) return Duplicate(Space, length);
var array = new char[length]; var array = new char[length];
var max = pool.Length - 1; var max = pool.Length - 1;
for (var i = 0; i < length; i++) array[i] = pool[NumberUtility.Random(0, max)]; for (var i = 0; i < length; i++) array[i] = pool[NumberUtility.Random(0, max)];
return new string(array); return new string(array);
} }
/// <summary>对字符串列表去重。指定 valid 参数时将去除无效字符串。</summary> /// <summary>对字符串集合去重,同时去除 NULL 值和空字符串。</summary>
/// <param name="strings"></param> public static string[] Distinct(this IEnumerable<string> strings) => Distinct(strings, false, false);
/// <param name="valid"></param>
public static string[] Distinct(IEnumerable<string> strings, bool valid = false) /// <summary>对字符串集合去重。</summary>
/// <param name="strings">字符串集合。</param>
/// <param name="withEmpty">保留空字符串。</param>
/// <param name="withNull">保留 NULL 值。</param>
public static string[] Distinct(this IEnumerable<string> strings, bool withEmpty, bool withNull)
{ {
if (strings == null) return new string[0]; if (strings == null) return new string[0];
const string space = " "; // 保留 NULL 和空字符串
var hasNull = false; var @null = false;
var hasEmpty = false; var empty = false;
var hasSpace = false;
var array = new ArrayBuilder<string>(); // 遍历,重组
foreach (var s in strings) var cache = new List<string>();
if (strings is IList<string> list)
{ {
if (s == null) var count = list.Count;
cache.Capacity = count;
for (var i = 0; i < count; i++)
{ {
if (valid) continue; var item = list[i];
if (hasNull) continue;
hasNull = true; if (item == null)
array.Add(s); {
if (withNull && !@null)
{
cache.Add(item);
@null = true;
}
continue; continue;
} }
if (s == Empty) if (item == Empty)
{
if (withEmpty && !empty)
{ {
if (valid) continue; cache.Add(item);
if (hasEmpty) continue; empty = true;
hasEmpty = true; }
array.Add(s);
continue; continue;
} }
if (s == space)
var added = false;
for (var j = 0; j < cache.Count; j++)
{ {
if (valid) continue; if (cache[j] == item)
if (hasSpace) continue; {
hasSpace = true; added = true;
array.Add(s); break;
}
}
if (!added) cache.Add(item);
}
}
else
{
foreach (var item in strings)
{
if (item == null)
{
if (withNull && !@null)
{
cache.Add(item);
@null = true;
}
continue;
}
if (item == Empty)
{
if (withEmpty && !empty)
{
cache.Add(item);
empty = true;
}
continue; continue;
} }
var exist = false; var added = false;
for (var i = 0; i < array.Length; i++) for (var j = 0; j < cache.Count; j++)
{ {
if (array[i] == s) if (cache[j] == item)
{ {
exist = true; added = true;
break; break;
} }
} }
if (exist) continue; if (!added) cache.Add(item);
array.Add(s);
} }
return array.Export();
} }
return cache.ToArray();
}
/// <summary>约束字符串中的字符,只包含指定的字符。</summary> /// <summary>约束字符串中的字符,只包含指定的字符。</summary>
public static string Restrict(string text, char[] chars) public static string Restrict(string text, char[] chars)
{ {
@ -748,7 +794,7 @@ namespace Apewer
public static string RestrictLetters(string text) => Restrict(text, LetterChars.ToCharArray()); public static string RestrictLetters(string text) => Restrict(text, LetterChars.ToCharArray());
/// <summary>约束字符串中的字符,只包含数字。</summary> /// <summary>约束字符串中的字符,只包含数字。</summary>
public static string RestrictNumeric(string text) => Restrict(text, NumericChars.ToCharArray()); public static string RestrictNumeric(string text) => Restrict(text, Decimal.ToCharArray());
/// <summary>返回此字符串的安全键副本,只保留数据记录主键中可能出现的字符,默认限制长度为 255 字符。</summary> /// <summary>返回此字符串的安全键副本,只保留数据记录主键中可能出现的字符,默认限制长度为 255 字符。</summary>
public static string SafeKey(string text, int maxLength = 255) public static string SafeKey(string text, int maxLength = 255)

Loading…
Cancel
Save