using Apewer.Internals;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Apewer
{
/// 文本实用工具。
public static class TextUtility
{
const string BlankChars = " \n\r\t\f\b\a"; // 在 IsBlank 和 Trim 中视为空白的字符。
const string LineFeed = "\r\n"; // 换行符,由 ASCII 13 和 ASCII 10 组成。
const string SpaceDbc = " ";
const string SpaceSbc = " ";
const string LucidChars = "3456789acefhknpstwxyz";
const string KeyChars = "0123456789abcdefghijklmnopqrstuvwxyz";
const string HexChars = "0123456789abcdef";
const string NumericChars = "0123456789";
const string LowerChars = "abcdefghijklmnopqrstuvwxyz";
const string UpperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string LetterChars = LowerChars + UpperChars;
/// UTF-8 BOM。
public static byte[] Bom { get => new byte[] { 0xEF, 0xBB, 0xBF }; }
/// CRLF。
public const string CRLF = "\r\n";
/// LF。
public const string LF = "\n";
/// 空文本。
public const string Empty = "";
/// 返回表示指定对象的字符串。
public static string Text(object value)
{
if (value is string str) return str;
if (value == null) return null;
if (value.Equals(DBNull.Value)) return null;
if (value is Type t) return t.Name;
if (value is char[] chars) return new string(chars);
var type = value.GetType();
var toString = type.GetMethod(nameof(object.ToString), Type.EmptyTypes);
if (toString.DeclaringType.Equals(type))
{
try { return value.ToString(); }
catch { return null; }
}
return "<" + type.Name + ">";
}
/// 字符串为空。
public static bool IsEmpty(string text) => text == null || text == Empty;
/// 字符串不为空。
public static bool NotEmpty(string text) => text != null && text != Empty;
/// 字符串为空,或只含有空白字符。
public static bool IsBlank(string text)
{
if (IsEmpty(text)) return true;
var length = text.Length;
var bcs = BlankChars.ToCharArray();
var bcl = bcs.Length;
bool b;
char c;
for (var i = 0; i < length; i++)
{
c = text[i];
b = false;
for (var j = 0; j < bcl; j++)
{
if (c == bcs[j])
{
b = true;
break;
}
}
if (!b) return false;
}
return true;
}
/// 字符串不为空,切含有非空白字符。
public static bool NotBlank(string text) => !IsBlank(text);
/// 获取文本的 Int32 哈希。
private static int HashCode(string text)
{
if (text == null) return 0;
int hash = 0;
var length = text.Length;
for (int i = 0; i < length; i++)
{
hash = 31 * hash + text[i];
}
return hash;
}
private static string PrivateJoin(string separator, IEnumerable cells)
{
if (cells == null) return Empty;
if (cells is string str) return str ?? "";
var sb = new StringBuilder();
var first = true;
var hasSeparator = !string.IsNullOrEmpty(separator);
foreach (var cell in cells)
{
if (cell == null) continue;
var text = null as string;
if (cell is string) text = cell as string;
else if (cell is Type type) text = type.Name;
else cell.ToString();
if (string.IsNullOrEmpty(text)) continue;
if (!first && hasSeparator) sb.Append(separator);
first = false;
sb.Append(text);
}
var result = sb.ToString();
return result;
}
/// 合并为字符串。
public static string Merge(params object[] cells) => Join(null, cells);
/// 合并为字符串。
public static string Join(string separator, params object[] cells)
{
if (cells == null) return Empty;
while (cells.Length == 1)
{
var first = cells[0];
if (first.IsNull()) return Empty;
if (first is string str) return str ?? Empty;
if (first is IEnumerable chars)
{
var list = new List();
foreach (var @char in chars) list.Add(@char);
return new string(list.ToArray());
}
if (!first.GetType().IsValueType && first is IEnumerable enumerable)
{
var list = new List