bug #2363919 [display] Incorrect size for view
[phpmyadmin/crack.git] / libraries / string.lib.php
blobc6225a061f9f0312e3106cfbcf674502502ae31d
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 * @version $Id$
16 * @uses extension_loaded()
17 * @uses substr()
18 * @uses function_exists()
19 * @uses mb_internal_encoding()
20 * @uses defined()
21 * @todo a .lib filename should not have code in main(), split or rename file
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 // This is for handling input better
35 if (defined('PMA_MULTIBYTE_ENCODING') || $GLOBALS['PMA_allow_mbstr']) {
36 $GLOBALS['PMA_strpos'] = 'mb_strpos';
37 $GLOBALS['PMA_substr'] = 'mb_substr';
38 require './libraries/string_mb.lib.php';
39 } else {
40 $GLOBALS['PMA_strpos'] = 'strpos';
41 $GLOBALS['PMA_substr'] = 'substr';
42 require './libraries/string_native.lib.php';
45 if ($GLOBALS['PMA_allow_ctype']) {
46 $GLOBALS['PMA_STR_isAlnum'] = 'ctype_alnum';
47 $GLOBALS['PMA_STR_isDigit'] = 'ctype_digit';
48 $GLOBALS['PMA_STR_isSpace'] = 'ctype_space';
49 require './libraries/string_type_ctype.lib.php';
50 } else {
51 $GLOBALS['PMA_STR_isAlnum'] = 'PMA_STR_isAlnum';
52 $GLOBALS['PMA_STR_isDigit'] = 'PMA_STR_isDigit';
53 $GLOBALS['PMA_STR_isSpace'] = 'PMA_STR_isSpace';
54 require './libraries/string_type_native.lib.php';
57 /**
58 * Checks if a given character position in the string is escaped or not
60 * @uses PMA_strlen()
61 * @uses PMA_substr()
62 * @uses max()
63 * @uses intval()
64 * @param string string to check for
65 * @param integer the character to check for
66 * @param integer starting position in the string
67 * @return boolean whether the character is escaped or not
69 function PMA_STR_charIsEscaped($string, $pos, $start = 0)
71 $pos = max(intval($pos), 0);
72 $start = max(intval($start), 0);
73 $len = PMA_strlen($string);
74 // Base case:
75 // Check for string length or invalid input or special case of input
76 // (pos == $start)
77 if ($pos <= $start || $len <= max($pos, $start)) {
78 return false;
81 $pos--;
82 $escaped = false;
83 while ($pos >= $start && PMA_substr($string, $pos, 1) == '\\') {
84 $escaped = !$escaped;
85 $pos--;
86 } // end while
88 return $escaped;
89 } // end of the "PMA_STR_charIsEscaped()" function
92 /**
93 * Checks if a number is in a range
95 * @param integer number to check for
96 * @param integer lower bound
97 * @param integer upper bound
98 * @return boolean whether the number is in the range or not
100 function PMA_STR_numberInRangeInclusive($num, $lower, $upper)
102 return ($num >= $lower && $num <= $upper);
103 } // end of the "PMA_STR_numberInRangeInclusive()" function
106 * Checks if a character is an SQL identifier
108 * @uses PMA_STR_isAlnum()
109 * @param string character to check for
110 * @param boolean whether the dot character is valid or not
111 * @return boolean whether the character is an SQL identifier or not
113 function PMA_STR_isSqlIdentifier($c, $dot_is_valid = false)
115 return ($GLOBALS['PMA_STR_isAlnum']($c)
116 || ($ord_c = ord($c)) && $ord_c >= 192 && $ord_c != 215 && $ord_c != 249
117 || $c == '_'
118 || $c == '$'
119 || ($dot_is_valid != false && $c == '.'));
120 } // end of the "PMA_STR_isSqlIdentifier()" function
124 * Binary search of a value in a sorted array
126 * $arr MUST be sorted, due to binary search
128 * @param string string to search for
129 * @param array sorted array to search into
130 * @param integer size of sorted array to search into
132 * @return boolean whether the string has been found or not
134 function PMA_STR_binarySearchInArr($str, $arr, $arrsize)
136 $top = $arrsize - 1;
137 $bottom = 0;
138 $found = false;
140 while ($top >= $bottom && $found == false) {
141 $mid = intval(($top + $bottom) / 2);
142 $res = strcmp($str, $arr[$mid]);
143 if ($res == 0) {
144 $found = true;
145 } elseif ($res < 0) {
146 $top = $mid - 1;
147 } else {
148 $bottom = $mid + 1;
150 } // end while
152 return $found;
153 } // end of the "PMA_STR_binarySearchInArr()" function