From fe62401907d7f2fce2a47d149f3a71dfebd508ef Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Wed, 11 Jan 2012 11:20:58 -0800 Subject: [PATCH] optimized string starts with checks Doing this with strpos() is slightly faster than substr(). --- .../Bundle/TwigBundle/DependencyInjection/Configuration.php | 2 +- src/Symfony/Component/BrowserKit/Client.php | 2 +- src/Symfony/Component/Console/Input/ArgvInput.php | 2 +- src/Symfony/Component/Console/Input/ArrayInput.php | 2 +- src/Symfony/Component/Console/Input/InputOption.php | 2 +- src/Symfony/Component/DomCrawler/Link.php | 2 +- src/Symfony/Component/HttpFoundation/Response.php | 2 +- src/Symfony/Component/HttpFoundation/ServerBag.php | 2 +- src/Symfony/Component/HttpKernel/Kernel.php | 2 +- .../Component/HttpKernel/Profiler/MysqlProfilerStorage.php | 2 +- .../Component/HttpKernel/Profiler/SqliteProfilerStorage.php | 2 +- src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php | 2 +- src/Symfony/Component/Yaml/Parser.php | 6 +++--- 13 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php index b7bc1b9313..f340bc3c79 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php @@ -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() diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index 5b3b628268..9d3bda69cf 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -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; } diff --git a/src/Symfony/Component/Console/Input/ArgvInput.php b/src/Symfony/Component/Console/Input/ArgvInput.php index 122cbb3fda..dda022206c 100644 --- a/src/Symfony/Component/Console/Input/ArgvInput.php +++ b/src/Symfony/Component/Console/Input/ArgvInput.php @@ -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); diff --git a/src/Symfony/Component/Console/Input/ArrayInput.php b/src/Symfony/Component/Console/Input/ArrayInput.php index a774c08a8f..c9d8ee98a3 100644 --- a/src/Symfony/Component/Console/Input/ArrayInput.php +++ b/src/Symfony/Component/Console/Input/ArrayInput.php @@ -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); diff --git a/src/Symfony/Component/Console/Input/InputOption.php b/src/Symfony/Component/Console/Input/InputOption.php index 15aa2f8188..cc609df756 100644 --- a/src/Symfony/Component/Console/Input/InputOption.php +++ b/src/Symfony/Component/Console/Input/InputOption.php @@ -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); } diff --git a/src/Symfony/Component/DomCrawler/Link.php b/src/Symfony/Component/DomCrawler/Link.php index 6367c88da2..39ddb8ca97 100644 --- a/src/Symfony/Component/DomCrawler/Link.php +++ b/src/Symfony/Component/DomCrawler/Link.php @@ -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; } diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index 8732e8261b..daf196b104 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -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); } diff --git a/src/Symfony/Component/HttpFoundation/ServerBag.php b/src/Symfony/Component/HttpFoundation/ServerBag.php index 02db3b1819..9cb7786129 100644 --- a/src/Symfony/Component/HttpFoundation/ServerBag.php +++ b/src/Symfony/Component/HttpFoundation/ServerBag.php @@ -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_ diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index ba4cf30f79..8a3b787d8c 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -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; } } diff --git a/src/Symfony/Component/HttpKernel/Profiler/MysqlProfilerStorage.php b/src/Symfony/Component/HttpKernel/Profiler/MysqlProfilerStorage.php index c78f836ec7..d40608d2e6 100644 --- a/src/Symfony/Component/HttpKernel/Profiler/MysqlProfilerStorage.php +++ b/src/Symfony/Component/HttpKernel/Profiler/MysqlProfilerStorage.php @@ -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.'"'); } diff --git a/src/Symfony/Component/HttpKernel/Profiler/SqliteProfilerStorage.php b/src/Symfony/Component/HttpKernel/Profiler/SqliteProfilerStorage.php index cb46ad98a9..5d7b93df98 100644 --- a/src/Symfony/Component/HttpKernel/Profiler/SqliteProfilerStorage.php +++ b/src/Symfony/Component/HttpKernel/Profiler/SqliteProfilerStorage.php @@ -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')) { diff --git a/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php b/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php index bf73fc3572..b53d55967a 100644 --- a/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php +++ b/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php @@ -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) { diff --git a/src/Symfony/Component/Yaml/Parser.php b/src/Symfony/Component/Yaml/Parser.php index 653047cc6c..074306969a 100644 --- a/src/Symfony/Component/Yaml/Parser.php +++ b/src/Symfony/Component/Yaml/Parser.php @@ -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 {