Browse Source

TextUtility.Distinct 返回值改为数组。

master
王厅 1 week ago
parent
commit
ac280ea733
  1. 32
      Apewer/TextUtility.cs

32
Apewer/TextUtility.cs

@ -2,6 +2,7 @@
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;
@ -657,18 +658,18 @@ namespace Apewer
} }
/// <summary>对字符串列表去重。指定 valid 参数时将去除无效字符串。</summary> /// <summary>对字符串列表去重。指定 valid 参数时将去除无效字符串。</summary>
public static List<string> Distinct(IEnumerable<string> strings, bool valid = false) /// <param name="strings"></param>
/// <param name="valid"></param>
public static string[] Distinct(IEnumerable<string> strings, bool valid = false)
{ {
if (strings == null) return new List<string>(); if (strings == null) return new string[0];
const string space = " "; const string space = " ";
var count = strings.Count();
var array = new string[count];
var added = 0;
var hasNull = false; var hasNull = false;
var hasEmpty = false; var hasEmpty = false;
var hasSpace = false; var hasSpace = false;
var array = new ArrayBuilder<string>();
foreach (var s in strings) foreach (var s in strings)
{ {
if (s == null) if (s == null)
@ -676,8 +677,7 @@ namespace Apewer
if (valid) continue; if (valid) continue;
if (hasNull) continue; if (hasNull) continue;
hasNull = true; hasNull = true;
array[added] = s; array.Add(s);
added += 1;
continue; continue;
} }
if (s == Empty) if (s == Empty)
@ -685,8 +685,7 @@ namespace Apewer
if (valid) continue; if (valid) continue;
if (hasEmpty) continue; if (hasEmpty) continue;
hasEmpty = true; hasEmpty = true;
array[added] = s; array.Add(s);
added += 1;
continue; continue;
} }
if (s == space) if (s == space)
@ -694,13 +693,12 @@ namespace Apewer
if (valid) continue; if (valid) continue;
if (hasSpace) continue; if (hasSpace) continue;
hasSpace = true; hasSpace = true;
array[added] = s; array.Add(s);
added += 1;
continue; continue;
} }
var exist = false; var exist = false;
for (var i = 0; i < added; i++) for (var i = 0; i < array.Length; i++)
{ {
if (array[i] == s) if (array[i] == s)
{ {
@ -710,14 +708,10 @@ namespace Apewer
} }
if (exist) continue; if (exist) continue;
array[added] = s; array.Add(s);
added += 1;
} }
if (added < 1) return new List<string>(); return array.Export();
var list = new List<string>(added);
for (var i = 0; i < added; i++) list.Add(array[i]);
return list;
} }
/// <summary>约束字符串中的字符,只包含指定的字符。</summary> /// <summary>约束字符串中的字符,只包含指定的字符。</summary>

Loading…
Cancel
Save