Token Map: Introduce an efficient lookup and translation class for string mappings. - #5373
Closed
dmsnell wants to merge 29 commits into
Closed
Token Map: Introduce an efficient lookup and translation class for string mappings.#5373dmsnell wants to merge 29 commits into
dmsnell wants to merge 29 commits into
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Trac ticket: Core-60698
(previously Core-60841)
Motivated by the need to properly transform HTML named character references (like &) I found the need for a new semantic class which can efficiently perform search and replacement of a set of static tokens. Existing patterns in the codebase are not sufficient for the HTML need, and I suspect there are other use-cases where this class would help.
In #6387 I have built a spec-compliant HTML5 text decoder utilizing this token map. The performance of the new decoder is approximately 20% slower than calling
html_entity_decode()directly, except that it properly decodes what PHP can't. In fact, the performance bottleneck in that PR comes from converting into UTF-8 the sequence of digits in numeric character references, not in looking up named character references.This proposal is adding a new class
WP_Token_Mapproviding at least two methods for normal use:contains( $token )returns whether the passed string is in the set.read_token( $text, $offset = 0, $skip_bytes )indicates if the character sequence starting at the given offset in the passed string forms a token in the set, and if so, returns the replacement for that token. It also sets &$skip_bytes to the length of the token so that calling code .It also provides utility functions for pre-computing these classes, as they are designed for relatively-static cases where the actual code is intended to be generated dynamically, but stay static over time. For example, HTML5 defines the set of named character references and indicates that the list shall not change or be expanded. HTML5 spec. Precomputing can save on the startup-up cost of building the optimized lookup tables.
WP_Token_Map::from_array( array $mappings )generates a new token map from the given array of whose keys are tokens and whose values are the replacements.to_array()dumps the set of mapping into an array suitable for passing back intofrom_array().WP_Token_Map::from_precomputed_table( ...$table )instantiates a token set from a precomputed table, skipping the computation for building the table and sorting the tokens.precomputed_php_source_table()generates PHP source code which can be loaded with the previous static method for maintenance of the core static token sets.Other potential uses