Changelog entry for patch #3313326
[phpmyadmin/ammaryasirr.git] / libraries / string.lib.php
blobe17a935b9d6f20994d55f0adbd5b54200fe51e31
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
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
11 * (that are slower).
13 * The SQL Parser code relies heavily on these functions.
15 * @uses extension_loaded()
16 * @uses substr()
17 * @uses function_exists()
18 * @uses mb_internal_encoding()
19 * @uses defined()
20 * @todo a .lib filename should not have code in main(), split or rename file
21 * @package phpMyAdmin
23 if (! defined('PHPMYADMIN')) {
24 exit;
27 $GLOBALS['PMA_allow_mbstr'] = @function_exists('mb_strlen');
28 $GLOBALS['PMA_allow_ctype'] = @extension_loaded('ctype');
30 if ($GLOBALS['PMA_allow_mbstr']) {
31 mb_internal_encoding($GLOBALS['charset']);
34 /**
35 * Load proper code for handling input.
37 if (defined('PMA_MULTIBYTE_ENCODING') || $GLOBALS['PMA_allow_mbstr']) {
38 $GLOBALS['PMA_strpos'] = 'mb_strpos';
39 $GLOBALS['PMA_substr'] = 'mb_substr';
40 require './libraries/string_mb.lib.php';
41 } else {
42 $GLOBALS['PMA_strpos'] = 'strpos';
43 $GLOBALS['PMA_substr'] = 'substr';
44 require './libraries/string_native.lib.php';
47 /**
48 * Load ctype handler.
50 if ($GLOBALS['PMA_allow_ctype']) {
51 $GLOBALS['PMA_STR_isAlnum'] = 'ctype_alnum';
52 $GLOBALS['PMA_STR_isDigit'] = 'ctype_digit';
53 $GLOBALS['PMA_STR_isSpace'] = 'ctype_space';
54 require './libraries/string_type_ctype.lib.php';
55 } else {
56 $GLOBALS['PMA_STR_isAlnum'] = 'PMA_STR_isAlnum';
57 $GLOBALS['PMA_STR_isDigit'] = 'PMA_STR_isDigit';
58 $GLOBALS['PMA_STR_isSpace'] = 'PMA_STR_isSpace';
59 require './libraries/string_type_native.lib.php';
62 /**
63 * Checks if a given character position in the string is escaped or not
65 * @uses PMA_strlen()
66 * @uses PMA_substr()
67 * @uses max()
68 * @uses intval()
69 * @param string string to check for
70 * @param integer the character to check for
71 * @param integer starting position in the string
72 * @return boolean whether the character is escaped or not
74 function PMA_STR_charIsEscaped($string, $pos, $start = 0)
76 $pos = max(intval($pos), 0);
77 $start = max(intval($start), 0);
78 $len = PMA_strlen($string);
79 // Base case:
80 // Check for string length or invalid input or special case of input
81 // (pos == $start)
82 if ($pos <= $start || $len <= max($pos, $start)) {
83 return false;
86 $pos--;
87 $escaped = false;
88 while ($pos >= $start && PMA_substr($string, $pos, 1) == '\\') {
89 $escaped = !$escaped;
90 $pos--;
91 } // end while
93 return $escaped;
94 } // end of the "PMA_STR_charIsEscaped()" function
97 /**
98 * Checks if a number is in a range
100 * @param integer number to check for
101 * @param integer lower bound
102 * @param integer upper bound
103 * @return boolean whether the number is in the range or not
105 function PMA_STR_numberInRangeInclusive($num, $lower, $upper)
107 return ($num >= $lower && $num <= $upper);
108 } // end of the "PMA_STR_numberInRangeInclusive()" function
111 * Checks if a character is an SQL identifier
113 * @uses PMA_STR_isAlnum()
114 * @param string character to check for
115 * @param boolean whether the dot character is valid or not
116 * @return boolean whether the character is an SQL identifier or not
118 function PMA_STR_isSqlIdentifier($c, $dot_is_valid = false)
120 return ($GLOBALS['PMA_STR_isAlnum']($c)
121 || ($ord_c = ord($c)) && $ord_c >= 192 && $ord_c != 215 && $ord_c != 249
122 || $c == '_'
123 || $c == '$'
124 || ($dot_is_valid != false && $c == '.'));
125 } // end of the "PMA_STR_isSqlIdentifier()" function
129 * Binary search of a value in a sorted array
131 * $arr MUST be sorted, due to binary search
133 * @param string string to search for
134 * @param array sorted array to search into
135 * @param integer size of sorted array to search into
137 * @return boolean whether the string has been found or not
139 function PMA_STR_binarySearchInArr($str, $arr, $arrsize)
141 $top = $arrsize - 1;
142 $bottom = 0;
143 $found = false;
145 while ($top >= $bottom && $found == false) {
146 $mid = intval(($top + $bottom) / 2);
147 $res = strcmp($str, $arr[$mid]);
148 if ($res == 0) {
149 $found = true;
150 } elseif ($res < 0) {
151 $top = $mid - 1;
152 } else {
153 $bottom = $mid + 1;
155 } // end while
157 return $found;
158 } // end of the "PMA_STR_binarySearchInArr()" function