StringStream.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.IO;
  3. namespace NTERA.EmuEra.Game.EraEmu.Sub
  4. {
  5. /// <summary>
  6. /// 文字列を1文字ずつ評価するためのクラス
  7. /// </summary>
  8. internal sealed class StringStream
  9. {
  10. public StringStream(string s)
  11. {
  12. source = s ?? "";
  13. CurrentPosition = 0;
  14. }
  15. string source;
  16. public const char EndOfString = '\0';
  17. public string RowString => source;
  18. public int CurrentPosition { get; set; }
  19. public char Current
  20. {
  21. get
  22. {
  23. if (CurrentPosition >= source.Length)
  24. return EndOfString;
  25. return source[CurrentPosition];
  26. }
  27. }
  28. public void AppendString(string str)
  29. {
  30. if (CurrentPosition > source.Length)
  31. CurrentPosition = source.Length;
  32. source += " " + str;
  33. }
  34. /// <summary>
  35. /// 文字列終端に達した
  36. /// </summary>
  37. public bool EOS => CurrentPosition >= source.Length;
  38. ///変数の区切りである"[["と"]]"の先読みなどに使用
  39. public char Next
  40. {
  41. get
  42. {
  43. if (CurrentPosition + 1 >= source.Length)
  44. return EndOfString;
  45. return source[CurrentPosition + 1];
  46. }
  47. }
  48. public string Substring()
  49. {
  50. if (CurrentPosition >= source.Length)
  51. return "";
  52. if (CurrentPosition == 0)
  53. return source;
  54. return source.Substring(CurrentPosition);
  55. }
  56. public string Substring(int start, int length)
  57. {
  58. if (start >= source.Length || length == 0)
  59. return "";
  60. if (start + length > source.Length)
  61. length = source.Length - start;
  62. return source.Substring(start, length);
  63. }
  64. internal void Replace(int start, int count, string src)
  65. {
  66. //引数に正しい数字が送られてくること前提
  67. source = (source.Remove(start, count)).Insert(start, src);
  68. CurrentPosition = start;
  69. }
  70. public void ShiftNext()
  71. {
  72. CurrentPosition++;
  73. }
  74. public void Jump(int skip)
  75. {
  76. CurrentPosition += skip;
  77. }
  78. /// <summary>
  79. /// 検索文字列の相対位置を返す。見つからない場合、負の値。
  80. /// </summary>
  81. /// <param name="str"></param>
  82. public int Find(string str)
  83. {
  84. return source.IndexOf(str, CurrentPosition) - CurrentPosition;
  85. }
  86. /// <summary>
  87. /// 検索文字列の相対位置を返す。見つからない場合、負の値。
  88. /// </summary>
  89. public int Find(char c)
  90. {
  91. return source.IndexOf(c, CurrentPosition) - CurrentPosition;
  92. }
  93. public override string ToString()
  94. {
  95. if (source == null)
  96. return "";
  97. return source;
  98. }
  99. public bool CurrentEqualTo(string rother)
  100. {
  101. if (CurrentPosition + rother.Length > source.Length)
  102. return false;
  103. for (int i = 0; i < rother.Length;i++)
  104. {
  105. if (source[CurrentPosition + i] != rother[i])
  106. return false;
  107. }
  108. return true;
  109. }
  110. public bool TripleSymbol()
  111. {
  112. if (CurrentPosition + 3 > source.Length)
  113. return false;
  114. return (source[CurrentPosition] == source[CurrentPosition + 1]) && (source[CurrentPosition] == source[CurrentPosition + 2]);
  115. }
  116. public bool CurrentEqualTo(string rother, StringComparison comp)
  117. {
  118. if (CurrentPosition + rother.Length > source.Length)
  119. return false;
  120. string sub = source.Substring(CurrentPosition, rother.Length);
  121. return sub.Equals(rother, comp);
  122. }
  123. public void Seek(int offset, SeekOrigin origin)
  124. {
  125. if (origin == SeekOrigin.Begin)
  126. CurrentPosition = offset;
  127. else if (origin == SeekOrigin.Current)
  128. CurrentPosition = CurrentPosition + offset;
  129. else if (origin == SeekOrigin.End)
  130. CurrentPosition = source.Length + offset;
  131. if (CurrentPosition < 0)
  132. CurrentPosition = 0;
  133. }
  134. }
  135. }