Skip to content

Instantly share code, notes, and snippets.

@iso2022jp
Created November 12, 2020 13:43
Show Gist options
  • Save iso2022jp/b7dc64e8741b4e543782bfbc026097db to your computer and use it in GitHub Desktop.
Save iso2022jp/b7dc64e8741b4e543782bfbc026097db to your computer and use it in GitHub Desktop.
NLS 更新に伴う濁点カタカナの半角全角一致テスト
using Microsoft.VisualBasic;
using NlsTest;
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
const int COMPARE_STRING = 1;
const int NORM_IGNOREWIDTH = 0x00020000;
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool GetNLSVersionEx(int function, string lpLocaleName, NLSVERSIONINFOEX lpVersionInformation);
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
static extern int CompareStringEx(
[MarshalAs(UnmanagedType.LPWStr)] string lpLocaleName, int dwCmpFlags,
[MarshalAs(UnmanagedType.LPWStr)] string lpString1, int cchCount1,
[MarshalAs(UnmanagedType.LPWStr)] string lpString2, int cchCount2,
IntPtr lpVersionInformation, IntPtr lpReserved, IntPtr lParam);
[DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true)]
static extern int CompareStringA(
int Locale, int dwCmpFlags,
[MarshalAs(UnmanagedType.LPStr)] string lpString1, int cchCount1,
[MarshalAs(UnmanagedType.LPStr)] string lpString2, int cchCount2);
var locale = "ja";
var ja = CultureInfo.GetCultureInfo(locale);
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = ja;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var version = new NLSVERSIONINFOEX();
GetNLSVersionEx(COMPARE_STRING, locale, version).OrThrow();
Console.WriteLine();
Console.WriteLine(version);
var a = "データ";
var b = "データ";
Console.WriteLine();
Console.WriteLine($"Compare '{a}' and '{b}' on text mode.");
Console.WriteLine("{0} # NLS CompareStringEx", CompareStringEx(locale, NORM_IGNOREWIDTH, a, -1, b, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero));
Console.WriteLine("{0} # NLS CompareStringA", CompareStringA(ja.LCID, NORM_IGNOREWIDTH, a, -1, b, -1));
Console.WriteLine("{0} # System.String.Compare", string.Compare(a, b, CultureInfo.GetCultureInfo(locale), CompareOptions.IgnoreWidth));
Console.WriteLine("{0} # Microsoft.VisualBasic.Strings.StrComp", Strings.StrComp(a, b, CompareMethod.Text));
[StructLayout(LayoutKind.Sequential)]
class NLSVERSIONINFOEX
{
public int dwNLSVersionInfoSize;
public int dwNLSVersion;
public int dwDefinedVersion;
public int dwEffectiveId;
public Guid guidCustomVersion;
public NLSVERSIONINFOEX()
{
dwNLSVersionInfoSize = Marshal.SizeOf(this);
}
// https://docs.microsoft.com/en-us/archive/blogs/shawnste/how-to-tell-if-the-collation-version-changed
public override string ToString() => $"NLS version = {dwNLSVersion:X6}, customVersion = {guidCustomVersion}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment