StringInlHelper.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Text;
  2. namespace Jint.Native.String
  3. {
  4. /// <summary>
  5. /// Some internacionalization logic that is special or specific to determined culture.
  6. /// </summary>
  7. internal class StringInlHelper
  8. {
  9. private static List<int> GetLithuaninanReplaceableCharIdx(string input)
  10. {
  11. List<int> replaceableCharsIdx = new List<int>();
  12. for (int i = 0; i < input.Length; i++)
  13. {
  14. if (input[i].Equals('\u0307'))
  15. {
  16. replaceableCharsIdx.Add(i);
  17. }
  18. }
  19. // For capital I and J we do not replace the dot above (\u3017).
  20. replaceableCharsIdx
  21. .RemoveAll(idx => (idx > 0) && input[idx - 1] == 'I' || input[idx - 1] == 'J');
  22. return replaceableCharsIdx;
  23. }
  24. /// <summary>
  25. /// Lithuanian case is a bit special. For more info see:
  26. /// https://github.com/tc39/test262/blob/main/test/intl402/String/prototype/toLocaleUpperCase/special_casing_Lithuanian.js
  27. /// </summary>
  28. public static string LithuanianStringProcessor(string input)
  29. {
  30. var replaceableCharsIdx = GetLithuaninanReplaceableCharIdx(input);
  31. if (replaceableCharsIdx.Count > 0)
  32. {
  33. StringBuilder stringBuilder = new StringBuilder(input);
  34. // Remove characters in reverse order to avoid index shifting
  35. for (int i = replaceableCharsIdx.Count - 1; i >= 0; i--)
  36. {
  37. int index = replaceableCharsIdx[i];
  38. if (index >= 0 && index < stringBuilder.Length)
  39. {
  40. stringBuilder.Remove(index, 1);
  41. }
  42. }
  43. return stringBuilder.ToString();
  44. }
  45. return input;
  46. }
  47. }
  48. }