Browse Source

[svn r21527] FS#306 - The multi-byte string library: Adding new functions for sorting by keys - api_knatsort(), api_knatrsort(), api_knatcasesort(), api_knatcasersort().

Ivan Tcholakov 15 years ago
parent
commit
8f367c5921
1 changed files with 161 additions and 69 deletions
  1. 161 69
      main/inc/lib/multibyte_string_functions.lib.php

+ 161 - 69
main/inc/lib/multibyte_string_functions.lib.php

@@ -1235,6 +1235,102 @@ function api_arsort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encod
 	return arsort($array, $sort_flag);
 }
 
+/**
+ * Sorts an array using natural order algorithm.
+ * @param array $array					The input array.
+ * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
+ * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
+ * @return bool							Returns TRUE on success, FALSE on error.
+ * This function is aimed at replacing the function natsort() for sorting human-language strings.
+ * @link http://php.net/manual/en/function.natsort.php
+ */
+function api_natsort(&$array, $language = null, $encoding = null) {
+	if (INTL_INSTALLED) {
+		if (empty($encoding)) {
+			$encoding = api_mb_internal_encoding();
+		}
+		$collator = _api_get_alpha_numerical_collator($language);
+		if (is_object($collator)) {
+			global $_api_collator, $_api_encoding;
+			$_api_collator = $collator;
+			$_api_encoding = $encoding;
+			return uasort($array, '_api_cmp');
+		}
+	}
+	return natsort($array);
+}
+
+/**
+ * Sorts an array using natural order algorithm in reverse order.
+ * @param array $array					The input array.
+ * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
+ * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
+ * @return bool							Returns TRUE on success, FALSE on error.
+ */
+function api_natrsort(&$array, $language = null, $encoding = null) {
+	if (INTL_INSTALLED) {
+		if (empty($encoding)) {
+			$encoding = api_mb_internal_encoding();
+		}
+		$collator = _api_get_alpha_numerical_collator($language);
+		if (is_object($collator)) {
+			global $_api_collator, $_api_encoding;
+			$_api_collator = $collator;
+			$_api_encoding = $encoding;
+			return uasort($array, '_api_rcmp');
+		}
+	}
+	return uasort($array, '_api_strnatrcmp');
+}
+
+/**
+ * Sorts an array using natural order algorithm, case-insensitive.
+ * @param array $array					The input array.
+ * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
+ * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
+ * @return bool							Returns TRUE on success, FALSE on error.
+ * This function is aimed at replacing the function natcasesort() for sorting human-language strings.
+ * @link http://php.net/manual/en/function.natcasesort.php
+ */
+function api_natcasesort(&$array, $language = null, $encoding = null) {
+	if (INTL_INSTALLED) {
+		if (empty($encoding)) {
+			$encoding = api_mb_internal_encoding();
+		}
+		$collator = _api_get_alpha_numerical_collator($language);
+		if (is_object($collator)) {
+			global $_api_collator, $_api_encoding;
+			$_api_collator = $collator;
+			$_api_encoding = $encoding;
+			return uasort($array, '_api_casecmp');
+		}
+	}
+	return natcasesort($array);
+}
+
+/**
+ * Sorts an array using natural order algorithm, case-insensitive, reverse order.
+ * @param array $array					The input array.
+ * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
+ * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
+ * @return bool							Returns TRUE on success, FALSE on error.
+ */
+function api_natcasersort(&$array, $language = null, $encoding = null) {
+	if (INTL_INSTALLED) {
+		if (empty($encoding)) {
+			$encoding = api_mb_internal_encoding();
+		}
+		$collator = _api_get_alpha_numerical_collator($language);
+		if (is_object($collator)) {
+			global $_api_collator, $_api_encoding;
+			$_api_collator = $collator;
+			$_api_encoding = $encoding;
+			return uasort($array, '_api_casercmp');
+		}
+	}
+	return uasort($array, '_api_strnatcasercmp');
+}
+
 /**
  * Sorts an array by keys, elements will be arranged from the lowest key to the highest key.
  * @param array $array					The input array.
@@ -1302,86 +1398,59 @@ function api_krsort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encod
 }
 
 /**
- * Sorts an array, elements will be arranged from the lowest to the highest.
+ * Sorts an array by keys using natural order algorithm.
  * @param array $array					The input array.
- * @param int $sort_flag (optional)		Shows how elements of the array to be compared.
  * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  * @return bool							Returns TRUE on success, FALSE on error.
- * Note: $sort_flag may have the following values:
- * SORT_REGULAR - internal PHP-rules for comparison will be applied, without preliminary changing types;
- * SORT_NUMERIC - items will be compared as numbers;
- * SORT_STRING - items will be compared as strings. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale;
- * SORT_LOCALE_STRING - items will be compared as strings depending on the current POSIX locale. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale.
- * This function is aimed at replacing the function sort() for sorting human-language strings.
- * @link http://php.net/manual/en/function.sort.php
- * @link http://php.net/manual/en/collator.sort.php
  */
