Comparing and sorting Unicode filenames

2020-06-06 05:18发布

Using Delphi 2007 and TMS components for Unicode utils and interface (upgrading to Delphi 2009 for Unicode support is not an option).

I'm storing a list of filenames in a string list (TTntStringList). It's sorted and case insensitive. The default sort routine uses CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, ...) to compare strings (and the same for Find). However, this is a problem because that will equate dummyss.txt with dummyß.txt (for example), but on NTFS it's perfectly legal to have those two files in the same folder, i.e. they are treated as different names.

My understanding is that on Vista and newer, the correct way to compare filenames is to use CompareStringOrdinal. Is this correct?

On pre-Vista systems, what would be the correct way? I believe it should be CompareStringW(LOCALE_INVARIANT, ...) but I'm not entirely sure.

Thanks

1条回答
We Are One
2楼-- · 2020-06-06 06:06

Quote from MSDN article Handling Sorting in Your Applications:

CompareStringOrdinal compares two Unicode strings to test for binary equality, as opposed to linguistic equality. Examples of such non-linguistic strings are NTFS file names, ...

CompareStringOrdinal requires Windows Vista or later.

Edit: Yes, it seems that in pre-Vista Windows you can use RtlCompareUnicodeString which is used internally by CompareStringOrdinal, too, and is available since Windows NT.

查看更多
登录 后发表回答