8000 optimized string starts with checks · lsmith77/symfony@fe62401 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe62401

Browse files
committed
optimized string starts with checks
Doing this with strpos() is slightly faster than substr().
1 parent 7ee2f6d commit fe62401

File tree

13 files changed

+15
-15
lines changed

13 files changed

+15
-15
lines changed

src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private function addGlobalsSection(ArrayNodeDefinition $rootNode)
7979
->useAttributeAsKey('key')
8080
->prototype('array')
8181
->beforeNormalization()
82-
->ifTrue(function($v){ return is_string($v) && '@' === substr($v, 0, 1); })
82+
->ifTrue(function($v){ return is_string($v) && 0 === strpos($v, '@'); })
8383
->then(function($v){ return array('id' => substr($v, 1), 'type' => 'service'); })
8484
->end()
8585
->beforeNormalization()

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ public function restart()
441441
protected function getAbsoluteUri($uri)
442442
{
443443
// already absolute?
444-
if ('http' === substr($uri, 0, 4)) {
444+
if (0 === strpos($uri, 'http')) {
445445
return $uri;
446446
}
447447

src/Symfony/Component/Console/Input/ArgvInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected function parse()
7777
{
7878
$this->parsed = $this->tokens;
7979
while (null !== $token = array_shift($this->parsed)) {
80-
if ('--' === substr($token, 0, 2)) {
80+
if (0 === strpos($token, '--')) {
8181
$this->parseLongOption($token);
8282
} elseif ('-' === $token[0]) {
8383
$this->parseShortOption($token);

src/Symfony/Component/Console/Input/ArrayInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function getParameterOption($values, $default = false)
116116
protected function parse()
117117
{
118118
foreach ($this->parameters as $key => $value) {
119-
if ('--' === substr($key, 0, 2)) {
119+
if (0 === strpos($key, '--')) {
120120
$this->addLongOption(substr($key, 2), $value);
121121
} elseif ('-' === $key[0]) {
122122
$this->addShortOption(substr($key, 1), $value);

src/Symfony/Component/Console/Input/InputOption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class InputOption
4646
*/
4747
public function __construct($name, $shortcut = null, $mode = null, $description = '', $default = null)
4848
{
49-
if ('--' === substr($name, 0, 2)) {
49+
if (0 === strpos($name, '--')) {
5050
$name = substr($name, 2);
5151
}
5252

src/Symfony/Component/DomCrawler/Link.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function getUri()
8080
$uri = trim($this->getRawUri());
8181

8282
// absolute URL?
83-
if ('http' === substr($uri, 0, 4)) {
83+
if (0 === strpos($uri, 'http')) {
8484
return $uri;
8585
}
8686

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function prepare()
134134
$charset = $this->charset ?: 'UTF-8';
135135
if (!$this->headers->has('Content-Type')) {
136136
$this->headers->set('Content-Type', 'text/html; charset='.$charset);
137-
} elseif ('text/' === substr($this->headers->get('Content-Type'), 0, 5) && false === strpos($this->headers->get('Content-Type'), 'charset')) {
137+
} elseif (0 === strpos($this->headers->get('Content-Type'), 'text/') && false === strpos($this->headers->get('Content-Type'), 'charset')) {
138138
// add the charset
139139
$this->headers->set('Content-Type', $this->headers->get('Content-Type').'; charset='.$charset);
140140
}

src/Symfony/Component/HttpFoundation/ServerBag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function getHeaders()
2323
{
2424
$headers = array();
2525
foreach ($this->parameters as $key => $value) {
26-
if ('HTTP_' === substr($key, 0, 5)) {
26+
if (0 === strpos($key, 'HTTP_')) {
2727
$headers[substr($key, 5)] = $value;
2828
}
2929
// CONTENT_* are not prefixed with HTTP_

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ protected function getEnvParameters()
597597
{
598598
$parameters = array();
599599
foreach ($_SERVER as $key => $value) {
600-
if ('SYMFONY__' === substr($key, 0, 9)) {
600+
if (0 === strpos($key, 'SYMFONY__')) {
601601
$parameters[strtolower(str_replace('__', '.', substr($key, 9)))] = $value;
602602
}
603603
}

src/Symfony/Component/HttpKernel/Profiler/MysqlProfilerStorage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MysqlProfilerStorage extends PdoProfilerStorage
2424
protected function initDb()
2525
{
2626
if (null === $this->db) {
27-
if ('mysql' !== substr($this->dsn, 0, 5)) {
27+
if (0 !== strpos($this->dsn, 'mysql')) {
2828
throw new \RuntimeException('Please check your configuration. You are trying to use Mysql with a wrong dsn. "'.$this->dsn.'"');
2929
}
3030

0 commit comments

Comments
 (0)
0