StringInlHelper.cs 1.7 KB

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