JsString.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 readonly 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. return b is null;
  111. }
  112. public static bool operator !=(JsString a, JsValue b)
  113. {
  114. return !(a == b);
  115. }
  116. public static bool operator ==(JsString? a, string? b)
  117. {
  118. if (a is not null)
  119. {
  120. return a.Equals(b);
  121. }
  122. return b is null;
  123. }
  124. public static bool operator !=(JsString? a, string? b)
  125. {
  126. return !(a == b);
  127. }
  128. public static bool operator !=(JsValue a, JsString b)
  129. {
  130. return !(a == b);
  131. }
  132. public static bool operator !=(JsString a, JsString b)
  133. {
  134. return !(a == b);
  135. }
  136. internal static JsString Create(string value)
  137. {
  138. if (value.Length > 1)
  139. {
  140. return new JsString(value);
  141. }
  142. if (value.Length == 0)
  143. {
  144. return Empty;
  145. }
  146. var i = (uint) value[0];
  147. var temp = _charToStringJsValue;
  148. if (i < (uint) temp.Length)
  149. {
  150. return temp[i];
  151. }
  152. return new JsString(value);
  153. }
  154. internal static JsString CachedCreate(string value)
  155. {
  156. if (value.Length is < 2 or > 10)
  157. {
  158. return Create(value);
  159. }
  160. return _stringCache.GetOrAdd(value, static x => new JsString(x));
  161. }
  162. internal static JsString Create(char value)
  163. {
  164. var temp = _charToJsValue;
  165. if (value < (uint) temp.Length)
  166. {
  167. return temp[value];
  168. }
  169. return new JsString(value);
  170. }
  171. internal static JsString Create(int value)
  172. {
  173. var temp = _intToStringJsValue;
  174. if (value < (uint) temp.Length)
  175. {
  176. return temp[value];
  177. }
  178. return new JsString(TypeConverter.ToString(value));
  179. }
  180. internal static JsValue Create(uint value)
  181. {
  182. var temp = _intToStringJsValue;
  183. if (value < (uint) temp.Length)
  184. {
  185. return temp[value];
  186. }
  187. return new JsString(TypeConverter.ToString(value));
  188. }
  189. internal static JsValue Create(ulong value)
  190. {
  191. var temp = _intToStringJsValue;
  192. if (value < (uint) temp.Length)
  193. {
  194. return temp[value];
  195. }
  196. return new JsString(TypeConverter.ToString(value));
  197. }
  198. public virtual char this[int index] => _value[index];
  199. public virtual int Length => _value.Length;
  200. internal virtual JsString Append(JsValue jsValue)
  201. {
  202. return new ConcatenatedString(string.Concat(ToString(), TypeConverter.ToString(jsValue)));
  203. }
  204. internal virtual JsString EnsureCapacity(int capacity)
  205. {
  206. return new ConcatenatedString(_value, capacity);
  207. }
  208. public sealed override object ToObject() => ToString();
  209. internal sealed override bool ToBoolean()
  210. {
  211. return Length > 0;
  212. }
  213. public override string ToString() => _value;
  214. internal bool Contains(char c)
  215. {
  216. if (c == 0)
  217. {
  218. return false;
  219. }
  220. return ToString().Contains(c);
  221. }
  222. internal int IndexOf(string value, int startIndex = 0)
  223. {
  224. if (Length - startIndex < value.Length)
  225. {
  226. return -1;
  227. }
  228. return ToString().IndexOf(value, startIndex, StringComparison.Ordinal);
  229. }
  230. internal bool StartsWith(string value, int start = 0)
  231. {
  232. return value.Length + start <= Length && ToString().AsSpan(start).StartsWith(value.AsSpan(), StringComparison.Ordinal);
  233. }
  234. internal bool EndsWith(string value, int end = 0)
  235. {
  236. var start = end - value.Length;
  237. return start >= 0 && ToString().AsSpan(start, value.Length).EndsWith(value.AsSpan(), StringComparison.Ordinal);
  238. }
  239. internal string Substring(int startIndex, int length)
  240. {
  241. return ToString().Substring(startIndex, length);
  242. }
  243. internal string Substring(int startIndex)
  244. {
  245. return ToString().Substring(startIndex);
  246. }
  247. internal override bool TryGetIterator(
  248. Realm realm,
  249. [NotNullWhen(true)] out IteratorInstance? iterator,
  250. GeneratorKind hint = GeneratorKind.Sync,
  251. ICallable? method = null)
  252. {
  253. if (realm.Intrinsics.String.PrototypeObject.HasOriginalIterator)
  254. {
  255. iterator = new IteratorInstance.StringIterator(realm.GlobalEnv._engine, ToString());
  256. return true;
  257. }
  258. return base.TryGetIterator(realm, out iterator, hint, method);
  259. }
  260. public sealed override bool Equals(object? obj) => Equals(obj as JsString);
  261. public sealed override bool Equals(JsValue? other) => Equals(other as JsString);
  262. public virtual bool Equals(string? other) => other != null && string.Equals(ToString(), other, StringComparison.Ordinal);
  263. public virtual bool Equals(JsString? other)
  264. {
  265. if (other is null)
  266. {
  267. return false;
  268. }
  269. if (ReferenceEquals(this, other))
  270. {
  271. return true;
  272. }
  273. return string.Equals(_value, other.ToString(), StringComparison.Ordinal);
  274. }
  275. protected internal override bool IsLooselyEqual(JsValue value)
  276. {
  277. if (value is JsString jsString)
  278. {
  279. return Equals(jsString);
  280. }
  281. if (value.IsBigInt())
  282. {
  283. return value.IsBigInt() && TypeConverter.TryStringToBigInt(ToString(), out var temp) && temp == value.AsBigInt();
  284. }
  285. return base.IsLooselyEqual(value);
  286. }
  287. public override int GetHashCode() => StringComparer.Ordinal.GetHashCode(_value);
  288. internal sealed class ConcatenatedString : JsString
  289. {
  290. private StringBuilder? _stringBuilder;
  291. private bool _dirty;
  292. internal ConcatenatedString(string value, int capacity = 0)
  293. : base(value, InternalTypes.String | InternalTypes.RequiresCloning)
  294. {
  295. if (capacity > 0)
  296. {
  297. _stringBuilder = new StringBuilder(value, capacity);
  298. }
  299. else
  300. {
  301. _value = value;
  302. }
  303. }
  304. public override string ToString()
  305. {
  306. if (_dirty)
  307. {
  308. _value = _stringBuilder!.ToString();
  309. _dirty = false;
  310. }
  311. return _value;
  312. }
  313. public override char this[int index] => _stringBuilder?[index] ?? _value[index];
  314. internal override JsString Append(JsValue jsValue)
  315. {
  316. var value = TypeConverter.ToString(jsValue);
  317. if (_stringBuilder == null)
  318. {
  319. _stringBuilder = new StringBuilder(_value, _value.Length + value.Length);
  320. }
  321. _stringBuilder.Append(value);
  322. _dirty = true;
  323. return this;
  324. }
  325. internal override JsString EnsureCapacity(int capacity)
  326. {
  327. _stringBuilder!.EnsureCapacity(capacity);
  328. return this;
  329. }
  330. public override int Length => _stringBuilder?.Length ?? _value?.Length ?? 0;
  331. public override bool Equals(string? s)
  332. {
  333. if (s is null || Length != s.Length)
  334. {
  335. return false;
  336. }
  337. // we cannot use StringBuilder.Equals as it also checks Capacity on full framework / pre .NET Core 3
  338. if (_stringBuilder != null)
  339. {
  340. for (var i = 0; i < _stringBuilder.Length; ++i)
  341. {
  342. if (_stringBuilder[i] != s[i])
  343. {
  344. return false;
  345. }
  346. }
  347. return true;
  348. }
  349. return string.Equals(_value, s, StringComparison.Ordinal);
  350. }
  351. public override bool Equals(JsString? other)
  352. {
  353. if (other is ConcatenatedString cs)
  354. {
  355. var stringBuilder = _stringBuilder;
  356. var csStringBuilder = cs._stringBuilder;
  357. // we cannot use StringBuilder.Equals as it also checks Capacity on full framework / pre .NET Core 3
  358. if (stringBuilder != null && csStringBuilder != null && stringBuilder.Length == csStringBuilder.Length)
  359. {
  360. for (var i = 0; i < stringBuilder.Length; ++i)
  361. {
  362. if (stringBuilder[i] != csStringBuilder[i])
  363. {
  364. return false;
  365. }
  366. }
  367. return true;
  368. }
  369. return string.Equals(ToString(), cs.ToString(), StringComparison.Ordinal);
  370. }
  371. if (other is null || other.Length != Length)
  372. {
  373. return false;
  374. }
  375. return string.Equals(ToString(), other.ToString(), StringComparison.Ordinal);
  376. }
  377. public override int GetHashCode() => _stringBuilder?.GetHashCode() ?? StringComparer.Ordinal.GetHashCode(_value);
  378. internal override JsValue DoClone()
  379. {
  380. return new JsString(ToString());
  381. }
  382. }
  383. }