-function api_sort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null) {
+function api_knatsort(&$array, $language = null, $encoding = null) {
 	if (INTL_INSTALLED) {
 		if (empty($encoding)) {
 			$encoding = api_mb_internal_encoding();
 		}
-		$collator = _api_get_collator($language);
+		$collator = _api_get_alpha_numerical_collator($language);
 		if (is_object($collator)) {
-			if (api_is_utf8($encoding)) {
-				$sort_flag = ($sort_flag == SORT_LOCALE_STRING) ? SORT_STRING : $sort_flag;
-				return collator_sort($collator, $array, _api_get_collator_sort_flag($sort_flag));
-			}
-			elseif ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
-				global $_api_collator, $_api_encoding;
-				$_api_collator = $collator;
-				$_api_encoding = $encoding;
-				return usort($array, '_api_cmp');
-			}
+			global $_api_collator, $_api_encoding;
+			$_api_collator = $collator;
+			$_api_encoding = $encoding;
+			return uksort($array, '_api_cmp');
 		}
 	}
-	return sort($array, $sort_flag);
+	return uksort($array, 'strnatcmp');
 }
 
 /**
- * Sorts an array, elements will be arranged from the highest to the lowest (in reverse order).
+ * Sorts an array by keys using natural order algorithm in reverse order.
  * @param array $array					The input array.
- * @param int $sort_flag (optional)		Shows how elements of the array to be compared.
  * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  * @return bool							Returns TRUE on success, FALSE on error.
- * Note: $sort_flag may have the following values:
- * SORT_REGULAR - internal PHP-rules for comparison will be applied, without preliminary changing types;
- * SORT_NUMERIC - items will be compared as numbers;
- * SORT_STRING - items will be compared as strings. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale;
- * SORT_LOCALE_STRING - items will be compared as strings depending on the current POSIX locale. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale.
- * This function is aimed at replacing the function rsort() for sorting human-language strings.
- * @link http://php.net/manual/en/function.rsort.php
  */
-function api_rsort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null) {
+function api_knatrsort(&$array, $language = null, $encoding = null) {
 	if (INTL_INSTALLED) {
 		if (empty($encoding)) {
 			$encoding = api_mb_internal_encoding();
 		}
-		$collator = _api_get_collator($language);
+		$collator = _api_get_alpha_numerical_collator($language);
 		if (is_object($collator)) {
-			if ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
-				global $_api_collator, $_api_encoding;
-				$_api_collator = $collator;
-				$_api_encoding = $encoding;
-				return usort($array, '_api_rcmp');
-			}
+			global $_api_collator, $_api_encoding;
+			$_api_collator = $collator;
+			$_api_encoding = $encoding;
+			return uksort($array, '_api_rcmp');
 		}
 	}
-	return rsort($array, $sort_flag);
+	return uksort($array, '_api_strnatrcmp');
 }
 
 /**
- * Sorts an array using natural order algorithm.
+ * Sorts an array by keys using natural order algorithm, case insensitive.
  * @param array $array					The input array.
  * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  * @return bool							Returns TRUE on success, FALSE on error.
- * This function is aimed at replacing the function natsort() for sorting human-language strings.
- * @link http://php.net/manual/en/function.natsort.php
  */
