RpcPath.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using JsonRPC4.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. namespace JsonRPC4.Router
  7. {
  8. public class RpcPath : IEquatable<RpcPath>
  9. {
  10. private char[] path;
  11. private int? hashCodeCache;
  12. private RpcPath(char[] path)
  13. {
  14. if (path == null || path.Length < 1)
  15. {
  16. throw new ArgumentNullException("path");
  17. }
  18. this.path = path;
  19. }
  20. public static bool operator ==(RpcPath path1, RpcPath path2)
  21. {
  22. return path1?.Equals(path2) ?? ((object)path2 == null);
  23. }
  24. public static bool operator !=(RpcPath path1, RpcPath path2)
  25. {
  26. return !(path1 == path2);
  27. }
  28. public bool StartsWith(RpcPath other)
  29. {
  30. if (other == null)
  31. {
  32. return true;
  33. }
  34. if (other.path.Length > path.Length)
  35. {
  36. return false;
  37. }
  38. for (int i = 0; i < other.path.Length; i++)
  39. {
  40. if (other.path[i] != path[i])
  41. {
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. public bool Equals(RpcPath other)
  48. {
  49. if ((object)other == null)
  50. {
  51. return false;
  52. }
  53. return GetHashCode() == other.GetHashCode();
  54. }
  55. public override bool Equals(object obj)
  56. {
  57. RpcPath rpcPath = obj as RpcPath;
  58. if ((object)rpcPath != null)
  59. {
  60. return Equals(rpcPath);
  61. }
  62. return false;
  63. }
  64. public override int GetHashCode()
  65. {
  66. if (!hashCodeCache.HasValue)
  67. {
  68. int num = 1337;
  69. char[] array = path;
  70. foreach (char c in array)
  71. {
  72. num = num * 7 + c.GetHashCode();
  73. }
  74. hashCodeCache = num;
  75. }
  76. return hashCodeCache.Value;
  77. }
  78. public static RpcPath Parse(ReadOnlySpan<char> path)
  79. {
  80. //IL_0000: Unknown result type (might be due to invalid IL or missing references)
  81. if (!TryParse(path, out RpcPath rpcPath))
  82. {
  83. throw new RpcException(RpcErrorCode.ParseError, "Rpc path could not be parsed from '" + new string(path.ToArray()) + "'.");
  84. }
  85. return rpcPath;
  86. }
  87. public static bool TryParse(ReadOnlySpan<char> path, out RpcPath rpcPath)
  88. {
  89. if (path.IsEmpty)
  90. {
  91. rpcPath = null;
  92. return true;
  93. }
  94. try
  95. {
  96. int num = IsSlash(path[0]) ? 1 : 0;
  97. if (path.Length <= num)
  98. {
  99. rpcPath = null;
  100. return true;
  101. }
  102. int num2 = IsSlash( path[path.Length - 1]) ? (path.Length- 1) : path.Length;
  103. if (num >= num2 - 1)
  104. {
  105. rpcPath = null;
  106. return true;
  107. }
  108. char[] array = new char[num2 - num];
  109. int num3 = 0;
  110. for (int i = num; i < num2; i++)
  111. {
  112. if (char.IsWhiteSpace(path[i]))
  113. {
  114. rpcPath = null;
  115. return false;
  116. }
  117. char c2 = array[num3] = (!IsSlash(path[i])) ? char.ToLowerInvariant(path[i]) : '/';
  118. num3++;
  119. }
  120. rpcPath = new RpcPath(array);
  121. return true;
  122. }
  123. catch
  124. {
  125. rpcPath = null;
  126. return false;
  127. }
  128. bool IsSlash(char c)
  129. {
  130. if (c != '/')
  131. {
  132. return c == '\\';
  133. }
  134. return true;
  135. }
  136. }
  137. public RpcPath RemoveBasePath(RpcPath basePath)
  138. {
  139. if (!TryRemoveBasePath(basePath, out RpcPath result))
  140. {
  141. throw new RpcException(RpcErrorCode.ParseError, $"Count not remove path '{basePath}' from path '{this}'.");
  142. }
  143. return result;
  144. }
  145. public bool TryRemoveBasePath(RpcPath basePath, out RpcPath path)
  146. {
  147. if (basePath == null)
  148. {
  149. path = Clone();
  150. return true;
  151. }
  152. if (!StartsWith(basePath))
  153. {
  154. path = null;
  155. return false;
  156. }
  157. int num = this.path.Length - basePath.path.Length;
  158. if (num < 1)
  159. {
  160. path = null;
  161. return true;
  162. }
  163. char[] array = new char[num - 1];
  164. MemoryExtensions.AsSpan<char>(this.path, basePath.path.Length + 1).CopyTo(array);
  165. path = new RpcPath(array);
  166. return true;
  167. }
  168. public RpcPath Add(RpcPath other)
  169. {
  170. if (other == null)
  171. {
  172. return Clone();
  173. }
  174. char[] array = new char[path.Length + other.path.Length + 1];
  175. path.CopyTo(array, 0);
  176. array[path.Length] = '/';
  177. other.path.CopyTo(array, path.Length + 1);
  178. return new RpcPath(array);
  179. }
  180. public override string ToString()
  181. {
  182. return new string(path);
  183. }
  184. public RpcPath Clone()
  185. {
  186. char[] array = new char[path.Length];
  187. path.CopyTo(array, 0);
  188. return new RpcPath(array);
  189. }
  190. public static implicit operator string(RpcPath path)
  191. {
  192. return path.ToString();
  193. }
  194. public static implicit operator RpcPath(string s)
  195. {
  196. //IL_0001: Unknown result type (might be due to invalid IL or missing references)
  197. return Parse(MemoryExtensions.AsSpan(s));
  198. }
  199. }
  200. }