optimized string starts with checks

Doing this with strpos() is slightly faster than substr().
This commit is contained in:
Kris Wallsmith 2012-01-11 11:20:58 -08:00
parent 7ee2f6da75
commit fe62401907
13 changed files with 15 additions and 15 deletions

View File

@ -79,7 +79,7 @@ class Configuration implements ConfigurationInterface
->useAttributeAsKey('key')
->prototype('array')
->beforeNormalization()
->ifTrue(function($v){ return is_string($v) && '@' === substr($v, 0, 1); })
->ifTrue(function($v){ return is_string($v) && 0 === strpos($v, '@'); })
->then(function($v){ return array('id' => substr($v, 1), 'type' => 'service'); })
->end()
->beforeNormalization()

View File

@ -441,7 +441,7 @@ abstract class Client
protected function getAbsoluteUri($uri)
{
// already absolute?
if ('http' === substr($uri, 0, 4)) {
if (0 === strpos($uri, 'http')) {
return $uri;
}

View File

@ -77,7 +77,7 @@ class ArgvInput extends Input
{
$this->parsed = $this->tokens;
while (null !== $token = array_shift($this->parsed)) {
if ('--' === substr($token, 0, 2)) {
if (0 === strpos($token, '--')) {
$this->parseLongOption($token);
} elseif ('-' === $token[0]) {
$this->parseShortOption($token);

View File

@ -116,7 +116,7 @@ class ArrayInput extends Input
protected function parse()
{
foreach ($this->parameters as $key => $value) {
if ('--' === substr($key, 0, 2)) {
if (0 === strpos($key, '--')) {
$this->addLongOption(substr($key, 2), $value);
} elseif ('-' === $key[0]) {
$this->addShortOption(substr($key, 1), $value);

View File

@ -46,7 +46,7 @@ class InputOption
*/
public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
{
if ('--' === substr($name, 0, 2)) {
if (0 === strpos($name, '--')) {
$name = substr($name, 2);
}

View File

@ -80,7 +80,7 @@ class Link
$uri = trim($this->getRawUri());
// absolute URL?
if ('http' === substr($uri, 0, 4)) {
if (0 === strpos($uri, 'http')) {
return $uri;
}

View File

@ -134,7 +134,7 @@ class Response
$charset = $this->charset ?: 'UTF-8';
if (!$this->headers->has('Content-Type')) {
$this->headers->set('Content-Type', 'text/html; charset='.$charset);
} elseif ('text/' === substr($this->headers->get('Content-Type'), 0, 5) && false === strpos($this->headers->get('Content-Type'), 'charset')) {
} elseif (0 === strpos($this->headers->get('Content-Type'), 'text/') && false === strpos($this->headers->get('Content-Type'), 'charset')) {
// add the charset
$this->headers->set('Content-Type', $this->headers->get('Content-Type').'; charset='.$charset);
}

View File

@ -23,7 +23,7 @@ class ServerBag extends ParameterBag
{
$headers = array();
foreach ($this->parameters as $key => $value) {
if ('HTTP_' === substr($key, 0, 5)) {
if (0 === strpos($key, 'HTTP_')) {
$headers[substr($key, 5)] = $value;
}
// CONTENT_* are not prefixed with HTTP_

View File

@ -597,7 +597,7 @@ abstract class Kernel implements KernelInterface
{
$parameters = array();
foreach ($_SERVER as $key => $value) {
if ('SYMFONY__' === substr($key, 0, 9)) {
if (0 === strpos($key, 'SYMFONY__')) {
$parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value;
}
}

View File

@ -24,7 +24,7 @@ class MysqlProfilerStorage extends PdoProfilerStorage
protected function initDb()
{
if (null === $this->db) {
if ('mysql' !== substr($this->dsn, 0, 5)) {
if (0 !== strpos($this->dsn, 'mysql')) {
throw new \RuntimeException('Please check your configuration. You are trying to use Mysql with a wrong dsn. "'.$this->dsn.'"');
}

View File

@ -25,7 +25,7 @@ class SqliteProfilerStorage extends PdoProfilerStorage
protected function initDb()
{
if (null === $this->db || $this->db instanceof \SQLite3) {
if ('sqlite' !== substr($this->dsn, 0, 6 )) {
if (0 !== strpos($this->dsn, 'sqlite')) {
throw new \RuntimeException('You are trying to use Sqlite with a wrong dsn. "'.$this->dsn.'"');
}
if (class_exists('SQLite3')) {

View File

@ -431,7 +431,7 @@ class StubIntlDateFormatter
$timeZone = $timeZoneId;
// Get an Etc/GMT time zone that is accepted for \DateTimeZone
if ('GMT' !== $timeZoneId && 'GMT' === substr($timeZoneId, 0, 3)) {
if ('GMT' !== $timeZoneId && 0 === strpos($timeZoneId, 'GMT')) {
try {
$timeZoneId = DateFormat\TimeZoneTransformer::getEtcTimeZoneId($timeZoneId);
} catch (\InvalidArgumentException $e) {

View File

@ -114,7 +114,7 @@ class Parser
}
if ('<<' === $key) {
if (isset($values['value']) && '*' === substr($values['value'], 0, 1)) {
if (isset($values['value']) && 0 === strpos($values['value'], '*')) {
$isInPlace = substr($values['value'], 1);
if (!array_key_exists($isInPlace, $this->refs)) {
throw new ParseException(sprintf('Reference "%s" does not exist.', $isInPlace), $this->getRealCurrentLineNb() + 1, $this->currentLine);
@ -188,7 +188,7 @@ class Parser
if (is_array($value)) {
$first = reset($value);
if (is_string($first) && '*' === substr($first, 0, 1)) {
if (is_string($first) && 0 === strpos($first, '*')) {
$data = array();
foreach ($value as $alias) {
$data[] = $this->refs[substr($alias, 1)];
@ -347,7 +347,7 @@ class Parser
*/
private function parseValue($value)
{
if ('*' === substr($value, 0, 1)) {
if (0 === strpos($value, '*')) {
if (false !== $pos = strpos($value, '#')) {
$value = substr($value, 1, $pos - 2);
} else {