JsString.cs 13 KB

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