3 // vim: expandtab sw=4 ts=4 sts=4:
6 /** Specialized String Functions for phpMyAdmin
8 * Copyright 2002 Robin Johnson <robbat2@users.sourceforge.net>
9 * http://www.orbis-terrarum.net/?l=people.robbat2
11 * Defines a set of function callbacks that have a pure C version available if
12 * the "ctype" extension is available, but otherwise have PHP versions to use
15 * The SQL Parser code relies heavily on these functions.
19 if (!defined('PMA_STR_LIB_INCLUDED')) {
20 define('PMA_STR_LIB_INCLUDED', 1);
22 // This is for handling input better
23 if (defined('PMA_MULTIBYTE_ENCODING')) {
24 $GLOBALS['PMA_strlen'] = 'mb_strlen';
25 $GLOBALS['PMA_strpos'] = 'mb_strpos';
26 $GLOBALS['PMA_strrpos'] = 'mb_strrpos';
27 $GLOBALS['PMA_substr'] = 'mb_substr';
29 $GLOBALS['PMA_strlen'] = 'strlen';
30 $GLOBALS['PMA_strpos'] = 'strpos';
31 $GLOBALS['PMA_strrpos'] = 'strrpos';
32 $GLOBALS['PMA_substr'] = 'substr';
37 * This checks if a string actually exists inside another string
38 * We try to do it in a PHP3-portable way.
39 * We don't care about the position it is in.
41 * @param string string to search for
42 * @param string string to search in
44 * @return boolean whether the needle is in the haystack or not
46 function PMA_STR_strInStr($needle, $haystack)
48 // $GLOBALS['PMA_strpos']($haystack, $needle) !== FALSE
49 // return (is_integer($GLOBALS['PMA_strpos']($haystack, $needle)));
50 return $GLOBALS['PMA_strpos'](' ' . $haystack, $needle);
51 } // end of the "PMA_STR_strInStr()" function
55 * Checks if a given character position in the string is escaped or not
57 * @param string string to check for
58 * @param integer the character to check for
59 * @param integer starting position in the string
61 * @return boolean whether the character is escaped or not
63 function PMA_STR_charIsEscaped($string, $pos, $start = 0)
65 $len = $GLOBALS['PMA_strlen']($string);
67 // Check for string length or invalid input or special case of input
69 if (($pos == $start) ||
($len <= $pos)) {
75 while (($p >= $start) && ($string[$p] == '\\')) {
81 // throw error about strings
85 } // end of the "PMA_STR_charIsEscaped()" function
89 * Checks if a number is in a range
91 * @param integer number to check for
92 * @param integer lower bound
93 * @param integer upper bound
95 * @return boolean whether the number is in the range or not
97 function PMA_STR_numberInRangeInclusive($num, $lower, $upper)
99 return (($num >= $lower) && ($num <= $upper));
100 } // end of the "PMA_STR_numberInRangeInclusive()" function
104 * Checks if a character is a digit
106 * @param string character to check for
108 * @return boolean whether the character is a digit or not
110 * @see PMA_STR_numberInRangeInclusive()
112 function PMA_STR_isDigit($c)
114 $ord_zero = 48; //ord('0');
115 $ord_nine = 57; //ord('9');
118 return PMA_STR_numberInRangeInclusive($ord_c, $ord_zero, $ord_nine);
119 } // end of the "PMA_STR_isDigit()" function
123 * Checks if a character is an hexadecimal digit
125 * @param string character to check for
127 * @return boolean whether the character is an hexadecimal digit or not
129 * @see PMA_STR_numberInRangeInclusive()
131 function PMA_STR_isHexDigit($c)
133 $ord_Aupper = 65; //ord('A');
134 $ord_Fupper = 70; //ord('F');
135 $ord_Alower = 97; //ord('a');
136 $ord_Flower = 102; //ord('f');
137 $ord_zero = 48; //ord('0');
138 $ord_nine = 57; //ord('9');
141 return (PMA_STR_numberInRangeInclusive($ord_c, $ord_zero, $ord_nine)
142 ||
PMA_STR_numberInRangeInclusive($ord_c, $ord_Aupper, $ord_Fupper)
143 ||
PMA_STR_numberInRangeInclusive($ord_c, $ord_Alower, $ord_Flower));
144 } // end of the "PMA_STR_isHexDigit()" function
148 * Checks if a character is an upper alphabetic one
150 * @param string character to check for
152 * @return boolean whether the character is an upper alphabetic one or
155 * @see PMA_STR_numberInRangeInclusive()
157 function PMA_STR_isUpper($c)
159 $ord_zero = 65; //ord('A');
160 $ord_nine = 90; //ord('Z');
163 return PMA_STR_numberInRangeInclusive($ord_c, $ord_zero, $ord_nine);
164 } // end of the "PMA_STR_isUpper()" function
168 * Checks if a character is a lower alphabetic one
170 * @param string character to check for
172 * @return boolean whether the character is a lower alphabetic one or
175 * @see PMA_STR_numberInRangeInclusive()
177 function PMA_STR_isLower($c)
179 $ord_zero = 97; //ord('a');
180 $ord_nine = 122; //ord('z');
183 return PMA_STR_numberInRangeInclusive($ord_c, $ord_zero, $ord_nine);
184 } // end of the "PMA_STR_isLower()" function
188 * Checks if a character is an alphabetic one
190 * @param string character to check for
192 * @return boolean whether the character is an alphabetic one or not
194 * @see PMA_STR_isUpper()
195 * @see PMA_STR_isLower()
197 function PMA_STR_isAlpha($c)
199 return (PMA_STR_isUpper($c) ||
PMA_STR_isLower($c));
200 } // end of the "PMA_STR_isAlpha()" function
204 * Checks if a character is an alphanumeric one
206 * @param string character to check for
208 * @return boolean whether the character is an alphanumeric one or not
210 * @see PMA_STR_isUpper()
211 * @see PMA_STR_isLower()
212 * @see PMA_STR_isDigit()
214 function PMA_STR_isAlnum($c)
216 return (PMA_STR_isUpper($c) ||
PMA_STR_isLower($c) ||
PMA_STR_isDigit($c));
217 } // end of the "PMA_STR_isAlnum()" function
221 * Checks if a character is a space one
223 * @param string character to check for
225 * @return boolean whether the character is a space one or not
227 * @see PMA_STR_numberInRangeInclusive()
229 function PMA_STR_isSpace($c)
231 $ord_space = 32; //ord(' ')
232 $ord_tab = 9; //ord('\t')
233 $ord_CR = 13; //ord('\n')
234 $ord_NOBR = 160; //ord('U+00A0);
237 return (($ord_c == $ord_space)
238 ||
($ord_c == $ord_NOBR)
239 ||
PMA_STR_numberInRangeInclusive($ord_c, $ord_tab, $ord_CR));
240 } // end of the "PMA_STR_isSpace()" function
244 * Checks if a character is an accented character
246 * @note Presently this only works for some character sets. More work
247 * may be needed to fix it.
249 * @param string character to check for
251 * @return boolean whether the character is an upper alphabetic one or
254 * @see PMA_STR_numberInRangeInclusive()
256 function PMA_STR_isAccented($c)
258 $ord_min1 = 192; //ord('A');
259 $ord_max1 = 214; //ord('Z');
260 $ord_min2 = 216; //ord('A');
261 $ord_max2 = 246; //ord('Z');
262 $ord_min3 = 248; //ord('A');
263 $ord_max3 = 255; //ord('Z');
267 return PMA_STR_numberInRangeInclusive($ord_c, $ord_min1, $ord_max1)
268 ||
PMA_STR_numberInRangeInclusive($ord_c, $ord_min2, $ord_max2)
269 ||
PMA_STR_numberInRangeInclusive($ord_c, $ord_min2, $ord_max2);
270 } // end of the "PMA_STR_isAccented()" function
274 * Checks if a character is an SQL identifier
276 * @param string character to check for
277 * @param boolean whether the dot character is valid or not
279 * @return boolean whether the character is an SQL identifier or not
281 * @see PMA_STR_isAlnum()
283 function PMA_STR_isSqlIdentifier($c, $dot_is_valid = FALSE)
285 return (PMA_STR_isAlnum($c)
286 ||
PMA_STR_isAccented($c)
287 ||
($c == '_') ||
($c == '$')
288 ||
(($dot_is_valid != FALSE) && ($c == '.')));
289 } // end of the "PMA_STR_isSqlIdentifier()" function
293 * Binary search of a value in a sorted array
295 * @param string string to search for
296 * @param array sorted array to search into
297 * @param integer size of sorted array to search into
299 * @return boolean whether the string has been found or not
301 function PMA_STR_binarySearchInArr($str, $arr, $arrsize)
303 // $arr NUST be sorted, due to binary search
308 while (($top >= $bottom) && ($found == FALSE)) {
309 $mid = intval(($top +
$bottom) / 2);
310 $res = strcmp($str, $arr[$mid]);
313 } else if ($res < 0) {
321 } // end of the "PMA_STR_binarySearchInArr()" function
323 } // $__PMA_STR_LIB__