zstring.cs 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. using System;
  2. using System.Collections.Generic;
  3. /*
  4. 介绍:
  5. C# 0GC字符串补充方案。结合gstring与CString两者特点(向这两个方案的作者致敬),只有一个文件,性能与使用方便性高于两者。
  6. 报告地址:
  7. https://coh5.cn/p/1ace6338.html
  8. 使用方式:
  9. 1.Unity引擎将zstring.cs文件放于plugins目录下即可使用(不在plugins目录,则IOS打包或IL2CPP打包等FULLAOT方式编译不过),或者直接把结构体定义放入zstring类中;其余C#程序将zstring.cs直接放入工程使用即可。
  10. 2.(最佳性能)当update每帧刷新标签显示,或者大量UI飘字,或者该字符串是短时间使用的则使用如下方式:
  11. using (zstring.Block())
  12. {
  13. uiText1.text=(zstring)"hello world"+" you";
  14. uiText2.text=zstring.format("{0},{1}","hello","world");
  15. }
  16. 此方式设置的string值位于浅拷贝缓存中,一定时间可能会改变,出作用域后正确性不予保证。
  17. 3.资源路径这种需要常驻的则需要intern一下在作用域外使用
  18. using (zstring.Block())
  19. {
  20. zstring a="Assets/";
  21. zstring b=a+"prefabs/"+"/solider.prefab";
  22. prefabPath1=b.Intern();
  23. prefabPath2=zstring.format("{0},{1}","hello","world").Intern();
  24. }
  25. 此方式设置的string值位于深拷贝缓存中,游戏运行期间不会改变,可以在作用域外使用。
  26. 4.不可使用zstring作为类的成员变量,不建议在using作用域中写for循环,而是在for循环内using。
  27. 5.首次调用时会初始化类,分配各种空间,建议游戏启动时调用一次using(zstring.Block()){}
  28. 6.0GC。时间消耗上,短字符串处理,zstring比gstring时间少20%~30%,比原生慢。大字符串处理,zstring比gstring时间少70%~80%,接近原生string速度。
  29. 7.追求极限性能的话,核心函数可以用C++Dll中的 memcpy内存拷贝函数,性能提升10%~20%,一般没这个必要。
  30. 8.测试打开zstringTest工程,在Test脚本上勾选与不勾选bigStringTest下查看Profile性能。(同时对比了zstring,gstring,CString,还有王国纪元里的string)
  31. 9.据热心用户反应,IL2CPP 2017.4 在 Android上有字节对齐问题,换成2018就木有了。所以此时解决办法有三个:1.IL2CPP换成2018以上版本。 2.719行左右的memcpy函数换成循环一次拷贝一个字节。 3.不怕麻烦的话此处调用C语言的内存拷贝函数dll,即C语言<string.h>中的memcpy,这样性能也更高。
  32. 10.有事请联系 871041532@outlook.com 或 QQ(微信):871041532
  33. */
  34. namespace Fort23.Core
  35. {
  36. struct Byte8192
  37. {
  38. Byte4096 a1; Byte4096 a2;
  39. }
  40. struct Byte4096
  41. {
  42. Byte2048 a1; Byte2048 a2;
  43. }
  44. struct Byte2048
  45. {
  46. Byte1024 a1; Byte1024 a2;
  47. }
  48. struct Byte1024
  49. {
  50. Byte512 a1; Byte512 a2;
  51. }
  52. struct Byte512
  53. {
  54. Byte256 a1; Byte256 a2;
  55. }
  56. struct Byte256
  57. {
  58. Byte128 a1; Byte128 a2;
  59. }
  60. struct Byte128
  61. {
  62. Byte64 a1; Byte64 a2;
  63. }
  64. struct Byte64
  65. {
  66. Byte32 a1; Byte32 a2;
  67. }
  68. struct Byte32
  69. {
  70. Byte16 a1; Byte16 a2;
  71. }
  72. struct Byte16
  73. {
  74. Byte8 a1; Byte8 a2;
  75. }
  76. struct Byte8
  77. {
  78. long a1;
  79. }
  80. struct Byte4
  81. {
  82. int a1;
  83. }
  84. struct Byte2
  85. {
  86. short a;
  87. }
  88. struct Byte1
  89. {
  90. byte a;
  91. }
  92. public class zstring
  93. {
  94. static Queue<zstring>[] g_cache;//idx特定字符串长度,深拷贝核心缓存
  95. static Dictionary<int, Queue<zstring>> g_secCache;//key特定字符串长度value字符串栈,深拷贝次级缓存
  96. static Stack<zstring> g_shallowCache;//浅拷贝缓存
  97. static Stack<zstring_block> g_blocks;//zstring_block缓存栈
  98. static Stack<zstring_block> g_open_blocks;//zstring已经打开的缓存栈
  99. static Dictionary<int, string> g_intern_table;//字符串intern表
  100. public static zstring_block g_current_block;//zstring所在的block块
  101. static List<int> g_finds;//字符串replace功能记录子串位置
  102. static zstring[] g_format_args;//存储格式化字符串值
  103. const int INITIAL_BLOCK_CAPACITY = 32;//gblock块数量
  104. const int INITIAL_CACHE_CAPACITY = 128;//cache缓存字典容量 128*4Byte 500多Byte
  105. const int INITIAL_STACK_CAPACITY = 48;//cache字典每个stack默认nstring容量
  106. const int INITIAL_INTERN_CAPACITY = 256;//Intern容量
  107. const int INITIAL_OPEN_CAPACITY = 5;//默认打开层数为5
  108. const int INITIAL_SHALLOW_CAPACITY = 100;//默认50个浅拷贝用
  109. const char NEW_ALLOC_CHAR = 'X';//填充char
  110. private bool isShallow = false;//是否浅拷贝
  111. [NonSerialized]
  112. string _value;//值
  113. [NonSerialized]
  114. bool _disposed;//销毁标记
  115. //不支持构造
  116. private zstring()
  117. {
  118. throw new NotSupportedException();
  119. }
  120. //带默认长度的构造
  121. private zstring(int length)
  122. {
  123. _value = new string(NEW_ALLOC_CHAR, length);
  124. }
  125. //浅拷贝专用构造
  126. private zstring(string value, bool shallow)
  127. {
  128. if (!shallow)
  129. {
  130. throw new NotSupportedException();
  131. }
  132. _value = value;
  133. isShallow = true;
  134. }
  135. static zstring()
  136. {
  137. Initialize(INITIAL_CACHE_CAPACITY,
  138. INITIAL_STACK_CAPACITY,
  139. INITIAL_BLOCK_CAPACITY,
  140. INITIAL_INTERN_CAPACITY,
  141. INITIAL_OPEN_CAPACITY,
  142. INITIAL_SHALLOW_CAPACITY
  143. );
  144. g_finds = new List<int>(10);
  145. g_format_args = new zstring[10];
  146. }
  147. //析构
  148. private void dispose()
  149. {
  150. if (_disposed)
  151. throw new ObjectDisposedException(this);
  152. if (isShallow)//深浅拷贝走不同缓存
  153. {
  154. g_shallowCache.Push(this);
  155. }
  156. else
  157. {
  158. Queue<zstring> stack;
  159. if (g_cache.Length > Length)
  160. {
  161. stack = g_cache[Length];//取出valuelength长度的栈,将自身push进去
  162. }
  163. else
  164. {
  165. stack = g_secCache[Length];
  166. }
  167. stack.Enqueue(this);
  168. }
  169. //memcpy(_value, NEW_ALLOC_CHAR);//内存拷贝至value
  170. _disposed = true;
  171. }
  172. //由string获取相同内容zstring,深拷贝
  173. private static zstring get(string value)
  174. {
  175. if (value == null)
  176. return null;
  177. #if DBG
  178. if (log != null)
  179. log("Getting: " + value);
  180. #endif
  181. var result = get(value.Length);
  182. memcpy(dst: result, src: value);//内存拷贝
  183. return result;
  184. }
  185. //由string浅拷贝入zstring
  186. private static zstring getShallow(string value)
  187. {
  188. if (g_current_block == null)
  189. {
  190. throw new InvalidOperationException("nstring 操作必须在一个nstring_block块中。");
  191. }
  192. zstring result;
  193. if (g_shallowCache.Count == 0)
  194. {
  195. result = new zstring(value, true);
  196. }
  197. else
  198. {
  199. result = g_shallowCache.Pop();
  200. result._value = value;
  201. }
  202. result._disposed = false;
  203. g_current_block.push(result);//zstring推入块所在栈
  204. return result;
  205. }
  206. //将string加入intern表中
  207. private static string __intern(string value)
  208. {
  209. int hash = value.GetHashCode();
  210. if (g_intern_table.ContainsKey(hash))
  211. {
  212. return g_intern_table[hash];
  213. }
  214. else
  215. {
  216. string interned = new string(NEW_ALLOC_CHAR, value.Length);
  217. memcpy(interned, value);
  218. g_intern_table.Add(hash, interned);
  219. return interned;
  220. }
  221. }
  222. //手动添加方法
  223. private static void getStackInCache(int index, out Queue<zstring> outStack)
  224. {
  225. int length = g_cache.Length;
  226. if (length > index)//从核心缓存中取
  227. {
  228. outStack = g_cache[index];
  229. }
  230. else//从次级缓存中取
  231. {
  232. if (!g_secCache.TryGetValue(index, out outStack))
  233. {
  234. outStack = new Queue<zstring>(INITIAL_STACK_CAPACITY);
  235. g_secCache[index] = outStack;
  236. }
  237. }
  238. }
  239. //获取特定长度zstring
  240. private static zstring get(int length)
  241. {
  242. if (g_current_block == null || length <= 0)
  243. throw new InvalidOperationException("zstring 操作必须在一个zstring_block块中。");
  244. zstring result;
  245. Queue<zstring> stack;
  246. getStackInCache(length, out stack);
  247. //从缓存中取Stack
  248. if (stack.Count == 0)
  249. {
  250. result = new zstring(length);
  251. }
  252. else
  253. {
  254. result = stack.Dequeue();
  255. }
  256. result._disposed = false;
  257. g_current_block.push(result);//zstring推入块所在栈
  258. return result;
  259. }
  260. //value是10的次方数
  261. private static int get_digit_count(long value)
  262. {
  263. int cnt;
  264. for (cnt = 1; (value /= 10) > 0; cnt++) ;
  265. return cnt;
  266. }
  267. //value是10的次方数
  268. private static uint get_digit_count(uint value)
  269. {
  270. uint cnt;
  271. for (cnt = 1; (value /= 10) > 0; cnt++) ;
  272. return cnt;
  273. }
  274. //value是10的次方数
  275. private static int get_digit_count(int value)
  276. {
  277. int cnt;
  278. for (cnt = 1; (value /= 10) > 0; cnt++) ;
  279. return cnt;
  280. }
  281. //获取char在input中start起往后的下标
  282. private static int internal_index_of(string input, char value, int start)
  283. {
  284. return internal_index_of(input, value, start, input.Length - start);
  285. }
  286. //获取string在input中起始0的下标
  287. private static int internal_index_of(string input, string value)
  288. {
  289. return internal_index_of(input, value, 0, input.Length);
  290. }
  291. //获取string在input中自0起始下标
  292. private static int internal_index_of(string input, string value, int start)
  293. {
  294. return internal_index_of(input, value, start, input.Length - start);
  295. }
  296. //获取格式化字符串
  297. private unsafe static zstring internal_format(string input, int num_args)
  298. {
  299. if (input == null)
  300. throw new ArgumentNullException("value");
  301. //新字符串长度
  302. int new_len = input.Length;
  303. for (int i = -3; ;)
  304. {
  305. i = internal_index_of(input, '{', i + 3);
  306. if (i == -1)
  307. {
  308. break;
  309. }
  310. new_len -= 3;
  311. int arg_idx = input[i + 1] - '0';
  312. zstring arg = g_format_args[arg_idx];
  313. new_len += arg.Length;
  314. }
  315. zstring result = get(new_len);
  316. string res_value = result._value;
  317. int next_output_idx = 0;
  318. int next_input_idx = 0;
  319. int brace_idx = -3;
  320. for (int i = 0, j = 0, x = 0; ; x++) // x < num_args
  321. {
  322. brace_idx = internal_index_of(input, '{', brace_idx + 3);
  323. if (brace_idx == -1)
  324. {
  325. break;
  326. }
  327. next_input_idx = brace_idx;
  328. int arg_idx = input[brace_idx + 1] - '0';
  329. string arg = g_format_args[arg_idx]._value;
  330. if (brace_idx == -1)
  331. throw new InvalidOperationException("没有发现大括号{ for argument " + arg);
  332. if (brace_idx + 2 >= input.Length || input[brace_idx + 2] != '}')
  333. throw new InvalidOperationException("没有发现大括号} for argument " + arg);
  334. fixed (char* ptr_input = input)
  335. {
  336. fixed (char* ptr_result = res_value)
  337. {
  338. for (int k = 0; i < new_len;)
  339. {
  340. if (j < brace_idx)
  341. {
  342. ptr_result[i++] = ptr_input[j++];
  343. ++next_output_idx;
  344. }
  345. else
  346. {
  347. ptr_result[i++] = arg[k++];
  348. ++next_output_idx;
  349. if (k == arg.Length)
  350. {
  351. j += 3;
  352. break;
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. next_input_idx += 3;
  360. for (int i = next_output_idx, j = 0; i < new_len; i++, j++)
  361. {
  362. fixed (char* ptr_input = input)
  363. {
  364. fixed (char* ptr_result = res_value)
  365. {
  366. ptr_result[i] = ptr_input[next_input_idx + j];
  367. }
  368. }
  369. }
  370. return result;
  371. }
  372. //获取char在字符串中start开始的下标
  373. private unsafe static int internal_index_of(string input, char value, int start, int count)
  374. {
  375. if (start < 0 || start >= input.Length)
  376. // throw new ArgumentOutOfRangeException("start");
  377. return -1;
  378. if (start + count > input.Length)
  379. return -1;
  380. // throw new ArgumentOutOfRangeException("count=" + count + " start+count=" + start + count);
  381. fixed (char* ptr_this = input)
  382. {
  383. int end = start + count;
  384. for (int i = start; i < end; i++)
  385. if (ptr_this[i] == value)
  386. return i;
  387. return -1;
  388. }
  389. }
  390. //获取value在input中自start起始下标
  391. private unsafe static int internal_index_of(string input, string value, int start, int count)
  392. {
  393. int input_len = input.Length;
  394. if (start < 0 || start >= input_len)
  395. throw new ArgumentOutOfRangeException("start");
  396. if (count < 0 || start + count > input_len)
  397. throw new ArgumentOutOfRangeException("count=" + count + " start+count=" + (start + count));
  398. if (count == 0)
  399. return -1;
  400. fixed (char* ptr_input = input)
  401. {
  402. fixed (char* ptr_value = value)
  403. {
  404. int found = 0;
  405. int end = start + count;
  406. for (int i = start; i < end; i++)
  407. {
  408. for (int j = 0; j < value.Length && i + j < input_len; j++)
  409. {
  410. if (ptr_input[i + j] == ptr_value[j])
  411. {
  412. found++;
  413. if (found == value.Length)
  414. return i;
  415. continue;
  416. }
  417. if (found > 0)
  418. break;
  419. }
  420. }
  421. return -1;
  422. }
  423. }
  424. }
  425. //移除string中自start起始count长度子串
  426. private unsafe static zstring internal_remove(string input, int start, int count)
  427. {
  428. if (start < 0 || start >= input.Length)
  429. throw new ArgumentOutOfRangeException("start=" + start + " Length=" + input.Length);
  430. if (count < 0 || start + count > input.Length)
  431. throw new ArgumentOutOfRangeException("count=" + count + " start+count=" + (start + count) + " Length=" + input.Length);
  432. if (count == 0)
  433. return input;
  434. zstring result = get(input.Length - count);
  435. internal_remove(result, input, start, count);
  436. return result;
  437. }
  438. //将src中自start起count长度子串复制入dst
  439. private unsafe static void internal_remove(string dst, string src, int start, int count)
  440. {
  441. fixed (char* src_ptr = src)
  442. {
  443. fixed (char* dst_ptr = dst)
  444. {
  445. for (int i = 0, j = 0; i < dst.Length; i++)
  446. {
  447. if (i >= start && i < start + count) // within removal range
  448. continue;
  449. dst_ptr[j++] = src_ptr[i];
  450. }
  451. }
  452. }
  453. }
  454. //字符串replace,原字符串,需替换子串,替换的新子串
  455. private unsafe static zstring internal_replace(string value, string old_value, string new_value)
  456. {
  457. // "Hello, World. There World" | World->Jon =
  458. // "000000000000000000000" (len = orig - 2 * (world-jon) = orig - 4
  459. // "Hello, 00000000000000"
  460. // "Hello, Jon00000000000"
  461. // "Hello, Jon. There 000"
  462. // "Hello, Jon. There Jon"
  463. // "Hello, World. There World" | World->Alexander =
  464. // "000000000000000000000000000000000" (len = orig + 2 * (alexander-world) = orig + 8
  465. // "Hello, 00000000000000000000000000"
  466. // "Hello, Alexander00000000000000000"
  467. // "Hello, Alexander. There 000000000"
  468. // "Hello, Alexander. There Alexander"
  469. if (old_value == null)
  470. throw new ArgumentNullException("old_value");
  471. if (new_value == null)
  472. throw new ArgumentNullException("new_value");
  473. int idx = internal_index_of(value, old_value);
  474. if (idx == -1)
  475. return value;
  476. g_finds.Clear();
  477. g_finds.Add(idx);
  478. // 记录所有需要替换的idx点
  479. while (idx + old_value.Length < value.Length)
  480. {
  481. idx = internal_index_of(value, old_value, idx + old_value.Length);
  482. if (idx == -1)
  483. break;
  484. g_finds.Add(idx);
  485. }
  486. // calc the right new total length
  487. int new_len;
  488. int dif = old_value.Length - new_value.Length;
  489. if (dif > 0)
  490. new_len = value.Length - (g_finds.Count * dif);
  491. else
  492. new_len = value.Length + (g_finds.Count * -dif);
  493. zstring result = get(new_len);
  494. fixed (char* ptr_this = value)
  495. {
  496. fixed (char* ptr_result = result._value)
  497. {
  498. for (int i = 0, x = 0, j = 0; i < new_len;)
  499. {
  500. if (x == g_finds.Count || g_finds[x] != j)
  501. {
  502. ptr_result[i++] = ptr_this[j++];
  503. }
  504. else
  505. {
  506. for (int n = 0; n < new_value.Length; n++)
  507. ptr_result[i + n] = new_value[n];
  508. x++;
  509. i += new_value.Length;
  510. j += old_value.Length;
  511. }
  512. }
  513. }
  514. }
  515. return result;
  516. }
  517. //向字符串value中自start位置插入count长度的to_insertChar
  518. private unsafe static zstring internal_insert(string value, char to_insert, int start, int count)
  519. {
  520. // "HelloWorld" (to_insert=x, start=5, count=3) -> "HelloxxxWorld"
  521. if (start < 0 || start >= value.Length)
  522. throw new ArgumentOutOfRangeException("start=" + start + " Length=" + value.Length);
  523. if (count < 0)
  524. throw new ArgumentOutOfRangeException("count=" + count);
  525. if (count == 0)
  526. return get(value);
  527. int new_len = value.Length + count;
  528. zstring result = get(new_len);
  529. fixed (char* ptr_value = value)
  530. {
  531. fixed (char* ptr_result = result._value)
  532. {
  533. for (int i = 0, j = 0; i < new_len; i++)
  534. {
  535. if (i >= start && i < start + count)
  536. ptr_result[i] = to_insert;
  537. else
  538. ptr_result[i] = ptr_value[j++];
  539. }
  540. }
  541. }
  542. return result;
  543. }
  544. //向input字符串中插入to_insert串,位置为start
  545. private unsafe static zstring internal_insert(string input, string to_insert, int start)
  546. {
  547. if (input == null)
  548. throw new ArgumentNullException("input");
  549. if (to_insert == null)
  550. throw new ArgumentNullException("to_insert");
  551. if (start < 0 || start >= input.Length)
  552. throw new ArgumentOutOfRangeException("start=" + start + " Length=" + input.Length);
  553. if (to_insert.Length == 0)
  554. return get(input);
  555. int new_len = input.Length + to_insert.Length;
  556. zstring result = get(new_len);
  557. internal_insert(result, input, to_insert, start);
  558. return result;
  559. }
  560. //字符串拼接
  561. private unsafe static zstring internal_concat(string s1, string s2)
  562. {
  563. int total_length = s1.Length + s2.Length;
  564. zstring result = get(total_length);
  565. fixed (char* ptr_result = result._value)
  566. {
  567. fixed (char* ptr_s1 = s1)
  568. {
  569. fixed (char* ptr_s2 = s2)
  570. {
  571. memcpy(dst: ptr_result, src: ptr_s1, length: s1.Length, src_offset: 0);
  572. memcpy(dst: ptr_result, src: ptr_s2, length: s2.Length, src_offset: s1.Length);
  573. }
  574. }
  575. }
  576. return result;
  577. }
  578. //将to_insert串插入src的start位置,内容写入dst
  579. private unsafe static void internal_insert(string dst, string src, string to_insert, int start)
  580. {
  581. fixed (char* ptr_src = src)
  582. {
  583. fixed (char* ptr_dst = dst)
  584. {
  585. fixed (char* ptr_to_insert = to_insert)
  586. {
  587. for (int i = 0, j = 0, k = 0; i < dst.Length; i++)
  588. {
  589. if (i >= start && i < start + to_insert.Length)
  590. ptr_dst[i] = ptr_to_insert[k++];
  591. else
  592. ptr_dst[i] = ptr_src[j++];
  593. }
  594. }
  595. }
  596. }
  597. }
  598. //将长度为count的数字插入dst中,起始位置为start,dst的长度需大于start+count
  599. private unsafe static void longcpy(char* dst, long value, int start, int count)
  600. {
  601. int end = start + count;
  602. for (int i = end - 1; i >= start; i--, value /= 10)
  603. *(dst + i) = (char)(value % 10 + 48);
  604. }
  605. //将长度为count的数字插入dst中,起始位置为start,dst的长度需大于start+count
  606. private unsafe static void intcpy(char* dst, int value, int start, int count)
  607. {
  608. int end = start + count;
  609. for (int i = end - 1; i >= start; i--, value /= 10)
  610. *(dst + i) = (char)(value % 10 + 48);
  611. }
  612. private static unsafe void _memcpy4(byte* dest, byte* src, int size)
  613. {
  614. /*while (size >= 32) {
  615. // using long is better than int and slower than double
  616. // FIXME: enable this only on correct alignment or on platforms
  617. // that can tolerate unaligned reads/writes of doubles
  618. ((double*)dest) [0] = ((double*)src) [0];
  619. ((double*)dest) [1] = ((double*)src) [1];
  620. ((double*)dest) [2] = ((double*)src) [2];
  621. ((double*)dest) [3] = ((double*)src) [3];
  622. dest += 32;
  623. src += 32;
  624. size -= 32;
  625. }*/
  626. while (size >= 16)
  627. {
  628. ((int*)dest)[0] = ((int*)src)[0];
  629. ((int*)dest)[1] = ((int*)src)[1];
  630. ((int*)dest)[2] = ((int*)src)[2];
  631. ((int*)dest)[3] = ((int*)src)[3];
  632. dest += 16;
  633. src += 16;
  634. size -= 16;
  635. }
  636. while (size >= 4)
  637. {
  638. ((int*)dest)[0] = ((int*)src)[0];
  639. dest += 4;
  640. src += 4;
  641. size -= 4;
  642. }
  643. while (size > 0)
  644. {
  645. ((byte*)dest)[0] = ((byte*)src)[0];
  646. dest += 1;
  647. src += 1;
  648. --size;
  649. }
  650. }
  651. private static unsafe void _memcpy2(byte* dest, byte* src, int size)
  652. {
  653. while (size >= 8)
  654. {
  655. ((short*)dest)[0] = ((short*)src)[0];
  656. ((short*)dest)[1] = ((short*)src)[1];
  657. ((short*)dest)[2] = ((short*)src)[2];
  658. ((short*)dest)[3] = ((short*)src)[3];
  659. dest += 8;
  660. src += 8;
  661. size -= 8;
  662. }
  663. while (size >= 2)
  664. {
  665. ((short*)dest)[0] = ((short*)src)[0];
  666. dest += 2;
  667. src += 2;
  668. size -= 2;
  669. }
  670. if (size > 0)
  671. {
  672. ((byte*)dest)[0] = ((byte*)src)[0];
  673. }
  674. }
  675. //从src,0位置起始拷贝count长度字符串src到dst中
  676. //private unsafe static void memcpy(char* dest, char* src, int count)
  677. //{
  678. // // Same rules as for memcpy, but with the premise that
  679. // // chars can only be aligned to even addresses if their
  680. // // enclosing types are correctly aligned
  681. // superMemcpy(dest, src, count);
  682. // //if ((((int)(byte*)dest | (int)(byte*)src) & 3) != 0)//转换为byte指针
  683. // //{
  684. // // if (((int)(byte*)dest & 2) != 0 && ((int)(byte*)src & 2) != 0 && count > 0)
  685. // // {
  686. // // ((short*)dest)[0] = ((short*)src)[0];
  687. // // dest++;
  688. // // src++;
  689. // // count--;
  690. // // }
  691. // // if ((((int)(byte*)dest | (int)(byte*)src) & 2) != 0)
  692. // // {
  693. // // _memcpy2((byte*)dest, (byte*)src, count * 2);//转换为short*指针一次两个字节拷贝
  694. // // return;
  695. // // }
  696. // //}
  697. // //_memcpy4((byte*)dest, (byte*)src, count * 2);//转换为int*指针一次四个字节拷贝
  698. //}
  699. //--------------------------------------手敲memcpy-------------------------------------//
  700. private static int m_charLen = sizeof(char);
  701. private unsafe static void memcpy(char* dest, char* src, int count)
  702. {
  703. byteCopy((byte*)dest, (byte*)src, count * m_charLen);
  704. }
  705. private unsafe static void byteCopy(byte* dest, byte* src, int byteCount)
  706. {
  707. if (byteCount < 128)
  708. {
  709. goto g64;
  710. }
  711. else if (byteCount < 2048)
  712. {
  713. goto g1024;
  714. }
  715. while (byteCount >= 8192)
  716. {
  717. ((Byte8192*)dest)[0] = ((Byte8192*)src)[0];
  718. dest += 8192;
  719. src += 8192;
  720. byteCount -= 8192;
  721. }
  722. if (byteCount >= 4096)
  723. {
  724. ((Byte4096*)dest)[0] = ((Byte4096*)src)[0];
  725. dest += 4096;
  726. src += 4096;
  727. byteCount -= 4096;
  728. }
  729. if (byteCount >= 2048)
  730. {
  731. ((Byte2048*)dest)[0] = ((Byte2048*)src)[0];
  732. dest += 2048;
  733. src += 2048;
  734. byteCount -= 2048;
  735. }
  736. g1024: if (byteCount >= 1024)
  737. {
  738. ((Byte1024*)dest)[0] = ((Byte1024*)src)[0];
  739. dest += 1024;
  740. src += 1024;
  741. byteCount -= 1024;
  742. }
  743. if (byteCount >= 512)
  744. {
  745. ((Byte512*)dest)[0] = ((Byte512*)src)[0];
  746. dest += 512;
  747. src += 512;
  748. byteCount -= 512;
  749. }
  750. if (byteCount >= 256)
  751. {
  752. ((Byte256*)dest)[0] = ((Byte256*)src)[0];
  753. dest += 256;
  754. src += 256;
  755. byteCount -= 256;
  756. }
  757. if (byteCount >= 128)
  758. {
  759. ((Byte128*)dest)[0] = ((Byte128*)src)[0];
  760. dest += 128;
  761. src += 128;
  762. byteCount -= 128;
  763. }
  764. g64: if (byteCount >= 64)
  765. {
  766. ((Byte64*)dest)[0] = ((Byte64*)src)[0];
  767. dest += 64;
  768. src += 64;
  769. byteCount -= 64;
  770. }
  771. if (byteCount >= 32)
  772. {
  773. ((Byte32*)dest)[0] = ((Byte32*)src)[0];
  774. dest += 32;
  775. src += 32;
  776. byteCount -= 32;
  777. }
  778. if (byteCount >= 16)
  779. {
  780. ((Byte16*)dest)[0] = ((Byte16*)src)[0];
  781. dest += 16;
  782. src += 16;
  783. byteCount -= 16;
  784. }
  785. if (byteCount >= 8)
  786. {
  787. ((Byte8*)dest)[0] = ((Byte8*)src)[0];
  788. dest += 8;
  789. src += 8;
  790. byteCount -= 8;
  791. }
  792. if (byteCount >= 4)
  793. {
  794. ((Byte4*)dest)[0] = ((Byte4*)src)[0];
  795. dest += 4;
  796. src += 4;
  797. byteCount -= 4;
  798. }
  799. if (byteCount >= 2)
  800. {
  801. ((Byte2*)dest)[0] = ((Byte2*)src)[0];
  802. dest += 2;
  803. src += 2;
  804. byteCount -= 2;
  805. }
  806. if (byteCount >= 1)
  807. {
  808. ((Byte1*)dest)[0] = ((Byte1*)src)[0];
  809. dest += 1;
  810. src += 1;
  811. byteCount -= 1;
  812. }
  813. }
  814. //-----------------------------------------------------------------------------------------//
  815. //将字符串dst用字符src填充
  816. private unsafe static void memcpy(string dst, char src)
  817. {
  818. fixed (char* ptr_dst = dst)
  819. {
  820. int len = dst.Length;
  821. for (int i = 0; i < len; i++)
  822. ptr_dst[i] = src;
  823. }
  824. }
  825. //将字符拷贝到dst指定index位置
  826. private unsafe static void memcpy(string dst, char src, int index)
  827. {
  828. fixed (char* ptr = dst)
  829. ptr[index] = src;
  830. }
  831. //将相同长度的src内容拷入dst
  832. private unsafe static void memcpy(string dst, string src)
  833. {
  834. if (dst.Length != src.Length)
  835. throw new InvalidOperationException("两个字符串参数长度不一致。");
  836. fixed (char* dst_ptr = dst)
  837. {
  838. fixed (char* src_ptr = src)
  839. {
  840. memcpy(dst_ptr, src_ptr, dst.Length);
  841. }
  842. }
  843. }
  844. //将src指定length内容拷入dst,dst下标src_offset偏移
  845. private unsafe static void memcpy(char* dst, char* src, int length, int src_offset)
  846. {
  847. memcpy(dst + src_offset, src, length);
  848. }
  849. private unsafe static void memcpy(string dst, string src, int length, int src_offset)
  850. {
  851. fixed (char* ptr_dst = dst)
  852. {
  853. fixed (char* ptr_src = src)
  854. {
  855. memcpy(ptr_dst + src_offset, ptr_src, length);
  856. }
  857. }
  858. }
  859. public class zstring_block : IDisposable
  860. {
  861. readonly Stack<zstring> stack;
  862. internal zstring_block(int capacity)
  863. {
  864. stack = new Stack<zstring>(capacity);
  865. }
  866. internal void push(zstring str)
  867. {
  868. stack.Push(str);
  869. }
  870. internal IDisposable begin()//构造函数
  871. {
  872. #if DBG
  873. if (log != null)
  874. log("Began block");
  875. #endif
  876. return this;
  877. }
  878. void IDisposable.Dispose()//析构函数
  879. {
  880. #if DBG
  881. if (log != null)
  882. log("Disposing block");
  883. #endif
  884. while (stack.Count > 0)
  885. {
  886. var str = stack.Pop();
  887. str.dispose();//循环调用栈中zstring的Dispose方法
  888. }
  889. zstring.g_blocks.Push(this);//将自身push入缓存栈
  890. //赋值currentBlock
  891. g_open_blocks.Pop();
  892. if (g_open_blocks.Count > 0)
  893. {
  894. zstring.g_current_block = g_open_blocks.Peek();
  895. }
  896. else
  897. {
  898. zstring.g_current_block = null;
  899. }
  900. }
  901. }
  902. // Public API
  903. #region
  904. public static Action<string> Log = null;
  905. public static uint DecimalAccuracy = 3; // 小数点后精度位数
  906. //获取字符串长度
  907. public int Length
  908. {
  909. get { return _value.Length; }
  910. }
  911. //类构造:cache_capacity缓存栈字典容量,stack_capacity缓存字符串栈容量,block_capacity缓存栈容量,intern_capacity缓存,open_capacity默认打开层数
  912. public static void Initialize(int cache_capacity, int stack_capacity, int block_capacity, int intern_capacity, int open_capacity, int shallowCache_capacity)
  913. {
  914. g_cache = new Queue<zstring>[cache_capacity];
  915. g_secCache = new Dictionary<int, Queue<zstring>>(cache_capacity);
  916. g_blocks = new Stack<zstring_block>(block_capacity);
  917. g_intern_table = new Dictionary<int, string>(intern_capacity);
  918. g_open_blocks = new Stack<zstring_block>(open_capacity);
  919. g_shallowCache = new Stack<zstring>(shallowCache_capacity);
  920. for (int c = 0; c < cache_capacity; c++)
  921. {
  922. var stack = new Queue<zstring>(stack_capacity);
  923. for (int j = 0; j < stack_capacity; j++)
  924. stack.Enqueue(new zstring(c));
  925. g_cache[c] = stack;
  926. }
  927. for (int i = 0; i < block_capacity; i++)
  928. {
  929. var block = new zstring_block(block_capacity * 2);
  930. g_blocks.Push(block);
  931. }
  932. for (int i = 0; i < shallowCache_capacity; i++)
  933. {
  934. g_shallowCache.Push(new zstring(null, true));
  935. }
  936. }
  937. //using语法所用。从zstring_block栈中取出一个block并将其置为当前g_current_block,在代码块{}中新生成的zstring都将push入块内部stack中。当离开块作用域时,调用块的Dispose函数,将内栈中所有zstring填充初始值并放入zstring缓存栈。同时将自身放入block缓存栈中。(此处有个问题:使用Stack缓存block,当block被dispose放入Stack后g_current_block仍然指向此block,无法记录此block之前的block,这样导致zstring.Block()无法嵌套使用)
  938. public static IDisposable Block()
  939. {
  940. if (g_blocks.Count == 0)
  941. g_current_block = new zstring_block(INITIAL_BLOCK_CAPACITY * 2);
  942. else
  943. g_current_block = g_blocks.Pop();
  944. g_open_blocks.Push(g_current_block);//新加代码,将此玩意压入open栈
  945. return g_current_block.begin();
  946. }
  947. //将zstring value放入intern缓存表中以供外部使用
  948. public string Intern()
  949. {
  950. //string interned = new string(NEW_ALLOC_CHAR, _value.Length);
  951. //memcpy(interned, _value);
  952. //return interned;
  953. return __intern(_value);
  954. }
  955. //将string放入zstring intern缓存表中以供外部使用
  956. public static string Intern(string value)
  957. {
  958. return __intern(value);
  959. }
  960. public static void Intern(string[] values)
  961. {
  962. for (int i = 0; i < values.Length; i++)
  963. __intern(values[i]);
  964. }
  965. //下标取值函数
  966. public char this[int i]
  967. {
  968. get { return _value[i]; }
  969. set { memcpy(this, value, i); }
  970. }
  971. //获取hashcode
  972. public override int GetHashCode()
  973. {
  974. return _value.GetHashCode();
  975. }
  976. //字面值比较
  977. public override bool Equals(object obj)
  978. {
  979. if (obj == null)
  980. return ReferenceEquals(this, null);
  981. var gstr = obj as zstring;
  982. if (gstr != null)
  983. return gstr._value == this._value;
  984. var str = obj as string;
  985. if (str != null)
  986. return str == this._value;
  987. return false;
  988. }
  989. //转化为string
  990. public override string ToString()
  991. {
  992. return _value;
  993. }
  994. //bool->zstring转换
  995. public static implicit operator zstring(bool value)
  996. {
  997. return get(value ? "True" : "False");
  998. }
  999. // long - >zstring转换
  1000. public unsafe static implicit operator zstring(long value)
  1001. {
  1002. // e.g. 125
  1003. // first pass: count the number of digits
  1004. // then: get a zstring with length = num digits
  1005. // finally: iterate again, get the char of each digit, memcpy char to result
  1006. bool negative = value < 0;
  1007. value = Math.Abs(value);
  1008. int num_digits = get_digit_count(value);
  1009. zstring result;
  1010. if (negative)
  1011. {
  1012. result = get(num_digits + 1);
  1013. fixed (char* ptr = result._value)
  1014. {
  1015. *ptr = '-';
  1016. longcpy(ptr, value, 1, num_digits);
  1017. }
  1018. }
  1019. else
  1020. {
  1021. result = get(num_digits);
  1022. fixed (char* ptr = result._value)
  1023. longcpy(ptr, value, 0, num_digits);
  1024. }
  1025. return result;
  1026. }
  1027. //int->zstring转换
  1028. public unsafe static implicit operator zstring(int value)
  1029. {
  1030. // e.g. 125
  1031. // first pass: count the number of digits
  1032. // then: get a zstring with length = num digits
  1033. // finally: iterate again, get the char of each digit, memcpy char to result
  1034. bool negative = value < 0;
  1035. value = Math.Abs(value);
  1036. int num_digits = get_digit_count(value);
  1037. zstring result;
  1038. if (negative)
  1039. {
  1040. result = get(num_digits + 1);
  1041. fixed (char* ptr = result._value)
  1042. {
  1043. *ptr = '-';
  1044. intcpy(ptr, value, 1, num_digits);
  1045. }
  1046. }
  1047. else
  1048. {
  1049. result = get(num_digits);
  1050. fixed (char* ptr = result._value)
  1051. intcpy(ptr, value, 0, num_digits);
  1052. }
  1053. return result;
  1054. }
  1055. //float->zstring转换
  1056. public unsafe static implicit operator zstring(float value)
  1057. {
  1058. // e.g. 3.148
  1059. bool negative = value < 0;
  1060. if (negative) value = -value;
  1061. long mul = (long)Math.Pow(10, DecimalAccuracy);
  1062. long number = (long)(value * mul); // gets the number as a whole, e.g. 3148
  1063. int left_num = (int)(number / mul); // left part of the decimal point, e.g. 3
  1064. int right_num = (int)(number % mul); // right part of the decimal pnt, e.g. 148
  1065. int left_digit_count = get_digit_count(left_num); // e.g. 1
  1066. int right_digit_count = get_digit_count(right_num); // e.g. 3
  1067. //int total = left_digit_count + right_digit_count + 1; // +1 for '.'
  1068. int total = left_digit_count + (int)DecimalAccuracy + 1; // +1 for '.'
  1069. zstring result;
  1070. if (negative)
  1071. {
  1072. result = get(total + 1); // +1 for '-'
  1073. fixed (char* ptr = result._value)
  1074. {
  1075. *ptr = '-';
  1076. intcpy(ptr, left_num, 1, left_digit_count);
  1077. *(ptr + left_digit_count + 1) = '.';
  1078. int offest = (int)DecimalAccuracy - right_digit_count;
  1079. for (int i = 0; i < offest; i++)
  1080. *(ptr + left_digit_count + i + 1) = '0';
  1081. intcpy(ptr, right_num, left_digit_count + 2 + offest, right_digit_count);
  1082. }
  1083. }
  1084. else
  1085. {
  1086. result = get(total);
  1087. fixed (char* ptr = result._value)
  1088. {
  1089. intcpy(ptr, left_num, 0, left_digit_count);
  1090. *(ptr + left_digit_count) = '.';
  1091. int offest = (int)DecimalAccuracy - right_digit_count;
  1092. for (int i = 0; i < offest; i++)
  1093. *(ptr + left_digit_count + i + 1) = '0';
  1094. intcpy(ptr, right_num, left_digit_count + 1 + offest, right_digit_count);
  1095. }
  1096. }
  1097. return result;
  1098. }
  1099. //string->zstring转换
  1100. public static implicit operator zstring(string value)
  1101. {
  1102. //return get(value);
  1103. return getShallow(value);
  1104. }
  1105. //string->zstring转换
  1106. public static zstring shallow(string value)
  1107. {
  1108. return getShallow(value);
  1109. }
  1110. //zstring->string转换
  1111. public static implicit operator string(zstring value)
  1112. {
  1113. return value._value;
  1114. }
  1115. //+重载
  1116. public static zstring operator +(zstring left, zstring right)
  1117. {
  1118. return internal_concat(left, right);
  1119. }
  1120. //==重载
  1121. public static bool operator ==(zstring left, zstring right)
  1122. {
  1123. if (ReferenceEquals(left, null))
  1124. return ReferenceEquals(right, null);
  1125. if (ReferenceEquals(right, null))
  1126. return false;
  1127. return left._value == right._value;
  1128. }
  1129. //!=重载
  1130. public static bool operator !=(zstring left, zstring right)
  1131. {
  1132. return !(left._value == right._value);
  1133. }
  1134. //转换为大写
  1135. public unsafe zstring ToUpper()
  1136. {
  1137. var result = get(Length);
  1138. fixed (char* ptr_this = this._value)
  1139. {
  1140. fixed (char* ptr_result = result._value)
  1141. {
  1142. for (int i = 0; i < _value.Length; i++)
  1143. {
  1144. var ch = ptr_this[i];
  1145. if (char.IsLower(ch))
  1146. ptr_result[i] = char.ToUpper(ch);
  1147. else
  1148. ptr_result[i] = ptr_this[i];
  1149. }
  1150. }
  1151. }
  1152. return result;
  1153. }
  1154. //转换为小写
  1155. public unsafe zstring ToLower()
  1156. {
  1157. var result = get(Length);
  1158. fixed (char* ptr_this = this._value)
  1159. {
  1160. fixed (char* ptr_result = result._value)
  1161. {
  1162. for (int i = 0; i < _value.Length; i++)
  1163. {
  1164. var ch = ptr_this[i];
  1165. if (char.IsUpper(ch))
  1166. ptr_result[i] = char.ToLower(ch);
  1167. else
  1168. ptr_result[i] = ptr_this[i];
  1169. }
  1170. }
  1171. }
  1172. return result;
  1173. }
  1174. //移除剪切
  1175. public zstring Remove(int start)
  1176. {
  1177. return Remove(start, Length - start);
  1178. }
  1179. //移除剪切
  1180. public zstring Remove(int start, int count)
  1181. {
  1182. return internal_remove(this._value, start, count);
  1183. }
  1184. //插入start起count长度字符
  1185. public zstring Insert(char value, int start, int count)
  1186. {
  1187. return internal_insert(this._value, value, start, count);
  1188. }
  1189. //插入start起字符串
  1190. public zstring Insert(string value, int start)
  1191. {
  1192. return internal_insert(this._value, value, start);
  1193. }
  1194. //子字符替换
  1195. public unsafe zstring Replace(char old_value, char new_value)
  1196. {
  1197. zstring result = get(Length);
  1198. fixed (char* ptr_this = this._value)
  1199. {
  1200. fixed (char* ptr_result = result._value)
  1201. {
  1202. for (int i = 0; i < Length; i++)
  1203. {
  1204. ptr_result[i] = ptr_this[i] == old_value ? new_value : ptr_this[i];
  1205. }
  1206. }
  1207. }
  1208. return result;
  1209. }
  1210. //子字符串替换
  1211. public zstring Replace(string old_value, string new_value)
  1212. {
  1213. return internal_replace(this._value, old_value, new_value);
  1214. }
  1215. //剪切start位置起后续子串
  1216. public zstring Substring(int start)
  1217. {
  1218. return Substring(start, Length - start);
  1219. }
  1220. //剪切start起count长度的子串
  1221. public unsafe zstring Substring(int start, int count)
  1222. {
  1223. if (start < 0 || start >= Length)
  1224. throw new ArgumentOutOfRangeException("start");
  1225. if (count > Length)
  1226. throw new ArgumentOutOfRangeException("count");
  1227. zstring result = get(count);
  1228. fixed (char* src = this._value)
  1229. fixed (char* dst = result._value)
  1230. memcpy(dst, src + start, count);
  1231. return result;
  1232. }
  1233. //子串包含判断
  1234. public bool Contains(string value)
  1235. {
  1236. return IndexOf(value) != -1;
  1237. }
  1238. //字符包含判断
  1239. public bool Contains(char value)
  1240. {
  1241. return IndexOf(value) != -1;
  1242. }
  1243. //子串第一次出现位置
  1244. public int LastIndexOf(string value)
  1245. {
  1246. int idx = -1;
  1247. int last_find = -1;
  1248. while (true)
  1249. {
  1250. idx = internal_index_of(this._value, value, idx + value.Length);
  1251. last_find = idx;
  1252. if (idx == -1 || idx + value.Length >= this._value.Length)
  1253. break;
  1254. }
  1255. return last_find;
  1256. }
  1257. //字符第一次出现位置
  1258. public int LastIndexOf(char value)
  1259. {
  1260. int idx = -1;
  1261. int last_find = -1;
  1262. while (true)
  1263. {
  1264. idx = internal_index_of(this._value, value, idx + 1);
  1265. last_find = idx;
  1266. if (idx == -1 || idx + 1 >= this._value.Length)
  1267. break;
  1268. }
  1269. return last_find;
  1270. }
  1271. //字符第一次出现位置
  1272. public int IndexOf(char value)
  1273. {
  1274. return IndexOf(value, 0, Length);
  1275. }
  1276. //字符自start起第一次出现位置
  1277. public int IndexOf(char value, int start)
  1278. {
  1279. return internal_index_of(this._value, value, start);
  1280. }
  1281. //字符自start起count长度内,
  1282. public int IndexOf(char value, int start, int count)
  1283. {
  1284. return internal_index_of(this._value, value, start, count);
  1285. }
  1286. //子串第一次出现位置
  1287. public int IndexOf(string value)
  1288. {
  1289. return IndexOf(value, 0, Length);
  1290. }
  1291. //子串自start位置起,第一次出现位置
  1292. public int IndexOf(string value, int start)
  1293. {
  1294. return IndexOf(value, start, Length - start);
  1295. }
  1296. //子串自start位置起,count长度内第一次出现位置
  1297. public int IndexOf(string value, int start, int count)
  1298. {
  1299. return internal_index_of(this._value, value, start, count);
  1300. }
  1301. //是否以某字符串结束
  1302. public unsafe bool EndsWith(string postfix)
  1303. {
  1304. if (postfix == null)
  1305. throw new ArgumentNullException("postfix");
  1306. if (this.Length < postfix.Length)
  1307. return false;
  1308. fixed (char* ptr_this = this._value)
  1309. {
  1310. fixed (char* ptr_postfix = postfix)
  1311. {
  1312. for (int i = this._value.Length - 1, j = postfix.Length - 1; j >= 0; i--, j--)
  1313. if (ptr_this[i] != ptr_postfix[j])
  1314. return false;
  1315. }
  1316. }
  1317. return true;
  1318. }
  1319. //是否以某字符串开始
  1320. public unsafe bool StartsWith(string prefix)
  1321. {
  1322. if (prefix == null)
  1323. throw new ArgumentNullException("prefix");
  1324. if (this.Length < prefix.Length)
  1325. return false;
  1326. fixed (char* ptr_this = this._value)
  1327. {
  1328. fixed (char* ptr_prefix = prefix)
  1329. {
  1330. for (int i = 0; i < prefix.Length; i++)
  1331. if (ptr_this[i] != ptr_prefix[i])
  1332. return false;
  1333. }
  1334. }
  1335. return true;
  1336. }
  1337. //获取某长度字符串缓存数量
  1338. public static int GetCacheCount(int length)
  1339. {
  1340. Queue<zstring> stack;
  1341. getStackInCache(length, out stack);
  1342. return stack.Count;
  1343. }
  1344. //自身+value拼接
  1345. public zstring Concat(zstring value)
  1346. {
  1347. return internal_concat(this, value);
  1348. }
  1349. //静态拼接方法簇
  1350. public static zstring Concat(zstring s0, zstring s1) { return s0 + s1; }
  1351. public static zstring Concat(zstring s0, zstring s1, zstring s2) { return s0 + s1 + s2; }
  1352. public static zstring Concat(zstring s0, zstring s1, zstring s2, zstring s3) { return s0 + s1 + s2 + s3; }
  1353. public static zstring Concat(zstring s0, zstring s1, zstring s2, zstring s3, zstring s4) { return s0 + s1 + s2 + s3 + s4; }
  1354. public static zstring Concat(zstring s0, zstring s1, zstring s2, zstring s3, zstring s4, zstring s5) { return s0 + s1 + s2 + s3 + s4 + s5; }
  1355. public static zstring Concat(zstring s0, zstring s1, zstring s2, zstring s3, zstring s4, zstring s5, zstring s6) { return s0 + s1 + s2 + s3 + s4 + s5 + s6; }
  1356. public static zstring Concat(zstring s0, zstring s1, zstring s2, zstring s3, zstring s4, zstring s5, zstring s6, zstring s7) { return s0 + s1 + s2 + s3 + s4 + s5 + s6 + s7; }
  1357. public static zstring Concat(zstring s0, zstring s1, zstring s2, zstring s3, zstring s4, zstring s5, zstring s6, zstring s7, zstring s8) { return s0 + s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8; }
  1358. public static zstring Concat(zstring s0, zstring s1, zstring s2, zstring s3, zstring s4, zstring s5, zstring s6, zstring s7, zstring s8, zstring s9) { return s0 + s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; }
  1359. //静态格式化方法簇
  1360. public static zstring Format(string input, zstring arg0, zstring arg1, zstring arg2, zstring arg3, zstring arg4, zstring arg5, zstring arg6, zstring arg7, zstring arg8, zstring arg9)
  1361. {
  1362. if (arg0 == null) throw new ArgumentNullException("arg0");
  1363. if (arg1 == null) throw new ArgumentNullException("arg1");
  1364. if (arg2 == null) throw new ArgumentNullException("arg2");
  1365. if (arg3 == null) throw new ArgumentNullException("arg3");
  1366. if (arg4 == null) throw new ArgumentNullException("arg4");
  1367. if (arg5 == null) throw new ArgumentNullException("arg5");
  1368. if (arg6 == null) throw new ArgumentNullException("arg6");
  1369. if (arg7 == null) throw new ArgumentNullException("arg7");
  1370. if (arg8 == null) throw new ArgumentNullException("arg8");
  1371. if (arg9 == null) throw new ArgumentNullException("arg9");
  1372. g_format_args[0] = arg0;
  1373. g_format_args[1] = arg1;
  1374. g_format_args[2] = arg2;
  1375. g_format_args[3] = arg3;
  1376. g_format_args[4] = arg4;
  1377. g_format_args[5] = arg5;
  1378. g_format_args[6] = arg6;
  1379. g_format_args[7] = arg7;
  1380. g_format_args[8] = arg8;
  1381. g_format_args[9] = arg9;
  1382. return internal_format(input, 10);
  1383. }
  1384. public static zstring Format(string input, zstring arg0, zstring arg1, zstring arg2, zstring arg3, zstring arg4, zstring arg5, zstring arg6, zstring arg7, zstring arg8)
  1385. {
  1386. if (arg0 == null) throw new ArgumentNullException("arg0");
  1387. if (arg1 == null) throw new ArgumentNullException("arg1");
  1388. if (arg2 == null) throw new ArgumentNullException("arg2");
  1389. if (arg3 == null) throw new ArgumentNullException("arg3");
  1390. if (arg4 == null) throw new ArgumentNullException("arg4");
  1391. if (arg5 == null) throw new ArgumentNullException("arg5");
  1392. if (arg6 == null) throw new ArgumentNullException("arg6");
  1393. if (arg7 == null) throw new ArgumentNullException("arg7");
  1394. if (arg8 == null) throw new ArgumentNullException("arg8");
  1395. g_format_args[0] = arg0;
  1396. g_format_args[1] = arg1;
  1397. g_format_args[2] = arg2;
  1398. g_format_args[3] = arg3;
  1399. g_format_args[4] = arg4;
  1400. g_format_args[5] = arg5;
  1401. g_format_args[6] = arg6;
  1402. g_format_args[7] = arg7;
  1403. g_format_args[8] = arg8;
  1404. return internal_format(input, 9);
  1405. }
  1406. public static zstring Format(string input, zstring arg0, zstring arg1, zstring arg2, zstring arg3, zstring arg4, zstring arg5, zstring arg6, zstring arg7)
  1407. {
  1408. if (arg0 == null) throw new ArgumentNullException("arg0");
  1409. if (arg1 == null) throw new ArgumentNullException("arg1");
  1410. if (arg2 == null) throw new ArgumentNullException("arg2");
  1411. if (arg3 == null) throw new ArgumentNullException("arg3");
  1412. if (arg4 == null) throw new ArgumentNullException("arg4");
  1413. if (arg5 == null) throw new ArgumentNullException("arg5");
  1414. if (arg6 == null) throw new ArgumentNullException("arg6");
  1415. if (arg7 == null) throw new ArgumentNullException("arg7");
  1416. g_format_args[0] = arg0;
  1417. g_format_args[1] = arg1;
  1418. g_format_args[2] = arg2;
  1419. g_format_args[3] = arg3;
  1420. g_format_args[4] = arg4;
  1421. g_format_args[5] = arg5;
  1422. g_format_args[6] = arg6;
  1423. g_format_args[7] = arg7;
  1424. return internal_format(input, 8);
  1425. }
  1426. public static zstring Format(string input, zstring arg0, zstring arg1, zstring arg2, zstring arg3, zstring arg4, zstring arg5, zstring arg6)
  1427. {
  1428. if (arg0 == null) throw new ArgumentNullException("arg0");
  1429. if (arg1 == null) throw new ArgumentNullException("arg1");
  1430. if (arg2 == null) throw new ArgumentNullException("arg2");
  1431. if (arg3 == null) throw new ArgumentNullException("arg3");
  1432. if (arg4 == null) throw new ArgumentNullException("arg4");
  1433. if (arg5 == null) throw new ArgumentNullException("arg5");
  1434. if (arg6 == null) throw new ArgumentNullException("arg6");
  1435. g_format_args[0] = arg0;
  1436. g_format_args[1] = arg1;
  1437. g_format_args[2] = arg2;
  1438. g_format_args[3] = arg3;
  1439. g_format_args[4] = arg4;
  1440. g_format_args[5] = arg5;
  1441. g_format_args[6] = arg6;
  1442. return internal_format(input, 7);
  1443. }
  1444. public static zstring Format(string input, zstring arg0, zstring arg1, zstring arg2, zstring arg3, zstring arg4, zstring arg5)
  1445. {
  1446. if (arg0 == null) throw new ArgumentNullException("arg0");
  1447. if (arg1 == null) throw new ArgumentNullException("arg1");
  1448. if (arg2 == null) throw new ArgumentNullException("arg2");
  1449. if (arg3 == null) throw new ArgumentNullException("arg3");
  1450. if (arg4 == null) throw new ArgumentNullException("arg4");
  1451. if (arg5 == null) throw new ArgumentNullException("arg5");
  1452. g_format_args[0] = arg0;
  1453. g_format_args[1] = arg1;
  1454. g_format_args[2] = arg2;
  1455. g_format_args[3] = arg3;
  1456. g_format_args[4] = arg4;
  1457. g_format_args[5] = arg5;
  1458. return internal_format(input, 6);
  1459. }
  1460. public static zstring Format(string input, zstring arg0, zstring arg1, zstring arg2, zstring arg3, zstring arg4)
  1461. {
  1462. if (arg0 == null) throw new ArgumentNullException("arg0");
  1463. if (arg1 == null) throw new ArgumentNullException("arg1");
  1464. if (arg2 == null) throw new ArgumentNullException("arg2");
  1465. if (arg3 == null) throw new ArgumentNullException("arg3");
  1466. if (arg4 == null) throw new ArgumentNullException("arg4");
  1467. g_format_args[0] = arg0;
  1468. g_format_args[1] = arg1;
  1469. g_format_args[2] = arg2;
  1470. g_format_args[3] = arg3;
  1471. g_format_args[4] = arg4;
  1472. return internal_format(input, 5);
  1473. }
  1474. public static zstring Format(string input, zstring arg0, zstring arg1, zstring arg2, zstring arg3)
  1475. {
  1476. if (arg0 == null) throw new ArgumentNullException("arg0");
  1477. if (arg1 == null) throw new ArgumentNullException("arg1");
  1478. if (arg2 == null) throw new ArgumentNullException("arg2");
  1479. if (arg3 == null) throw new ArgumentNullException("arg3");
  1480. g_format_args[0] = arg0;
  1481. g_format_args[1] = arg1;
  1482. g_format_args[2] = arg2;
  1483. g_format_args[3] = arg3;
  1484. return internal_format(input, 4);
  1485. }
  1486. public static zstring Format(string input, zstring arg0, zstring arg1, zstring arg2)
  1487. {
  1488. if (arg0 == null) throw new ArgumentNullException("arg0");
  1489. if (arg1 == null) throw new ArgumentNullException("arg1");
  1490. if (arg2 == null) throw new ArgumentNullException("arg2");
  1491. g_format_args[0] = arg0;
  1492. g_format_args[1] = arg1;
  1493. g_format_args[2] = arg2;
  1494. return internal_format(input, 3);
  1495. }
  1496. public static zstring Format(string input, zstring arg0, zstring arg1)
  1497. {
  1498. if (arg0 == null) throw new ArgumentNullException("arg0");
  1499. if (arg1 == null) throw new ArgumentNullException("arg1");
  1500. g_format_args[0] = arg0;
  1501. g_format_args[1] = arg1;
  1502. return internal_format(input, 2);
  1503. }
  1504. public static zstring Format(string input, zstring arg0)
  1505. {
  1506. if (arg0 == null) throw new ArgumentNullException("arg0");
  1507. g_format_args[0] = arg0;
  1508. return internal_format(input, 1);
  1509. }
  1510. // 普通的float->string是隐式转换,小数点后只保留三位有效数字
  1511. // 对于更高精确度需求,隐式转换,可以修改静态变量DecimalAccuracy
  1512. // 显式转换使用此方法即可,函数结束DecimalAccuracy值和之前的一样
  1513. public static zstring FloatToZstring(float value, uint DecimalAccuracy)
  1514. {
  1515. uint oldValue = zstring.DecimalAccuracy;
  1516. zstring.DecimalAccuracy = DecimalAccuracy;
  1517. zstring target = (zstring)value;
  1518. zstring.DecimalAccuracy = oldValue;
  1519. return target;
  1520. }
  1521. //判空或长度
  1522. public static bool IsNullOrEmpty(zstring str)
  1523. {
  1524. return str == null || str.Length == 0;
  1525. }
  1526. //是否以value结束
  1527. public static bool IsPrefix(zstring str, string value)
  1528. {
  1529. return str.StartsWith(value);
  1530. }
  1531. //是否以value开始
  1532. public static bool isPostfix(zstring str, string postfix)
  1533. {
  1534. return str.EndsWith(postfix);
  1535. }
  1536. #endregion
  1537. }
  1538. }