@@ -785,7 +785,7 @@ public function precomputed_php_source_table( string $indent = "\t" ): string {
785785 $ output .= "{$ i2 }\"storage_version \" => \"{$ class_version }\", \n" ;
786786 $ output .= "{$ i2 }\"key_length \" => {$ this ->key_length }, \n" ;
787787
788- $ group_line = str_replace ( "\x00" , "\\ x00 " , $ this ->groups );
788+ $ group_line = self :: escape_precomputed_php_string ( $ this ->groups );
789789 $ output .= "{$ i2 }\"groups \" => \"{$ group_line }\", \n" ;
790790
791791 $ output .= "{$ i2 }\"large_words \" => array( \n" ;
@@ -798,7 +798,7 @@ public function precomputed_php_source_table( string $indent = "\t" ): string {
798798 $ group = $ this ->large_words [ $ index ];
799799 $ group_length = strlen ( $ group );
800800 $ comment_line = "{$ i3 }// " ;
801- $ data_line = "{ $ i3 }\"" ;
801+ $ group_data = '' ;
802802 $ at = 0 ;
803803 while ( $ at < $ group_length ) {
804804 $ token_length = unpack ( 'C ' , $ group [ $ at ++ ] )[1 ];
@@ -808,32 +808,11 @@ public function precomputed_php_source_table( string $indent = "\t" ): string {
808808 $ mapping = substr ( $ group , $ at , $ mapping_length );
809809 $ at += $ mapping_length ;
810810
811- $ token_digits = str_pad ( dechex ( $ token_length ), 2 , '0 ' , STR_PAD_LEFT );
812- $ mapping_digits = str_pad ( dechex ( $ mapping_length ), 2 , '0 ' , STR_PAD_LEFT );
813-
814- $ mapping = preg_replace_callback (
815- "~[ \\x00- \\x1f \\x22 \\x5c]~ " ,
816- static function ( $ match_result ) {
817- switch ( $ match_result [0 ] ) {
818- case '" ' :
819- return '\\" ' ;
820-
821- case '\\' :
822- return '\\\\' ;
823-
824- default :
825- $ hex = dechex ( ord ( $ match_result [0 ] ) );
826- return "\\x {$ hex }" ;
827- }
828- },
829- $ mapping
830- );
831-
832- $ comment_line .= " {$ prefix }{$ token }[ {$ mapping }] " ;
833- $ data_line .= "\\x {$ token_digits }{$ token }\\x {$ mapping_digits }{$ mapping }" ;
811+ $ group_data .= pack ( 'C ' , $ token_length ) . $ token . pack ( 'C ' , $ mapping_length ) . $ mapping ;
812+ $ comment_line .= ' ' . self ::escape_precomputed_php_comment ( "{$ prefix }{$ token }" ) . '[ ' . self ::escape_precomputed_php_comment ( $ mapping ) . '] ' ;
834813 }
835814 $ comment_line .= ". \n" ;
836- $ data_line .= "\", \n" ;
815+ $ data_line = "{ $ i3 }\"" . self :: escape_precomputed_php_string ( $ group_data ) . "\", \n" ;
837816
838817 $ output .= $ comment_line ;
839818 $ output .= $ data_line ;
@@ -849,12 +828,12 @@ static function ( $match_result ) {
849828 $ at += $ this ->key_length + 1 ;
850829 }
851830
852- $ small_text = str_replace ( "\x00" , ' \x00 ' , implode ( '' , $ small_words ) );
831+ $ small_text = self :: escape_precomputed_php_string ( implode ( '' , $ small_words ) );
853832 $ output .= "{$ i2 }\"small_words \" => \"{$ small_text }\", \n" ;
854833
855834 $ output .= "{$ i2 }\"small_mappings \" => array( \n" ;
856835 foreach ( $ this ->small_mappings as $ mapping ) {
857- $ output .= "{$ i3 }\"{ $ mapping} \", \n" ;
836+ $ output .= "{$ i3 }\"" . self :: escape_precomputed_php_string ( $ mapping ) . " \", \n" ;
858837 }
859838 $ output .= "{$ i2 }) \n" ;
860839 $ output .= "{$ i1 }) \n" ;
@@ -964,4 +943,65 @@ private static function matches_at( string $haystack, string $needle, int $offse
964943 private static function ascii_lowercase ( string $ text ): string {
965944 return strtr ( $ text , 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ' , 'abcdefghijklmnopqrstuvwxyz ' );
966945 }
946+
947+ /**
948+ * Escapes text for use inside a double-quoted PHP string literal.
949+ *
950+ * @since 6.6.0
951+ *
952+ * @param string $text Text to escape.
953+ * @return string Escaped string literal body.
954+ */
955+ private static function escape_precomputed_php_string ( string $ text ): string {
956+ $ escaped = '' ;
957+ $ length = strlen ( $ text );
958+
959+ for ( $ i = 0 ; $ i < $ length ; $ i ++ ) {
960+ $ byte = ord ( $ text [ $ i ] );
961+ switch ( $ text [ $ i ] ) {
962+ case '" ' :
963+ $ escaped .= '\\" ' ;
964+ break ;
965+
966+ case '\\' :
967+ $ escaped .= '\\\\' ;
968+ break ;
969+
970+ case '$ ' :
971+ $ escaped .= '\\$ ' ;
972+ break ;
973+
974+ default :
975+ $ escaped .= ( $ byte < 0x20 || $ byte >= 0x7f )
976+ ? sprintf ( '\\x%02x ' , $ byte )
977+ : $ text [ $ i ];
978+ }
979+ }
980+
981+ return $ escaped ;
982+ }
983+
984+ /**
985+ * Escapes text for use inside generated PHP comments.
986+ *
987+ * @since 6.6.0
988+ *
989+ * @param string $text Text to escape.
990+ * @return string Escaped comment text.
991+ */
992+ private static function escape_precomputed_php_comment ( string $ text ): string {
993+ $ escaped = '' ;
994+ $ length = strlen ( $ text );
995+
996+ for ( $ i = 0 ; $ i < $ length ; $ i ++ ) {
997+ $ byte = ord ( $ text [ $ i ] );
998+ $ char = $ text [ $ i ];
999+
1000+ $ escaped .= ( $ byte < 0x20 || $ byte >= 0x7f || '? ' === $ char || '\\' === $ char )
1001+ ? sprintf ( '\\x%02x ' , $ byte )
1002+ : $ char ;
1003+ }
1004+
1005+ return $ escaped ;
1006+ }
9671007}
0 commit comments