PageRenderTime 48ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/src/BuildUtil/CoreUtil/Str.cs

https://github.com/NealSCarffery/SoftEtherVPN
C# | 4392 lines | 4048 code | 239 blank | 105 comment | 524 complexity | 4001b5b40ffd11e9a67a41f2a45fbbf4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. // CoreUtil
  2. //
  3. // Copyright (C) 2012-2014 Daiyuu Nobori. All Rights Reserved.
  4. // Copyright (C) 2012-2014 SoftEther VPN Project at University of Tsukuba. All Rights Reserved.
  5. // Comments: Tetsuo Sugiyama, Ph.D.
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // version 2 as published by the Free Software Foundation.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License version 2
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  29. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  30. //
  31. //
  32. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  33. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  34. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  35. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  36. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  37. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  38. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  39. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  40. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  41. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  42. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  43. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  44. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  45. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  46. //
  47. // USE ONLY IN JAPAN. DO NOT USE IT IN OTHER COUNTRIES. IMPORTING THIS
  48. // SOFTWARE INTO OTHER COUNTRIES IS AT YOUR OWN RISK. SOME COUNTRIES
  49. // PROHIBIT ENCRYPTED COMMUNICATIONS. USING THIS SOFTWARE IN OTHER
  50. // COUNTRIES MIGHT BE RESTRICTED.
  51. //
  52. //
  53. // SOURCE CODE CONTRIBUTION
  54. // ------------------------
  55. //
  56. // Your contribution to SoftEther VPN Project is much appreciated.
  57. // Please send patches to us through GitHub.
  58. // Read the SoftEther VPN Patch Acceptance Policy in advance:
  59. // http://www.softether.org/5-download/src/9.patch
  60. //
  61. //
  62. // DEAR SECURITY EXPERTS
  63. // ---------------------
  64. //
  65. // If you find a bug or a security vulnerability please kindly inform us
  66. // about the problem immediately so that we can fix the security problem
  67. // to protect a lot of users around the world as soon as possible.
  68. //
  69. // Our e-mail address for security reports is:
  70. // softether-vpn-security [at] softether.org
  71. //
  72. // Please note that the above e-mail address is not a technical support
  73. // inquiry address. If you need technical assistance, please visit
  74. // http://www.softether.org/ and ask your question on the users forum.
  75. //
  76. // Thank you for your cooperation.
  77. //
  78. //
  79. // NO MEMORY OR RESOURCE LEAKS
  80. // ---------------------------
  81. //
  82. // The memory-leaks and resource-leaks verification under the stress
  83. // test has been passed before release this source code.
  84. using System;
  85. using System.Threading;
  86. using System.Data;
  87. using System.Data.Sql;
  88. using System.Data.SqlClient;
  89. using System.Data.SqlTypes;
  90. using System.Text;
  91. using System.Configuration;
  92. using System.Collections;
  93. using System.Collections.Generic;
  94. using System.Security.Cryptography;
  95. using System.Web;
  96. using System.Web.Security;
  97. using System.Web.UI;
  98. using System.Web.UI.WebControls;
  99. using System.Web.UI.WebControls.WebParts;
  100. using System.Web.UI.HtmlControls;
  101. using System.IO;
  102. using System.Drawing;
  103. using System.Drawing.Imaging;
  104. using System.Drawing.Drawing2D;
  105. using System.Runtime.Serialization.Formatters.Soap;
  106. using System.Runtime.Serialization.Formatters.Binary;
  107. using System.Xml;
  108. using System.Xml.Serialization;
  109. using System.Web.Mail;
  110. using System.Runtime.InteropServices;
  111. namespace CoreUtil
  112. {
  113. [FlagsAttribute]
  114. public enum PrintFFLags
  115. {
  116. Minus = 1,
  117. Plus = 2,
  118. Zero = 4,
  119. Blank = 8,
  120. Sharp = 16,
  121. }
  122. public class PrintFParsedParam
  123. {
  124. public bool Ok = false;
  125. public readonly PrintFFLags Flags = 0;
  126. public readonly int Width = 0;
  127. public readonly int Precision = 0;
  128. public readonly bool NoPrecision = true;
  129. public readonly string Type = "";
  130. static PrintFFLags charToFlag(char c)
  131. {
  132. switch (c)
  133. {
  134. case '-':
  135. return PrintFFLags.Minus;
  136. case '+':
  137. return PrintFFLags.Plus;
  138. case '0':
  139. return PrintFFLags.Zero;
  140. case ' ':
  141. return PrintFFLags.Blank;
  142. case '#':
  143. return PrintFFLags.Sharp;
  144. }
  145. return 0;
  146. }
  147. public string GetString(object param)
  148. {
  149. int i;
  150. StringBuilder sb;
  151. string tmp = "(error)";
  152. double f;
  153. bool signed = false;
  154. switch (this.Type)
  155. {
  156. case "c":
  157. case "C":
  158. if (param is char)
  159. {
  160. tmp += (char)param;
  161. }
  162. else if (param is string)
  163. {
  164. string s = (string)param;
  165. if (s.Length >= 1)
  166. {
  167. tmp += s[0];
  168. }
  169. }
  170. break;
  171. case "d":
  172. case "i":
  173. sb = new StringBuilder();
  174. int count = this.Width;
  175. if (this.Precision != 0)
  176. {
  177. count = this.Precision;
  178. }
  179. for (i = 1; i < this.Precision; i++)
  180. {
  181. sb.Append("#");
  182. }
  183. sb.Append("0");
  184. if (param is int)
  185. {
  186. tmp = ((int)param).ToString(sb.ToString());
  187. }
  188. else if (param is long)
  189. {
  190. tmp = ((long)param).ToString(sb.ToString());
  191. }
  192. else if (param is uint)
  193. {
  194. tmp = ((int)((uint)param)).ToString(sb.ToString());
  195. }
  196. else if (param is ulong)
  197. {
  198. tmp = ((long)((ulong)param)).ToString(sb.ToString());
  199. }
  200. else if (param is decimal)
  201. {
  202. tmp = ((decimal)param).ToString(sb.ToString());
  203. }
  204. signed = true;
  205. break;
  206. case "u":
  207. sb = new StringBuilder();
  208. for (i = 1; i < this.Precision; i++)
  209. {
  210. sb.Append("#");
  211. }
  212. sb.Append("0");
  213. if (param is int)
  214. {
  215. tmp = ((uint)((int)param)).ToString(sb.ToString());
  216. }
  217. else if (param is long)
  218. {
  219. tmp = ((ulong)((long)param)).ToString(sb.ToString());
  220. }
  221. else if (param is uint)
  222. {
  223. tmp = ((uint)param).ToString(sb.ToString());
  224. }
  225. else if (param is ulong)
  226. {
  227. tmp = ((ulong)param).ToString(sb.ToString());
  228. }
  229. else if (param is decimal)
  230. {
  231. tmp = ((decimal)param).ToString(sb.ToString());
  232. }
  233. break;
  234. case "x":
  235. case "X":
  236. sb = new StringBuilder();
  237. sb.Append(this.Type);
  238. sb.Append(this.Precision.ToString());
  239. if (param is int)
  240. {
  241. tmp = ((uint)((int)param)).ToString(sb.ToString());
  242. }
  243. else if (param is long)
  244. {
  245. tmp = ((ulong)((long)param)).ToString(sb.ToString());
  246. }
  247. else if (param is uint)
  248. {
  249. tmp = ((uint)param).ToString(sb.ToString());
  250. }
  251. else if (param is ulong)
  252. {
  253. tmp = ((ulong)param).ToString(sb.ToString());
  254. }
  255. break;
  256. case "e":
  257. case "E":
  258. case "f":
  259. f = 0;
  260. if (param is int)
  261. {
  262. f = (double)((int)param);
  263. }
  264. else if (param is long)
  265. {
  266. f = (double)((long)param);
  267. }
  268. else if (param is uint)
  269. {
  270. f = (double)((uint)param);
  271. }
  272. else if (param is ulong)
  273. {
  274. f = (double)((ulong)param);
  275. }
  276. else if (param is decimal)
  277. {
  278. f = (double)((long)param);
  279. }
  280. else if (param is float)
  281. {
  282. f = (double)((float)param);
  283. }
  284. else if (param is double)
  285. {
  286. f = (double)param;
  287. }
  288. else
  289. {
  290. break;
  291. }
  292. int prectmp = Precision;
  293. if (prectmp == 0 && NoPrecision)
  294. {
  295. prectmp = 6;
  296. }
  297. tmp = f.ToString(string.Format("{0}{1}", Type, prectmp));
  298. break;
  299. case "s":
  300. case "S":
  301. if (param == null)
  302. {
  303. tmp = "(null)";
  304. }
  305. else
  306. {
  307. tmp = param.ToString();
  308. }
  309. break;
  310. }
  311. int normalWidth = Str.GetStrWidth(tmp);
  312. int targetWidth = Math.Max(this.Width, normalWidth);
  313. if ((this.Flags & PrintFFLags.Plus) != 0)
  314. {
  315. if (signed)
  316. {
  317. if (tmp.StartsWith("-") == false)
  318. {
  319. tmp = "+" + tmp;
  320. }
  321. }
  322. }
  323. else
  324. {
  325. if ((this.Flags & PrintFFLags.Blank) != 0)
  326. {
  327. if (signed)
  328. {
  329. if (tmp.StartsWith("-") == false)
  330. {
  331. tmp = " " + tmp;
  332. }
  333. }
  334. }
  335. }
  336. if ((this.Flags & PrintFFLags.Minus) != 0)
  337. {
  338. int w = targetWidth - Str.GetStrWidth(tmp);
  339. if (w < 0)
  340. {
  341. w = 0;
  342. }
  343. tmp += Str.MakeCharArray(' ', w);
  344. }
  345. else if ((this.Flags & PrintFFLags.Zero) != 0)
  346. {
  347. int w = targetWidth - Str.GetStrWidth(tmp);
  348. if (w < 0)
  349. {
  350. w = 0;
  351. }
  352. tmp = Str.MakeCharArray('0', w) + tmp;
  353. }
  354. else
  355. {
  356. int w = targetWidth - Str.GetStrWidth(tmp);
  357. if (w < 0)
  358. {
  359. w = 0;
  360. }
  361. tmp = Str.MakeCharArray(' ', w) + tmp;
  362. }
  363. if ((this.Flags & PrintFFLags.Sharp) != 0)
  364. {
  365. if (Type == "x" || Type == "X")
  366. {
  367. tmp = "0x" + tmp;
  368. }
  369. }
  370. return tmp;
  371. }
  372. public PrintFParsedParam(string str)
  373. {
  374. Str.NormalizeString(ref str);
  375. if (str.StartsWith("%") == false)
  376. {
  377. return;
  378. }
  379. str = str.Substring(1);
  380. Queue<char> q = new Queue<char>();
  381. foreach (char c in str)
  382. {
  383. q.Enqueue(c);
  384. }
  385. while (q.Count >= 1)
  386. {
  387. char c = q.Peek();
  388. PrintFFLags f = charToFlag(c);
  389. if (f == 0)
  390. {
  391. break;
  392. }
  393. this.Flags |= f;
  394. q.Dequeue();
  395. }
  396. Queue<char> q2 = new Queue<char>();
  397. while (q.Count >= 1)
  398. {
  399. bool bf = false;
  400. char c = q.Peek();
  401. switch (c)
  402. {
  403. case 'h':
  404. case 'l':
  405. case 'I':
  406. case 'c':
  407. case 'C':
  408. case 'd':
  409. case 'i':
  410. case 'o':
  411. case 'u':
  412. case 'x':
  413. case 'X':
  414. case 'e':
  415. case 'E':
  416. case 'f':
  417. case 'g':
  418. case 'G':
  419. case 'n':
  420. case 'p':
  421. case 's':
  422. case 'S':
  423. bf = true;
  424. break;
  425. default:
  426. q2.Enqueue(c);
  427. break;
  428. }
  429. if (bf)
  430. {
  431. break;
  432. }
  433. q.Dequeue();
  434. }
  435. string[] widthAndPrec = (new string(q2.ToArray())).Split('.');
  436. if (widthAndPrec.Length == 1)
  437. {
  438. this.Width = Str.StrToInt(widthAndPrec[0]);
  439. }
  440. else if (widthAndPrec.Length == 2)
  441. {
  442. this.Width = Str.StrToInt(widthAndPrec[0]);
  443. this.Precision = Str.StrToInt(widthAndPrec[1]);
  444. this.NoPrecision = false;
  445. }
  446. this.Width = Math.Max(this.Width, 0);
  447. this.Precision = Math.Max(this.Precision, 0);
  448. while (q.Count >= 1)
  449. {
  450. char c = q.Peek();
  451. bool bf = false;
  452. switch (c)
  453. {
  454. case 'c':
  455. case 'C':
  456. case 'd':
  457. case 'i':
  458. case 'o':
  459. case 'u':
  460. case 'x':
  461. case 'X':
  462. case 'e':
  463. case 'E':
  464. case 'f':
  465. case 'g':
  466. case 'G':
  467. case 'a':
  468. case 'A':
  469. case 'n':
  470. case 'p':
  471. case 's':
  472. case 'S':
  473. bf = true;
  474. break;
  475. default:
  476. break;
  477. }
  478. if (bf)
  479. {
  480. break;
  481. }
  482. q.Dequeue();
  483. }
  484. this.Type = new string(q.ToArray());
  485. if (this.Type.Length >= 1)
  486. {
  487. this.Type = this.Type.Substring(0, 1);
  488. }
  489. this.Ok = (Str.IsEmptyStr(this.Type) == false);
  490. }
  491. }
  492. public class StrEqualityComparer : IEqualityComparer<string>
  493. {
  494. bool caseSensitive;
  495. public StrEqualityComparer()
  496. {
  497. this.caseSensitive = false;
  498. }
  499. public StrEqualityComparer(bool caseSensitive)
  500. {
  501. this.caseSensitive = caseSensitive;
  502. }
  503. public bool Equals(string x, string y)
  504. {
  505. return x.Equals(y, caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase);
  506. }
  507. public int GetHashCode(string obj)
  508. {
  509. return obj.GetHashCode();
  510. }
  511. }
  512. public class StrComparer : IComparer<string>
  513. {
  514. bool caseSensitive;
  515. public StrComparer()
  516. {
  517. this.caseSensitive = false;
  518. }
  519. public StrComparer(bool caseSensitive)
  520. {
  521. this.caseSensitive = caseSensitive;
  522. }
  523. public int Compare(string x, string y)
  524. {
  525. return string.Compare(x, y, !caseSensitive);
  526. }
  527. }
  528. public delegate bool RemoveStringFunction(string str);
  529. public static class Str
  530. {
  531. public static string NormalizeStrSoftEther(string str)
  532. {
  533. return NormalizeStrSoftEther(str, false);
  534. }
  535. public static string NormalizeStrSoftEther(string str, bool trim)
  536. {
  537. bool b = false;
  538. StringReader sr = new StringReader(str);
  539. StringWriter sw = new StringWriter();
  540. while (true)
  541. {
  542. string line = sr.ReadLine();
  543. if (line == null)
  544. {
  545. break;
  546. }
  547. if (b)
  548. {
  549. sw.WriteLine();
  550. }
  551. b = true;
  552. line = normalizeStrSoftEtherInternal(line);
  553. sw.Write(line);
  554. }
  555. int len = str.Length;
  556. try
  557. {
  558. if (str[len - 1] == '\n' || str[len - 1] == '\r')
  559. {
  560. sw.WriteLine();
  561. }
  562. }
  563. catch
  564. {
  565. }
  566. str = sw.ToString();
  567. if (trim)
  568. {
  569. str = str.Trim();
  570. }
  571. return str;
  572. }
  573. static string normalizeStrSoftEtherInternal(string str)
  574. {
  575. if (str.Trim().Length == 0)
  576. {
  577. return "";
  578. }
  579. int i;
  580. StringBuilder sb1 = new StringBuilder();
  581. for (i = 0; i < str.Length; i++)
  582. {
  583. char c = str[i];
  584. if (c == ' ' || c == ' ' || c == '\t')
  585. {
  586. sb1.Append(c);
  587. }
  588. else
  589. {
  590. break;
  591. }
  592. }
  593. string str2 = str.Substring(i).Trim();
  594. string str1 = sb1.ToString();
  595. str1 = ReplaceStr(str1, " ", " ");
  596. str1 = ReplaceStr(str1, "\t", " ");
  597. return str1 + normalizeStrSoftEtherInternal2(str2);
  598. }
  599. static string normalizeStrSoftEtherInternal2(string str)
  600. {
  601. NormalizeString(ref str, true, true, false, true);
  602. char[] chars = str.ToCharArray();
  603. StringBuilder sb = new StringBuilder();
  604. int i;
  605. for (i = 0; i < chars.Length; i++)
  606. {
  607. char c = chars[i];
  608. bool insert_space = false;
  609. bool insert_space2 = false;
  610. char c1 = (char)0;
  611. if (i >= 1)
  612. {
  613. c1 = chars[i - 1];
  614. }
  615. char c2 = (char)0;
  616. if (i < (chars.Length - 1))
  617. {
  618. c2 = chars[i + 1];
  619. }
  620. if (c == '\'' || c1 == '\'' || c2 == '\'' || c == '\"' || c1 == '\"' || c2 == '\"' || c == '>' || c1 == '>' || c2 == '>' || c == '<' || c1 == '<' || c2 == '<')
  621. {
  622. }
  623. else if (c == '(' || c == '[' || c == '{' || c == '<')
  624. {
  625. if (c1 != '「' && c1 != '『' && c1 != '。' && c1 != '、' && c1 != '・')
  626. {
  627. insert_space = true;
  628. }
  629. }
  630. else if (c == ')' || c == ']' || c == '}' || c == '>')
  631. {
  632. if (c2 != '.' && c2 != ',' && c2 != '。' && c2 != '、')
  633. {
  634. insert_space2 = true;
  635. }
  636. }
  637. else if (c == '~')
  638. {
  639. if (c1 != '~')
  640. {
  641. insert_space = true;
  642. }
  643. if (c2 != '~')
  644. {
  645. insert_space2 = true;
  646. }
  647. }
  648. else if (IsZenkaku(c) == false)
  649. {
  650. if (IsZenkaku(c1))
  651. {
  652. if (c != '.' && c != ',' && c != ';' && c != ':' && c1 != '※' && c1 != '〒' && c1 != '℡' && c1 != '「' && c1 != '『' && c1 != '。' && c1 != '、' && c1 != '・')
  653. {
  654. insert_space = true;
  655. }
  656. }
  657. }
  658. else
  659. {
  660. if (IsZenkaku(c1) == false)
  661. {
  662. if (c != '。' && c != '、' && c != '」' && c != '』' && c != '・' && c1 != '(' && c1 != '[' && c1 != '{' && c1 != '<' && c1 != ';' && c1 != ':')
  663. {
  664. insert_space = true;
  665. }
  666. }
  667. }
  668. if (insert_space)
  669. {
  670. sb.Append(' ');
  671. }
  672. sb.Append(c);
  673. if (insert_space2)
  674. {
  675. sb.Append(' ');
  676. }
  677. }
  678. str = sb.ToString();
  679. NormalizeString(ref str, true, true, false, true);
  680. return str;
  681. }
  682. public static bool IsZenkaku(char c)
  683. {
  684. return !((c >= (char)0) && (c <= (char)256));
  685. }
  686. public static string[] DivideStringMulti(string str, bool caseSensitive, params string[] keywords)
  687. {
  688. List<string> ret = new List<string>();
  689. int next = 0;
  690. while (true)
  691. {
  692. int foundIndex;
  693. string foundKeyword;
  694. int r = Str.SearchStrMulti(str, next, caseSensitive, out foundIndex, out foundKeyword, keywords);
  695. if (r == -1)
  696. {
  697. ret.Add(str.Substring(next));
  698. break;
  699. }
  700. else
  701. {
  702. ret.Add(str.Substring(next, r - next));
  703. ret.Add(foundKeyword);
  704. next = r + foundKeyword.Length;
  705. }
  706. }
  707. return ret.ToArray();
  708. }
  709. public static bool IsSuitableEncodingForString(string str, Encoding enc)
  710. {
  711. try
  712. {
  713. str = Str.NormalizeCrlf(str);
  714. byte[] utf1 = Str.Utf8Encoding.GetBytes(str);
  715. byte[] b = enc.GetBytes(str);
  716. string str2 = enc.GetString(b);
  717. byte[] utf2 = Str.Utf8Encoding.GetBytes(str2);
  718. return Util.CompareByte(utf1, utf2);
  719. }
  720. catch
  721. {
  722. return false;
  723. }
  724. }
  725. public static bool IsCharNumOrAlpha(char c)
  726. {
  727. if (c >= 'a' && c <= 'z')
  728. {
  729. return true;
  730. }
  731. if (c >= 'A' && c <= 'Z')
  732. {
  733. return true;
  734. }
  735. if (c >= '0' && c <= '9')
  736. {
  737. return true;
  738. }
  739. return false;
  740. }
  741. public static bool IsStringNumOrAlpha(string s)
  742. {
  743. foreach (char c in s)
  744. {
  745. if (IsCharNumOrAlpha(c) == false)
  746. {
  747. return false;
  748. }
  749. }
  750. return true;
  751. }
  752. public static string[] StrToStrLineBySplitting(string str)
  753. {
  754. StringReader r = new StringReader(str);
  755. List<string> ret = new List<string>();
  756. while (true)
  757. {
  758. string line = r.ReadLine();
  759. if (line == null)
  760. {
  761. break;
  762. }
  763. if (IsEmptyStr(line) == false)
  764. {
  765. ret.Add(line.Trim());
  766. }
  767. }
  768. return ret.ToArray();
  769. }
  770. public static string GetLeft(string str, int len)
  771. {
  772. if (str == null)
  773. {
  774. return null;
  775. }
  776. if (str.Length > len)
  777. {
  778. return str.Substring(0, len);
  779. }
  780. else
  781. {
  782. return str;
  783. }
  784. }
  785. public static string[] SplitStringForSearch(string str)
  786. {
  787. bool b = false;
  788. int i, len;
  789. len = str.Length;
  790. List<string> ret = new List<string>();
  791. string currentStr = "";
  792. for (i = 0; i < len; i++)
  793. {
  794. char c = str[i];
  795. if (c == '\"')
  796. {
  797. b = !b;
  798. if (b == false)
  799. {
  800. currentStr = currentStr.Trim();
  801. if (Str.IsEmptyStr(currentStr) == false)
  802. {
  803. ret.Add(currentStr);
  804. currentStr = "";
  805. }
  806. }
  807. }
  808. else
  809. {
  810. if (b == false && (c == ' ' || c == ' ' || c == '\t'))
  811. {
  812. currentStr = currentStr.Trim();
  813. if (Str.IsEmptyStr(currentStr) == false)
  814. {
  815. ret.Add(currentStr);
  816. currentStr = "";
  817. }
  818. }
  819. else
  820. {
  821. currentStr += c;
  822. }
  823. }
  824. }
  825. currentStr = currentStr.Trim();
  826. if (Str.IsEmptyStr(currentStr) == false)
  827. {
  828. ret.Add(currentStr);
  829. }
  830. return ret.ToArray();
  831. }
  832. public static string AppendZeroToNumString(string str, int numKeta)
  833. {
  834. int n = numKeta - str.Length;
  835. if (n >= 1)
  836. {
  837. return MakeCharArray('0', n) + str;
  838. }
  839. else
  840. {
  841. return str;
  842. }
  843. }
  844. public static Encoding CheckBOM(byte[] data)
  845. {
  846. int i;
  847. return CheckBOM(data, out i);
  848. }
  849. public static Encoding CheckBOM(byte[] data, out int bomNumBytes)
  850. {
  851. bomNumBytes = 0;
  852. try
  853. {
  854. if (data[0] == 0x00 && data[1] == 0x00 && data[2] == 0xfe && data[3] == 0xff)
  855. {
  856. bomNumBytes = 3;
  857. return Encoding.GetEncoding("utf-32BE");
  858. }
  859. else if (data[0] == 0xff && data[1] == 0xfe && data[2] == 0x00 && data[3] == 0x00)
  860. {
  861. bomNumBytes = 4;
  862. return Encoding.GetEncoding("utf-32");
  863. }
  864. else if (data[0] == 0xff && data[1] == 0xfe)
  865. {
  866. bomNumBytes = 2;
  867. return Encoding.GetEncoding("utf-16");
  868. }
  869. else if (data[0] == 0xfe && data[1] == 0xff)
  870. {
  871. bomNumBytes = 2;
  872. return Encoding.GetEncoding("unicodeFFFE");
  873. }
  874. else if (data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf)
  875. {
  876. bomNumBytes = 3;
  877. return Encoding.GetEncoding("utf-8");
  878. }
  879. else
  880. {
  881. return null;
  882. }
  883. }
  884. catch
  885. {
  886. return null;
  887. }
  888. }
  889. public static byte[] GetBOM(Encoding encoding)
  890. {
  891. if (Str.StrCmpi(encoding.BodyName, "utf-32BE"))
  892. {
  893. return new byte[] { 0x00, 0x00, 0xfe, 0xff };
  894. }
  895. else if (Str.StrCmpi(encoding.BodyName, "utf-32"))
  896. {
  897. return new byte[] {0xff, 0xfe, 0x00, 0x00 };
  898. }
  899. else if (Str.StrCmpi(encoding.BodyName, "utf-16"))
  900. {
  901. return new byte[] { 0xff, 0xfe };
  902. }
  903. else if (Str.StrCmpi(encoding.BodyName, "unicodeFFFE"))
  904. {
  905. return new byte[] { 0xfe, 0xff };
  906. }
  907. else if (Str.StrCmpi(encoding.BodyName, "utf-8"))
  908. {
  909. return new byte[] { 0xef, 0xbb, 0xbf };
  910. }
  911. else
  912. {
  913. return null;
  914. }
  915. }
  916. public static byte[] ConvertEncoding(byte[] srcData, Encoding destEncoding)
  917. {
  918. return ConvertEncoding(srcData, destEncoding, false);
  919. }
  920. public static byte[] ConvertEncoding(byte[] srcData, Encoding destEncoding, bool appendBom)
  921. {
  922. Encoding srcEncoding = GetEncoding(srcData);
  923. if (srcEncoding == null)
  924. {
  925. srcEncoding = Str.ShiftJisEncoding;
  926. }
  927. int nb;
  928. if (CheckBOM(srcData, out nb) != null)
  929. {
  930. srcData = Util.RemoveStartByteArray(srcData, nb);
  931. }
  932. string str = srcEncoding.GetString(srcData);
  933. byte[] b1 = null;
  934. if (appendBom)
  935. {
  936. b1 = GetBOM(destEncoding);
  937. }
  938. byte[] b2 = destEncoding.GetBytes(str);
  939. return Util.CombineByteArray(b1, b2);
  940. }
  941. public static string ReadTextFile(string filename)
  942. {
  943. byte[] data = IO.ReadFileData(filename);
  944. int bomSize = 0;
  945. Encoding enc = GetEncoding(data, out bomSize);
  946. if (enc == null)
  947. {
  948. enc = Str.Utf8Encoding;
  949. }
  950. if (bomSize >= 1)
  951. {
  952. data = Util.CopyByte(data, bomSize);
  953. }
  954. return enc.GetString(data);
  955. }
  956. public static void WriteTextFile(string filename, Encoding enc, bool writeBom)
  957. {
  958. Buf buf = new Buf();
  959. byte[] bom = GetBOM(enc);
  960. if (writeBom && bom != null && bom.Length >= 1)
  961. {
  962. buf.Write(bom);
  963. }
  964. buf.Write(enc.GetBytes(filename));
  965. buf.SeekToBegin();
  966. File.WriteAllBytes(filename, buf.Read());
  967. }
  968. public static Encoding GetEncoding(byte[] data)
  969. {
  970. int i;
  971. return GetEncoding(data, out i);
  972. }
  973. public static Encoding GetEncoding(byte[] data, out int bomSize)
  974. {
  975. const byte bESC = 0x1B;
  976. const byte bAT = 0x40;
  977. const byte bDollar = 0x24;
  978. const byte bAnd = 0x26;
  979. const byte bOP = 0x28;
  980. const byte bB = 0x42;
  981. const byte bD = 0x44;
  982. const byte bJ = 0x4A;
  983. const byte bI = 0x49;
  984. bomSize = 0;
  985. int len = data.Length;
  986. int binary = 0;
  987. int ucs2 = 0;
  988. int sjis = 0;
  989. int euc = 0;
  990. int utf8 = 0;
  991. byte b1, b2;
  992. Encoding bomEncoding = CheckBOM(data, out bomSize);
  993. if (bomEncoding != null)
  994. {
  995. return bomEncoding;
  996. }
  997. for (int i = 0; i < len; i++)
  998. {
  999. if (data[i] <= 0x06 || data[i] == 0x7F || data[i] == 0xFF)
  1000. {
  1001. //'binary'
  1002. binary++;
  1003. if (len - 1 > i && data[i] == 0x00
  1004. && i > 0 && data[i - 1] <= 0x7F)
  1005. {
  1006. //smells like raw unicode
  1007. ucs2++;
  1008. }
  1009. }
  1010. }
  1011. if (binary > 0)
  1012. {
  1013. if (ucs2 > 0)
  1014. {
  1015. //JIS
  1016. //ucs2(Unicode)
  1017. int n1 = 0, n2 = 0;
  1018. for (int i = 0; i < (len / 2); i++)
  1019. {
  1020. byte e1 = data[i * 2];
  1021. byte e2 = data[i * 2 + 1];
  1022. if (e1 == 0 && e2 != 0)
  1023. {
  1024. n1++;
  1025. }
  1026. else if (e1 != 0 && e2 == 0)
  1027. {
  1028. n2++;
  1029. }
  1030. }
  1031. if (n1 > n2)
  1032. {
  1033. return Encoding.GetEncoding("unicodeFFFE");
  1034. }
  1035. else
  1036. {
  1037. return System.Text.Encoding.Unicode;
  1038. }
  1039. }
  1040. else
  1041. {
  1042. //binary
  1043. return null;
  1044. }
  1045. }
  1046. for (int i = 0; i < len - 1; i++)
  1047. {
  1048. b1 = data[i];
  1049. b2 = data[i + 1];
  1050. if (b1 == bESC)
  1051. {
  1052. if (b2 >= 0x80)
  1053. //not Japanese
  1054. //ASCII
  1055. return System.Text.Encoding.ASCII;
  1056. else if (len - 2 > i &&
  1057. b2 == bDollar && data[i + 2] == bAT)
  1058. //JIS_0208 1978
  1059. //JIS
  1060. return System.Text.Encoding.GetEncoding(50220);
  1061. else if (len - 2 > i &&
  1062. b2 == bDollar && data[i + 2] == bB)
  1063. //JIS_0208 1983
  1064. //JIS
  1065. return System.Text.Encoding.GetEncoding(50220);
  1066. else if (len - 5 > i &&
  1067. b2 == bAnd && data[i + 2] == bAT && data[i + 3] == bESC &&
  1068. data[i + 4] == bDollar && data[i + 5] == bB)
  1069. //JIS_0208 1990
  1070. //JIS
  1071. return System.Text.Encoding.GetEncoding(50220);
  1072. else if (len - 3 > i &&
  1073. b2 == bDollar && data[i + 2] == bOP && data[i + 3] == bD)
  1074. //JIS_0212
  1075. //JIS
  1076. return System.Text.Encoding.GetEncoding(50220);
  1077. else if (len - 2 > i &&
  1078. b2 == bOP && (data[i + 2] == bB || data[i + 2] == bJ))
  1079. //JIS_ASC
  1080. //JIS
  1081. return System.Text.Encoding.GetEncoding(50220);
  1082. else if (len - 2 > i &&
  1083. b2 == bOP && data[i + 2] == bI)
  1084. //JIS_KANA
  1085. //JIS
  1086. return System.Text.Encoding.GetEncoding(50220);
  1087. }
  1088. }
  1089. for (int i = 0; i < len - 1; i++)
  1090. {
  1091. b1 = data[i];
  1092. b2 = data[i + 1];
  1093. if (((b1 >= 0x81 && b1 <= 0x9F) || (b1 >= 0xE0 && b1 <= 0xFC)) &&
  1094. ((b2 >= 0x40 && b2 <= 0x7E) || (b2 >= 0x80 && b2 <= 0xFC)))
  1095. {
  1096. sjis += 2;
  1097. i++;
  1098. }
  1099. }
  1100. for (int i = 0; i < len - 1; i++)
  1101. {
  1102. b1 = data[i];
  1103. b2 = data[i + 1];
  1104. if (((b1 >= 0xA1 && b1 <= 0xFE) && (b2 >= 0xA1 && b2 <= 0xFE)) ||
  1105. (b1 == 0x8E && (b2 >= 0xA1 && b2 <= 0xDF)))
  1106. {
  1107. euc += 2;
  1108. i++;
  1109. }
  1110. else if (len - 2 > i &&
  1111. b1 == 0x8F && (b2 >= 0xA1 && b2 <= 0xFE) &&
  1112. (data[i + 2] >= 0xA1 && data[i + 2] <= 0xFE))
  1113. {
  1114. euc += 3;
  1115. i += 2;
  1116. }
  1117. }
  1118. for (int i = 0; i < len - 1; i++)
  1119. {
  1120. b1 = data[i];
  1121. b2 = data[i + 1];
  1122. if ((b1 >= 0xC0 && b1 <= 0xDF) && (b2 >= 0x80 && b2 <= 0xBF))
  1123. {
  1124. utf8 += 2;
  1125. i++;
  1126. }
  1127. else if (len - 2 > i &&
  1128. (b1 >= 0xE0 && b1 <= 0xEF) && (b2 >= 0x80 && b2 <= 0xBF) &&
  1129. (data[i + 2] >= 0x80 && data[i + 2] <= 0xBF))
  1130. {
  1131. utf8 += 3;
  1132. i += 2;
  1133. }
  1134. }
  1135. if (euc > sjis && euc > utf8)
  1136. //EUC
  1137. return System.Text.Encoding.GetEncoding(51932);
  1138. else if (sjis > euc && sjis > utf8)
  1139. //SJIS
  1140. return System.Text.Encoding.GetEncoding(932);
  1141. else if (utf8 > euc && utf8 > sjis)
  1142. //UTF8
  1143. return System.Text.Encoding.UTF8;
  1144. return null;
  1145. }
  1146. public static bool StartsWithMulti(string str, StringComparison comp, params string[] keys)
  1147. {
  1148. NormalizeString(ref str);
  1149. foreach (string key in keys)
  1150. {
  1151. if (str.StartsWith(key, comp))
  1152. {
  1153. return true;
  1154. }
  1155. }
  1156. return false;
  1157. }
  1158. public static bool IsCharForMail(char c)
  1159. {
  1160. switch (c)
  1161. {
  1162. case '<':
  1163. case '>':
  1164. case ' ':
  1165. case ';':
  1166. case ':':
  1167. case '/':
  1168. case '(':
  1169. case ')':
  1170. case '&':
  1171. case ',':
  1172. case '%':
  1173. case '$':
  1174. case '#':
  1175. case '\"':
  1176. case '\'':
  1177. case '!':
  1178. case '=':
  1179. case '\\':
  1180. return false;
  1181. }
  1182. if (c >= 0x80)
  1183. {
  1184. return false;
  1185. }
  1186. if (IsAscii(c) == false)
  1187. {
  1188. return false;
  1189. }
  1190. return true;
  1191. }
  1192. public static string LinkMailtoOnText(string text)
  1193. {
  1194. NormalizeString(ref text);
  1195. StringBuilder sb = new StringBuilder();
  1196. string tmp = "";
  1197. int i;
  1198. for (i = 0; i < text.Length; i++)
  1199. {
  1200. char c = text[i];
  1201. if (IsCharForMail(c) == false)
  1202. {
  1203. if (Str.CheckMailAddress(tmp) == false)
  1204. {
  1205. tmp += c;
  1206. sb.Append(tmp);
  1207. tmp = "";
  1208. }
  1209. else
  1210. {
  1211. sb.AppendFormat("<a href=\"mailto:{0}\">{0}</a>", tmp);
  1212. sb.Append(c);
  1213. tmp = "";
  1214. }
  1215. }
  1216. else
  1217. {
  1218. tmp += c;
  1219. }
  1220. }
  1221. if (Str.CheckMailAddress(tmp) == false)
  1222. {
  1223. sb.Append(tmp);
  1224. tmp = "";
  1225. }
  1226. else
  1227. {
  1228. sb.AppendFormat("<a href=\"mailto:{0}\">{0}</a>", tmp);
  1229. tmp = "";
  1230. }
  1231. return sb.ToString();
  1232. }
  1233. public static string LinkUrlOnText(string text, string target)
  1234. {
  1235. int findStart = 0;
  1236. NormalizeString(ref text);
  1237. NormalizeString(ref target);
  1238. StringBuilder sb = new StringBuilder();
  1239. while (true)
  1240. {
  1241. int foundStrIndex;
  1242. int foundIndex = FindStrings(text, findStart, StringComparison.InvariantCultureIgnoreCase, out foundStrIndex,
  1243. "http://", "https://", "ftp://", "telnet://", "mailto://", "news://");
  1244. if (foundIndex != -1)
  1245. {
  1246. int i;
  1247. int endOfUrl = -1;
  1248. for (i = foundIndex; i < text.Length; i++)
  1249. {
  1250. char c = text[i];
  1251. if (IsValidForUrl(c) == false)
  1252. {
  1253. endOfUrl = i;
  1254. break;
  1255. }
  1256. if (c == '<' || c == '&')
  1257. {
  1258. if (StartsWithMulti(text.Substring(i), StringComparison.InvariantCultureIgnoreCase,
  1259. HtmlSpacing, HtmlCrlf, HtmlBr, HtmlLt, HtmlGt))
  1260. {
  1261. endOfUrl = i;
  1262. break;
  1263. }
  1264. }
  1265. }
  1266. if (endOfUrl == -1)
  1267. {
  1268. endOfUrl = text.Length;
  1269. }
  1270. string url = text.Substring(foundIndex, endOfUrl - foundIndex);
  1271. string beforeUrl = text.Substring(findStart, foundIndex - findStart);
  1272. sb.Append(beforeUrl);
  1273. if (Str.IsEmptyStr(target) == false)
  1274. {
  1275. sb.AppendFormat("<a href=\"{0}\" target=\"{2}\">{1}</a>", url, url, target);
  1276. }
  1277. else
  1278. {
  1279. sb.AppendFormat("<a href=\"{0}\">{1}</a>", url, url);
  1280. }
  1281. findStart = endOfUrl;
  1282. }
  1283. else
  1284. {
  1285. sb.Append(text.Substring(findStart));
  1286. break;
  1287. }
  1288. }
  1289. return LinkMailtoOnText(sb.ToString());
  1290. }
  1291. public static int FindStrings(string str, int findStartIndex, StringComparison comp, out int foundKeyIndex, params string[] keys)
  1292. {
  1293. int ret = -1;
  1294. foundKeyIndex = -1;
  1295. int n = 0;
  1296. foreach (string key in keys)
  1297. {
  1298. int i = str.IndexOf(key, findStartIndex, comp);
  1299. if (i != -1)
  1300. {
  1301. if (ret == -1)
  1302. {
  1303. ret = i;
  1304. foundKeyIndex = n;
  1305. }
  1306. else
  1307. {
  1308. if (ret > i)
  1309. {
  1310. ret = i;
  1311. foundKeyIndex = n;
  1312. }
  1313. }
  1314. }
  1315. n++;
  1316. }
  1317. return ret;
  1318. }
  1319. public static bool IsValidForUrl(char c)
  1320. {
  1321. if (c >= '0' && c <= '9')
  1322. {
  1323. return true;
  1324. }
  1325. if (c >= 'a' && c <= 'z')
  1326. {
  1327. return true;
  1328. }
  1329. if (c >= 'A' && c <= 'Z')
  1330. {
  1331. return true;
  1332. }
  1333. switch (c)
  1334. {
  1335. case '_':
  1336. case '-':
  1337. case '?':
  1338. case '!':
  1339. case '\"':
  1340. case ',':
  1341. case '\'':
  1342. case '/':
  1343. case '\\':
  1344. case '&':
  1345. case ';':
  1346. case '%':
  1347. case '#':
  1348. case '@':
  1349. case '~':
  1350. case ':':
  1351. case '=':
  1352. case '+':
  1353. case '*':
  1354. case '$':
  1355. case '.':
  1356. return true;
  1357. }
  1358. return false;
  1359. }
  1360. public static List<string> RemoteStringFromList(List<string> str, RemoveStringFunction func)
  1361. {
  1362. List<string> ret = new List<string>();
  1363. foreach (string s in str)
  1364. {
  1365. if (func(s) == false)
  1366. {
  1367. ret.Add(s);
  1368. }
  1369. }
  1370. return ret;
  1371. }
  1372. public const string ConstZenkaku = "`{}0123456789/*-+!”#$%&’()= ̄|¥[]@;:<>?_^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ‘";
  1373. public const string ConstHankaku = "`{}0123456789/*-+!\"#$%&'()=~|\\[]@;:<>?_^ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'";
  1374. public const string ConstKanaZenkaku = "ー「」アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンァゥェォャュョッィ゛゜";
  1375. public const string ConstKanaHankaku = "ー「」アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンァゥェォャュョッィ゙゚";
  1376. public static void RemoveSpace(ref string str)
  1377. {
  1378. NormalizeString(ref str);
  1379. str = str.Replace(" ", "").Replace(" ", "").Replace("\t", "");
  1380. }
  1381. public static void TrimStartWith(ref string str, string key, StringComparison sc)
  1382. {
  1383. if (str.StartsWith(key, sc))
  1384. {
  1385. str = str.Substring(key.Length);
  1386. }
  1387. }
  1388. public static void TrimEndsWith(ref string str, string key, StringComparison sc)
  1389. {
  1390. if (str.EndsWith(key, sc))
  1391. {
  1392. str = str.Substring(0, str.Length - key.Length);
  1393. }
  1394. }
  1395. public static void RemoveSpaceChar(ref string str)
  1396. {
  1397. if (Str.IsEmptyStr(str))
  1398. {
  1399. return;
  1400. }
  1401. StringBuilder sb = new StringBuilder();
  1402. foreach (char c in str)
  1403. {
  1404. if (c == ' ' || c == '\t' || c == ' ')
  1405. {
  1406. }
  1407. else
  1408. {
  1409. sb.Append(c);
  1410. }
  1411. }
  1412. str = sb.ToString();
  1413. }
  1414. public static void NormalizeStringStandard(ref string str)
  1415. {
  1416. NormalizeString(ref str, true, true, false, true);
  1417. }
  1418. public static void NormalizeString(ref string str, bool space, bool toHankaku, bool toZenkaku, bool toZenkakuKana)
  1419. {
  1420. NormalizeString(ref str);
  1421. if (space)
  1422. {
  1423. str = NormalizeSpace(str);
  1424. }
  1425. if (toHankaku)
  1426. {
  1427. str = ZenkakuToHankaku(str);
  1428. }
  1429. if (toZenkaku)
  1430. {
  1431. str = HankakuToZenkaku(str);
  1432. }
  1433. if (toZenkakuKana)
  1434. {
  1435. str = KanaHankakuToZenkaku(str);
  1436. }
  1437. }
  1438. public static string NormalizeSpace(string str)
  1439. {
  1440. NormalizeString(ref str);
  1441. char[] sps =
  1442. {
  1443. ' ', ' ', '\t',
  1444. };
  1445. string[] tokens = str.Split(sps, StringSplitOptions.RemoveEmptyEntries);
  1446. return Str.CombineStringArray(tokens, " ");
  1447. }
  1448. public static string KanaHankakuToZenkaku(string str)
  1449. {
  1450. NormalizeString(ref str);
  1451. str = str.Replace("ガ", "ガ");
  1452. str = str.Replace("ギ", "ギ");
  1453. str = str.Replace("グ", "グ");
  1454. str = str.Replace("ゲ", "ゲ");
  1455. str = str.Replace("ゴ", "ゴ");
  1456. str = str.Replace("ザ", "ザ");
  1457. str = str.Replace("ジ", "ジ");
  1458. str = str.Replace("ズ", "ズ");
  1459. str = str.Replace("ゼ", "ゼ");
  1460. str = str.Replace("ゾ", "ゾ");
  1461. str = str.Replace("ダ", "ダ");
  1462. str = str.Replace("ヂ", "ヂ");
  1463. str = str.Replace("ヅ", "ヅ");
  1464. str = str.Replace("デ", "デ");
  1465. str = str.Replace("ド", "ド");
  1466. str = str.Replace("バ", "バ");
  1467. str = str.Replace("ビ", "ビ");
  1468. str = str.Replace("ブ", "ブ");
  1469. str = str.Replace("ベ", "ベ");
  1470. str = str.Replace("ボ", "ボ");
  1471. char[] a = str.ToCharArray();
  1472. int i;
  1473. for (i = 0; i < a.Length; i++)
  1474. {
  1475. int j = ConstKanaHankaku.IndexOf(a[i]);
  1476. if (j != -1)
  1477. {
  1478. a[i] = ConstKanaZenkaku[j];
  1479. }
  1480. }
  1481. return new string(a);
  1482. }
  1483. public static string ZenkakuToHankaku(string str)
  1484. {
  1485. NormalizeString(ref str);
  1486. str = ReplaceStr(str, "“", " \"");
  1487. str = ReplaceStr(str, "”", "\" ");
  1488. str = ReplaceStr(str, "‘", "'");
  1489. str = ReplaceStr(str, "’", "'");
  1490. char[] a = str.ToCharArray();
  1491. int i;
  1492. for (i = 0; i < a.Length; i++)
  1493. {
  1494. int j = ConstZenkaku.IndexOf(a[i]);
  1495. if (j != -1)
  1496. {
  1497. a[i] = ConstHankaku[j];
  1498. }
  1499. }
  1500. return new string(a);
  1501. }
  1502. public static string HankakuToZenkaku(string str)
  1503. {
  1504. NormalizeString(ref str);
  1505. str = KanaHankakuToZenkaku(str);
  1506. char[] a = str.ToCharArray();
  1507. int i;
  1508. for (i = 0; i < a.Length; i++)
  1509. {
  1510. int j = ConstHankaku.IndexOf(a[i]);
  1511. if (j != -1)
  1512. {
  1513. a[i] = ConstZenkaku[j];
  1514. }
  1515. }
  1516. return new string(a);
  1517. }
  1518. public const string HtmlSpacing = "&nbsp;";
  1519. public const string HtmlCrlf = "<BR>";
  1520. public const string HtmlBr = "<BR>";
  1521. public const string HtmlLt = "&lt;";
  1522. public const string HtmlGt = "&gt;";
  1523. public const string HtmlAmp = "&amp;";
  1524. public const int HtmlNumTabChar = 8;
  1525. public static string HtmlTab
  1526. {
  1527. get
  1528. {
  1529. int i;
  1530. StringBuilder sb = new StringBuilder();
  1531. for (i = 0; i < HtmlNumTabChar; i++)
  1532. {
  1533. sb.Append(HtmlSpacing);
  1534. }
  1535. return sb.ToString();
  1536. }
  1537. }
  1538. public static string ToUrl(string str, Encoding e)
  1539. {
  1540. Str.NormalizeString(ref str);
  1541. return HttpUtility.UrlEncode(str, e);
  1542. }
  1543. public static string FromUrl(string str, Encoding e)
  1544. {
  1545. Str.NormalizeString(ref str);
  1546. return HttpUtility.UrlDecode(str, e);
  1547. }
  1548. public static string FromHtml(string str)
  1549. {
  1550. str = Str.ReplaceStr(str, HtmlCrlf, "\r\n", false);
  1551. str = str.Replace(HtmlSpacing, " ");
  1552. str = str.Replace(HtmlLt, "<").Replace(HtmlGt, ">").Replace(HtmlAmp, "&");
  1553. str = NormalizeCrlf(str);
  1554. return str;
  1555. }
  1556. public static string ToHtml(string str)
  1557. {
  1558. return ToHtml(str, false);
  1559. }
  1560. public static string ToHtml(string str, bool forceAllSpaceToTag)
  1561. {
  1562. str = NormalizeCrlf(str);
  1563. str = str.Replace("&", HtmlAmp);
  1564. str = str.Replace("<", HtmlLt).Replace(">", HtmlGt);
  1565. if (str.IndexOf(' ') != -1)
  1566. {
  1567. if (forceAllSpaceToTag)
  1568. {
  1569. str = str.Replace(" ", HtmlSpacing);
  1570. }
  1571. else
  1572. {
  1573. int i;
  1574. StringBuilder sb = new StringBuilder();
  1575. bool flag = false;
  1576. for (i = 0; i < str.Length; i++)
  1577. {
  1578. char c = str[i];
  1579. if (c == ' ')
  1580. {
  1581. if (flag == false)
  1582. {
  1583. flag = true;
  1584. sb.Append(' ');
  1585. }
  1586. else
  1587. {
  1588. sb.Append(HtmlSpacing);
  1589. }
  1590. }
  1591. else
  1592. {
  1593. flag = false;
  1594. sb.Append(c);
  1595. }
  1596. }
  1597. str = sb.ToString();
  1598. }
  1599. }
  1600. str = str.Replace("\t", HtmlTab);
  1601. str = str.Replace("\r\n", HtmlCrlf);
  1602. return str;
  1603. }
  1604. public static bool IsPrintable(char c)
  1605. {
  1606. if (c >= 256)
  1607. {
  1608. return true;
  1609. }
  1610. if (c >= 32 && c <= 126)
  1611. {
  1612. return true;
  1613. }
  1614. return false;
  1615. }
  1616. public static bool IsPrintable(string str)
  1617. {
  1618. foreach (char c in str)
  1619. {
  1620. if (IsPrintable(c) == false)
  1621. {
  1622. return false;
  1623. }
  1624. }
  1625. return true;
  1626. }
  1627. public static string Unescape(string str)
  1628. {
  1629. StringBuilder sb = new StringBuilder();
  1630. int i;
  1631. for (i = 0; i < str.Length; i++)
  1632. {
  1633. char c = str[i];
  1634. if (IsPrintable(c) && c != '\\')
  1635. {
  1636. sb.Append(c);
  1637. }
  1638. else
  1639. {
  1640. string s = "" + c;
  1641. switch (c)
  1642. {
  1643. case '\r':
  1644. s = "\\r";
  1645. break;
  1646. case '\n':
  1647. s = "\\n";
  1648. break;
  1649. case '\0':
  1650. s = "\\0";
  1651. break;
  1652. case '\t':
  1653. s = "\\t";
  1654. break;
  1655. case '\\':
  1656. s = "\\\\";
  1657. break;
  1658. default:
  1659. s = "0x" + Convert.ToString((int)c, 16);
  1660. break;
  1661. }
  1662. sb.Append(s);
  1663. }
  1664. }
  1665. return sb.ToString();
  1666. }
  1667. public static string Escape(string str)
  1668. {
  1669. StringBuilder sb = new StringBuilder();
  1670. int i, j, hex;
  1671. string padding = "00000000";
  1672. str = str + padding;
  1673. StringBuilder sb2;
  1674. for (i = 0; i < str.Length - padding.Length; i++)
  1675. {
  1676. char c = str[i];
  1677. char d = c;
  1678. if (c == '\\')
  1679. {
  1680. char c1 = str[i + 1];
  1681. switch (c1)
  1682. {
  1683. case '\'':
  1684. d = '\'';
  1685. i++;
  1686. break;
  1687. case '?':
  1688. d = '?';
  1689. i++;
  1690. break;
  1691. case '\\':
  1692. d = '\\';
  1693. i++;
  1694. break;
  1695. case 't':
  1696. d = '\t';
  1697. i++;
  1698. break;
  1699. case 'r':
  1700. d = '\r';
  1701. i++;
  1702. break;
  1703. case 'n':
  1704. d = '\n';
  1705. i++;
  1706. break;
  1707. case ' ':
  1708. d = ' ';
  1709. i++;
  1710. break;
  1711. case ' ':
  1712. d = ' ';
  1713. i++;
  1714. break;
  1715. case '\t':
  1716. d = '\t';
  1717. i++;
  1718. break;
  1719. case '0':
  1720. d = '\0';
  1721. i++;
  1722. break;
  1723. case 'x':
  1724. i++;
  1725. sb2 = new StringBuilder();
  1726. for (j = 0; j < 4; j++)
  1727. {
  1728. char c2 = str[++i];
  1729. if ((c2 >= '0' && c2 <= '9') || (c2 >= 'a' && c2 <= 'f') || (c2 >= 'A' && c2 <= 'F'))
  1730. {
  1731. sb2.Append(c2);
  1732. }
  1733. else
  1734. {
  1735. i--;
  1736. break;
  1737. }
  1738. }
  1739. hex = Convert.ToInt32(sb2.ToString(), 16);
  1740. d = (char)hex;
  1741. break;
  1742. default:
  1743. if (c1 >= '0' && c1 <= '7')
  1744. {
  1745. sb2 = new StringBuilder();
  1746. for (j = 0; j < 3; j++)
  1747. {
  1748. char c2 = str[++i];
  1749. if (c2 >= '0' && c2 <= '7')
  1750. {
  1751. sb2.Append(c2);
  1752. }
  1753. else
  1754. {
  1755. i--;
  1756. break;
  1757. }
  1758. }
  1759. hex = Convert.ToInt32(sb2.ToString(), 8);
  1760. d = (char)hex;
  1761. }
  1762. else
  1763. {
  1764. d = '\\';
  1765. i++;
  1766. }
  1767. break;
  1768. }
  1769. }
  1770. if (d != '\0')
  1771. {
  1772. sb.Append(d);
  1773. }
  1774. else
  1775. {
  1776. break;
  1777. }
  1778. }
  1779. return sb.ToString();
  1780. }
  1781. public static int GetStrWidth(string str)
  1782. {
  1783. int ret = 0;
  1784. foreach (char c in str)
  1785. {
  1786. if (c <= 255)
  1787. {
  1788. ret++;
  1789. }
  1790. else
  1791. {
  1792. ret += 2;
  1793. }
  1794. }
  1795. return ret;
  1796. }
  1797. public static string TrimCrlf(string str)
  1798. {
  1799. int len;
  1800. if (str == null)
  1801. {
  1802. return "";
  1803. }
  1804. len = str.Length;
  1805. if (len == 0)
  1806. {
  1807. return "";
  1808. }
  1809. if (str[len - 1] == '\n')
  1810. {
  1811. if (len >= 2 && str[len - 2] == '\r')
  1812. {
  1813. str = str.Substring(0, len - 2);
  1814. }
  1815. str = str.Substring(0, len - 1);
  1816. }
  1817. else if (str[len - 1] == '\r')
  1818. {
  1819. str = str.Substring(0, len - 1);
  1820. }
  1821. return str;
  1822. }
  1823. public static bool IsAllUpperStr(string str)
  1824. {
  1825. int i, len;
  1826. if (str == null)
  1827. {
  1828. return false;
  1829. }
  1830. len = str.Length;
  1831. for (i = 0; i < len; i++)
  1832. {
  1833. char c = str[i];
  1834. if ((c >= '0' && c <= '9') ||
  1835. (c >= 'A' && c <= 'Z'))
  1836. {
  1837. }
  1838. else
  1839. {
  1840. return false;
  1841. }
  1842. }
  1843. return true;
  1844. }
  1845. public static List<string> StrArrayToList(string[] strArray)
  1846. {
  1847. List<string> ret = new List<string>();
  1848. foreach (string s in strArray)
  1849. {
  1850. ret.Add(s);
  1851. }
  1852. return ret;
  1853. }
  1854. private static string[] __new_ParseCmdLine(string str)
  1855. {
  1856. List<string> o;
  1857. int i, len, mode;
  1858. char c;
  1859. StringBuilder tmp;
  1860. bool ignore_space = false;
  1861. if (str == null)
  1862. {
  1863. return new string[0];
  1864. }
  1865. o = new List<string>();
  1866. tmp = new StringBuilder();
  1867. mode = 0;
  1868. len = str.Length;
  1869. for (i = 0; i < len; i++)
  1870. {
  1871. c = str[i];
  1872. switch (mode)
  1873. {
  1874. case 0:
  1875. if (c == ' ' || c == '\t')
  1876. {
  1877. }
  1878. else
  1879. {
  1880. if (c == '\"')
  1881. {
  1882. if (str[i + 1] == '\"')
  1883. {
  1884. tmp.Append("\"");
  1885. i++;
  1886. }
  1887. else
  1888. {
  1889. ignore_space = true;
  1890. }
  1891. }
  1892. else
  1893. {
  1894. tmp.Append(c);
  1895. }
  1896. }
  1897. mode = 1;
  1898. break;
  1899. case 1:
  1900. if (ignore_space == false && (c == ' ' || c == '\t'))
  1901. {
  1902. o.Add(tmp.ToString());
  1903. tmp = new StringBuilder();
  1904. mode = 0;
  1905. }
  1906. else
  1907. {
  1908. if (c == '\"')
  1909. {
  1910. if (str[i + 1] == '\"')
  1911. {
  1912. tmp.Append("\"");
  1913. i++;
  1914. }
  1915. else
  1916. {
  1917. if (ignore_space == false)
  1918. {
  1919. ignore_space = true;
  1920. }
  1921. else
  1922. {
  1923. ignore_space = false;
  1924. }
  1925. }
  1926. }
  1927. else
  1928. {
  1929. tmp.Append(c);
  1930. }
  1931. }
  1932. break;
  1933. }
  1934. }
  1935. if (tmp.Length >= 1)
  1936. {
  1937. o.Add(tmp.ToString());
  1938. }
  1939. List<string> ret = new List<string>();
  1940. foreach (string s in o)
  1941. {
  1942. ret.Add(s);
  1943. }
  1944. return ret.ToArray();
  1945. }
  1946. public static int CompareString(string s1, string s2)
  1947. {
  1948. try
  1949. {
  1950. return string.Compare(s1, s2, true);
  1951. }
  1952. catch
  1953. {
  1954. return 0;
  1955. }
  1956. }
  1957. public static int CompareStringCaseSensitive(string s1, string s2)
  1958. {
  1959. try
  1960. {
  1961. return string.Compare(s1, s2, false);
  1962. }
  1963. catch
  1964. {
  1965. return 0;
  1966. }
  1967. }
  1968. public static string ReplaceStr(string str, string oldKeyword, string newKeyword)
  1969. {
  1970. return ReplaceStr(str, oldKeyword, newKeyword, false);
  1971. }
  1972. public static string ReplaceStr(string str, string oldKeyword, string newKeyword, bool caseSensitive)
  1973. {
  1974. int len_string, len_old, len_new;
  1975. if (str == null || oldKeyword == null || newKeyword == null)
  1976. {
  1977. return null;
  1978. }
  1979. if (caseSensitive == false)
  1980. {
  1981. return str.Replace(oldKeyword, newKeyword);
  1982. }
  1983. int i, j, num;
  1984. StringBuilder sb = new StringBuilder();
  1985. len_string = str.Length;
  1986. len_old = oldKeyword.Length;
  1987. len_new = newKeyword.Length;
  1988. i = j = num = 0;
  1989. while (true)
  1990. {
  1991. i = SearchStr(str, oldKeyword, i, caseSensitive);
  1992. if (i == -1)
  1993. {
  1994. sb.Append(str.Substring(j, len_string - j));
  1995. break;
  1996. }
  1997. num++;
  1998. sb.Append(str.Substring(j, i - j));
  1999. sb.Append(newKeyword);
  2000. i += len_old;
  2001. j = i;
  2002. }
  2003. return sb.ToString();
  2004. }
  2005. public static int SearchStrMulti(string str, int start, bool caseSensitive, out int foundIndex, out string foundKeyword, params string[] keywords)
  2006. {
  2007. int i;
  2008. foundIndex = -1;
  2009. foundKeyword = "";
  2010. int ret = -1;
  2011. int min = int.MaxValue;
  2012. for (i = 0; i < keywords.Length; i++)
  2013. {
  2014. string keyword = keywords[i];
  2015. int r = Str.SearchStr(str, keyword, start, caseSensitive);
  2016. if (r != -1)
  2017. {
  2018. if (min > r)
  2019. {
  2020. min = r;
  2021. foundKeyword = str.Substring(r, keyword.Length);
  2022. foundIndex = i;
  2023. }
  2024. }
  2025. }
  2026. if (foundIndex != -1)
  2027. {
  2028. ret = min;
  2029. }
  2030. return ret;
  2031. }
  2032. public static int SearchStr(string str, string keyword, int start)
  2033. {
  2034. return SearchStr(str, keyword, start, false);
  2035. }
  2036. public static int SearchStr(string str, string keyword, int start, bool caseSensitive)
  2037. {
  2038. if (str == null || keyword == null)
  2039. {
  2040. return -1;
  2041. }
  2042. try
  2043. {
  2044. return str.IndexOf(keyword, start, (caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase));
  2045. }
  2046. catch
  2047. {
  2048. return -1;
  2049. }
  2050. }
  2051. public static void Printf(string fmt, params object[] args)
  2052. {
  2053. if (args.Length == 0)
  2054. {
  2055. Console.Write(fmt);
  2056. }
  2057. else
  2058. {
  2059. Console.Write(FormatC(fmt, args));
  2060. }
  2061. }
  2062. public static string FormatC(string fmt)
  2063. {
  2064. return FormatC(fmt, new object[0]);
  2065. }
  2066. public static string FormatC(string fmt, params object[] args)
  2067. {
  2068. int i, len;
  2069. StringBuilder tmp;
  2070. List<string> o;
  2071. int mode = 0;
  2072. int pos = 0;
  2073. if (fmt == null)
  2074. {
  2075. return null;
  2076. }
  2077. len = fmt.Length;
  2078. tmp = new StringBuilder();
  2079. o = new List<string>();
  2080. mode = 0;
  2081. for (i = 0; i < len; i++)
  2082. {
  2083. char c = fmt[i];
  2084. if (mode == 0)
  2085. {
  2086. switch (c)
  2087. {
  2088. case '%':
  2089. if (fmt[i + 1] == '%')
  2090. {
  2091. i++;
  2092. tmp.Append("%");
  2093. }
  2094. else
  2095. {
  2096. mode = 1;
  2097. o.Add(tmp.ToString());
  2098. tmp = new StringBuilder();
  2099. tmp.Append(c);
  2100. }
  2101. break;
  2102. default:
  2103. tmp.Append(c);
  2104. break;
  2105. }
  2106. }
  2107. else
  2108. {
  2109. switch (c)
  2110. {
  2111. case 'c':
  2112. case 'C':
  2113. case 'd':
  2114. case 'i':
  2115. case 'o':
  2116. case 'u':
  2117. case 'x':
  2118. case 'X':
  2119. case 'e':
  2120. case 'E':
  2121. case 'f':
  2122. case 'g':
  2123. case 'G':
  2124. case 'a':
  2125. case 'A':
  2126. case 'n':
  2127. case 'p':
  2128. case 's':
  2129. case 'S':
  2130. tmp.Append(c);
  2131. PrintFParsedParam pp = new PrintFParsedParam(tmp.ToString());
  2132. string s;
  2133. if (pp.Ok)
  2134. {
  2135. s = pp.GetString(args[pos++]);
  2136. }
  2137. else
  2138. {
  2139. s = "(parse_error)";
  2140. }
  2141. o.Add(s);
  2142. tmp = new StringBuilder();
  2143. mode = 0;
  2144. break;
  2145. default:
  2146. tmp.Append(c);
  2147. break;
  2148. }
  2149. }
  2150. }
  2151. if (tmp.Length >= 1)
  2152. {
  2153. o.Add(tmp.ToString());
  2154. }
  2155. StringBuilder retstr = new StringBuilder();
  2156. foreach (string stmp in o)
  2157. {
  2158. retstr.Append(stmp);
  2159. }
  2160. return retstr.ToString();
  2161. }
  2162. static Encoding asciiEncoding = Encoding.ASCII;
  2163. public st

Large files files are truncated, but you can click here to view the full file