Skip to content

Commit

Permalink
Don't use Multibyte String functions for parsing query, as it's done …
Browse files Browse the repository at this point in the history
…manually byte per byte.
  • Loading branch information
ddebin committed Aug 22, 2020
1 parent 009d4c4 commit 5bf4eb5
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .idea/php-test-framework.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ return Config::create()
'final_static_access' => true,
'global_namespace_import' => true,
'linebreak_after_opening_tag' => true,
'mb_str_functions' => true,
'native_function_invocation' => false,
'no_unset_on_property' => false,
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"require": {
"php": "^7.0",
"ext-json": "*",
"ext-pdo": "*",
"symfony/polyfill-php74": "^1.0"
"ext-pdo": "*"
},
"require-dev": {
"phpunit/phpunit": "^6.0|^7.0",
Expand Down
4 changes: 2 additions & 2 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<ul id="example-list">
<?php
foreach (new DirectoryIterator(__DIR__) as $file) {
if ($file->isFile() && '.php' === mb_substr($file->getFilename(), -4) && !in_array($file->getFilename(), ['index.php', 'init.php'], true)) {
echo '<li><a href="'.$file->getFilename().'">'.ucwords(str_replace('_', ' ', mb_substr($file->getFilename(), 0, -4))).'</a></li>';
if ($file->isFile() && '.php' === substr($file->getFilename(), -4) && !in_array($file->getFilename(), ['index.php', 'init.php'], true)) {
echo '<li><a href="'.$file->getFilename().'">'.ucwords(str_replace('_', ' ', substr($file->getFilename(), 0, -4))).'</a></li>';
}
}
?>
Expand Down
20 changes: 10 additions & 10 deletions lib/MC/Google/Visualization.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function setDefaultFormat(string $type, string $format)
if (!isset($this->defaultFormat[$type])) {
throw new Visualization_Error('Unknown or unformattable type: "'.$type.'"');
}
if ('boolean' === $type && false === mb_strpos($format, ':')) {
if ('boolean' === $type && false === strpos($format, ':')) {
throw new Visualization_Error('Invalid boolean format string: "'.$format.'"');
}
$this->defaultFormat[$type] = $format;
Expand Down Expand Up @@ -535,14 +535,14 @@ public function getRowValues(array $row, array $meta): string
break;

case 'date':
if (!is_numeric($val) || (is_string($val) && (6 !== mb_strlen($val)))) {
if (!is_numeric($val) || (is_string($val) && (6 !== strlen($val)))) {
$time = strtotime($val);
list($year, $month, $day) = explode('-', date('Y-m-d', $time));
$formatted = date($format, $time);
} else {
assert(is_string($val));
$year = mb_substr($val, 0, 4);
$week = mb_substr($val, -2);
$year = substr($val, 0, 4);
$week = substr($val, -2);
$time = strtotime($year.'0104 +'.$week.' weeks');
assert(false !== $time);
$monday = strtotime('-'.((int) date('w', $time) - 1).' days', $time);
Expand Down Expand Up @@ -754,7 +754,7 @@ public function parseQuery(string $str): array
$field = $orderby[$i];
$dir = 'asc';
if (isset($orderby[$i + 1])) {
$dir = mb_strtolower($orderby[$i + 1]);
$dir = strtolower($orderby[$i + 1]);
if ('asc' === $dir || 'desc' === $dir) {
++$i;
} else {
Expand Down Expand Up @@ -1015,7 +1015,7 @@ protected function generateSQL(array &$meta): string

case 'null':
case 'notnull':
$wherePart['value'] = mb_strtoupper(implode(' ', $wherePart['value']));
$wherePart['value'] = strtoupper(implode(' ', $wherePart['value']));
break;
}

Expand Down Expand Up @@ -1064,7 +1064,7 @@ protected function generateSQL(array &$meta): string
$sql .= ',';
}

$sql .= ' '.$this->getFieldSQL($field, $spec).' '.mb_strtoupper($dir);
$sql .= ' '.$this->getFieldSQL($field, $spec).' '.strtoupper($dir);
$first = false;
}
}
Expand Down Expand Up @@ -1248,7 +1248,7 @@ protected function getFieldSQL(string $name, array $spec, bool $alias = false, s
$q = $this->getFieldQuote();
if (null !== $func) {
if (null === $pivot) {
$sql = mb_strtoupper($func).'('.$sql.')';
$sql = strtoupper($func).'('.$sql.')';
if ($alias) {
$sql .= ' AS '.$q.$func.'-'.$name.$q;
}
Expand All @@ -1260,7 +1260,7 @@ protected function getFieldSQL(string $name, array $spec, bool $alias = false, s
$pivotField = $pivotFields[$key];
$casewhen[] = $pivotField['field'].'='.$this->db->quote($val);
}
$sql = mb_strtoupper($func).'(CASE WHEN '.implode(' AND ', $casewhen).' THEN '.$sql.' ELSE NULL END)';
$sql = strtoupper($func).'(CASE WHEN '.implode(' AND ', $casewhen).' THEN '.$sql.' ELSE NULL END)';
if ($alias) {
$sql .= ' AS '.$q.implode(',', $pivot).' '.$func.'-'.$name.$q;
}
Expand Down Expand Up @@ -1320,7 +1320,7 @@ protected function parseFieldTokens(Token $token, array &$fields = null)
if ($token->hasChildren()) {
if ('function' === $token->name) {
$field = $token->getValues();
$field[0] = mb_strtolower($field[0]);
$field[0] = strtolower($field[0]);
$fields[] = $field;
} else {
foreach ($token->getChildren() as $field) {
Expand Down
7 changes: 3 additions & 4 deletions lib/MC/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function word(string $firstChars, string $restChars = null): Word
public function quotedString(string $quoteChars = '\'"', string $escChar = '\\'): Regex
{
$quoteChars = trim($quoteChars);
if (mb_strlen($escChar) > 1) {
if (strlen($escChar) > 1) {
throw new DefError('Only one $escChar can be defined');
}
if ('' !== $escChar) {
Expand All @@ -71,8 +71,7 @@ public function quotedString(string $quoteChars = '\'"', string $escChar = '\\')
if ('' === $quoteChars) {
throw new DefError('$quoteChars cannot be an empty string');
}
$quoteCharsArray = mb_str_split($quoteChars);
assert(is_array($quoteCharsArray));
$quoteCharsArray = str_split($quoteChars);

$tpl = '(?:Q(?:[^Q\n\rE]|(?:QQ)|(?:Ex[0-9a-fA-F]+)|(?:E.))*Q)';
$pats = [];
Expand Down Expand Up @@ -229,6 +228,6 @@ public function nums(): string
*/
public static function isWhitespace($test): bool
{
return false !== mb_strpos(self::$whitespace, $test);
return false !== strpos(self::$whitespace, $test);
}
}
4 changes: 2 additions & 2 deletions lib/MC/Parser/Def.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function parse(string $str): Token
$str = ltrim($str);

list($loc, $tok) = $this->parsePart($str, 0);
if ($loc !== mb_strlen($str)) {
throw new ParseError('An error occurred: "'.mb_substr($str, $loc).'"', $str, $loc);
if ($loc !== strlen($str)) {
throw new ParseError('An error occurred: "'.substr($str, $loc).'"', $str, $loc);
}

return $tok;
Expand Down
6 changes: 3 additions & 3 deletions lib/MC/Parser/Def/Literal.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public function __construct(string $str, bool $caseless = false, bool $fullword
*/
public function _parse(string $str, int $loc): array
{
$match = !$this->caseless ? mb_strpos($str, $this->search, $loc) : mb_stripos($str, $this->search, $loc);
$match = !$this->caseless ? strpos($str, $this->search, $loc) : stripos($str, $this->search, $loc);

if ($match !== $loc) {
throw new ParseError('Expected: '.$this->search, $str, $loc);
}

$loc += mb_strlen($this->search);
$loc += strlen($this->search);

if ($this->fullword && $loc < mb_strlen($str) && !Parser::isWhitespace($str[$loc])) {
if ($this->fullword && $loc < strlen($str) && !Parser::isWhitespace($str[$loc])) {
throw new ParseError('Expected: '.$this->search, $str, $loc);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/MC/Parser/Def/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public function __construct(string $regex = null, string $flags = null, string $
*/
public function _parse(string $str, int $loc): array
{
preg_match(self::DELIMITER.'^'.$this->regex.self::DELIMITER.$this->flags, mb_substr($str, $loc), $matches, PREG_OFFSET_CAPTURE);
preg_match(self::DELIMITER.'^'.$this->regex.self::DELIMITER.$this->flags, substr($str, $loc), $matches, PREG_OFFSET_CAPTURE);
$success = $matches[$this->retgroup] ?? null;
if ((null === $success) || 0 !== $success[1]) {
throw new ParseError('Expected: '.($this->errstr ?: 'matching '.$this->regex), $str, $loc);
}

$loc += mb_strlen($success[0]);
$loc += strlen($success[0]);

return [$loc, $this->token($success[0])];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testQueryComplete()
$vis->setDefaultEntity('timeline');

$parameters = [
'tq' => 'select country, year, birth_control, infant_mort where birth_control!=0 AND infant_mort!=0 group by country, year label country "Country", year "Year", birth_control "Birth Control Penetration", gdp_us "Per-capita GDP (US Dollars)", savings_rate "Savings Rate", investment_rate "Investment Rate", infant_mort "Infant Mortality", life_expect "Life Expectancy" format year "%d"',
'tq' => 'select country, year, birth_control, infant_mort where birth_control!=0 AND infant_mort!=0 group by country, year label country "Country", year "Année", birth_control "Birth Control Penetration", gdp_us "Per-capita GDP (US Dollars)", savings_rate "Savings Rate", investment_rate "Investment Rate", infant_mort "Infant Mortality", life_expect "Life Expectancy" format year "%d"',
'tqx' => 'reqId:1',
];

Expand Down
2 changes: 1 addition & 1 deletion tests/result1.js

Large diffs are not rendered by default.

0 comments on commit 5bf4eb5

Please sign in to comment.