Skip to content

Commit

Permalink
Some micro-optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
narfbg committed Nov 12, 2012
1 parent a9ab46d commit 7a7ad78
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 30 deletions.
4 changes: 2 additions & 2 deletions system/core/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -768,9 +768,9 @@ public function request_headers($xss_clean = FALSE)

foreach ($_SERVER as $key => $val)
{
if (strpos($key, 'HTTP_') === 0)
if (sscanf($key, 'HTTP_%s', $header) === 1)
{
$headers[substr($key, 5)] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
$headers[$header] = $this->_fetch_from_array($_SERVER, $key, $xss_clean);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions system/core/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ public function get_content_type()
{
for ($i = 0, $c = count($this->headers); $i < $c; $i++)
{
if (preg_match('/^Content-Type:\s(.+)$/', $this->headers[$i][0], $matches))
if (sscanf($this->headers[$i][0], 'Content-Type: %s', $content_type) === 1)
{
return $matches[1];
return $content_type;
}
}

Expand Down
4 changes: 2 additions & 2 deletions system/helpers/date_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,8 @@ function date_range($unix_start = '', $mixed = '', $is_unix = TRUE, $format = 'Y
$is_unix = ! ( ! $is_unix OR $is_unix === 'days');

// Validate input and try strtotime() on invalid timestamps/intervals, just in case
if ( ( ! preg_match('/^[0-9]+$/', $unix_start) && ($unix_start = @strtotime($unix_time)) === FALSE)
OR ( ! preg_match('/^[0-9]+$/', $mixed) && ($is_unix === FALSE OR ($mixed = @strtotime($mixed)) === FALSE))
if ( ( ! ctype_digit((string) $unix_start) && ($unix_start = @strtotime($unix_time)) === FALSE)
OR ( ! ctype_digit((string) $mixed) && ($is_unix === FALSE OR ($mixed = @strtotime($mixed)) === FALSE))
OR ($is_unix === TRUE && $mixed < $unix_start))
{
return FALSE;
Expand Down
4 changes: 2 additions & 2 deletions system/libraries/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,7 @@ protected function _send_command($cmd, $data = '')

$this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';

if ( (int) substr($reply, 0, 3) !== $resp)
if ((int) substr($reply, 0, 3) !== $resp)
{
$this->_set_error_message('lang:email_smtp_error', $reply);
return FALSE;
Expand Down Expand Up @@ -2093,7 +2093,7 @@ protected function _set_error_message($msg, $val = '')
$CI =& get_instance();
$CI->lang->load('email');

if (substr($msg, 0, 5) !== 'lang:' OR FALSE === ($line = $CI->lang->line(substr($msg, 5))))
if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line)))
{
$this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
}
Expand Down
20 changes: 5 additions & 15 deletions system/libraries/Form_validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,10 @@ public function set_rules($field, $label = '', $rules = '')

// Is the field name an array? If it is an array, we break it apart
// into its components so that we can fetch the corresponding POST data later
$indexes = array();
if (preg_match_all('/\[(.*?)\]/', $field, $matches))
{
// Note: Due to a bug in current() that affects some versions
// of PHP we can not pass function call directly into it
$x = explode('[', $field);
$indexes[] = current($x);
sscanf($field, '%[^[][', $indexes[0]);

for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
{
Expand All @@ -218,7 +216,6 @@ public function set_rules($field, $label = '', $rules = '')
}
else
{
$indexes = array();
$is_array = FALSE;
}

Expand Down Expand Up @@ -673,11 +670,7 @@ protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
// Strip the parameter (if exists) from the rule
// Rules can contain a parameter: max_length[5]
$param = FALSE;
if (preg_match('/(.*?)\[(.*)\]/', $rule, $match))
{
$rule = $match[1];
$param = $match[2];
}
sscanf($rule, '%[^[][%[^]]', $rule, $param);

// Call the function that corresponds to the rule
if ($callback === TRUE)
Expand Down Expand Up @@ -796,11 +789,8 @@ protected function _translate_fieldname($fieldname)
{
// Do we need to translate the field name?
// We look for the prefix lang: to determine this
if (strpos($fieldname, 'lang:') === 0)
if (sscanf($fieldname, 'lang:%s', $line) === 1)
{
// Grab the variable
$line = substr($fieldname, 5);

// Were we able to translate the field name? If not we use $line
if (FALSE === ($fieldname = $this->CI->lang->line($line)))
{
Expand Down Expand Up @@ -1002,7 +992,7 @@ public function differs($str, $field)
*/
public function is_unique($str, $field)
{
list($table, $field) = explode('.', $field);
sscanf($field, '%[^.].%[^.]', $table, $field);
if (isset($this->CI->db))
{
$query = $this->CI->db->limit(1)->get_where($table, array($field => $str));
Expand Down
12 changes: 6 additions & 6 deletions system/libraries/Image_lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ public function initialize($props = array())
// Set the quality
$this->quality = trim(str_replace('%', '', $this->quality));

if ($this->quality === '' OR $this->quality === 0 OR ! preg_match('/^[0-9]+$/', $this->quality))
if ($this->quality === '' OR $this->quality === 0 OR ! ctype_digit($this->quality))
{
$this->quality = 90;
}
Expand Down Expand Up @@ -1277,8 +1277,8 @@ public function text_watermark()
if ($this->wm_use_drop_shadow === FALSE)
$this->wm_shadow_distance = 0;

$this->wm_vrt_alignment = strtoupper(substr($this->wm_vrt_alignment, 0, 1));
$this->wm_hor_alignment = strtoupper(substr($this->wm_hor_alignment, 0, 1));
$this->wm_vrt_alignment = strtoupper($this->wm_vrt_alignment[0]);
$this->wm_hor_alignment = strtoupper($this->wm_hor_alignment[0]);

// Set verticle alignment
if ($this->wm_vrt_alignment === 'M')
Expand Down Expand Up @@ -1518,13 +1518,13 @@ public function image_display_gd($resource)
public function image_reproportion()
{
if (($this->width === 0 && $this->height === 0) OR $this->orig_width === 0 OR $this->orig_height === 0
OR ( ! preg_match('/^[0-9]+$/', $this->width) && ! preg_match('/^[0-9]+$/', $this->height))
OR ! preg_match('/^[0-9]+$/', $this->orig_width) OR ! preg_match('/^[0-9]+$/', $this->orig_height))
OR ( ! ctype_digit((string) $this->width) && ! ctype_digit((string) $this->height))
OR ! ctype_digit((string) $this->orig_width) OR ! ctype_digit((string) $this->orig_height))
{
return;
}

// Sanitize so we don't call preg_match() anymore
// Sanitize
$this->width = (int) $this->width;
$this->height = (int) $this->height;

Expand Down
2 changes: 1 addition & 1 deletion system/libraries/Trackback.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public function get_id($url)
}
}

return preg_match('/^[0-9]+$/', $tb_id) ? $tb_id : FALSE;
return ctype_digit((string) $tb_id) ? $tb_id : FALSE;
}

// --------------------------------------------------------------------
Expand Down

0 comments on commit 7a7ad78

Please sign in to comment.