C#实现,或替代,StrCmpLogicalW在SHLWAPI.DLL

问题描述:

有关在我的应用程序自然排序我目前的P / Invoke在一个名为SHLWAPI.DLL功能StrCmpLogicalW。我在想尝试运行我在Mono下的应用程序,但我当然不能有这样的的P / Invoke东西(据我反正知道)。

For natural sorting in my application I currently P/Invoke a function called StrCmpLogicalW in shlwapi.dll. I was thinking about trying to run my application under Mono, but then of course I can't have this P/Invoke stuff (as far as I know anyways).

是否有可能看到某个方法的实现,或者是有一个良好的,清洁高效的C#代码片段而做同样的事情?

Is it possible to see the implementation of that method somewhere, or is there a good, clean and efficient C# snippet which does the same thing?

我的code目前看起来是这样的:

My code currently looks like this:

[SuppressUnmanagedCodeSecurity]
internal static class SafeNativeMethods
{
    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
    public static extern int StrCmpLogicalW(string psz1, string psz2);
}

public class NaturalStringComparer : IComparer<string>
{
    private readonly int modifier = 1;

    public NaturalStringComparer() : this(false) {}
    public NaturalStringComparer(bool descending)
    {
        if (descending) modifier = -1;
    }

    public int Compare(string a, string b)
    {
        return SafeNativeMethods.StrCmpLogicalW(a ?? "", b ?? "") * modifier;
    }
}

所以,我正在寻找的是哪个不使用外部函数上面的类的替代品。

So, what I'm looking for is an alternative to the above class which doesn't use an extern function.

我在C#刚刚实现自然字符串比较,也许有人会发现它很有用:

I just implemented natural string comparison in C#, perhaps someone might find it useful:

public class NaturalComparer : IComparer<string>
{
   public int Compare(string x, string y)
   {
      if (x == null && y == null) return 0;
      if (x == null) return -1;
      if (y == null) return 1;

      int lx = x.Length, ly = y.Length;

      for (int mx = 0, my = 0; mx < lx && my < ly; mx++, my++)
      {
         if (char.IsDigit(x[mx]) && char.IsDigit(y[my]))
         {
            long vx = 0, vy = 0;

            for (; mx < lx && char.IsDigit(x[mx]); mx++)
               vx = vx * 10 + x[mx] - '0';

            for (; my < ly && char.IsDigit(y[my]); my++)
               vy = vy * 10 + y[my] - '0';

            if (vx != vy)
               return vx > vy ? 1 : -1;
         }

         if (mx < lx && my < ly && x[mx] != y[my])
            return x[mx] > y[my] ? 1 : -1;
      }

      return lx - ly;
   }
}