11#include "rbs_extension.h"
22
3- static VALUE REGEXP = 0 ;
3+ static VALUE DQ_REGEXP = 0 ;
4+ static VALUE SQ_REGEXP = 0 ;
45static VALUE HASH = 0 ;
56
6- static const char * regexp_str = "\\\\[abefnrstv\"\\\\]" ;
7+ static const char * dq_regexp_str = "\\\\[abefnrstv\"\\\\]" ;
8+ static const char * sq_regexp_str = "\\\\[\'\\\\]" ;
79
810static ID gsub = 0 ;
911
10- void rbs_unescape_string (VALUE string ) {
11- if (!REGEXP ) {
12- REGEXP = rb_reg_new (regexp_str , strlen (regexp_str ), 0 );
13- rb_global_variable (& REGEXP );
12+ void rbs_unescape_string (VALUE string , bool dq_string ) {
13+ if (!DQ_REGEXP ) {
14+ DQ_REGEXP = rb_reg_new (dq_regexp_str , strlen (dq_regexp_str ), 0 );
15+ rb_global_variable (& DQ_REGEXP );
16+ }
17+
18+ if (!SQ_REGEXP ) {
19+ SQ_REGEXP = rb_reg_new (sq_regexp_str , strlen (sq_regexp_str ), 0 );
20+ rb_global_variable (& SQ_REGEXP );
1421 }
1522
1623 if (!gsub ) {
@@ -30,10 +37,11 @@ void rbs_unescape_string(VALUE string) {
3037 rb_hash_aset (HASH , rb_str_new_literal ("\\t" ), rb_str_new_literal ("\t" ));
3138 rb_hash_aset (HASH , rb_str_new_literal ("\\v" ), rb_str_new_literal ("\v" ));
3239 rb_hash_aset (HASH , rb_str_new_literal ("\\\"" ), rb_str_new_literal ("\"" ));
40+ rb_hash_aset (HASH , rb_str_new_literal ("\\\'" ), rb_str_new_literal ("'" ));
3341 rb_hash_aset (HASH , rb_str_new_literal ("\\\\" ), rb_str_new_literal ("\\" ));
3442 }
3543
36- rb_funcall (string , gsub , 2 , REGEXP , HASH );
44+ rb_funcall (string , gsub , 2 , dq_string ? DQ_REGEXP : SQ_REGEXP , HASH );
3745}
3846
3947VALUE rbs_unquote_string (parserstate * state , range rg , int offset_bytes ) {
@@ -57,9 +65,7 @@ VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes) {
5765 char * buffer = RSTRING_PTR (state -> lexstate -> string ) + rg .start .byte_pos + offset_bytes ;
5866 VALUE str = rb_enc_str_new (buffer , byte_length , enc );
5967
60- if (first_char == '\"' ) {
61- rbs_unescape_string (str );
62- }
68+ rbs_unescape_string (str , first_char == '\"' );
6369
6470 return str ;
6571}
0 commit comments