-function api_natsort(&$array, $language = null, $encoding = null) {
+function api_knatcasesort(&$array, $language = null, $encoding = null) {
 	if (INTL_INSTALLED) {
 		if (empty($encoding)) {
 			$encoding = api_mb_internal_encoding();
@@ -1391,20 +1460,20 @@ function api_natsort(&$array, $language = null, $encoding = null) {
 			global $_api_collator, $_api_encoding;
 			$_api_collator = $collator;
 			$_api_encoding = $encoding;
-			return uasort($array, '_api_cmp');
+			return uksort($array, '_api_casecmp');
 		}
 	}
-	return natsort($array);
+	return uksort($array, 'strnatcasecmp');
 }
 
 /**
- * Sorts an array using natural order algorithm in reverse order.
+ * Sorts an array by keys using natural order algorithm, case insensitive, reverse order.
  * @param array $array					The input array.
  * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  * @return bool							Returns TRUE on success, FALSE on error.
  */
-function api_natrsort(&$array, $language = null, $encoding = null) {
+function api_knatcasersort(&$array, $language = null, $encoding = null) {
 	if (INTL_INSTALLED) {
 		if (empty($encoding)) {
 			$encoding = api_mb_internal_encoding();
@@ -1414,58 +1483,81 @@ function api_natrsort(&$array, $language = null, $encoding = null) {
 			global $_api_collator, $_api_encoding;
 			$_api_collator = $collator;
 			$_api_encoding = $encoding;
-			return uasort($array, '_api_rcmp');
+			return uksort($array, '_api_casercmp');
 		}
 	}
-	return uasort($array, '_api_strnatrcmp');
+	return uksort($array, '_api_strnatcasercmp');
 }
 
 /**
- * Sorts an array using natural order algorithm, case-insensitive.
+ * Sorts an array, elements will be arranged from the lowest to the highest.
  * @param array $array					The input array.
+ * @param int $sort_flag (optional)		Shows how elements of the array to be compared.
  * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  * @return bool							Returns TRUE on success, FALSE on error.
- * This function is aimed at replacing the function natcasesort() for sorting human-language strings.
- * @link http://php.net/manual/en/function.natcasesort.php
+ * Note: $sort_flag may have the following values:
+ * SORT_REGULAR - internal PHP-rules for comparison will be applied, without preliminary changing types;
+ * SORT_NUMERIC - items will be compared as numbers;
+ * SORT_STRING - items will be compared as strings. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale;
+ * SORT_LOCALE_STRING - items will be compared as strings depending on the current POSIX locale. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale.
+ * This function is aimed at replacing the function sort() for sorting human-language strings.
+ * @link http://php.net/manual/en/function.sort.php
+ * @link http://php.net/manual/en/collator.sort.php
  */
-function api_natcasesort(&$array, $language = null, $encoding = null) {
+function api_sort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null) {
 	if (INTL_INSTALLED) {
 		if (empty($encoding)) {
 			$encoding = api_mb_internal_encoding();
 		}
-		$collator = _api_get_alpha_numerical_collator($language);
+		$collator = _api_get_collator($language);
 		if (is_object($collator)) {
-			global $_api_collator, $_api_encoding;
-			$_api_collator = $collator;
-			$_api_encoding = $encoding;
-			return uasort($array, '_api_casecmp');
+			if (api_is_utf8($encoding)) {
+				$sort_flag = ($sort_flag == SORT_LOCALE_STRING) ? SORT_STRING : $sort_flag;
+				return collator_sort($collator, $array, _api_get_collator_sort_flag($sort_flag));
+			}
+			elseif ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
+				global $_api_collator, $_api_encoding;
+				$_api_collator = $collator;
+				$_api_encoding = $encoding;
+				return usort($array, '_api_cmp');
+			}
 		}
 	}
-	return natcasesort($array);
+	return sort($array, $sort_flag);
 }
 
 /**
- * Sorts an array using natural order algorithm, case-insensitive, reverse order.
+ * Sorts an array, elements will be arranged from the highest to the lowest (in reverse order).
  * @param array $array					The input array.
+ * @param int $sort_flag (optional)		Shows how elements of the array to be compared.
  * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
  * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
  * @return bool							Returns TRUE on success, FALSE on error.
+ * Note: $sort_flag may have the following values:
+ * SORT_REGULAR - internal PHP-rules for comparison will be applied, without preliminary changing types;
+ * SORT_NUMERIC - items will be compared as numbers;
+ * SORT_STRING - items will be compared as strings. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale;
+ * SORT_LOCALE_STRING - items will be compared as strings depending on the current POSIX locale. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale.
+ * This function is aimed at replacing the function rsort() for sorting human-language strings.
+ * @link http://php.net/manual/en/function.rsort.php
  */
-function api_natcasersort(&$array, $language = null, $encoding = null) {
+function api_rsort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null) {
 	if (INTL_INSTALLED) {
 		if (empty($encoding)) {
 			$encoding = api_mb_internal_encoding();
 		}
-		$collator = _api_get_alpha_numerical_collator($language);
+		$collator = _api_get_collator($language);
 		if (is_object($collator)) {
-			global $_api_collator, $_api_encoding;
-			$_api_collator = $collator;
-			$_api_encoding = $encoding;
-			return uasort($array, '_api_casercmp');
+			if ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
+				global $_api_collator, $_api_encoding;
+				$_api_collator = $collator;
+				$_api_encoding = $encoding;
+				return usort($array, '_api_rcmp');
+			}
 		}
 	}
-	return uasort($array, '_api_strnatcasercmp');
+	return rsort($array, $sort_flag);
 }
 
 // Global variables used by the sorting functions, for internal use.