mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Experiment: Run through esc_attr() in a single optimized pass. #5337
Draft
dmsnell
wants to merge
10
commits into
WordPress:trunk
Choose a base branch
from
dmsnell:experiment/single-pass-esc-attr
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b99162d
Experiment: Run through esc_attr() in a single optimized pass.
dmsnell 43db5f0
Put it in its own file.
dmsnell 1bc7b33
Advance the pointer to avoid an infinite loop
dmsnell 2450dd9
Final fixes
dmsnell 41365a6
Small docs changes
dmsnell 0a4d4c2
More small changes.
dmsnell 804969a
Add question about limiting the list of allowable names
dmsnell 35cc667
Preserve more Core behaviors
dmsnell 49af2fd
Skip HTML4 allowable entity names check.
dmsnell b5caa5c
Major refactor: Introduce and use WP_Token_Set
dmsnell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
<?php | ||
|
||
class WP_Token_Set { | ||
const KEY_LENGTH = 2; | ||
|
||
const MAX_LENGTH = 256; | ||
|
||
/** | ||
* Stores an optimized form of the word set, where words are grouped | ||
* by first two letters and then collapsed into a string. | ||
* | ||
* @var array | ||
*/ | ||
private $large_words = array(); | ||
|
||
/** | ||
* Stores an optimized row of short words, where every entry is two | ||
* bytes long and zero-extended if the word is only a single byte. | ||
* | ||
* @var string | ||
*/ | ||
private $small_words = ''; | ||
|
||
public static function from_array( $words ) { | ||
$set = new WP_Token_Set(); | ||
|
||
// Start by grouping words. | ||
|
||
$groups = array(); | ||
$shorts = array(); | ||
foreach ( $words as $word ) { | ||
if ( ! is_string( $word ) || self::MAX_LENGTH <= strlen( $word ) ) { | ||
return null; | ||
} | ||
|
||
$length = strlen( $word ); | ||
|
||
if ( self::KEY_LENGTH >= $length ) { | ||
$shorts[] = $word; | ||
} else { | ||
$group = substr( $word, 0, self::KEY_LENGTH ); | ||
|
||
if ( ! isset( $groups[ $group ] ) ) { | ||
$groups[ $group ] = array(); | ||
} | ||
|
||
$groups[ $group ][] = substr( $word, self::KEY_LENGTH ); | ||
} | ||
} | ||
|
||
// Sort the words by longest-first, then alphabetical. | ||
|
||
usort( $shorts, array( self::class, 'longest_first_then_alphabetical' ) ); | ||
foreach ( $groups as $group_key => $group ) { | ||
usort( $groups[ $group_key ], array( self::class, 'longest_first_then_alphabetical' ) ); | ||
} | ||
|
||
// Finally construct the optimized lookups. | ||
|
||
foreach ( $shorts as $word ) { | ||
$set->small_words .= str_pad( $word, self::KEY_LENGTH, "\x00" ); | ||
} | ||
|
||
foreach ( $groups as $group => $group_words ) { | ||
$group_string = ''; | ||
|
||
foreach ( $group_words as $word ) { | ||
$group_string .= chr( strlen( $word ) ) . $word; | ||
} | ||
|
||
$set->large_words[ $group ] = $group_string; | ||
} | ||
|
||
return $set; | ||
} | ||
|
||
public static function from_precomputed_table( $large_words, $small_words ) { | ||
$set = new WP_Token_Set(); | ||
|
||
$set->large_words = $large_words; | ||
$set->small_words = $small_words; | ||
|
||
return $set; | ||
} | ||
|
||
public function contains( $word ) { | ||
if ( self::KEY_LENGTH >= strlen( $word ) ) { | ||
return str_contains( $this->small_words, str_pad( $word, self::KEY_LENGTH, "\x00" ) ); | ||
} | ||
|
||
$group_key = substr( $word, 0, self::KEY_LENGTH ); | ||
if ( ! isset( $this->large_words[ $group_key ] ) ) { | ||
return false; | ||
} | ||
|
||
$group = $this->large_words[ $group_key ]; | ||
$slug = substr( $word, self::KEY_LENGTH ); | ||
$length = strlen( $slug ); | ||
$at = 0; | ||
while ( $at < strlen( $group ) ) { | ||
$token_length = ord( $group[ $at++ ] ); | ||
if ( $token_length === $length && 0 === substr_compare( $group, $slug, $at, $token_length ) ) { | ||
return true; | ||
} | ||
|
||
$at += $token_length; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function read_token( $text, $offset ) { | ||
$text_length = strlen( $text ); | ||
|
||
// Search for a long word first, if the text is long enough, and if that fails, a short one. | ||
if ( self::KEY_LENGTH < $text_length ) { | ||
$group_key = substr( $text, $offset, self::KEY_LENGTH ); | ||
|
||
if ( ! isset( $this->large_words[ $group_key ] ) ) { | ||
return false; | ||
} | ||
|
||
$group = $this->large_words[ $group_key ]; | ||
$group_length = strlen( $group ); | ||
$at = 0; | ||
while ( $at < $group_length ) { | ||
$token_length = ord( $group[ $at++ ] ); | ||
$token = substr( $group, $at, $token_length ); | ||
|
||
if ( 0 === substr_compare( $text, $token, $offset + self::KEY_LENGTH, $token_length ) ) { | ||
return $group_key . $token; | ||
} | ||
|
||
$at += $token_length; | ||
} | ||
} | ||
|
||
// Perhaps a short word then. | ||
$small_text = str_pad( substr( $text, $offset, self::KEY_LENGTH ), self::KEY_LENGTH, "\x00" ); | ||
$at = strpos( $this->small_words, $small_text ); | ||
|
||
return false !== $at | ||
? rtrim( substr( $this->small_words, $at, self::KEY_LENGTH ), "\x00" ) | ||
: false; | ||
} | ||
|
||
public function precomputed_php_source_table( $indent = "\t" ) { | ||
$i1 = $indent; | ||
$i2 = $indent . $indent; | ||
|
||
$output = self::class . "::from_precomputed_table(\n"; | ||
$output .= $i1 . "array(\n"; | ||
|
||
foreach ( $this->large_words as $prefix => $group ) { | ||
$comment_line = "{$i2}//"; | ||
$data_line = "{$i2}'{$prefix}' => \""; | ||
$at = 0; | ||
while ( $at < strlen( $group ) ) { | ||
$length = ord( $group[ $at++ ] ); | ||
$digits = str_pad( dechex( $length ), 2, '0', STR_PAD_LEFT ); | ||
$token = substr( $group, $at, $length ); | ||
$at += $length; | ||
|
||
$comment_line .= " &{$prefix}{$token}"; | ||
$data_line .= "\\x{$digits}{$token}"; | ||
} | ||
$comment_line .= "\n"; | ||
$data_line .= "\",\n"; | ||
|
||
$output .= $comment_line; | ||
$output .= $data_line; | ||
} | ||
|
||
$output .= "{$i1}),\n"; | ||
$small_text = str_replace( "\x00", '\x00', $this->small_words ); | ||
$output .= "{$i1}'{$small_text}'\n"; | ||
$output .= ");\n"; | ||
|
||
return $output; | ||
} | ||
|
||
private static function longest_first_then_alphabetical( $a, $b ) { | ||
if ( $a === $b ) { | ||
return 0; | ||
} | ||
|
||
$la = strlen( $a ); | ||
$lb = strlen( $b ); | ||
|
||
// Longer strings are less-than for comparison's sake. | ||
if ( $la !== $lb ) { | ||
return $lb - $la; | ||
} | ||
|
||
return strcmp( $a, $b ); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bug! if the group doesn't exist, it doesn't also imply that the short word doesn't exist. this should
break
but not return