JsString.cs 10 KB

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