improved doc for function
[phpmyadmin/crack.git] / libraries / string.lib.php
blob54eef96ac563b8cb30d54c4700b2af5be6ba2a65
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 PMA_dl()
17 * @uses extension_loaded()
18 * @uses substr()
19 * @uses function_exists()
20 * @uses mb_internal_encoding()
21 * @uses defined()
22 * @todo a .lib filename should not have code in main(), split or rename file
25 /* Try to load mbstring */
26 if (!@extension_loaded('mbstring')) {
27 PMA_dl('mbstring');
30 /**
31 * windows-* and tis-620 are not supported and are not multibyte,
32 * others can be ignored as they're not multibyte
34 * @global boolean $GLOBALS['using_mb_charset']
36 $GLOBALS['using_mb_charset'] =
37 substr($GLOBALS['charset'], 0, 8) != 'windows-' &&
38 substr($GLOBALS['charset'], 0, 9) != 'iso-8859-' &&
39 substr($GLOBALS['charset'], 0, 3) != 'cp-' &&
40 $GLOBALS['charset'] != 'koi8-r' &&
41 $GLOBALS['charset'] != 'tis-620';
43 $GLOBALS['PMA_allow_mbstr'] = @function_exists('mb_strlen') && $GLOBALS['using_mb_charset'];
45 if ($GLOBALS['PMA_allow_mbstr']) {
46 // the hebrew lang file uses iso-8859-8-i, encoded RTL,
47 // but mb_internal_encoding only supports iso-8859-8
48 if ($GLOBALS['charset'] == 'iso-8859-8-i'){
49 mb_internal_encoding('iso-8859-8');
50 } else {
51 mb_internal_encoding($GLOBALS['charset']);
55 // This is for handling input better
56 if (defined('PMA_MULTIBYTE_ENCODING') || $GLOBALS['PMA_allow_mbstr']) {
57 $GLOBALS['PMA_strpos'] = 'mb_strpos';
58 require './libraries/string_mb.lib.php';
59 } else {
60 $GLOBALS['PMA_strpos'] = 'strpos';
61 require './libraries/string_native.lib.php';
64 if (!@extension_loaded('ctype')) {
65 PMA_dl('ctype');
68 if (@extension_loaded('ctype')) {
69 require './libraries/string_type_ctype.lib.php';
70 } else {
71 require './libraries/string_type_native.lib.php';
74 /**
75 * This checks if a string actually exists inside another string
76 * We don't care about the position it is in.
78 * @uses PMA_STR_pos()
79 * @param string string to search for
80 * @param string string to search in
81 * @return boolean whether the needle is in the haystack or not
82 * @todo rename PMA_STR_inStr()
84 function PMA_STR_strInStr($needle, $haystack)
86 // PMA_STR_pos($haystack, $needle) !== false
87 // return (is_integer(PMA_STR_pos($haystack, $needle)));
88 return (bool) PMA_STR_pos(' ' . $haystack, $needle);
89 } // end of the "PMA_STR_strInStr()" function
91 /**
92 * Checks if a given character position in the string is escaped or not
94 * @uses PMA_strlen()
95 * @uses PMA_substr()
96 * @uses max()
97 * @uses intval()
98 * @param string string to check for
99 * @param integer the character to check for
100 * @param integer starting position in the string
101 * @return boolean whether the character is escaped or not
103 function PMA_STR_charIsEscaped($string, $pos, $start = 0)
105 $pos = max(intval($pos), 0);
106 $start = max(intval($start), 0);
107 $len = PMA_strlen($string);
108 // Base case:
109 // Check for string length or invalid input or special case of input
110 // (pos == $start)
111 if ($pos <= $start || $len <= max($pos, $start)) {
112 return false;
115 $pos--;
116 $escaped = false;
117 while ($pos >= $start && PMA_substr($string, $pos, 1) == '\\') {
118 $escaped = !$escaped;
119 $pos--;
120 } // end while
122 return $escaped;
123 } // end of the "PMA_STR_charIsEscaped()" function
127 * Checks if a number is in a range
129 * @param integer number to check for
130 * @param integer lower bound
131 * @param integer upper bound
132 * @return boolean whether the number is in the range or not
134 function PMA_STR_numberInRangeInclusive($num, $lower, $upper)
136 return ($num >= $lower && $num <= $upper);
137 } // end of the "PMA_STR_numberInRangeInclusive()" function
141 * Checks if a character is an accented character
143 * Presently this only works for some character sets. More work may be needed
144 * to fix it.
146 * @uses PMA_STR_numberInRangeInclusive()
147 * @uses ord()
148 * @param string character to check for
149 * @return boolean whether the character is an accented one or not
151 function PMA_STR_isAccented($c)
153 $ord_min1 = 192; //ord('A');
154 $ord_max1 = 214; //ord('Z');
155 $ord_min2 = 216; //ord('A');
156 $ord_max2 = 246; //ord('Z');
157 $ord_min3 = 248; //ord('A');
158 $ord_max3 = 255; //ord('Z');
160 $ord_c = ord($c);
162 return PMA_STR_numberInRangeInclusive($ord_c, $ord_min1, $ord_max1)
163 || PMA_STR_numberInRangeInclusive($ord_c, $ord_min2, $ord_max2)
164 || PMA_STR_numberInRangeInclusive($ord_c, $ord_min2, $ord_max2);
165 } // end of the "PMA_STR_isAccented()" function
169 * Checks if a character is an SQL identifier
171 * @uses PMA_STR_isAlnum()
172 * @uses PMA_STR_isAccented()
173 * @param string character to check for
174 * @param boolean whether the dot character is valid or not
175 * @return boolean whether the character is an SQL identifier or not
177 function PMA_STR_isSqlIdentifier($c, $dot_is_valid = false)
179 return (PMA_STR_isAlnum($c)
180 || PMA_STR_isAccented($c)
181 || $c == '_'
182 || $c == '$'
183 || ($dot_is_valid != false && $c == '.'));
184 } // end of the "PMA_STR_isSqlIdentifier()" function
188 * Binary search of a value in a sorted array
190 * $arr MUST be sorted, due to binary search
192 * @param string string to search for
193 * @param array sorted array to search into
194 * @param integer size of sorted array to search into
196 * @return boolean whether the string has been found or not
198 function PMA_STR_binarySearchInArr($str, $arr, $arrsize)
200 $top = $arrsize - 1;
201 $bottom = 0;
202 $found = false;
204 while ($top >= $bottom && $found == false) {
205 $mid = intval(($top + $bottom) / 2);
206 $res = strcmp($str, $arr[$mid]);
207 if ($res == 0) {
208 $found = true;
209 } elseif ($res < 0) {
210 $top = $mid - 1;
211 } else {
212 $bottom = $mid + 1;
214 } // end while
216 return $found;
217 } // end of the "PMA_STR_binarySearchInArr()" function