MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / SortWithOptions

Function SortWithOptions

source/script2.cpp:6257–6294  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6255
6256
6257int SortWithOptions(const void *a1, const void *a2)
6258// Decided to just have one sort function since there are so many permutations. The performance
6259// will be a little bit worse, but it seems simpler to implement and maintain.
6260// This function's input parameters are pointers to the elements of the array. Since those elements
6261// are themselves pointers, the input parameters are therefore pointers to pointers (handles).
6262{
6263 LPTSTR sort_item1 = *(LPTSTR *)a1;
6264 LPTSTR sort_item2 = *(LPTSTR *)a2;
6265 if (g_SortColumnOffset > 0)
6266 {
6267 // Adjust each string (even for numerical sort) to be the right column position,
6268 // or the position of its zero terminator if the column offset goes beyond its length:
6269 size_t length = _tcslen(sort_item1);
6270 sort_item1 += (size_t)g_SortColumnOffset > length ? length : g_SortColumnOffset;
6271 length = _tcslen(sort_item2);
6272 sort_item2 += (size_t)g_SortColumnOffset > length ? length : g_SortColumnOffset;
6273 }
6274 if (g_SortNumeric) // Takes precedence over g_SortCaseSensitive
6275 {
6276 // For now, assume both are numbers. If one of them isn't, it will be sorted as a zero.
6277 // Thus, all non-numeric items should wind up in a sequential, unsorted group.
6278 // Resolve only once since parts of the ATOF() macro are inline:
6279 double item1_minus_2 = ATOF(sort_item1) - ATOF(sort_item2);
6280 if (!item1_minus_2) // Exactly equal.
6281 return (sort_item1 > sort_item2) ? 1 : -1; // Stable sort.
6282 // Otherwise, it's either greater or less than zero:
6283 int result = (item1_minus_2 > 0.0) ? 1 : -1;
6284 return g_SortReverse ? -result : result;
6285 }
6286 // Otherwise, it's a non-numeric sort.
6287 // v1.0.43.03: Added support the new locale-insensitive mode.
6288 int result = (g_SortCaseSensitive != SCS_INSENSITIVE_LOGICAL)
6289 ? tcscmp2(sort_item1, sort_item2, g_SortCaseSensitive) // Resolve large macro only once for code size reduction.
6290 : StrCmpLogicalW(sort_item1, sort_item2);
6291 if (!result)
6292 result = (sort_item1 > sort_item2) ? 1 : -1; // Stable sort.
6293 return g_SortReverse ? -result : result;
6294}
6295
6296
6297

Callers

nothing calls this directly

Calls 1

ATOFFunction · 0.85

Tested by

no test coverage detected