123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- using JsonRPC4.Common;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace JsonRPC4.Router
- {
- public class RpcPath : IEquatable<RpcPath>
- {
- private char[] path;
- private int? hashCodeCache;
- private RpcPath(char[] path)
- {
- if (path == null || path.Length < 1)
- {
- throw new ArgumentNullException("path");
- }
- this.path = path;
- }
- public static bool operator ==(RpcPath path1, RpcPath path2)
- {
- return path1?.Equals(path2) ?? ((object)path2 == null);
- }
- public static bool operator !=(RpcPath path1, RpcPath path2)
- {
- return !(path1 == path2);
- }
- public bool StartsWith(RpcPath other)
- {
- if (other == null)
- {
- return true;
- }
- if (other.path.Length > path.Length)
- {
- return false;
- }
- for (int i = 0; i < other.path.Length; i++)
- {
- if (other.path[i] != path[i])
- {
- return false;
- }
- }
- return true;
- }
- public bool Equals(RpcPath other)
- {
- if ((object)other == null)
- {
- return false;
- }
- return GetHashCode() == other.GetHashCode();
- }
- public override bool Equals(object obj)
- {
- RpcPath rpcPath = obj as RpcPath;
- if ((object)rpcPath != null)
- {
- return Equals(rpcPath);
- }
- return false;
- }
- public override int GetHashCode()
- {
- if (!hashCodeCache.HasValue)
- {
- int num = 1337;
- char[] array = path;
- foreach (char c in array)
- {
- num = num * 7 + c.GetHashCode();
- }
- hashCodeCache = num;
- }
- return hashCodeCache.Value;
- }
- public static RpcPath Parse(ReadOnlySpan<char> path)
- {
- //IL_0000: Unknown result type (might be due to invalid IL or missing references)
- if (!TryParse(path, out RpcPath rpcPath))
- {
- throw new RpcException(RpcErrorCode.ParseError, "Rpc path could not be parsed from '" + new string(path.ToArray()) + "'.");
- }
- return rpcPath;
- }
- public static bool TryParse(ReadOnlySpan<char> path, out RpcPath rpcPath)
- {
- if (path.IsEmpty)
- {
- rpcPath = null;
- return true;
- }
- try
- {
-
- int num = IsSlash(path[0]) ? 1 : 0;
- if (path.Length <= num)
- {
- rpcPath = null;
- return true;
- }
- int num2 = IsSlash( path[path.Length - 1]) ? (path.Length- 1) : path.Length;
- if (num >= num2 - 1)
- {
- rpcPath = null;
- return true;
- }
- char[] array = new char[num2 - num];
- int num3 = 0;
- for (int i = num; i < num2; i++)
- {
- if (char.IsWhiteSpace(path[i]))
- {
- rpcPath = null;
- return false;
- }
- char c2 = array[num3] = (!IsSlash(path[i])) ? char.ToLowerInvariant(path[i]) : '/';
- num3++;
- }
- rpcPath = new RpcPath(array);
- return true;
- }
- catch
- {
- rpcPath = null;
- return false;
- }
- bool IsSlash(char c)
- {
- if (c != '/')
- {
- return c == '\\';
- }
- return true;
- }
- }
- public RpcPath RemoveBasePath(RpcPath basePath)
- {
- if (!TryRemoveBasePath(basePath, out RpcPath result))
- {
- throw new RpcException(RpcErrorCode.ParseError, $"Count not remove path '{basePath}' from path '{this}'.");
- }
- return result;
- }
- public bool TryRemoveBasePath(RpcPath basePath, out RpcPath path)
- {
- if (basePath == null)
- {
- path = Clone();
- return true;
- }
- if (!StartsWith(basePath))
- {
- path = null;
- return false;
- }
- int num = this.path.Length - basePath.path.Length;
- if (num < 1)
- {
- path = null;
- return true;
- }
- char[] array = new char[num - 1];
- MemoryExtensions.AsSpan<char>(this.path, basePath.path.Length + 1).CopyTo(array);
- path = new RpcPath(array);
- return true;
- }
- public RpcPath Add(RpcPath other)
- {
- if (other == null)
- {
- return Clone();
- }
- char[] array = new char[path.Length + other.path.Length + 1];
- path.CopyTo(array, 0);
- array[path.Length] = '/';
- other.path.CopyTo(array, path.Length + 1);
- return new RpcPath(array);
- }
- public override string ToString()
- {
- return new string(path);
- }
- public RpcPath Clone()
- {
- char[] array = new char[path.Length];
- path.CopyTo(array, 0);
- return new RpcPath(array);
- }
- public static implicit operator string(RpcPath path)
- {
- return path.ToString();
- }
- public static implicit operator RpcPath(string s)
- {
- //IL_0001: Unknown result type (might be due to invalid IL or missing references)
- return Parse(MemoryExtensions.AsSpan(s));
- }
- }
- }
|