TextInfo.Windows.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. namespace System.Globalization
  6. {
  7. public partial class TextInfo
  8. {
  9. private unsafe void FinishInitialization()
  10. {
  11. if (GlobalizationMode.Invariant)
  12. {
  13. _sortHandle = IntPtr.Zero;
  14. return;
  15. }
  16. const uint LCMAP_SORTHANDLE = 0x20000000;
  17. IntPtr handle;
  18. int ret = Interop.Kernel32.LCMapStringEx(_textInfoName, LCMAP_SORTHANDLE, null, 0, &handle, IntPtr.Size, null, null, IntPtr.Zero);
  19. _sortHandle = ret > 0 ? handle : IntPtr.Zero;
  20. }
  21. private unsafe void ChangeCase(char* pSource, int pSourceLen, char* pResult, int pResultLen, bool toUpper)
  22. {
  23. Debug.Assert(!GlobalizationMode.Invariant);
  24. Debug.Assert(pSource != null);
  25. Debug.Assert(pResult != null);
  26. Debug.Assert(pSourceLen >= 0);
  27. Debug.Assert(pResultLen >= 0);
  28. Debug.Assert(pSourceLen <= pResultLen);
  29. // Check for Invariant to avoid A/V in LCMapStringEx
  30. uint linguisticCasing = IsInvariantLocale(_textInfoName) ? 0 : LCMAP_LINGUISTIC_CASING;
  31. int ret = Interop.Kernel32.LCMapStringEx(_sortHandle != IntPtr.Zero ? null : _textInfoName,
  32. linguisticCasing | (toUpper ? LCMAP_UPPERCASE : LCMAP_LOWERCASE),
  33. pSource,
  34. pSourceLen,
  35. pResult,
  36. pSourceLen,
  37. null,
  38. null,
  39. _sortHandle);
  40. if (ret == 0)
  41. {
  42. throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
  43. }
  44. Debug.Assert(ret == pSourceLen, "Expected getting the same length of the original string");
  45. }
  46. // PAL Ends here
  47. private IntPtr _sortHandle;
  48. private const uint LCMAP_LINGUISTIC_CASING = 0x01000000;
  49. private const uint LCMAP_LOWERCASE = 0x00000100;
  50. private const uint LCMAP_UPPERCASE = 0x00000200;
  51. private static bool IsInvariantLocale(string localeName) => localeName == "";
  52. }
  53. }