StringPrototype.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using System.Globalization;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using Jint.Native.Json;
  7. using Jint.Native.Object;
  8. using Jint.Native.RegExp;
  9. using Jint.Native.Symbol;
  10. using Jint.Runtime;
  11. using Jint.Runtime.Descriptors;
  12. using Jint.Runtime.Descriptors.Specialized;
  13. using Jint.Runtime.Interop;
  14. namespace Jint.Native.String;
  15. /// <summary>
  16. /// https://tc39.es/ecma262/#sec-properties-of-the-string-prototype-object
  17. /// </summary>
  18. internal sealed class StringPrototype : StringInstance
  19. {
  20. private readonly Realm _realm;
  21. private readonly StringConstructor _constructor;
  22. internal ClrFunction? _originalIteratorFunction;
  23. internal StringPrototype(
  24. Engine engine,
  25. Realm realm,
  26. StringConstructor constructor,
  27. ObjectPrototype objectPrototype)
  28. : base(engine, JsString.Empty)
  29. {
  30. _prototype = objectPrototype;
  31. _length = PropertyDescriptor.AllForbiddenDescriptor.NumberZero;
  32. _realm = realm;
  33. _constructor = constructor;
  34. }
  35. protected override void Initialize()
  36. {
  37. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  38. const PropertyFlag propertyFlags = lengthFlags | PropertyFlag.Writable;
  39. var trimStart = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "trimStart", prototype.TrimStart, 0, lengthFlags), propertyFlags);
  40. var trimEnd = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "trimEnd", prototype.TrimEnd, 0, lengthFlags), propertyFlags);
  41. var properties = new PropertyDictionary(37, checkExistingKeys: false)
  42. {
  43. ["constructor"] = new PropertyDescriptor(_constructor, PropertyFlag.NonEnumerable),
  44. ["toString"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toString", prototype.ToStringString, 0, lengthFlags), propertyFlags),
  45. ["valueOf"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "valueOf", prototype.ValueOf, 0, lengthFlags), propertyFlags),
  46. ["charAt"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "charAt", prototype.CharAt, 1, lengthFlags), propertyFlags),
  47. ["charCodeAt"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "charCodeAt", prototype.CharCodeAt, 1, lengthFlags), propertyFlags),
  48. ["codePointAt"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "codePointAt", prototype.CodePointAt, 1, lengthFlags), propertyFlags),
  49. ["concat"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "concat", prototype.Concat, 1, lengthFlags), propertyFlags),
  50. ["indexOf"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "indexOf", prototype.IndexOf, 1, lengthFlags), propertyFlags),
  51. ["endsWith"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "endsWith", prototype.EndsWith, 1, lengthFlags), propertyFlags),
  52. ["startsWith"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "startsWith", prototype.StartsWith, 1, lengthFlags), propertyFlags),
  53. ["lastIndexOf"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "lastIndexOf", prototype.LastIndexOf, 1, lengthFlags), propertyFlags),
  54. ["localeCompare"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "localeCompare", prototype.LocaleCompare, 1, lengthFlags), propertyFlags),
  55. ["match"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "match", prototype.Match, 1, lengthFlags), propertyFlags),
  56. ["matchAll"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "matchAll", prototype.MatchAll, 1, lengthFlags), propertyFlags),
  57. ["replace"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "replace", prototype.Replace, 2, lengthFlags), propertyFlags),
  58. ["replaceAll"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "replaceAll", prototype.ReplaceAll, 2, lengthFlags), propertyFlags),
  59. ["search"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "search", prototype.Search, 1, lengthFlags), propertyFlags),
  60. ["slice"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "slice", prototype.Slice, 2, lengthFlags), propertyFlags),
  61. ["split"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "split", prototype.Split, 2, lengthFlags), propertyFlags),
  62. ["substr"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "substr", Substr, 2), propertyFlags),
  63. ["substring"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "substring", prototype.Substring, 2, lengthFlags), propertyFlags),
  64. ["toLowerCase"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toLowerCase", prototype.ToLowerCase, 0, lengthFlags), propertyFlags),
  65. ["toLocaleLowerCase"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toLocaleLowerCase", prototype.ToLocaleLowerCase, 0, lengthFlags), propertyFlags),
  66. ["toUpperCase"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toUpperCase", prototype.ToUpperCase, 0, lengthFlags), propertyFlags),
  67. ["toLocaleUpperCase"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toLocaleUpperCase", prototype.ToLocaleUpperCase, 0, lengthFlags), propertyFlags),
  68. ["trim"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "trim", prototype.Trim, 0, lengthFlags), propertyFlags),
  69. ["trimStart"] = trimStart,
  70. ["trimEnd"] = trimEnd,
  71. ["trimLeft"] = trimStart,
  72. ["trimRight"] = trimEnd,
  73. ["padStart"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "padStart", prototype.PadStart, 1, lengthFlags), propertyFlags),
  74. ["padEnd"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "padEnd", prototype.PadEnd, 1, lengthFlags), propertyFlags),
  75. ["includes"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "includes", prototype.Includes, 1, lengthFlags), propertyFlags),
  76. ["normalize"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "normalize", prototype.Normalize, 0, lengthFlags), propertyFlags),
  77. ["repeat"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "repeat", prototype.Repeat, 1, lengthFlags), propertyFlags),
  78. ["at"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "at", prototype.At, 1, lengthFlags), propertyFlags),
  79. ["isWellFormed"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "isWellFormed", prototype.IsWellFormed, 0, lengthFlags), propertyFlags),
  80. ["toWellFormed"] = new LazyPropertyDescriptor<StringPrototype>(this, static prototype => new ClrFunction(prototype._engine, "toWellFormed", prototype.ToWellFormed, 0, lengthFlags), propertyFlags),
  81. };
  82. SetProperties(properties);
  83. _originalIteratorFunction = new ClrFunction(_engine, "[Symbol.iterator]", Iterator, 0, lengthFlags);
  84. var symbols = new SymbolDictionary(1)
  85. {
  86. [GlobalSymbolRegistry.Iterator] = new PropertyDescriptor(_originalIteratorFunction, propertyFlags)
  87. };
  88. SetSymbols(symbols);
  89. }
  90. internal override bool HasOriginalIterator => ReferenceEquals(Get(GlobalSymbolRegistry.Iterator), _originalIteratorFunction);
  91. private ObjectInstance Iterator(JsValue thisObject, JsCallArguments arguments)
  92. {
  93. TypeConverter.RequireObjectCoercible(_engine, thisObject);
  94. var str = TypeConverter.ToString(thisObject);
  95. return _realm.Intrinsics.StringIteratorPrototype.Construct(str);
  96. }
  97. private JsValue ToStringString(JsValue thisObject, JsCallArguments arguments)
  98. {
  99. if (thisObject.IsString())
  100. {
  101. return thisObject;
  102. }
  103. var s = TypeConverter.ToObject(_realm, thisObject) as StringInstance;
  104. if (s is null)
  105. {
  106. Throw.TypeError(_realm);
  107. }
  108. return s.StringData;
  109. }
  110. // http://msdn.microsoft.com/en-us/library/system.char.iswhitespace(v=vs.110).aspx
  111. // http://en.wikipedia.org/wiki/Byte_order_mark
  112. const char BOM_CHAR = '\uFEFF';
  113. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  114. private static bool IsWhiteSpaceEx(char c)
  115. {
  116. return char.IsWhiteSpace(c) || c == BOM_CHAR;
  117. }
  118. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  119. private static string TrimEndEx(string s)
  120. {
  121. if (s.Length == 0)
  122. return string.Empty;
  123. if (!IsWhiteSpaceEx(s[s.Length - 1]))
  124. return s;
  125. return TrimEnd(s);
  126. }
  127. private static string TrimEnd(string s)
  128. {
  129. var i = s.Length - 1;
  130. while (i >= 0)
  131. {
  132. if (IsWhiteSpaceEx(s[i]))
  133. i--;
  134. else
  135. break;
  136. }
  137. return i >= 0 ? s.Substring(0, i + 1) : string.Empty;
  138. }
  139. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  140. internal static string TrimStartEx(string s)
  141. {
  142. if (s.Length == 0)
  143. return string.Empty;
  144. if (!IsWhiteSpaceEx(s[0]))
  145. return s;
  146. return TrimStart(s);
  147. }
  148. private static string TrimStart(string s)
  149. {
  150. var i = 0;
  151. while (i < s.Length)
  152. {
  153. if (IsWhiteSpaceEx(s[i]))
  154. i++;
  155. else
  156. break;
  157. }
  158. return i >= s.Length ? string.Empty : s.Substring(i);
  159. }
  160. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  161. internal static string TrimEx(string s)
  162. {
  163. return TrimEndEx(TrimStartEx(s));
  164. }
  165. /// <summary>
  166. /// https://tc39.es/ecma262/#sec-string.prototype.trim
  167. /// </summary>
  168. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  169. private JsValue Trim(JsValue thisObject, JsCallArguments arguments)
  170. {
  171. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  172. var s = TypeConverter.ToJsString(thisObject);
  173. if (s.Length == 0 || (!IsWhiteSpaceEx(s[0]) && !IsWhiteSpaceEx(s[s.Length - 1])))
  174. {
  175. return s;
  176. }
  177. return TrimEx(s.ToString());
  178. }
  179. /// <summary>
  180. /// https://tc39.es/ecma262/#sec-string.prototype.trimstart
  181. /// </summary>
  182. private JsValue TrimStart(JsValue thisObject, JsCallArguments arguments)
  183. {
  184. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  185. var s = TypeConverter.ToJsString(thisObject);
  186. if (s.Length == 0 || !IsWhiteSpaceEx(s[0]))
  187. {
  188. return s;
  189. }
  190. return TrimStartEx(s.ToString());
  191. }
  192. /// <summary>
  193. /// https://tc39.es/ecma262/#sec-string.prototype.trimend
  194. /// </summary>
  195. private JsValue TrimEnd(JsValue thisObject, JsCallArguments arguments)
  196. {
  197. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  198. var s = TypeConverter.ToJsString(thisObject);
  199. if (s.Length == 0 || !IsWhiteSpaceEx(s[s.Length - 1]))
  200. {
  201. return s;
  202. }
  203. return TrimEndEx(s.ToString());
  204. }
  205. private JsValue ToLocaleUpperCase(JsValue thisObject, JsCallArguments arguments)
  206. {
  207. TypeConverter.RequireObjectCoercible(_engine, thisObject);
  208. var s = TypeConverter.ToString(thisObject);
  209. var culture = CultureInfo.InvariantCulture;
  210. if (arguments.Length > 0 && arguments[0].IsString())
  211. {
  212. try
  213. {
  214. var cultureArgument = arguments[0].ToString();
  215. culture = CultureInfo.GetCultureInfo(cultureArgument);
  216. }
  217. catch (CultureNotFoundException)
  218. {
  219. Throw.RangeError(_realm, "Incorrect culture information provided");
  220. }
  221. }
  222. if (string.Equals("lt", culture.Name, StringComparison.OrdinalIgnoreCase))
  223. {
  224. s = StringInlHelper.LithuanianStringProcessor(s);
  225. #if NET462
  226. // Code specific to .NET Framework 4.6.2.
  227. // For no good reason this verison does not upper case these characters correctly.
  228. return new JsString(s.ToUpper(culture)
  229. .Replace("ϳ", "Ϳ")
  230. .Replace("ʝ", "Ʝ"));
  231. #endif
  232. }
  233. return new JsString(s.ToUpper(culture));
  234. }
  235. private JsValue ToUpperCase(JsValue thisObject, JsCallArguments arguments)
  236. {
  237. TypeConverter.RequireObjectCoercible(_engine, thisObject);
  238. var s = TypeConverter.ToString(thisObject);
  239. return new JsString(s.ToUpperInvariant());
  240. }
  241. private JsValue ToLocaleLowerCase(JsValue thisObject, JsCallArguments arguments)
  242. {
  243. TypeConverter.RequireObjectCoercible(_engine, thisObject);
  244. var s = TypeConverter.ToString(thisObject);
  245. return new JsString(s.ToLower(CultureInfo.InvariantCulture));
  246. }
  247. private JsValue ToLowerCase(JsValue thisObject, JsCallArguments arguments)
  248. {
  249. TypeConverter.RequireObjectCoercible(_engine, thisObject);
  250. var s = TypeConverter.ToString(thisObject);
  251. return s.ToLowerInvariant();
  252. }
  253. private static int ToIntegerSupportInfinity(JsValue numberVal)
  254. {
  255. return numberVal._type == InternalTypes.Integer
  256. ? numberVal.AsInteger()
  257. : ToIntegerSupportInfinityUnlikely(numberVal);
  258. }
  259. [MethodImpl(MethodImplOptions.NoInlining)]
  260. private static int ToIntegerSupportInfinityUnlikely(JsValue numberVal)
  261. {
  262. var doubleVal = TypeConverter.ToInteger(numberVal);
  263. int intVal;
  264. if (double.IsPositiveInfinity(doubleVal))
  265. intVal = int.MaxValue;
  266. else if (double.IsNegativeInfinity(doubleVal))
  267. intVal = int.MinValue;
  268. else
  269. intVal = (int) doubleVal;
  270. return intVal;
  271. }
  272. private JsValue Substring(JsValue thisObject, JsCallArguments arguments)
  273. {
  274. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  275. var s = TypeConverter.ToString(thisObject);
  276. var start = TypeConverter.ToNumber(arguments.At(0));
  277. var end = TypeConverter.ToNumber(arguments.At(1));
  278. if (double.IsNaN(start) || start < 0)
  279. {
  280. start = 0;
  281. }
  282. if (double.IsNaN(end) || end < 0)
  283. {
  284. end = 0;
  285. }
  286. var len = s.Length;
  287. var intStart = ToIntegerSupportInfinity(start);
  288. var intEnd = arguments.At(1).IsUndefined() ? len : ToIntegerSupportInfinity(end);
  289. var finalStart = System.Math.Min(len, System.Math.Max(intStart, 0));
  290. var finalEnd = System.Math.Min(len, System.Math.Max(intEnd, 0));
  291. // Swap value if finalStart < finalEnd
  292. var from = System.Math.Min(finalStart, finalEnd);
  293. var to = System.Math.Max(finalStart, finalEnd);
  294. var length = to - from;
  295. if (length == 0)
  296. {
  297. return JsString.Empty;
  298. }
  299. if (length == 1)
  300. {
  301. return JsString.Create(s[from]);
  302. }
  303. return new JsString(s.Substring(from, length));
  304. }
  305. private static JsValue Substr(JsValue thisObject, JsCallArguments arguments)
  306. {
  307. var s = TypeConverter.ToString(thisObject);
  308. var start = TypeConverter.ToInteger(arguments.At(0));
  309. var length = arguments.At(1).IsUndefined()
  310. ? double.PositiveInfinity
  311. : TypeConverter.ToInteger(arguments.At(1));
  312. start = start >= 0 ? start : System.Math.Max(s.Length + start, 0);
  313. length = System.Math.Min(System.Math.Max(length, 0), s.Length - start);
  314. if (length <= 0)
  315. {
  316. return JsString.Empty;
  317. }
  318. var startIndex = TypeConverter.ToInt32(start);
  319. var l = TypeConverter.ToInt32(length);
  320. if (l == 1)
  321. {
  322. return TypeConverter.ToString(s[startIndex]);
  323. }
  324. return s.Substring(startIndex, l);
  325. }
  326. /// <summary>
  327. /// https://tc39.es/ecma262/#sec-string.prototype.split
  328. /// </summary>
  329. private JsValue Split(JsValue thisObject, JsCallArguments arguments)
  330. {
  331. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  332. var separator = arguments.At(0);
  333. var limit = arguments.At(1);
  334. // fast path for empty regexp
  335. if (separator is JsRegExp R && string.Equals(R.Source, JsRegExp.regExpForMatchingAllCharacters, StringComparison.Ordinal))
  336. {
  337. separator = JsString.Empty;
  338. }
  339. if (separator is ObjectInstance oi)
  340. {
  341. var splitter = GetMethod(_realm, oi, GlobalSymbolRegistry.Split);
  342. if (splitter != null)
  343. {
  344. return splitter.Call(separator, thisObject, limit);
  345. }
  346. }
  347. var s = TypeConverter.ToString(thisObject);
  348. // Coerce into a number, true will become 1
  349. var lim = limit.IsUndefined() ? uint.MaxValue : TypeConverter.ToUint32(limit);
  350. if (separator.IsNull())
  351. {
  352. separator = "null";
  353. }
  354. else if (!separator.IsUndefined())
  355. {
  356. if (!separator.IsRegExp())
  357. {
  358. separator = TypeConverter.ToJsString(separator); // Coerce into a string, for an object call toString()
  359. }
  360. }
  361. if (lim == 0)
  362. {
  363. return _realm.Intrinsics.Array.ArrayCreate(0);
  364. }
  365. if (separator.IsUndefined())
  366. {
  367. var arrayInstance = _realm.Intrinsics.Array.ArrayCreate(1);
  368. arrayInstance.SetIndexValue(0, s, updateLength: false);
  369. return arrayInstance;
  370. }
  371. return SplitWithStringSeparator(_realm, separator, s, lim);
  372. }
  373. internal static JsValue SplitWithStringSeparator(Realm realm, JsValue separator, string s, uint lim)
  374. {
  375. var segments = StringExecutionContext.Current.SplitSegmentList;
  376. segments.Clear();
  377. var sep = TypeConverter.ToString(separator);
  378. if (sep == string.Empty)
  379. {
  380. if (s.Length > segments.Capacity)
  381. {
  382. segments.Capacity = s.Length;
  383. }
  384. for (var i = 0; i < s.Length; i++)
  385. {
  386. segments.Add(TypeConverter.ToString(s[i]));
  387. }
  388. }
  389. else
  390. {
  391. var array = StringExecutionContext.Current.SplitArray1;
  392. array[0] = sep;
  393. segments.AddRange(s.Split(array, StringSplitOptions.None));
  394. }
  395. var length = (uint) System.Math.Min(segments.Count, lim);
  396. var a = realm.Intrinsics.Array.ArrayCreate(length);
  397. for (int i = 0; i < length; i++)
  398. {
  399. a.SetIndexValue((uint) i, segments[i], updateLength: false);
  400. }
  401. a.SetLength(length);
  402. return a;
  403. }
  404. /// <summary>
  405. /// https://tc39.es/proposal-relative-indexing-method/#sec-string-prototype-additions
  406. /// </summary>
  407. private JsValue At(JsValue thisObject, JsCallArguments arguments)
  408. {
  409. TypeConverter.RequireObjectCoercible(_engine, thisObject);
  410. var start = arguments.At(0);
  411. var o = thisObject.ToString();
  412. long len = o.Length;
  413. var relativeIndex = TypeConverter.ToInteger(start);
  414. int k;
  415. if (relativeIndex < 0)
  416. {
  417. k = (int) (len + relativeIndex);
  418. }
  419. else
  420. {
  421. k = (int) relativeIndex;
  422. }
  423. if (k < 0 || k >= len)
  424. {
  425. return Undefined;
  426. }
  427. return o[k];
  428. }
  429. private JsValue Slice(JsValue thisObject, JsCallArguments arguments)
  430. {
  431. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  432. var start = TypeConverter.ToNumber(arguments.At(0));
  433. if (double.IsNegativeInfinity(start))
  434. {
  435. start = 0;
  436. }
  437. if (double.IsPositiveInfinity(start))
  438. {
  439. return JsString.Empty;
  440. }
  441. var s = TypeConverter.ToJsString(thisObject);
  442. var end = TypeConverter.ToNumber(arguments.At(1));
  443. if (double.IsPositiveInfinity(end))
  444. {
  445. end = s.Length;
  446. }
  447. var len = s.Length;
  448. var intStart = (int) start;
  449. var intEnd = arguments.At(1).IsUndefined() ? len : (int) TypeConverter.ToInteger(end);
  450. var from = intStart < 0 ? System.Math.Max(len + intStart, 0) : System.Math.Min(intStart, len);
  451. var to = intEnd < 0 ? System.Math.Max(len + intEnd, 0) : System.Math.Min(intEnd, len);
  452. var span = System.Math.Max(to - from, 0);
  453. if (span == 0)
  454. {
  455. return JsString.Empty;
  456. }
  457. if (span == 1)
  458. {
  459. return JsString.Create(s[from]);
  460. }
  461. return s.Substring(from, span);
  462. }
  463. private JsValue Search(JsValue thisObject, JsCallArguments arguments)
  464. {
  465. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  466. var regex = arguments.At(0);
  467. if (regex is ObjectInstance oi)
  468. {
  469. var searcher = GetMethod(_realm, oi, GlobalSymbolRegistry.Search);
  470. if (searcher != null)
  471. {
  472. return searcher.Call(regex, thisObject);
  473. }
  474. }
  475. var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct([regex]);
  476. var s = TypeConverter.ToJsString(thisObject);
  477. return _engine.Invoke(rx, GlobalSymbolRegistry.Search, [s]);
  478. }
  479. /// <summary>
  480. /// https://tc39.es/ecma262/#sec-string.prototype.replace
  481. /// </summary>
  482. private JsValue Replace(JsValue thisObject, JsCallArguments arguments)
  483. {
  484. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  485. var searchValue = arguments.At(0);
  486. var replaceValue = arguments.At(1);
  487. if (!searchValue.IsNullOrUndefined())
  488. {
  489. var replacer = GetMethod(_realm, searchValue, GlobalSymbolRegistry.Replace);
  490. if (replacer != null)
  491. {
  492. return replacer.Call(searchValue, thisObject, replaceValue);
  493. }
  494. }
  495. var thisString = TypeConverter.ToJsString(thisObject);
  496. var searchString = TypeConverter.ToString(searchValue);
  497. var functionalReplace = replaceValue is ICallable;
  498. if (!functionalReplace)
  499. {
  500. replaceValue = TypeConverter.ToJsString(replaceValue);
  501. }
  502. var position = thisString.IndexOf(searchString);
  503. if (position < 0)
  504. {
  505. return thisString;
  506. }
  507. string replStr;
  508. if (functionalReplace)
  509. {
  510. var replValue = ((ICallable) replaceValue).Call(Undefined, searchString, position, thisString);
  511. replStr = TypeConverter.ToString(replValue);
  512. }
  513. else
  514. {
  515. var captures = System.Array.Empty<string>();
  516. replStr = RegExpPrototype.GetSubstitution(searchString, thisString.ToString(), position, captures, Undefined, TypeConverter.ToString(replaceValue));
  517. }
  518. var tailPos = position + searchString.Length;
  519. var newString = thisString.Substring(0, position) + replStr + thisString.Substring(tailPos);
  520. return newString;
  521. }
  522. /// <summary>
  523. /// https://tc39.es/ecma262/#sec-string.prototype.replaceall
  524. /// </summary>
  525. private JsValue ReplaceAll(JsValue thisObject, JsCallArguments arguments)
  526. {
  527. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  528. var searchValue = arguments.At(0);
  529. var replaceValue = arguments.At(1);
  530. if (!searchValue.IsNullOrUndefined())
  531. {
  532. if (searchValue.IsRegExp())
  533. {
  534. var flags = searchValue.Get(RegExpPrototype.PropertyFlags);
  535. TypeConverter.RequireObjectCoercible(_engine, flags);
  536. if (!TypeConverter.ToString(flags).Contains('g'))
  537. {
  538. Throw.TypeError(_realm, "String.prototype.replaceAll called with a non-global RegExp argument");
  539. }
  540. }
  541. var replacer = GetMethod(_realm, searchValue, GlobalSymbolRegistry.Replace);
  542. if (replacer != null)
  543. {
  544. return replacer.Call(searchValue, thisObject, replaceValue);
  545. }
  546. }
  547. var thisString = TypeConverter.ToString(thisObject);
  548. var searchString = TypeConverter.ToString(searchValue);
  549. var functionalReplace = replaceValue is ICallable;
  550. if (!functionalReplace)
  551. {
  552. replaceValue = TypeConverter.ToJsString(replaceValue);
  553. // check fast case
  554. var newValue = replaceValue.ToString();
  555. if (!newValue.Contains('$') && searchString.Length > 0)
  556. {
  557. // just plain old string replace
  558. return thisString.Replace(searchString, newValue);
  559. }
  560. }
  561. // https://tc39.es/ecma262/#sec-stringindexof
  562. static int StringIndexOf(string s, string search, int fromIndex)
  563. {
  564. if (search.Length == 0 && fromIndex <= s.Length)
  565. {
  566. return fromIndex;
  567. }
  568. return fromIndex < s.Length
  569. ? s.IndexOf(search, fromIndex, StringComparison.Ordinal)
  570. : -1;
  571. }
  572. var searchLength = searchString.Length;
  573. var advanceBy = System.Math.Max(1, searchLength);
  574. var endOfLastMatch = 0;
  575. using var result = new ValueStringBuilder();
  576. var position = StringIndexOf(thisString, searchString, 0);
  577. while (position != -1)
  578. {
  579. string replacement;
  580. var preserved = thisString.Substring(endOfLastMatch, position - endOfLastMatch);
  581. if (functionalReplace)
  582. {
  583. var replValue = ((ICallable) replaceValue).Call(Undefined, searchString, position, thisString);
  584. replacement = TypeConverter.ToString(replValue);
  585. }
  586. else
  587. {
  588. var captures = System.Array.Empty<string>();
  589. replacement = RegExpPrototype.GetSubstitution(searchString, thisString, position, captures, Undefined, TypeConverter.ToString(replaceValue));
  590. }
  591. result.Append(preserved);
  592. result.Append(replacement);
  593. endOfLastMatch = position + searchLength;
  594. position = StringIndexOf(thisString, searchString, position + advanceBy);
  595. }
  596. if (endOfLastMatch < thisString.Length)
  597. {
  598. result.Append(thisString.AsSpan(endOfLastMatch));
  599. }
  600. return result.ToString();
  601. }
  602. private JsValue Match(JsValue thisObject, JsCallArguments arguments)
  603. {
  604. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  605. var regex = arguments.At(0);
  606. if (regex is ObjectInstance oi)
  607. {
  608. var matcher = GetMethod(_realm, oi, GlobalSymbolRegistry.Match);
  609. if (matcher != null)
  610. {
  611. return matcher.Call(regex, thisObject);
  612. }
  613. }
  614. var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct([regex]);
  615. var s = TypeConverter.ToJsString(thisObject);
  616. return _engine.Invoke(rx, GlobalSymbolRegistry.Match, [s]);
  617. }
  618. private JsValue MatchAll(JsValue thisObject, JsCallArguments arguments)
  619. {
  620. TypeConverter.RequireObjectCoercible(_engine, thisObject);
  621. var regex = arguments.At(0);
  622. if (!regex.IsNullOrUndefined())
  623. {
  624. if (regex.IsRegExp())
  625. {
  626. var flags = regex.Get(RegExpPrototype.PropertyFlags);
  627. TypeConverter.RequireObjectCoercible(_engine, flags);
  628. if (!TypeConverter.ToString(flags).Contains('g'))
  629. {
  630. Throw.TypeError(_realm);
  631. }
  632. }
  633. var matcher = GetMethod(_realm, regex, GlobalSymbolRegistry.MatchAll);
  634. if (matcher != null)
  635. {
  636. return matcher.Call(regex, thisObject);
  637. }
  638. }
  639. var s = TypeConverter.ToJsString(thisObject);
  640. var rx = (JsRegExp) _realm.Intrinsics.RegExp.Construct([regex, "g"]);
  641. return _engine.Invoke(rx, GlobalSymbolRegistry.MatchAll, [s]);
  642. }
  643. private JsValue LocaleCompare(JsValue thisObject, JsCallArguments arguments)
  644. {
  645. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  646. var s = TypeConverter.ToString(thisObject);
  647. var that = TypeConverter.ToString(arguments.At(0));
  648. var culture = Engine.Options.Culture;
  649. if (arguments.Length > 1 && arguments[1].IsString())
  650. {
  651. culture = CultureInfo.GetCultureInfo(arguments.At(1).AsString());
  652. }
  653. return culture.CompareInfo.Compare(s.Normalize(NormalizationForm.FormKD), that.Normalize(NormalizationForm.FormKD));
  654. }
  655. /// <summary>
  656. /// https://tc39.es/ecma262/#sec-string.prototype.lastindexof
  657. /// </summary>
  658. private JsValue LastIndexOf(JsValue thisObject, JsCallArguments arguments)
  659. {
  660. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  661. var jsString = TypeConverter.ToJsString(thisObject);
  662. var searchStr = TypeConverter.ToString(arguments.At(0));
  663. double numPos = double.NaN;
  664. if (arguments.Length > 1 && !arguments[1].IsUndefined())
  665. {
  666. numPos = TypeConverter.ToNumber(arguments[1]);
  667. }
  668. var pos = double.IsNaN(numPos) ? double.PositiveInfinity : TypeConverter.ToInteger(numPos);
  669. var len = jsString.Length;
  670. var start = (int) System.Math.Min(System.Math.Max(pos, 0), len);
  671. var searchLen = searchStr.Length;
  672. if (searchLen > len)
  673. {
  674. return JsNumber.IntegerNegativeOne;
  675. }
  676. var s = jsString.ToString();
  677. var i = start;
  678. bool found;
  679. do
  680. {
  681. found = true;
  682. var j = 0;
  683. while (found && j < searchLen)
  684. {
  685. if (i + searchLen > len || s[i + j] != searchStr[j])
  686. {
  687. found = false;
  688. }
  689. else
  690. {
  691. j++;
  692. }
  693. }
  694. if (!found)
  695. {
  696. i--;
  697. }
  698. } while (!found && i >= 0);
  699. return i;
  700. }
  701. /// <summary>
  702. /// https://tc39.es/ecma262/#sec-string.prototype.indexof
  703. /// </summary>
  704. private JsValue IndexOf(JsValue thisObject, JsCallArguments arguments)
  705. {
  706. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  707. var s = TypeConverter.ToJsString(thisObject);
  708. var searchStr = TypeConverter.ToString(arguments.At(0));
  709. double pos = 0;
  710. if (arguments.Length > 1 && !arguments[1].IsUndefined())
  711. {
  712. pos = TypeConverter.ToInteger(arguments[1]);
  713. }
  714. if (pos > s.Length)
  715. {
  716. pos = s.Length;
  717. }
  718. if (pos < 0)
  719. {
  720. pos = 0;
  721. }
  722. return s.IndexOf(searchStr, (int) pos);
  723. }
  724. private JsValue Concat(JsValue thisObject, JsCallArguments arguments)
  725. {
  726. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  727. if (thisObject is not JsString jsString)
  728. {
  729. jsString = new JsString.ConcatenatedString(TypeConverter.ToString(thisObject));
  730. }
  731. else
  732. {
  733. jsString = jsString.EnsureCapacity(0);
  734. }
  735. foreach (var argument in arguments)
  736. {
  737. jsString = jsString.Append(argument);
  738. }
  739. return jsString;
  740. }
  741. private JsValue CharCodeAt(JsValue thisObject, JsCallArguments arguments)
  742. {
  743. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  744. JsValue pos = arguments.Length > 0 ? arguments[0] : 0;
  745. var s = TypeConverter.ToJsString(thisObject);
  746. var position = (int) TypeConverter.ToInteger(pos);
  747. if (position < 0 || position >= s.Length)
  748. {
  749. return JsNumber.DoubleNaN;
  750. }
  751. return (long) s[position];
  752. }
  753. /// <summary>
  754. /// https://tc39.es/ecma262/#sec-string.prototype.codepointat
  755. /// </summary>
  756. private JsValue CodePointAt(JsValue thisObject, JsCallArguments arguments)
  757. {
  758. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  759. JsValue pos = arguments.Length > 0 ? arguments[0] : 0;
  760. var s = TypeConverter.ToString(thisObject);
  761. var position = (int) TypeConverter.ToInteger(pos);
  762. if (position < 0 || position >= s.Length)
  763. {
  764. return Undefined;
  765. }
  766. return CodePointAt(s, position).CodePoint;
  767. }
  768. [StructLayout(LayoutKind.Auto)]
  769. private readonly record struct CodePointResult(int CodePoint, int CodeUnitCount, bool IsUnpairedSurrogate);
  770. private static CodePointResult CodePointAt(string s, int position)
  771. {
  772. var size = s.Length;
  773. var first = s.CharCodeAt(position);
  774. var cp = s.CharCodeAt(position);
  775. var firstIsLeading = char.IsHighSurrogate(first);
  776. var firstIsTrailing = char.IsLowSurrogate(first);
  777. if (!firstIsLeading && !firstIsTrailing)
  778. {
  779. return new CodePointResult(cp, 1, false);
  780. }
  781. if (firstIsTrailing || position + 1 == size)
  782. {
  783. return new CodePointResult(cp, 1, true);
  784. }
  785. var second = s.CharCodeAt(position + 1);
  786. if (!char.IsLowSurrogate(second))
  787. {
  788. return new CodePointResult(cp, 1, true);
  789. }
  790. return new CodePointResult(char.ConvertToUtf32(first, second), 2, false);
  791. }
  792. private JsValue CharAt(JsValue thisObject, JsCallArguments arguments)
  793. {
  794. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  795. var s = TypeConverter.ToJsString(thisObject);
  796. var position = TypeConverter.ToInteger(arguments.At(0));
  797. var size = s.Length;
  798. if (position >= size || position < 0)
  799. {
  800. return JsString.Empty;
  801. }
  802. return JsString.Create(s[(int) position]);
  803. }
  804. private JsValue ValueOf(JsValue thisObject, JsCallArguments arguments)
  805. {
  806. if (thisObject is StringInstance si)
  807. {
  808. return si.StringData;
  809. }
  810. if (thisObject is JsString)
  811. {
  812. return thisObject;
  813. }
  814. Throw.TypeError(_realm);
  815. return Undefined;
  816. }
  817. /// <summary>
  818. /// https://tc39.es/ecma262/#sec-string.prototype.padstart
  819. /// </summary>
  820. private JsValue PadStart(JsValue thisObject, JsCallArguments arguments)
  821. {
  822. return StringPad(thisObject, arguments, true);
  823. }
  824. /// <summary>
  825. /// https://tc39.es/ecma262/#sec-string.prototype.padend
  826. /// </summary>
  827. private JsValue PadEnd(JsValue thisObject, JsCallArguments arguments)
  828. {
  829. return StringPad(thisObject, arguments, false);
  830. }
  831. /// <summary>
  832. /// https://tc39.es/ecma262/#sec-stringpad
  833. /// </summary>
  834. private JsValue StringPad(JsValue thisObject, JsCallArguments arguments, bool padStart)
  835. {
  836. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  837. var s = TypeConverter.ToJsString(thisObject);
  838. var targetLength = TypeConverter.ToInt32(arguments.At(0));
  839. var padStringValue = arguments.At(1);
  840. var padString = padStringValue.IsUndefined()
  841. ? " "
  842. : TypeConverter.ToString(padStringValue);
  843. if (s.Length > targetLength || padString.Length == 0)
  844. {
  845. return s;
  846. }
  847. targetLength -= s.Length;
  848. if (targetLength > padString.Length)
  849. {
  850. padString = string.Join("", System.Linq.Enumerable.Repeat(padString, (targetLength / padString.Length) + 1));
  851. }
  852. return padStart
  853. ? $"{padString.Substring(0, targetLength)}{s}"
  854. : $"{s}{padString.Substring(0, targetLength)}";
  855. }
  856. /// <summary>
  857. /// https://tc39.es/ecma262/#sec-string.prototype.startswith
  858. /// </summary>
  859. private JsValue StartsWith(JsValue thisObject, JsCallArguments arguments)
  860. {
  861. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  862. var s = TypeConverter.ToJsString(thisObject);
  863. var searchString = arguments.At(0);
  864. if (ReferenceEquals(searchString, Null))
  865. {
  866. searchString = "null";
  867. }
  868. else
  869. {
  870. if (searchString.IsRegExp())
  871. {
  872. Throw.TypeError(_realm);
  873. }
  874. }
  875. var searchStr = TypeConverter.ToString(searchString);
  876. var pos = TypeConverter.ToInt32(arguments.At(1));
  877. var len = s.Length;
  878. var start = System.Math.Min(System.Math.Max(pos, 0), len);
  879. return s.StartsWith(searchStr, start);
  880. }
  881. /// <summary>
  882. /// https://tc39.es/ecma262/#sec-string.prototype.endswith
  883. /// </summary>
  884. private JsValue EndsWith(JsValue thisObject, JsCallArguments arguments)
  885. {
  886. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  887. var s = TypeConverter.ToJsString(thisObject);
  888. var searchString = arguments.At(0);
  889. if (ReferenceEquals(searchString, Null))
  890. {
  891. searchString = "null";
  892. }
  893. else
  894. {
  895. if (searchString.IsRegExp())
  896. {
  897. Throw.TypeError(_realm);
  898. }
  899. }
  900. var searchStr = TypeConverter.ToString(searchString);
  901. var len = s.Length;
  902. var pos = TypeConverter.ToInt32(arguments.At(1, len));
  903. var end = System.Math.Min(System.Math.Max(pos, 0), len);
  904. return s.EndsWith(searchStr, end);
  905. }
  906. /// <summary>
  907. /// https://tc39.es/ecma262/#sec-string.prototype.includes
  908. /// </summary>
  909. private JsValue Includes(JsValue thisObject, JsCallArguments arguments)
  910. {
  911. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  912. var s = TypeConverter.ToJsString(thisObject);
  913. var searchString = arguments.At(0);
  914. if (searchString.IsRegExp())
  915. {
  916. Throw.TypeError(_realm, "First argument to String.prototype.includes must not be a regular expression");
  917. }
  918. var searchStr = TypeConverter.ToString(searchString);
  919. double pos = 0;
  920. if (arguments.Length > 1 && !arguments[1].IsUndefined())
  921. {
  922. pos = TypeConverter.ToInteger(arguments[1]);
  923. }
  924. if (searchStr.Length == 0)
  925. {
  926. return JsBoolean.True;
  927. }
  928. if (pos < 0)
  929. {
  930. pos = 0;
  931. }
  932. return s.IndexOf(searchStr, (int) pos) > -1;
  933. }
  934. private JsValue Normalize(JsValue thisObject, JsCallArguments arguments)
  935. {
  936. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  937. var str = TypeConverter.ToString(thisObject);
  938. var param = arguments.At(0);
  939. var form = "NFC";
  940. if (!param.IsUndefined())
  941. {
  942. form = TypeConverter.ToString(param);
  943. }
  944. var nf = NormalizationForm.FormC;
  945. switch (form)
  946. {
  947. case "NFC":
  948. nf = NormalizationForm.FormC;
  949. break;
  950. case "NFD":
  951. nf = NormalizationForm.FormD;
  952. break;
  953. case "NFKC":
  954. nf = NormalizationForm.FormKC;
  955. break;
  956. case "NFKD":
  957. nf = NormalizationForm.FormKD;
  958. break;
  959. default:
  960. Throw.RangeError(
  961. _realm,
  962. "The normalization form should be one of NFC, NFD, NFKC, NFKD.");
  963. break;
  964. }
  965. return str.Normalize(nf);
  966. }
  967. /// <summary>
  968. /// https://tc39.es/ecma262/#sec-string.prototype.repeat
  969. /// </summary>
  970. private JsValue Repeat(JsValue thisObject, JsCallArguments arguments)
  971. {
  972. TypeConverter.RequireObjectCoercible(Engine, thisObject);
  973. var s = TypeConverter.ToString(thisObject);
  974. var count = arguments.At(0);
  975. var n = TypeConverter.ToIntegerOrInfinity(count);
  976. if (n < 0 || double.IsPositiveInfinity(n))
  977. {
  978. Throw.RangeError(_realm, "Invalid count value");
  979. }
  980. if (n == 0 || s.Length == 0)
  981. {
  982. return JsString.Empty;
  983. }
  984. if (s.Length == 1)
  985. {
  986. return new string(s[0], (int) n);
  987. }
  988. var sb = new ValueStringBuilder((int) (n * s.Length));
  989. for (var i = 0; i < n; ++i)
  990. {
  991. sb.Append(s);
  992. }
  993. return sb.ToString();
  994. }
  995. private JsValue IsWellFormed(JsValue thisObject, JsCallArguments arguments)
  996. {
  997. TypeConverter.RequireObjectCoercible(_engine, thisObject);
  998. var s = TypeConverter.ToString(thisObject);
  999. return IsStringWellFormedUnicode(s);
  1000. }
  1001. private JsValue ToWellFormed(JsValue thisObject, JsCallArguments arguments)
  1002. {
  1003. TypeConverter.RequireObjectCoercible(_engine, thisObject);
  1004. var s = TypeConverter.ToString(thisObject);
  1005. var strLen = s.Length;
  1006. var k = 0;
  1007. var result = new ValueStringBuilder();
  1008. while (k < strLen)
  1009. {
  1010. var cp = CodePointAt(s, k);
  1011. if (cp.IsUnpairedSurrogate)
  1012. {
  1013. // \uFFFD
  1014. result.Append('�');
  1015. }
  1016. else
  1017. {
  1018. result.Append(s.AsSpan(k, cp.CodeUnitCount));
  1019. }
  1020. k += cp.CodeUnitCount;
  1021. }
  1022. return result.ToString();
  1023. }
  1024. private static bool IsStringWellFormedUnicode(string s)
  1025. {
  1026. for (var i = 0; i < s.Length; ++i)
  1027. {
  1028. var isSurrogate = (s.CharCodeAt(i) & 0xF800) == 0xD800;
  1029. if (!isSurrogate)
  1030. {
  1031. continue;
  1032. }
  1033. var isLeadingSurrogate = s.CharCodeAt(i) < 0xDC00;
  1034. if (!isLeadingSurrogate)
  1035. {
  1036. return false; // unpaired trailing surrogate
  1037. }
  1038. var isFollowedByTrailingSurrogate = i + 1 < s.Length && (s.CharCodeAt(i + 1) & 0xFC00) == 0xDC00;
  1039. if (!isFollowedByTrailingSurrogate)
  1040. {
  1041. return false; // unpaired leading surrogate
  1042. }
  1043. ++i;
  1044. }
  1045. return true;
  1046. }
  1047. }