Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of ctype #206

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
}
],
"require" : {
"ext-ctype": "*",
"ext-dom": "*",
"ext-libxml" : "*",
"php" : ">=5.3.0"
Expand Down
18 changes: 16 additions & 2 deletions src/HTML5/Parser/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected function consumeData()
$this->endTag();
} elseif ('?' === $tok) {
$this->processingInstruction();
} elseif (ctype_alpha($tok)) {
} elseif ($this->is_alpha($tok)) {
$this->tagName();
} else {
$this->parseError('Illegal tag opening');
Expand Down Expand Up @@ -347,7 +347,7 @@ protected function endTag()
// > -> parse error
// EOF -> parse error
// -> parse error
if (!ctype_alpha($tok)) {
if (!$this->is_alpha($tok)) {
$this->parseError("Expected tag name, got '%s'", $tok);
if ("\0" == $tok || false === $tok) {
return false;
Expand Down Expand Up @@ -1188,4 +1188,18 @@ protected function decodeCharacterReference($inAttribute = false)

return '&';
}

/**
* Checks whether a (single-byte) character is an ASCII letter or not.
*
* @param string $input A single-byte string
*
* @return bool True if it is a letter, False otherwise
*/
protected function is_alpha($input)
{
$code = ord($input);

return ($code >= 97 && $code <= 122) || ($code >= 65 && $code <= 90);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to import readability of the code, you might replace the numbers with \ord('A') and so on. PHP optimizes such calls (qualified calls to ord with a static string as argument) at compile time, so the runtime is the same than putting the number directly. But it makes it easier to understand what the code is about (as it avoids magic numbers)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
}