2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Specialized String Functions for phpMyAdmin
6 * Copyright 2002 Robin Johnson <robbat2@users.sourceforge.net>
7 * http://www.orbis-terrarum.net/?l=people.robbat2
9 * Defines a set of function callbacks that have a pure C version available if
10 * the "ctype" extension is available, but otherwise have PHP versions to use
13 * The SQL Parser code relies heavily on these functions.
16 * @uses extension_loaded()
18 * @uses function_exists()
19 * @uses mb_internal_encoding()
21 * @todo a .lib filename should not have code in main(), split or rename file
23 if (! defined('PHPMYADMIN')) {
27 $GLOBALS['PMA_allow_mbstr'] = @function_exists
('mb_strlen');
29 if ($GLOBALS['PMA_allow_mbstr']) {
30 mb_internal_encoding($GLOBALS['charset']);
34 * Load proper code for handling input.
36 if (defined('PMA_MULTIBYTE_ENCODING') ||
$GLOBALS['PMA_allow_mbstr']) {
37 $GLOBALS['PMA_strpos'] = 'mb_strpos';
38 $GLOBALS['PMA_substr'] = 'mb_substr';
39 $GLOBALS['PMA_STR_isAlnum'] = 'ctype_alnum';
40 $GLOBALS['PMA_STR_isDigit'] = 'ctype_digit';
41 $GLOBALS['PMA_STR_isSpace'] = 'ctype_space';
42 require './libraries/string_mb.lib.php';
44 $GLOBALS['PMA_strpos'] = 'strpos';
45 $GLOBALS['PMA_substr'] = 'substr';
46 $GLOBALS['PMA_STR_isAlnum'] = 'PMA_STR_isAlnum';
47 $GLOBALS['PMA_STR_isDigit'] = 'PMA_STR_isDigit';
48 $GLOBALS['PMA_STR_isSpace'] = 'PMA_STR_isSpace';
49 require './libraries/string_native.lib.php';
55 if (@extension_loaded
('ctype')) {
56 require './libraries/string_type_ctype.lib.php';
58 require './libraries/string_type_native.lib.php';
62 * Checks if a given character position in the string is escaped or not
68 * @param string string to check for
69 * @param integer the character to check for
70 * @param integer starting position in the string
71 * @return boolean whether the character is escaped or not
73 function PMA_STR_charIsEscaped($string, $pos, $start = 0)
75 $pos = max(intval($pos), 0);
76 $start = max(intval($start), 0);
77 $len = PMA_strlen($string);
79 // Check for string length or invalid input or special case of input
81 if ($pos <= $start ||
$len <= max($pos, $start)) {
87 while ($pos >= $start && PMA_substr($string, $pos, 1) == '\\') {
93 } // end of the "PMA_STR_charIsEscaped()" function
97 * Checks if a number is in a range
99 * @param integer number to check for
100 * @param integer lower bound
101 * @param integer upper bound
102 * @return boolean whether the number is in the range or not
104 function PMA_STR_numberInRangeInclusive($num, $lower, $upper)
106 return ($num >= $lower && $num <= $upper);
107 } // end of the "PMA_STR_numberInRangeInclusive()" function
110 * Checks if a character is an SQL identifier
112 * @uses PMA_STR_isAlnum()
113 * @param string character to check for
114 * @param boolean whether the dot character is valid or not
115 * @return boolean whether the character is an SQL identifier or not
117 function PMA_STR_isSqlIdentifier($c, $dot_is_valid = false)
119 return ($GLOBALS['PMA_STR_isAlnum']($c)
120 ||
($ord_c = ord($c)) && $ord_c >= 192 && $ord_c != 215 && $ord_c != 249
123 ||
($dot_is_valid != false && $c == '.'));
124 } // end of the "PMA_STR_isSqlIdentifier()" function
128 * Binary search of a value in a sorted array
130 * $arr MUST be sorted, due to binary search
132 * @param string string to search for
133 * @param array sorted array to search into
134 * @param integer size of sorted array to search into
136 * @return boolean whether the string has been found or not
138 function PMA_STR_binarySearchInArr($str, $arr, $arrsize)
144 while ($top >= $bottom && $found == false) {
145 $mid = intval(($top +
$bottom) / 2);
146 $res = strcmp($str, $arr[$mid]);
149 } elseif ($res < 0) {
157 } // end of the "PMA_STR_binarySearchInArr()" function