public unsafe static extern int unorm_normalize( char[] source, int sourceLength, UNormalizationMode mode, int options, char *result, int resultLength, ref UErrorCode status );
private static Normalizer2 GetNormalizer(UNormalizationMode mode) { return(Normalizer2.GetInstance(null, mode == UNormalizationMode.UNORM_NFC || mode == UNormalizationMode.UNORM_NFD ? "nfc" : "nfkc", mode == UNormalizationMode.UNORM_NFC || mode == UNormalizationMode.UNORM_NFKC ? Normalizer2.Mode.COMPOSE : Normalizer2.Mode.DECOMPOSE)); }
private static IntPtr GetNormalizer(UNormalizationMode mode) { ErrorCode errorCode; var ret = NativeMethods.unorm2_getInstance(IntPtr.Zero, (mode == UNormalizationMode.UNORM_NFC || mode == UNormalizationMode.UNORM_NFD) ? "nfc" : "nfkc", (mode == UNormalizationMode.UNORM_NFC || mode == UNormalizationMode.UNORM_NFKC) ? UNormalization2Mode.UNORM2_COMPOSE : UNormalization2Mode.UNORM2_DECOMPOSE, out errorCode); if (errorCode != ErrorCode.NoErrors) { throw new Exception("Normalizer.Normalize() failed with code " + errorCode); } return(ret); }
protected override void loadTestFunction() { if (testName == "TestWin_NFC") { myTestFunction = new TestFunction(TestWin); wmode = NormalizationForm.FormC; } else if (testName == "TestWin_NFD") { myTestFunction = new TestFunction(TestWin); wmode = NormalizationForm.FormD; } else if (testName == "TestWin_NFKC") { myTestFunction = new TestFunction(TestWin); wmode = NormalizationForm.FormKC; } else if (testName == "TestWin_NFKD") { myTestFunction = new TestFunction(TestWin); wmode = NormalizationForm.FormKD; } else if (testName == "TestICU_NFC") { myTestFunction = new TestFunction(TestICU); mode = (UNormalizationMode)4; } else if (testName == "TestICU_NFD") { myTestFunction = new TestFunction(TestICU); mode = (UNormalizationMode)2; } else if (testName == "TestICU_NFKC") { myTestFunction = new TestFunction(TestICU); mode = (UNormalizationMode)5; } else if (testName == "TestICU_NFKD") { myTestFunction = new TestFunction(TestICU); mode = (UNormalizationMode)3; } else { Console.Out.WriteLine("Unknown test name: {0}", testName); Environment.Exit(2); } }
/// <summary> /// Check whether the string is normalized according to the given mode. /// </summary> /// <param name="src"></param> /// <param name="mode"></param> /// <returns></returns> public static bool IsNormalized(string src, UNormalizationMode mode) { if (string.IsNullOrEmpty(src)) { return(true); } ErrorCode err; var fIsNorm = NativeMethods.unorm2_isNormalized(GetNormalizer(mode), src, src.Length, out err); if (err != ErrorCode.NoErrors) { throw new Exception("Normalizer.IsNormalized() failed with code " + err); } return(fIsNorm != 0); }
/// <summary> /// Normalize the string according to the given mode. /// </summary> /// <param name="src"></param> /// <param name="mode"></param> /// <returns></returns> public static string Normalize(string src, UNormalizationMode mode) { if (string.IsNullOrEmpty(src)) { return(string.Empty); } var length = src.Length + 10; var resPtr = Marshal.AllocCoTaskMem(length * 2); try { ErrorCode err; var normalizer = GetNormalizer(mode); var outLength = NativeMethods.unorm2_normalize(normalizer, src, src.Length, resPtr, length, out err); if (err > 0 && err != ErrorCode.BUFFER_OVERFLOW_ERROR) { throw new Exception("Normalizer.Normalize() failed with code " + err); } if (outLength >= length) { Marshal.FreeCoTaskMem(resPtr); length = outLength + 1; // allow room for the terminating NUL (FWR-505) resPtr = Marshal.AllocCoTaskMem(length * 2); outLength = NativeMethods.unorm2_normalize(normalizer, src, src.Length, resPtr, length, out err); } if (err > 0) { throw new Exception("Normalizer.Normalize() failed with code " + err); } var result = Marshal.PtrToStringUni(resPtr); // Strip any garbage left over at the end of the string. if (err == ErrorCode.STRING_NOT_TERMINATED_WARNING && result != null) { return(result.Substring(0, outLength)); } return(result); } finally { Marshal.FreeCoTaskMem(resPtr); } }
/// <summary> /// Check whether the string is normalized according to the given mode. /// </summary> /// <param name="src"></param> /// <param name="mode"></param> /// <returns></returns> public static bool IsNormalized(string src, UNormalizationMode mode) { var normalizer = GetNormalizer(mode); return(normalizer.IsNormalized(src)); }
/// <summary> /// Normalize the string according to the given mode. /// </summary> /// <param name="src"></param> /// <param name="mode"></param> /// <returns></returns> public static string Normalize(string src, UNormalizationMode mode) { var normalizer = GetNormalizer(mode); return(normalizer.Normalize(src)); }