Fix unsafe casts in test_sorting.sp.

This commit is contained in:
David Anderson
2026-09-01 18:58:42 -07:00
parent 001ea29ee8
commit b1ac6f9a80
3 changed files with 31 additions and 1 deletions
+1
View File
@@ -696,6 +696,7 @@ REGISTER_NATIVES(sortNatives)
{"SortStrings", sm_SortStrings},
{"SortCustom1D", sm_SortCustom1D},
{"SortCustom2D", sm_SortCustom2D},
{"SortCustom2DStrings", sm_SortCustom2D},
{"SortADTArray", sm_SortADTArray},
{"SortADTArrayCustom", sm_SortADTArrayCustom},
+29
View File
@@ -119,11 +119,15 @@ native void SortCustom1D(int[] array, int array_size, SortFunc1D sortfunc, Handl
* 0 if first is equal to second
* 1 if first should go after second
*/
#if defined __sourcepawn2
typedef SortFunc2D = function int (int[] elem1, int[] elem2, const int[][] array, Handle hndl);
#else
typeset SortFunc2D
{
function int (int[] elem1, int[] elem2, const int[][] array, Handle hndl);
function int (char[] elem1, char[] elem2, const char[][] array, Handle hndl);
};
#endif
/**
* Sorts a custom 2D array. You must pass in a comparison function.
@@ -135,6 +139,31 @@ typeset SortFunc2D
*/
native void SortCustom2D(any[][] array, int array_size, SortFunc2D sortfunc, Handle hndl=INVALID_HANDLE);
#if defined __sourcepawn2
/**
* Sort comparison function for 2D char-array elements (sub-strings).
*
* @param elem1 First string to compare.
* @param elem2 Second string to compare.
* @param array Array that is being sorted (order is undefined).
* @param hndl Handle optionally passed in while sorting.
* @return -1 if first should go before second
* 0 if first is equal to second
* 1 if first should go after second
*/
typedef SortFunc2DStrings = function int (char[] elem1, char[] elem2, const char[][] array, Handle hndl);
/**
* Sorts a custom 2D char-array. You must pass in a comparison function.
*
* @param array Array to sort.
* @param array_size Size of the major array to sort (first index, outermost).
* @param sortfunc Sort comparison function to use.
* @param hndl Optional Handle to pass through the comparison calls.
*/
native void SortCustom2DStrings(char[][] array, int array_size, SortFunc2DStrings sortfunc, Handle hndl=INVALID_HANDLE);
#endif
/**
* Sort an ADT Array. Specify the type as Integer, Float, or String.
*
+1 -1
View File
@@ -163,7 +163,7 @@ void Test_Sort2D() {
"sidluke",
"sniperbeamer"
};
SortCustom2D(view_as<any>(strarray), sizeof(strarray), Custom2DSortString);
SortCustom2DStrings(strarray, sizeof(strarray), Custom2DSortString);
AssertStrArrayEq("SortCustom2D char[][] Ascending", strarray, expected_ascending, sizeof(strarray));
}