-
Notifications
You must be signed in to change notification settings - Fork 14
Module quote strings
This module was introduced in RegPack v5.
Strings in Javascript are enclosed in single ' or double " quotes, or in backticks ` since ES6. To feature one of these characters inside a string, one must either escape it (costing one byte) or wrap the string with different quotes.
// These definitions are equivalent
var message='time\'s up';
var message="time's up";
var message=`time's up`; // since ES6The packer transforms the input code into a string, which needs quotation marks. Any occurrence of the same quote within the packed code will have to be escaped, costing extra bytes. This module optimizes the choice of string delimiters for size, both inside the code and for the packed string.
Note : before the advent of this module, the choice of the quotation mark was done inside the crusher and packer through a count of occurrences, with no attempt to optimize by changing the quotes inside the code.
The module first lists all strings present in the input code, identifying delimiters used and also counting all instances of ', " and ` inside the string.
These will be used later to determine whether the string delimiter should be changed.
When a given character is used as a delimiter for a string, all copies of that character inside the string must be escaped :
// those two are equivalent, but the first one is one byte shorter
var message = "Time's up!";
var message = 'Time\'s up!';This also applies to the final string representing the packed input code : all copies of its delimiter within the string will cost one extra byte.
The algorithm iterates on the three candidate delimiters ' " ` to find the one for the packed string that will require the least escaping inside the input (and thus procude the shortest string), as follows :
- each string using that delimiter costs 2 extra bytes to escape (one at the beginning, one at the end)
- the extra cost may be reduced by changing the string delimiter : this implies that all copies of the new delimiter be escaped
- the algorithm will choose the new delimiter with the least escaping required, with a maximum of one character. //Since the original string costs two bytes to escape as-is, if the cost of changing is 2 or higher, it is not worth it.//
- backtick
`delimiter for template literals is never replaced as it has extra features that are not supported by the quotes"and'. - quote
' "delimiters are only replaced with backtick`if they do not contain the substring${, as it would be misinterpreted as a placeholder for an expression.
The chosen delimiter is stored as PackerData.packedStringDelimiter.
| Parameter name | Type | Default | Effect |
|---|---|---|---|
useES6 |
boolean | true |
enable or disable backtick ` as string delimiter |
When ES6 use is disabled, the backtick is not considered as a potential replacement for other quotes.
A preliminary analysis is performed by Shapeshifter.identifyStrings(). The methods fills the member variable PackerData.containedStrings without altering the input.
The entry point of the module is the method Shapeshifter.quoteStrings().