JsString.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. using System.Collections.Concurrent;
  2. using System.Diagnostics;
  3. using System.Text;
  4. using Jint.Runtime;
  5. namespace Jint.Native;
  6. [DebuggerDisplay("{ToString()}")]
  7. public class JsString : JsValue, IEquatable<JsString>, IEquatable<string>
  8. {
  9. private const int AsciiMax = 126;
  10. private static readonly JsString[] _charToJsValue;
  11. private static readonly JsString[] _charToStringJsValue;
  12. private static readonly JsString[] _intToStringJsValue;
  13. public static readonly JsString Empty;
  14. internal static readonly JsString NullString;
  15. internal static readonly JsString UndefinedString;
  16. internal static readonly JsString ObjectString;
  17. internal static readonly JsString FunctionString;
  18. internal static readonly JsString BooleanString;
  19. internal static readonly JsString StringString;
  20. internal static readonly JsString NumberString;
  21. internal static readonly JsString BigIntString;
  22. internal static readonly JsString SymbolString;
  23. internal static readonly JsString DefaultString;
  24. internal static readonly JsString NumberZeroString;
  25. internal static readonly JsString NumberOneString;
  26. internal static readonly JsString TrueString;
  27. internal static readonly JsString FalseString;
  28. internal static readonly JsString LengthString;
  29. internal static readonly JsValue CommaString;
  30. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  31. internal string _value;
  32. private static ConcurrentDictionary<string, JsString> _stringCache;
  33. static JsString()
  34. {
  35. _charToJsValue = new JsString[AsciiMax + 1];
  36. _charToStringJsValue = new JsString[AsciiMax + 1];
  37. for (var i = 0; i <= AsciiMax; i++)
  38. {
  39. _charToJsValue[i] = new JsString((char) i);
  40. _charToStringJsValue[i] = new JsString(((char) i).ToString());
  41. }
  42. _intToStringJsValue = new JsString[1024];
  43. for (var i = 0; i < _intToStringJsValue.Length; ++i)
  44. {
  45. _intToStringJsValue[i] = new JsString(TypeConverter.ToString(i));
  46. }
  47. _stringCache = new ConcurrentDictionary<string, JsString>(StringComparer.Ordinal);
  48. Empty = new JsString("", InternalTypes.String);
  49. NullString = CachedCreate("null");
  50. UndefinedString = CachedCreate("undefined");
  51. ObjectString = CachedCreate("object");
  52. FunctionString = CachedCreate("function");
  53. BooleanString = CachedCreate("boolean");
  54. StringString = CachedCreate("string");
  55. NumberString = CachedCreate("number");
  56. BigIntString = CachedCreate("bigint");
  57. SymbolString = CachedCreate("symbol");
  58. DefaultString = CachedCreate("default");
  59. NumberZeroString = CachedCreate("0");
  60. NumberOneString = CachedCreate("1");
  61. TrueString = CachedCreate("true");
  62. FalseString = CachedCreate("false");
  63. LengthString = CachedCreate("length");
  64. CommaString = CachedCreate(",");
  65. }
  66. public JsString(string value) : this(value, InternalTypes.String)
  67. {
  68. }
  69. private JsString(string value, InternalTypes type) : base(type)
  70. {
  71. _value = value;
  72. }
  73. public JsString(char value) : base(Types.String)
  74. {
  75. _value = value.ToString();
  76. }
  77. public static bool operator ==(JsString? a, JsString? b)
  78. {
  79. if (a is not null)
  80. {
  81. return a.Equals(b);
  82. }
  83. if (a is null)
  84. {
  85. return b is null;
  86. }
  87. return b is not null && a.Equals(b);
  88. }
  89. public static bool operator ==(JsValue? a, JsString? b)
  90. {
  91. if (a is JsString s && b is not null)
  92. {
  93. return s.Equals(b);
  94. }
  95. if (a is null)
  96. {
  97. return b is null;
  98. }
  99. return b is not null && a.Equals(b);
  100. }
  101. public static bool operator ==(JsString? a, JsValue? b)
  102. {
  103. if (a is not null)
  104. {
  105. return a.Equals(b);
  106. }
  107. if (a is null)
  108. {
  109. return b is null;
  110. }
  111. return b is not null && a.Equals(b);
  112. }
  113. public static bool operator !=(JsString a, JsValue b)
  114. {
  115. return !(a == b);
  116. }
  117. public static bool operator !=(JsValue a, JsString b)
  118. {
  119. return !(a == b);
  120. }
  121. public static bool operator !=(JsString a, JsString b)
  122. {
  123. return !(a == b);
  124. }
  125. internal static JsString Create(string value)
  126. {
  127. if (value.Length > 1)
  128. {
  129. return new JsString(value);
  130. }
  131. if (value.Length == 0)
  132. {
  133. return Empty;
  134. }
  135. var i = (uint) value[0];
  136. var temp = _charToStringJsValue;
  137. if (i < (uint) temp.Length)
  138. {
  139. return temp[i];
  140. }
  141. return new JsString(value);
  142. }
  143. internal static JsString CachedCreate(string value)
  144. {
  145. if (value.Length is < 2 or > 10)
  146. {
  147. return Create(value);
  148. }
  149. return _stringCache.GetOrAdd(value, static x => new JsString(x));
  150. }
  151. internal static JsString Create(char value)
  152. {
  153. var temp = _charToJsValue;
  154. if (value < (uint) temp.Length)
  155. {
  156. return temp[value];
  157. }
  158. return new JsString(value);
  159. }
  160. internal static JsString Create(int value)
  161. {
  162. var temp = _intToStringJsValue;
  163. if (value < (uint) temp.Length)
  164. {
  165. return temp[value];
  166. }
  167. return new JsString(TypeConverter.ToString(value));
  168. }
  169. internal static JsValue Create(uint value)
  170. {
  171. var temp = _intToStringJsValue;
  172. if (value < (uint) temp.Length)
  173. {
  174. return temp[value];
  175. }
  176. return new JsString(TypeConverter.ToString(value));
  177. }
  178. internal static JsValue Create(ulong value)
  179. {
  180. var temp = _intToStringJsValue;
  181. if (value < (uint) temp.Length)
  182. {
  183. return temp[value];
  184. }
  185. return new JsString(TypeConverter.ToString(value));
  186. }
  187. public virtual char this[int index] => _value[index];
  188. public virtual int Length => _value.Length;
  189. internal virtual JsString Append(JsValue jsValue)
  190. {
  191. return new ConcatenatedString(string.Concat(ToString(), TypeConverter.ToString(jsValue)));
  192. }
  193. internal virtual JsString EnsureCapacity(int capacity)
  194. {
  195. return new ConcatenatedString(_value, capacity);
  196. }
  197. public sealed override object ToObject() => ToString();
  198. internal sealed override bool ToBoolean()
  199. {
  200. return Length > 0;
  201. }
  202. public override string ToString() => _value;
  203. internal bool Contains(char c)
  204. {
  205. if (c == 0)
  206. {
  207. return false;
  208. }
  209. return ToString().Contains(c);
  210. }
  211. internal int IndexOf(string value, int startIndex = 0)
  212. {
  213. if (Length - startIndex < value.Length)
  214. {
  215. return -1;
  216. }
  217. return ToString().IndexOf(value, startIndex, StringComparison.Ordinal);
  218. }
  219. internal bool StartsWith(string value, int start = 0)
  220. {
  221. return value.Length + start <= Length && ToString().AsSpan(start).StartsWith(value.AsSpan(), StringComparison.Ordinal);
  222. }
  223. internal bool EndsWith(string value, int end = 0)
  224. {
  225. var start = end - value.Length;
  226. return start >= 0 && ToString().AsSpan(start, value.Length).EndsWith(value.AsSpan(), StringComparison.Ordinal);
  227. }
  228. internal string Substring(int startIndex, int length)
  229. {
  230. return ToString().Substring(startIndex, length);
  231. }
  232. internal string Substring(int startIndex)
  233. {
  234. return ToString().Substring(startIndex);
  235. }
  236. public sealed override bool Equals(object? obj) => Equals(obj as JsString);
  237. public sealed override bool Equals(JsValue? other) => Equals(other as JsString);
  238. public virtual bool Equals(string? other) => other != null && string.Equals(ToString(), other, StringComparison.Ordinal);
  239. public virtual bool Equals(JsString? other)
  240. {
  241. if (other is null)
  242. {
  243. return false;
  244. }
  245. if (ReferenceEquals(this, other))
  246. {
  247. return true;
  248. }
  249. return string.Equals(_value, other.ToString(), StringComparison.Ordinal);
  250. }
  251. public override bool IsLooselyEqual(JsValue value)
  252. {
  253. if (value is JsString jsString)
  254. {
  255. return Equals(jsString);
  256. }
  257. if (value.IsBigInt())
  258. {
  259. return value.IsBigInt() && TypeConverter.TryStringToBigInt(ToString(), out var temp) && temp == value.AsBigInt();
  260. }
  261. return base.IsLooselyEqual(value);
  262. }
  263. public override int GetHashCode() => StringComparer.Ordinal.GetHashCode(_value);
  264. internal sealed class ConcatenatedString : JsString
  265. {
  266. private StringBuilder? _stringBuilder;
  267. private bool _dirty;
  268. internal ConcatenatedString(string value, int capacity = 0)
  269. : base(value, InternalTypes.String | InternalTypes.RequiresCloning)
  270. {
  271. if (capacity > 0)
  272. {
  273. _stringBuilder = new StringBuilder(value, capacity);
  274. }
  275. else
  276. {
  277. _value = value;
  278. }
  279. }
  280. public override string ToString()
  281. {
  282. if (_dirty)
  283. {
  284. _value = _stringBuilder!.ToString();
  285. _dirty = false;
  286. }
  287. return _value;
  288. }
  289. public override char this[int index] => _stringBuilder?[index] ?? _value[index];
  290. internal override JsString Append(JsValue jsValue)
  291. {
  292. var value = TypeConverter.ToString(jsValue);
  293. if (_stringBuilder == null)
  294. {
  295. _stringBuilder = new StringBuilder(_value, _value.Length + value.Length);
  296. }
  297. _stringBuilder.Append(value);
  298. _dirty = true;
  299. return this;
  300. }
  301. internal override JsString EnsureCapacity(int capacity)
  302. {
  303. _stringBuilder!.EnsureCapacity(capacity);
  304. return this;
  305. }
  306. public override int Length => _stringBuilder?.Length ?? _value?.Length ?? 0;
  307. public override bool Equals(string? s)
  308. {
  309. if (s is null || Length != s.Length)
  310. {
  311. return false;
  312. }
  313. // we cannot use StringBuilder.Equals as it also checks Capacity on full framework / pre .NET Core 3
  314. if (_stringBuilder != null)
  315. {
  316. for (var i = 0; i < _stringBuilder.Length; ++i)
  317. {
  318. if (_stringBuilder[i] != s[i])
  319. {
  320. return false;
  321. }
  322. }
  323. return true;
  324. }
  325. return string.Equals(_value, s, StringComparison.Ordinal);
  326. }
  327. public override bool Equals(JsString? other)
  328. {
  329. if (other is ConcatenatedString cs)
  330. {
  331. var stringBuilder = _stringBuilder;
  332. var csStringBuilder = cs._stringBuilder;
  333. // we cannot use StringBuilder.Equals as it also checks Capacity on full framework / pre .NET Core 3
  334. if (stringBuilder != null && csStringBuilder != null && stringBuilder.Length == csStringBuilder.Length)
  335. {
  336. for (var i = 0; i < stringBuilder.Length; ++i)
  337. {
  338. if (stringBuilder[i] != csStringBuilder[i])
  339. {
  340. return false;
  341. }
  342. }
  343. return true;
  344. }
  345. return string.Equals(ToString(), cs.ToString(), StringComparison.Ordinal);
  346. }
  347. if (other is null || other.Length != Length)
  348. {
  349. return false;
  350. }
  351. return string.Equals(ToString(), other.ToString(), StringComparison.Ordinal);
  352. }
  353. public override int GetHashCode() => _stringBuilder?.GetHashCode() ?? StringComparer.Ordinal.GetHashCode(_value);
  354. internal override JsValue DoClone()
  355. {
  356. return new JsString(ToString());
  357. }
  358. }
  359. }