Convert some comments to phpdoc.
[phpmyadmin/crack.git] / libraries / string.lib.php
blob8ca51c9edc2307085c9e9baba6a8bdc9b3f17ce4
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');
29 if ($GLOBALS['PMA_allow_mbstr']) {
30 mb_internal_encoding($GLOBALS['charset']);
33 /**
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';
43 } else {
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';
52 /**
53 * Load ctype handler.
55 if (@extension_loaded('ctype')) {
56 require './libraries/string_type_ctype.lib.php';
57 } else {
58 require './libraries/string_type_native.lib.php';
61 /**
62 * Checks if a given character position in the string is escaped or not
64 * @uses PMA_strlen()
65 * @uses PMA_substr()
66 * @uses max()
67 * @uses intval()
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);
78 // Base case:
79 // Check for string length or invalid input or special case of input
80 // (pos == $start)
81 if ($pos <= $start || $len <= max($pos, $start)) {
82 return false;
85 $pos--;
86 $escaped = false;
87 while ($pos >= $start && PMA_substr($string, $pos, 1) == '\\') {
88 $escaped = !$escaped;
89 $pos--;
90 } // end while
92 return $escaped;
93 } // end of the "PMA_STR_charIsEscaped()" function
96 /**
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
121 || $c == '_'
122 || $c == '$'
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)
140 $top = $arrsize - 1;
141 $bottom = 0;
142 $found = false;
144 while ($top >= $bottom && $found == false) {
145 $mid = intval(($top + $bottom) / 2);
146 $res = strcmp($str, $arr[$mid]);
147 if ($res == 0) {
148 $found = true;
149 } elseif ($res < 0) {
150 $top = $mid - 1;
151 } else {
152 $bottom = $mid + 1;
154 } // end while
156 return $found;
157 } // end of the "PMA_STR_binarySearchInArr()" function