diff options
Diffstat (limited to 'WorldlineKeepers/Assets/ThirdParty/StringUtil')
14 files changed, 2121 insertions, 0 deletions
diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/DoubleVString.cs b/WorldlineKeepers/Assets/ThirdParty/StringUtil/DoubleVString.cs new file mode 100644 index 0000000..5d9c6e8 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/DoubleVString.cs @@ -0,0 +1,37 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + + +/// <summary> +/// 给UI.Text使用的double buffer string +/// </summary> +public class DoubleVString +{ + private int index; + private VString stringA; + private VString stringB; + + public DoubleVString(int maxCount) + { + stringA = new VString(maxCount); + stringB = new VString(maxCount); + } + + public VString GetCurrentVString() + { + return (index % 2) == 0 ? stringA : stringB; + } + + public VString GetNextVString() + { + return (index % 2) == 0 ? stringB : stringA; + } + + + //交换current 和Next string对象 + public void SwapVString() + { + index = (index + 1) % 2; + } +}
\ No newline at end of file diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/DoubleVString.cs.meta b/WorldlineKeepers/Assets/ThirdParty/StringUtil/DoubleVString.cs.meta new file mode 100644 index 0000000..5e79416 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/DoubleVString.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 9a865a66c8bd9444f9b481e3a62e500e +timeCreated: 1526353967 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil.cs b/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil.cs new file mode 100644 index 0000000..1b885bd --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil.cs @@ -0,0 +1,750 @@ +using UnityEngine; +using System.Collections; +using System.Text; +using System; +using System.Collections.Generic; + +public partial class StringUtil +{ + //自定义字符串函数公用的StringBuilder + static StringBuilder _customSB = new StringBuilder(); + //共享的StringBuilder + static StringBuilder shareSB = new StringBuilder(); + + #region Concat + /// <summary> + /// 拼接字符串(2个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <returns></returns> + public static string Concat(string a1, string a2) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(3个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <param name="a3"></param> + /// <returns></returns> + public static string Concat(string a1, string a2, string a3) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(4个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <param name="a3"></param> + /// <param name="a4"></param> + /// <returns></returns> + public static string Concat(string a1, string a2, string a3, string a4) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(5个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <param name="a3"></param> + /// <param name="a4"></param> + /// <param name="a5"></param> + /// <returns></returns> + public static string Concat(string a1, string a2, string a3, string a4, string a5) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(6个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <param name="a3"></param> + /// <param name="a4"></param> + /// <param name="a5"></param> + /// <param name="a6"></param> + /// <returns></returns> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(7个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <param name="a3"></param> + /// <param name="a4"></param> + /// <param name="a5"></param> + /// <param name="a6"></param> + /// <param name="a7"></param> + /// <returns></returns> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(8个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <param name="a3"></param> + /// <param name="a4"></param> + /// <param name="a5"></param> + /// <param name="a6"></param> + /// <param name="a7"></param> + /// <param name="a8"></param> + /// <returns></returns> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(9个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <param name="a3"></param> + /// <param name="a4"></param> + /// <param name="a5"></param> + /// <param name="a6"></param> + /// <param name="a7"></param> + /// <param name="a8"></param> + /// <param name="a9"></param> + /// <returns></returns> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8, string a9) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + _customSB.Append(a9); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(10个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <param name="a3"></param> + /// <param name="a4"></param> + /// <param name="a5"></param> + /// <param name="a6"></param> + /// <param name="a7"></param> + /// <param name="a8"></param> + /// <param name="a9"></param> + /// <param name="a10"></param> + /// <returns></returns> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8, string a9, string a10) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + _customSB.Append(a9); + _customSB.Append(a10); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(11个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <param name="a3"></param> + /// <param name="a4"></param> + /// <param name="a5"></param> + /// <param name="a6"></param> + /// <param name="a7"></param> + /// <param name="a8"></param> + /// <param name="a9"></param> + /// <param name="a10"></param> + /// <param name="a11"></param> + /// <returns></returns> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8, string a9, string a10, + string a11) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + _customSB.Append(a9); + _customSB.Append(a10); + _customSB.Append(a11); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(12个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <param name="a3"></param> + /// <param name="a4"></param> + /// <param name="a5"></param> + /// <param name="a6"></param> + /// <param name="a7"></param> + /// <param name="a8"></param> + /// <param name="a9"></param> + /// <param name="a10"></param> + /// <param name="a11"></param> + /// <param name="a12"></param> + /// <returns></returns> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8, string a9, string a10, + string a11, string a12) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + _customSB.Append(a9); + _customSB.Append(a10); + _customSB.Append(a11); + _customSB.Append(a12); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(13个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <param name="a3"></param> + /// <param name="a4"></param> + /// <param name="a5"></param> + /// <param name="a6"></param> + /// <param name="a7"></param> + /// <param name="a8"></param> + /// <param name="a9"></param> + /// <param name="a10"></param> + /// <param name="a11"></param> + /// <param name="a12"></param> + /// <param name="a13"></param> + /// <returns></returns> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8, string a9, string a10, + string a11, string a12, string a13) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + _customSB.Append(a9); + _customSB.Append(a10); + _customSB.Append(a11); + _customSB.Append(a12); + _customSB.Append(a13); + return _customSB.ToString(); + } + + /// <summary> + /// 拼接字符串(14个) + /// </summary> + /// <param name="a1"></param> + /// <param name="a2"></param> + /// <param name="a3"></param> + /// <param name="a4"></param> + /// <param name="a5"></param> + /// <param name="a6"></param> + /// <param name="a7"></param> + /// <param name="a8"></param> + /// <param name="a9"></param> + /// <param name="a10"></param> + /// <param name="a11"></param> + /// <param name="a12"></param> + /// <param name="a13"></param> + /// <param name="a14"></param> + /// <returns></returns> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8, string a9, string a10, + string a11, string a12, string a13, string a14) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + _customSB.Append(a9); + _customSB.Append(a10); + _customSB.Append(a11); + _customSB.Append(a12); + _customSB.Append(a13); + _customSB.Append(a14); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(15个) + /// </summary> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8, string a9, string a10, + string a11, string a12, string a13, string a14, string a15) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + _customSB.Append(a9); + _customSB.Append(a10); + _customSB.Append(a11); + _customSB.Append(a12); + _customSB.Append(a13); + _customSB.Append(a14); + _customSB.Append(a15); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(16个) + /// </summary> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8, string a9, string a10, + string a11, string a12, string a13, string a14, string a15, + string a16) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + _customSB.Append(a9); + _customSB.Append(a10); + _customSB.Append(a11); + _customSB.Append(a12); + _customSB.Append(a13); + _customSB.Append(a14); + _customSB.Append(a15); + _customSB.Append(a16); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(17个) + /// </summary> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8, string a9, string a10, + string a11, string a12, string a13, string a14, string a15, + string a16, string a17) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + _customSB.Append(a9); + _customSB.Append(a10); + _customSB.Append(a11); + _customSB.Append(a12); + _customSB.Append(a13); + _customSB.Append(a14); + _customSB.Append(a15); + _customSB.Append(a16); + _customSB.Append(a17); + return _customSB.ToString(); + } + + /// <summary> + /// 拼接字符串(18个) + /// </summary> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8, string a9, string a10, + string a11, string a12, string a13, string a14, string a15, + string a16, string a17, string a18) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + _customSB.Append(a9); + _customSB.Append(a10); + _customSB.Append(a11); + _customSB.Append(a12); + _customSB.Append(a13); + _customSB.Append(a14); + _customSB.Append(a15); + _customSB.Append(a16); + _customSB.Append(a17); + _customSB.Append(a18); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(19个) + /// </summary> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8, string a9, string a10, + string a11, string a12, string a13, string a14, string a15, + string a16, string a17, string a18, string a19) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + _customSB.Append(a9); + _customSB.Append(a10); + _customSB.Append(a11); + _customSB.Append(a12); + _customSB.Append(a13); + _customSB.Append(a14); + _customSB.Append(a15); + _customSB.Append(a16); + _customSB.Append(a17); + _customSB.Append(a18); + _customSB.Append(a19); + return _customSB.ToString(); + } + /// <summary> + /// 拼接字符串(20个) + /// </summary> + public static string Concat(string a1, string a2, string a3, string a4, string a5, + string a6, string a7, string a8, string a9, string a10, + string a11, string a12, string a13, string a14, string a15, + string a16, string a17, string a18, string a19, string a20) + { + _customSB.Remove(0, _customSB.Length); + _customSB.Append(a1); + _customSB.Append(a2); + _customSB.Append(a3); + _customSB.Append(a4); + _customSB.Append(a5); + _customSB.Append(a6); + _customSB.Append(a7); + _customSB.Append(a8); + _customSB.Append(a9); + _customSB.Append(a10); + _customSB.Append(a11); + _customSB.Append(a12); + _customSB.Append(a13); + _customSB.Append(a14); + _customSB.Append(a15); + _customSB.Append(a16); + _customSB.Append(a17); + _customSB.Append(a18); + _customSB.Append(a19); + _customSB.Append(a20); + return _customSB.ToString(); + } + #endregion + + /// <summary> + /// 获得公用的StringBuilder + /// </summary> + /// <returns></returns> + public static StringBuilder GetShareStringBuilder(bool bReset = true) + { + if (bReset) + { + shareSB.Remove(0, shareSB.Length); + } + return shareSB; + } + + /// <summary> + /// 格式化字符串 + /// </summary> + /// <param name="format"></param> + /// <param name="args"></param> + /// <returns></returns> + public static string Format(string format, params object[] args) + { + try + { + _customSB.Remove(0, _customSB.Length); + _customSB.AppendFormat(format, args); + return _customSB.ToString(); + } + catch (Exception e) + { + LogHelper.LogError(e.Message); + return format; + } + } + + /// <summary> + /// 替换\\n 为\n + /// </summary> + /// <param name="baseStr"></param> + /// <returns></returns> + public static string ReplaceNewLineChar(string baseStr) + { + //if (!baseStr.Contains("\\n")) + //{ + // return baseStr; + //} + return baseStr.Replace("\\n", "\n"); + } + + /// <summary> + /// 替换转义字符 + /// </summary> + /// <param name="baseStr"></param> + /// <returns></returns> + public static string ReplaceTranslateChar(string baseStr) + { + baseStr = baseStr.Replace("\\n", "\n"); + baseStr = baseStr.Replace("\\t", "\t"); + baseStr = baseStr.Replace("\\b", " "); + return baseStr; + } + + /// <summary> + /// 替换\\s 为(全角)空格 + /// </summary> + /// <param name="baseStr"></param> + /// <returns></returns> + public static string ReplaceNewBlankSpaceChar(string baseStr) + { + //if (!baseStr.Contains("\\s")) + //{ + // return baseStr; + //} + return baseStr.Replace("\\s", " "); + } + + // 正则匹配空格规则 + static System.Text.RegularExpressions.Regex SpaceRgx = null; + private static string GetSpacePattern() + { + return "\\s(?![a-z]|\\s)"; + } + + public static System.Text.RegularExpressions.Regex GetSpaceRgx() + { + if(SpaceRgx == null) + { + SpaceRgx = new System.Text.RegularExpressions.Regex(GetSpacePattern(), System.Text.RegularExpressions.RegexOptions.IgnoreCase); + } + return SpaceRgx; + } + // 处理英文混排时空格换行的问题 + public static string ProSpace(string value) + { + if (string.IsNullOrEmpty(value)) + return value; + return ProSpaceSp(value); + } + private static string ProSpaceSp(string value) + { + return value.Replace("{sp}", "\u00A0"); + } + private static string ProSpaceNormal(string value) + { + return GetSpaceRgx().Replace(value, "\u00A0"); + } + + + /// <summary> + /// 文本加持颜色 + /// </summary> + /// <param name="color"></param> + /// <param name="text"></param> + /// <returns></returns> + public static string UITextColor(string color, string text) + { + return StringUtil.Format("<color=#{0}>{1}</color>", color, text); + } + + /// <summary> + /// 文本加持颜色 + /// </summary> + /// <param name="color"></param> + /// <param name="text"></param> + /// <returns></returns> + public static string UITextColor(Color color, string text) + { + return UITextColor(ColorUtility.ToHtmlStringRGBA(color), text); + } + + /// <summary> + /// 整数转字符串 + /// </summary> + /// <param name="num"></param> + /// <param name="limit"></param> + /// <param name="param"></param> + /// <returns></returns> + public static string Int2StringLimit(int num, int limit, string param = "") + { + if (num < limit) + { + return num.ToString(param); + } + else + { + return limit.ToString(param); + } + } + + /// <summary> + /// 替换为linux的斜杠 + /// </summary> + /// <param name="str"></param> + /// <returns></returns> + public static string StringSlashOfLinux(string str) + { + return str.Replace('\\', '/'); ; + } + + /// <summary> + /// 替换为win的斜杠 + /// </summary> + /// <param name="str"></param> + /// <returns></returns> + public static string StringSlashOfWin(string str) + { + return str.Replace('/', '\\'); ; + } + + /// <summary> + /// 服务器接收的字符串不可有竖线 + /// </summary> + /// <param name="str"></param> + /// <returns></returns> + public static string ToServerSafeString(string str) + { + return str.Replace("|", "/"); + } + + public static string DictionaryConvertString(Dictionary<int, int> dic) + { + _customSB.Remove(0, _customSB.Length); + if(dic != null) + { + Dictionary<int, int>.Enumerator itor = dic.GetEnumerator(); + while(itor.MoveNext()) + { + _customSB.Append(itor.Current.Key); + _customSB.Append(","); + _customSB.Append(itor.Current.Value); + _customSB.Append(";"); + } + } + return _customSB.ToString(); + } + #region 数字转美式字符串 +#if VERSION_OVERSEA_GAIYA && !(GAIYA_BURNING_GAME ||GAIYA_MYCARD_GAME) + public static string Num2US(float num) + { + return num.ToString("n2"); + } + public static string Num2US(int num) + { + return num.ToString("n0"); + } + public static string Num2US(long num) + { + return num.ToString("n0"); + } +#else + public static string Num2US(float num) + { + return num.ToString("n2").Replace(',',' '); + } + public static string Num2US(int num) + { + return num.ToString("n0").Replace(',', ' '); + } + public static string Num2US(long num) + { + return num.ToString("n0").Replace(',', ' '); + } +#endif + #endregion +}
\ No newline at end of file diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil.cs.meta b/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil.cs.meta new file mode 100644 index 0000000..ad52d51 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 657e94996dc5d074a9944a0c4f8df3c9 +timeCreated: 1493006408 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil_Other.cs b/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil_Other.cs new file mode 100644 index 0000000..d9d0616 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil_Other.cs @@ -0,0 +1,300 @@ +using UnityEngine; +using System.Collections; +using System.Text; +using System; + +public partial class StringUtil +{ + /// <summary> + /// 计算字符串的长度(包含处理中文字符) + /// </summary> + /// <param name="input"></param> + /// <returns></returns> + public static int CalcStringLetterNum(string input) + { + int counter = 0; + for (int i = 0, imax = input.Length; i < imax; ++i) + { + if (IsChineseLetter(input, i)) + { + counter += 2; + } + else + { + counter += 1; + } + } + + return counter; + } + + /// <summary> + /// 截断字符串(包含处理中文字符) + /// </summary> + /// <param name="input"></param> + /// <param name="maxEnglishLength"></param> + /// <returns></returns> + public static string TrancString(string input, int maxEnglishLength) + { + int counter = 0; + for (int i = 0, imax = input.Length; i < imax; ++i) + { + if (IsChineseLetter(input, i)) + { + if (counter <= maxEnglishLength && maxEnglishLength < (counter + 2)) + return input.Substring(0, i); + counter += 2; + } + else + { + if (counter <= maxEnglishLength && maxEnglishLength < (counter + 1)) + return input.Substring(0, i); + counter += 1; + } + } + + return input; + } + + /// <summary> + /// 是不是中文字符 + /// </summary> + /// <param name="input"></param> + /// <param name="index"></param> + /// <returns></returns> + public static bool IsChineseLetter(string input, int index) + { + int code = 0; + int chfrom = System.Convert.ToInt32("4e00", 16); + int chend = System.Convert.ToInt32("9fff", 16); + if (input != "") + { + code = System.Char.ConvertToUtf32(input, index); + + if (code >= chfrom && code <= chend) + { + return true; + } + else + { + return false; + } + } + return false; + } + + /// <summary> + /// 缩减字符串 + /// </summary> + /// <param name="str"></param> + /// <returns></returns> + public static string ShrinkString(string str) + { + StringBuilder sb = new StringBuilder(); + for (int i = 0, imax = str.Length; i < imax; i++) + { + if (str[i] == '\0') + break; + sb.Append(str[i]); + } + + return sb.ToString(); + } + + /// <summary> + /// 自定义的字符串比较 + /// </summary> + /// <param name="left"></param> + /// <param name="right"></param> + /// <returns></returns> + public static bool IsStringEqual(string left, string right) + { + if (System.Object.ReferenceEquals(left, right)) + { + return true; + } + + if (left == null && right == null) + return true; + if (left == null) + return false; + if (right == null) + return false; + + int leftLen = left.Length; + int rightLen = right.Length; + int count = Mathf.Min(leftLen, rightLen); + for (int index = 0; index < count; ++index) + { + char leftChar = left[index]; + char rightChar = right[index]; + if (leftChar != rightChar) + return false; + } + + if (leftLen > count && left[count] == '\0') + return true; + if (rightLen > count && right[count] == '\0') + return true; + if (leftLen == rightLen) + return true; + + return false; + } + + /// <summary> + /// Bytes数组转utf8字符串 + /// </summary> + /// <param name="buffer"></param> + /// <param name="fromIndex"></param> + /// <param name="count"></param> + /// <param name="bufferSize"></param> + /// <returns></returns> + public static string GetUtf8StringFromByteBuffer(ref byte[] buffer, int fromIndex, int count, int bufferSize) + { + if (buffer == null) + return string.Empty; + if (fromIndex < bufferSize) + { + int maxCount = bufferSize - fromIndex; + count = Mathf.Min(maxCount, count); + + string str = ShrinkString(System.Text.Encoding.UTF8.GetString(buffer, fromIndex, count)); + return str; + } + + LogHelper.LogError("fromIndex over flow."); + return ""; + } + + /// <summary> + /// Bytes数组转utf8字符串 + /// </summary> + /// <param name="buffer"></param> + /// <returns></returns> + public static string GetUtf8StringFromByteBuffer(ref byte[] buffer) + { + return GetUtf8StringFromByteBuffer(ref buffer, 0, buffer.Length, buffer.Length); + } + + /// <summary> + /// Utf8字符串转Byte数组 + /// </summary> + /// <param name="srcStr"></param> + /// <param name="srcOffset"></param> + /// <param name="dstBuffer"></param> + /// <param name="dstOffset"></param> + /// <param name="dstBufferSize"></param> + public static void CopyByteBufferFromUtf8String(string srcStr, int srcOffset, ref byte[] dstBuffer, int dstOffset, int dstBufferSize) + { + int copyCount; + CopyByteBufferFromUtf8String(srcStr, srcOffset, ref dstBuffer, dstOffset, dstBufferSize, out copyCount); + } + + /// <summary> + /// Utf8字符串转Byte数组 + /// </summary> + /// <param name="srcStr"></param> + /// <param name="srcOffset"></param> + /// <param name="dstBuffer"></param> + /// <param name="dstOffset"></param> + /// <param name="dstBufferSize"></param> + /// <param name="copyCount"></param> + public static void CopyByteBufferFromUtf8String(string srcStr, int srcOffset, ref byte[] dstBuffer, int dstOffset, int dstBufferSize, out int copyCount) + { + byte[] srcBuffer = System.Text.Encoding.UTF8.GetBytes(srcStr); + int srcLen = srcBuffer.Length; + srcLen = Mathf.Max(srcLen - srcOffset, 0); + int dstMaxCopyCount = dstBufferSize - dstOffset; + dstMaxCopyCount = Mathf.Max(dstMaxCopyCount, 0); + dstMaxCopyCount = Mathf.Min(dstMaxCopyCount, srcLen); + System.Buffer.BlockCopy(srcBuffer, srcOffset, dstBuffer, dstOffset, dstMaxCopyCount); + + copyCount = dstMaxCopyCount; + } + + /// <summary> + /// Byte数组复制 + /// </summary> + /// <param name="srcBuffer"></param> + /// <param name="srcOffset"></param> + /// <param name="dstBuffer"></param> + /// <param name="dstOffset"></param> + /// <param name="dstBufferSize"></param> + public static void CopyByteBuffer(ref byte[] srcBuffer, int srcOffset, ref byte[] dstBuffer, int dstOffset, int dstBufferSize) + { + int copyCount; + CopyByteBuffer(ref srcBuffer, srcOffset, ref dstBuffer, dstOffset, dstBufferSize, out copyCount); + } + + /// <summary> + /// Byte数组复制 + /// </summary> + /// <param name="srcBuffer"></param> + /// <param name="srcOffset"></param> + /// <param name="dstBuffer"></param> + /// <param name="dstOffset"></param> + /// <param name="dstBufferSize"></param> + /// <param name="copyCount"></param> + public static void CopyByteBuffer(ref byte[] srcBuffer, int srcOffset, ref byte[] dstBuffer, int dstOffset, int dstBufferSize, out int copyCount) + { + int srcLen = srcBuffer.Length; + srcLen = Mathf.Max(srcLen - srcOffset, 0); + int dstMaxCopyCount = dstBufferSize - dstOffset; + dstMaxCopyCount = Mathf.Max(dstMaxCopyCount, 0); + dstMaxCopyCount = Mathf.Min(dstMaxCopyCount, srcLen); + System.Buffer.BlockCopy(srcBuffer, srcOffset, dstBuffer, dstOffset, dstMaxCopyCount); + + copyCount = dstMaxCopyCount; + } + + /// <summary> + /// Byte数组转十六进制字符串 + /// </summary> + /// <param name="bytes"></param> + /// <returns></returns> + public static string BytesToHex(byte[] bytes) + { + char[] c = new char[bytes.Length * 2]; + + byte b; + + for (int bx = 0, cx = 0; bx < bytes.Length; ++bx, ++cx) + { + b = ((byte)(bytes[bx] >> 4)); + c[cx] = (char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30); + + b = ((byte)(bytes[bx] & 0x0F)); + c[++cx] = (char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30); + } + + return new string(c); + } + + /// <summary> + /// 十六进制字符串转Byte数组 + /// </summary> + /// <param name="str"></param> + /// <returns></returns> + public static byte[] HexToBytes(string str) + { + if (str.Length == 0 || str.Length % 2 != 0) + return new byte[0]; + + byte[] buffer = new byte[str.Length / 2]; + char c; + for (int bx = 0, sx = 0; bx < buffer.Length; ++bx, ++sx) + { + // Convert first half of byte + c = str[sx]; + buffer[bx] = (byte)((c > '9' ? (c > 'Z' ? (c - 'a' + 10) : (c - 'A' + 10)) : (c - '0')) << 4); + + // Convert second half of byte + c = str[++sx]; + buffer[bx] |= (byte)(c > '9' ? (c > 'Z' ? (c - 'a' + 10) : (c - 'A' + 10)) : (c - '0')); + } + + return buffer; + } + +}
\ No newline at end of file diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil_Other.cs.meta b/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil_Other.cs.meta new file mode 100644 index 0000000..6db4c41 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil_Other.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 5ae4f16a2f4c7254da9d834963922b33 +timeCreated: 1493352224 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/VString.cs b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VString.cs new file mode 100644 index 0000000..9563280 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VString.cs @@ -0,0 +1,618 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + + + +public static class StringExtend +{ + private static volatile object lockThis = new object(); + + public static string ToTempString(this int i) + { + lock (lockThis) + { + return VString.IntToString(i); + } + } + + public static string ToTempString(this float f, int digits = 2) + { + lock (lockThis) + { + return VString.FloatToString(f, digits); + } + } + + public static string ToTempString(this long l) + { + lock (lockThis) + { + return VString.LongToString(l); + } + } + + public static string ToTempStringLower(this string str) + { + lock (lockThis) + { + return VString.ToLower(str); + } + } + + public static string ToTempStringUpper(this string str) + { + lock (lockThis) + { + return VString.ToUpper(str); + } + } + + public static string ToTempSubString(this string str, int index, int count) + { + lock (lockThis) + { + return VString.ToTempSubString(str, index, count); + } + } + + + + #region 转美式数字 + public static string ToStringUS(this float f) + { + return StringUtil.Num2US(f); + } + public static string ToStringUS(this int i) + { + return StringUtil.Num2US(i); + } + public static string ToStringUS(this long i) + { + return StringUtil.Num2US(i); + } + #endregion + +} + + + + + + +/// <summary> +/// 内容可变的字符串 +/// !!!只能作为临时变量使用,绝对不可以在逻辑中存储引用,包含VString和返回的string对象 +/// </summary> +public class VString +{ + private string _data; + private int maxCount; + + private static int _internalVsIndex; + private static VString[] _internalVSArray = new VString[] + { + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64), + new VString(64) + }; + private static string[] digitalNumberArray = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; + + + public VString(int maxCount = 1024) + { + this.maxCount = maxCount + 1; //多加一个,用于留1个给字符串结束符 + _data = new string('\0', this.maxCount); + Clear(); + } + + public string GetString() + { + return _data; + } + + + + /// <summary> + /// int转string,无GC,注意生成的string一定不能进行存贮 + /// </summary> + /// <param name="val"></param> + /// <returns></returns> + public static string IntToString(int val) + { + return LongToString(val); + } + + /// <summary> + /// long转string,无GC,注意生成的string一定不能进行存贮 + /// </summary> + /// <param name="val"></param> + /// <returns></returns> + public static string LongToString(long val) + { + if (val == 0) + { + return "0"; + } + + VString tempVS = GetInternalVString(); + bool isNegative = false; + if (val < 0) + { + val = -val; + isNegative = true; + } + + while (val != 0) + { + long mod = val % 10; + val = val / 10; + tempVS.Push(digitalNumberArray[mod]); + } + + if (isNegative) + { + tempVS.Push("-"); + } + + tempVS.ReverseString(); + return tempVS.GetString(); + } + + /// <summary> + /// float转string,无GC,注意生成的string一定不能进行存贮 + /// </summary> + /// <param name="f"></param> + /// <param name="digits">小数的位数</param> + /// <returns></returns> + public static string FloatToString(float f, int digits = 2) + { + bool isNegative = false; + if (f < 0) + { + f = -f; + isNegative = true; + } + + int iPart = Mathf.FloorToInt(f); + float fPart = f - iPart; + + VString tempVS0 = GetInternalVString(); + + + if (iPart != 0) + { + while (iPart != 0) + { + long mod = iPart % 10; + iPart = iPart / 10; + tempVS0.Push(digitalNumberArray[mod]); + } + } + else + { + tempVS0.Push("0"); + } + + if (isNegative) + { + tempVS0.Push("-"); + } + tempVS0.ReverseString(); + + + if (digits != 0) + { + VString tempVS1 = GetInternalVString(); + fPart = fPart * Mathf.Pow(10, digits); + int iPart2 = Mathf.RoundToInt(fPart); + + int i = 0; + while (iPart2 != 0 && i < digits) + { + long mod = iPart2 % 10; + iPart2 = iPart2 / 10; + i++; + tempVS1.Push(digitalNumberArray[mod]); + } + tempVS1.ReverseString(); + + tempVS0.Push("."); + tempVS0.Push(tempVS1.GetString()); + while (i < digits) + { + i++; + tempVS0.Push("0"); + } + } + else + { + tempVS0.Push("."); + for (int i = 0; i < digits; ++i) + { + tempVS0.Push("0"); + } + } + + return tempVS0.GetString(); + } + + + /// <summary> + /// 把一个字符串拷贝后,转换为lower case,,注意生成的string一定不能进行存贮 + /// </summary> + /// <param name="str"></param> + /// <returns></returns> + public static string ToLower(string str) + { + if (!string.IsNullOrEmpty(str)) + { + VString tempVS = VStringShareObject.GetShareVString(); + tempVS.Push(str); + tempVS.ToLower(); + return tempVS.GetString(); + } + return str; + } + + /// <summary> + /// 把一个字符串拷贝后,转换为upper case,,注意生成的string一定不能进行存贮 + /// </summary> + /// <param name="str"></param> + /// <returns></returns> + public static string ToUpper(string str) + { + if (!string.IsNullOrEmpty(str)) + { + VString tempVS = VStringShareObject.GetShareVString(); + tempVS.Push(str); + tempVS.ToUpper(); + return tempVS.GetString(); + } + return str; + } + + public static string ToTempSubString(string str, int index, int count) + { + if (string.IsNullOrEmpty(str) || count <= 0 || index < 0) + { + LogHelper.LogError(VStringUtil.Concat("ToTempSubString IsNullOrEmpty ", index.ToTempString(), "/", count.ToTempString())); + return str; + } + + if (index + count > str.Length) + { + LogHelper.LogError(VStringUtil.Concat("ToTempSubString ", str, index.ToTempString(), "/", count.ToTempString())); + return str; + } + + VString tempVS1 = VStringShareObject.GetShareVString(); + tempVS1.Push(str); + VString tempVS2 = VStringShareObject.GetShareVString(); + tempVS2.CopyFrom(tempVS1, index, count); + return tempVS2.GetString(); + } + + + /// <summary> + /// 拼接两个字符串 + /// </summary> + /// <param name="a"></param> + /// <param name="b"></param> + /// <param name="clear"></param> + /// <returns></returns> + public string Concat(string a, string b, bool clear = true) + { + if (clear) + { + Clear(); + } + + Push(a); + Push(b); + return _data; + } + + + + public string Concat(string a, string b, string c, bool clear = true) + { + if (clear) + { + Clear(); + } + + Push(a); + Push(b); + Push(c); + return _data; + } + public string Concat(string a, string b, string c, string d, bool clear = true) + { + if (clear) + { + Clear(); + } + + Push(a); + Push(b); + Push(c); + Push(d); + return _data; + } + public string Concat(string a, string b, string c, string d, string e, bool clear = true) + { + if (clear) + { + Clear(); + } + + Push(a); + Push(b); + Push(c); + Push(d); + Push(e); + return _data; + } + public string Concat(string a, string b, string c, string d, string e, string f, bool clear = true) + { + if (clear) + { + Clear(); + } + + Push(a); + Push(b); + Push(c); + Push(d); + Push(e); + Push(f); + return _data; + } + public string Concat(string a, string b, string c, string d, string e, string f, string g, bool clear = true) + { + if (clear) + { + Clear(); + } + + Push(a); + Push(b); + Push(c); + Push(d); + Push(e); + Push(f); + Push(g); + return _data; + } + public string Concat(string a, string b, string c, string d, string e, string f, string g, string h, bool clear = true) + { + if (clear) + { + Clear(); + } + + Push(a); + Push(b); + Push(c); + Push(d); + Push(e); + Push(f); + Push(g); + Push(h); + return _data; + } + public string Concat(string a, string b, string c, string d, string e, string f, string g, string h, string i, bool clear = true) + { + if (clear) + { + Clear(); + } + + Push(a); + Push(b); + Push(c); + Push(d); + Push(e); + Push(f); + Push(g); + Push(h); + Push(i); + return _data; + } + public string Concat(string a, string b, string c, string d, string e, string f, string g, string h, string i, string j, bool clear = true) + { + if (clear) + { + Clear(); + } + + Push(a); + Push(b); + Push(c); + Push(d); + Push(e); + Push(f); + Push(g); + Push(h); + Push(i); + Push(j); + return _data; + } + + + public static bool UseShareObject(string str) + { + for (int i = 0; i < _internalVSArray.Length; ++i) + { + if (string.ReferenceEquals(str, _internalVSArray[i].GetString())) + { + return true; + } + } + return false; + } + + + //往当前的字符串中添加字符串 + public unsafe void Push(string newStr) + { + if (string.IsNullOrEmpty(newStr)) + { + return; + } + + int copyLen = newStr.Length; + int newLen = _data.Length + copyLen; + if ((newLen + 1) > maxCount) //留1个给字符串结束符 + { + int len = newLen; + copyLen = maxCount - _data.Length - 1; + newLen = maxCount - 1; //设置新的长度 + //这个地方不使用VstringUtil.Concat避免死循环 + LogHelper.LogEditorError(StringUtil.Concat("超过了最大添加长度 ", maxCount.ToTempString(), " ", len.ToTempString())); + } + + if (copyLen <= 0) + { + return; + } + + fixed (char* src = newStr) + { + fixed (char* dst = _data) + { + UnsafeFunction.memcpyimpl((byte*)src, (byte*)(dst + _data.Length), copyLen * 2); //system.string的存储每个元素两个字节 + + int* iDst = (int*)dst; + iDst = iDst - 1; //字符串的长度在第一个元素的前面4个字节 + *iDst = newLen; + + char* iEnd = (char*)(dst + newLen); + *iEnd = (char)0;//设置字符串结束符 + } + } + } + + + public unsafe void Clear() + { + fixed (char* p = _data) + { + int* pSize = (int*)p; + pSize = pSize - 1; + *pSize = 0; + } + } + + public unsafe void CopyFrom(VString srcVstring, int startIndex, int count) + { + if ((count + 1) > maxCount) //留1个给字符串结束符 + { + throw new ArgumentException(VStringUtil.Concat("copy count is larger then maxCount ", + count.ToTempString(), " ", maxCount.ToTempString())); + } + + string srcStr = srcVstring.GetString(); + if (startIndex + count > srcStr.Length) + { + throw new ArgumentException(VStringUtil.Concat("copy count is larger then srcString len ", + count.ToTempString(), " ", srcStr.Length.ToTempString(), " ", startIndex.ToTempString())); + } + + Clear(); + + fixed (char* src = srcStr) + { + fixed (char* dst = _data) + { + UnsafeFunction.memcpyimpl((byte*)(src + startIndex), (byte*)dst, count * 2); //system.string的存储每个元素两个字节 + + int* iDst = (int*)dst; + iDst = iDst - 1; //字符串的长度在第一个元素的前面4个字节 + *iDst = count; + + char* iEnd = (char*)(dst + _data.Length); + *iEnd = (char)0;//设置字符串结束符 + } + } + } + + public unsafe void ToLower() + { + int index = 0; + int len = _data.Length; + fixed (char* dst = _data) + { + while (index < len) + { + char tempChar = *(dst + index); + *(dst + index) = char.ToLower(tempChar); + ++index; + } + } + } + + public unsafe void ToUpper() + { + int index = 0; + int len = _data.Length; + fixed (char* dst = _data) + { + while (index < len) + { + char tempChar = *(dst + index); + *(dst + index) = char.ToUpper(tempChar); + ++index; + } + } + } + + //反转字符串的内容 + private unsafe string ReverseString() + { + int len = _data.Length; + if (len > 0) + { + fixed (char* pHead = _data) + { + int count = len / 2; + for (int i = 0; i < count; ++i) + { + char temp = pHead[i]; + pHead[i] = pHead[len - 1 - i]; + pHead[len - 1 - i] = temp; + } + } + } + return _data; + } + + + private static VString GetInternalVString() + { + _internalVsIndex = (_internalVsIndex + 1) % _internalVSArray.Length; + VString vString = _internalVSArray[_internalVsIndex]; + vString.Clear(); + return vString; + } + +}
\ No newline at end of file diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/VString.cs.meta b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VString.cs.meta new file mode 100644 index 0000000..1a0afa7 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VString.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 070edb2882bc15d49917028ddea695e2 +timeCreated: 1525942883 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/VStringShareObject.cs b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VStringShareObject.cs new file mode 100644 index 0000000..0cd147d --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VStringShareObject.cs @@ -0,0 +1,54 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + + + +public static class VStringShareObject +{ + + private static volatile object lockThis = new object(); + private static int _internalVsIndex; + private static VString[] _internalVSArray = new VString[] + { + new VString(2048), + new VString(2048), + new VString(2048), + new VString(2048), + new VString(2048), + new VString(2048), + new VString(2048), + new VString(2048), + new VString(2048), + new VString(2048), + new VString(2048), + new VString(2048), + new VString(2048), + new VString(2048), + new VString(2048) + }; + + + public static VString GetShareVString() + { + lock(lockThis) + { + _internalVsIndex = (_internalVsIndex + 1) % _internalVSArray.Length; + VString vString = _internalVSArray[_internalVsIndex]; + vString.Clear(); + return vString; + } + } + + public static bool UseShareObject(string str) + { + for (int i = 0; i < _internalVSArray.Length; ++i) + { + if (string.ReferenceEquals(str, _internalVSArray[i].GetString())) + { + return true; + } + } + return false; + } +}
\ No newline at end of file diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/VStringShareObject.cs.meta b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VStringShareObject.cs.meta new file mode 100644 index 0000000..b40aa4a --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VStringShareObject.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: c6d08c5051585094fb4d23fdf6fe4b7b +timeCreated: 1552276176 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/VStringUtil.cs b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VStringUtil.cs new file mode 100644 index 0000000..d8d717c --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VStringUtil.cs @@ -0,0 +1,107 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + + +/// <summary> +/// 只能作为临时字符串使用,代码任何地方使用只能赋值给临时变量,不可保存 +/// </summary> +public class VStringUtil +{ + /// <summary> + /// 只能作为临时字符串使用,代码任何地方使用只能赋值给临时变量,不可保存 + /// </summary> + public static string Concat(string a, string b) + { + VString vString = VStringShareObject.GetShareVString(); + vString.Concat(a, b, true); + return vString.GetString(); + } + /// <summary> + /// 只能作为临时字符串使用,代码任何地方使用只能赋值给临时变量,不可保存 + /// </summary> + public static string Concat(string a, string b, string c) + { + VString vString = VStringShareObject.GetShareVString(); + vString.Concat(a, b, c, true); + return vString.GetString(); + } + /// <summary> + /// 只能作为临时字符串使用,代码任何地方使用只能赋值给临时变量,不可保存 + /// </summary> + public static string Concat(string a, string b, string c, string d) + { + VString vString = VStringShareObject.GetShareVString(); + vString.Concat(a, b, c, d, true); + return vString.GetString(); + } + /// <summary> + /// 只能作为临时字符串使用,代码任何地方使用只能赋值给临时变量,不可保存 + /// </summary> + public static string Concat(string a, string b, string c, string d, string e) + { + VString vString = VStringShareObject.GetShareVString(); + vString.Concat(a, b, c, d, e, true); + return vString.GetString(); + } + /// <summary> + /// 只能作为临时字符串使用,代码任何地方使用只能赋值给临时变量,不可保存 + /// </summary> + public static string Concat(string a, string b, string c, string d, string e, string f) + { + VString vString = VStringShareObject.GetShareVString(); + vString.Concat(a, b, c, d, e, f, true); + return vString.GetString(); + } + /// <summary> + /// 只能作为临时字符串使用,代码任何地方使用只能赋值给临时变量,不可保存 + /// </summary> + public static string Concat(string a, string b, string c, string d, string e, string f, string g) + { + VString vString = VStringShareObject.GetShareVString(); + vString.Concat(a, b, c, d, e, f, g, true); + return vString.GetString(); + } + /// <summary> + /// 只能作为临时字符串使用,代码任何地方使用只能赋值给临时变量,不可保存 + /// </summary> + public static string Concat(string a, string b, string c, string d, string e, string f, string g, string h) + { + VString vString = VStringShareObject.GetShareVString(); + vString.Concat(a, b, c, d, e, f, g, h, true); + return vString.GetString(); + } + /// <summary> + /// 只能作为临时字符串使用,代码任何地方使用只能赋值给临时变量,不可保存 + /// </summary> + public static string Concat(string a, string b, string c, string d, string e, string f, string g, string h, string i) + { + VString vString = VStringShareObject.GetShareVString(); + vString.Concat(a, b, c, d, e, f, g, h, i, true); + return vString.GetString(); + } + /// <summary> + /// 只能作为临时字符串使用,代码任何地方使用只能赋值给临时变量,不可保存 + /// </summary> + public static string Concat(string a, string b, string c, string d, string e, string f, string g, string h, string i, string j) + { + VString vString = VStringShareObject.GetShareVString(); + vString.Concat(a, b, c, d, e, f, g, h, i, j, true); + return vString.GetString(); + } + + /// <summary> + /// 如果不是共享string,则返回str,如果是共享string则返回copy str + /// </summary> + /// <param name="str"></param> + /// <returns></returns> + public static string ConvertToNormalString(string str) + { + if(VStringShareObject.UseShareObject(str) || VString.UseShareObject(str)) + { + return string.Copy(str); + } + return str; + } + +}
\ No newline at end of file diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/VStringUtil.cs.meta b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VStringUtil.cs.meta new file mode 100644 index 0000000..721f40c --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VStringUtil.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 1816e678bcdfdb646b247fb5419f1279 +timeCreated: 1525954142 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/VTypingString.cs b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VTypingString.cs new file mode 100644 index 0000000..50b63e6 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VTypingString.cs @@ -0,0 +1,171 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +/// <summary> +/// 优化打字功能的字符串 +/// </summary> +public class VTypingString +{ + enum Tags + { + Color = 0, + Size, + B, + I + } + + + private VString allContentString; + private DoubleVString partialDisplayString; + private VString willShowString; + + private int _timerMS; + private int _factor; + private int _miniSecondPerWord; + + private static readonly string[] endTags = new string[] { "</color>", "</size>", "</b>", "</i>" }; + private static readonly string[] startTags = new string[] { "<color", "<size", "<b", "<i" }; + + List<int> endTagCaches = new List<int>(); + + + /// <summary> + /// + /// </summary> + /// <param name="text"></param> + /// <param name="miniSecondPerWord">出一个字符需要的毫秒时间</param> + public VTypingString(string text, int miniSecondPerWord = 240) + { + if(string.IsNullOrEmpty(text)) + { + throw new ArgumentException("text is null or empty"); + } + + _miniSecondPerWord = Mathf.Max(miniSecondPerWord, 10); + allContentString = new VString(text.Length); + allContentString.Push(text); + partialDisplayString = new DoubleVString(text.Length); + willShowString = new VString(text.Length); + JumpToBegin(); + } + + public bool IsEnd() + { + return _factor > allContentString.GetString().Length; + } + + public string GetString() + { + return partialDisplayString.GetCurrentVString().GetString(); + } + + /// <summary> + /// + /// </summary> + /// <param name="deltaTimeMS"></param> + /// <returns>true表示触发了一次打字变化</returns> + public bool OnUpdate(int deltaTimeMS) + { + _timerMS += deltaTimeMS; + if(_timerMS >= _miniSecondPerWord) + { + _timerMS = 0; + OnTyping(); + return true; + } + return false; + } + + private void OnTyping() + { + if (CheckStart(Tags.Color)) { } + else if (CheckStart(Tags.Size)) { } + else if (CheckStart(Tags.B)) { } + else if (CheckStart(Tags.I)) { } + else + { + partialDisplayString.GetCurrentVString().Clear(); + partialDisplayString.SwapVString(); + partialDisplayString.GetCurrentVString().CopyFrom(allContentString, 0, Mathf.Min(_factor, allContentString.GetString().Length)); + for (int i = endTagCaches.Count - 1; i >= 0; --i) + { + partialDisplayString.GetCurrentVString().Push(endTags[endTagCaches[i]]); + } + _factor++; + } + } + + public void JumpToBegin() + { + _factor = 0; + _timerMS = -_miniSecondPerWord; + endTagCaches.Clear(); + partialDisplayString.GetCurrentVString().Clear(); + partialDisplayString.GetNextVString().Clear(); + } + + public void JumpToEnd() + { + _factor = allContentString.GetString().Length; + endTagCaches.Clear(); + OnTyping(); + } + + + bool CheckStart(Tags tag) + { + if (_factor >= allContentString.GetString().Length) + { + return false; + } + + int iTag = (int)tag; + willShowString.CopyFrom(allContentString, _factor, allContentString.GetString().Length - _factor); + string willShow = willShowString.GetString(); + string endTag = endTags[iTag]; + if (willShow.StartsWith(startTags[iTag])) + { + int tagLeng = willShow.IndexOf(">") + 1; + _factor += tagLeng; + endTagCaches.Add(iTag);//倒叙 + + if (CheckStart(Tags.Color)) { } + else if (CheckStart(Tags.Size)) { } + else if (CheckStart(Tags.B)) { } + else if (CheckStart(Tags.I)) { } + else + { + return false; + } + return true; + } + else if (willShow.StartsWith(endTag)) + { + int endleng = endTag.Length;//"</color>"的长度 + _factor += endleng; + for (int i = endTagCaches.Count - 1; i >= 0; --i) + { + if(iTag == endTagCaches[i]) + { + endTagCaches.RemoveAt(i); + } + } + + if (CheckStart(Tags.Color)) { } + else if (CheckStart(Tags.Size)) { } + else if (CheckStart(Tags.B)) { } + else if (CheckStart(Tags.I)) { } + else + { + return false; + } + return true; + } + return false; + } + + +} diff --git a/WorldlineKeepers/Assets/ThirdParty/StringUtil/VTypingString.cs.meta b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VTypingString.cs.meta new file mode 100644 index 0000000..6f7b5ae --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/StringUtil/VTypingString.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 49843f8b3aca65e4ca985d3226e77ad0 +timeCreated: 1542200708 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |