@@ -147,8 +147,14 @@ impl State {
147
147
}
148
148
149
149
struct Naming {
150
- identifier : Option < String > ,
151
150
name : String ,
151
+ kind : NamingKind ,
152
+ }
153
+
154
+ enum NamingKind {
155
+ DollarName ,
156
+ DollarQuotedName ,
157
+ SyntheticPrefix ( String ) ,
152
158
}
153
159
154
160
impl Config {
@@ -410,7 +416,7 @@ impl Printer<'_, '_> {
410
416
if len == 1 {
411
417
if let Some ( name) = state. name . as_ref ( ) {
412
418
self . result . write_str ( " " ) ?;
413
- name. write ( self . result ) ?;
419
+ name. write ( self ) ?;
414
420
}
415
421
}
416
422
}
@@ -925,7 +931,10 @@ impl Printer<'_, '_> {
925
931
self . result . write_str ( " " ) ?;
926
932
if let Some ( idxs @ ( _, field_idx) ) = ty_field_idx {
927
933
match state. core . field_names . index_to_name . get ( & idxs) {
928
- Some ( name) => write ! ( self . result, "${} " , name. identifier( ) ) ?,
934
+ Some ( name) => {
935
+ name. write_identifier ( self ) ?;
936
+ self . result . write_str ( " " ) ?;
937
+ }
929
938
None if self . config . name_unnamed => write ! ( self . result, "$#field{field_idx} " ) ?,
930
939
None => { }
931
940
}
@@ -1453,7 +1462,7 @@ impl Printer<'_, '_> {
1453
1462
fn _print_idx ( & mut self , names : & HashMap < u32 , Naming > , idx : u32 , desc : & str ) -> Result < ( ) > {
1454
1463
self . result . start_name ( ) ?;
1455
1464
match names. get ( & idx) {
1456
- Some ( name) => write ! ( self . result , "${}" , name. identifier ( ) ) ?,
1465
+ Some ( name) => name. write_identifier ( self ) ?,
1457
1466
None if self . config . name_unnamed => write ! ( self . result, "$#{desc}{idx}" ) ?,
1458
1467
None => write ! ( self . result, "{idx}" ) ?,
1459
1468
}
@@ -1464,7 +1473,7 @@ impl Printer<'_, '_> {
1464
1473
fn print_local_idx ( & mut self , state : & State , func : u32 , idx : u32 ) -> Result < ( ) > {
1465
1474
self . result . start_name ( ) ?;
1466
1475
match state. core . local_names . index_to_name . get ( & ( func, idx) ) {
1467
- Some ( name) => write ! ( self . result , "${}" , name. identifier ( ) ) ?,
1476
+ Some ( name) => name. write_identifier ( self ) ?,
1468
1477
None if self . config . name_unnamed => write ! ( self . result, "$#local{idx}" ) ?,
1469
1478
None => write ! ( self . result, "{}" , idx) ?,
1470
1479
}
@@ -1475,7 +1484,7 @@ impl Printer<'_, '_> {
1475
1484
fn print_field_idx ( & mut self , state : & State , ty : u32 , idx : u32 ) -> Result < ( ) > {
1476
1485
self . result . start_name ( ) ?;
1477
1486
match state. core . field_names . index_to_name . get ( & ( ty, idx) ) {
1478
- Some ( name) => write ! ( self . result , "${}" , name. identifier ( ) ) ?,
1487
+ Some ( name) => name. write_identifier ( self ) ?,
1479
1488
None if self . config . name_unnamed => write ! ( self . result, "$#field{idx}" ) ?,
1480
1489
None => write ! ( self . result, "{}" , idx) ?,
1481
1490
}
@@ -1499,7 +1508,7 @@ impl Printer<'_, '_> {
1499
1508
self . result . start_name ( ) ?;
1500
1509
match names. get ( & cur_idx) {
1501
1510
Some ( name) => {
1502
- name. write ( self . result ) ?;
1511
+ name. write ( self ) ?;
1503
1512
self . result . write_str ( " " ) ?;
1504
1513
}
1505
1514
None if self . config . name_unnamed => {
@@ -1911,7 +1920,7 @@ impl Printer<'_, '_> {
1911
1920
let outer = Self :: outer_state ( states, count) ?;
1912
1921
self . start_group ( "alias outer " ) ?;
1913
1922
if let Some ( name) = outer. name . as_ref ( ) {
1914
- name. write ( self . result ) ?;
1923
+ name. write ( self ) ?;
1915
1924
} else {
1916
1925
write ! ( self . result, "{count}" ) ?;
1917
1926
}
@@ -2592,7 +2601,7 @@ impl Printer<'_, '_> {
2592
2601
let outer = Self :: outer_state ( states, count) ?;
2593
2602
self . start_group ( "alias outer " ) ?;
2594
2603
if let Some ( name) = outer. name . as_ref ( ) {
2595
- name. write ( self . result ) ?;
2604
+ name. write ( self ) ?;
2596
2605
} else {
2597
2606
write ! ( self . result, "{count}" ) ?;
2598
2607
}
@@ -2643,20 +2652,22 @@ impl Printer<'_, '_> {
2643
2652
2644
2653
fn print_str ( & mut self , name : & str ) -> Result < ( ) > {
2645
2654
self . result . start_literal ( ) ?;
2646
- let mut bytes = [ 0 ; 4 ] ;
2647
2655
self . result . write_str ( "\" " ) ?;
2656
+ self . print_str_contents ( name) ?;
2657
+ self . result . write_str ( "\" " ) ?;
2658
+ self . result . reset_color ( ) ?;
2659
+ Ok ( ( ) )
2660
+ }
2661
+
2662
+ fn print_str_contents ( & mut self , name : & str ) -> Result < ( ) > {
2648
2663
for c in name. chars ( ) {
2649
2664
let v = c as u32 ;
2650
2665
if ( 0x20 ..0x7f ) . contains ( & v) && c != '"' && c != '\\' && v < 0xff {
2651
2666
write ! ( self . result, "{c}" ) ?;
2652
2667
} else {
2653
- for byte in c. encode_utf8 ( & mut bytes) . as_bytes ( ) {
2654
- self . hex_byte ( * byte) ?;
2655
- }
2668
+ write ! ( self . result, "\\ u{{{v:x}}}" , ) ?;
2656
2669
}
2657
2670
}
2658
- self . result . write_str ( "\" " ) ?;
2659
- self . result . reset_color ( ) ?;
2660
2671
Ok ( ( ) )
2661
2672
}
2662
2673
@@ -2917,7 +2928,7 @@ impl NamedLocalPrinter {
2917
2928
// Print the optional name if given...
2918
2929
match name {
2919
2930
Some ( name) => {
2920
- name. write ( dst. result ) ?;
2931
+ name. write ( dst) ?;
2921
2932
dst. result . write_str ( " " ) ?;
2922
2933
self . end_group_after_local = true ;
2923
2934
}
@@ -3057,7 +3068,10 @@ impl Naming {
3057
3068
group : & str ,
3058
3069
used : Option < & mut HashSet < & ' a str > > ,
3059
3070
) -> Naming {
3060
- let mut identifier = None ;
3071
+ let mut kind = NamingKind :: DollarName ;
3072
+ if name. chars ( ) . any ( |c| !is_idchar ( c) ) {
3073
+ kind = NamingKind :: DollarQuotedName ;
3074
+ }
3061
3075
3062
3076
// If the `name` provided can't be used as the raw identifier for the
3063
3077
// item that it's describing then a synthetic name must be made. The
@@ -3078,17 +3092,13 @@ impl Naming {
3078
3092
// valid identifier characters of `name` still appear in the returned
3079
3093
// name).
3080
3094
if name. is_empty ( )
3081
- || name. chars ( ) . any ( |c| !is_idchar ( c) )
3082
3095
|| name. starts_with ( '#' )
3083
3096
|| used. map ( |set| !set. insert ( name) ) . unwrap_or ( false )
3084
3097
{
3085
- let mut id = format ! ( "#{group}{index}<" ) ;
3086
- id. extend ( name. chars ( ) . map ( |c| if is_idchar ( c) { c } else { '_' } ) ) ;
3087
- id. push ( '>' ) ;
3088
- identifier = Some ( id) ;
3098
+ kind = NamingKind :: SyntheticPrefix ( format ! ( "#{group}{index}" ) ) ;
3089
3099
}
3090
3100
return Naming {
3091
- identifier ,
3101
+ kind ,
3092
3102
name : name. to_string ( ) ,
3093
3103
} ;
3094
3104
@@ -3126,37 +3136,39 @@ impl Naming {
3126
3136
}
3127
3137
}
3128
3138
3129
- fn identifier ( & self ) -> & str {
3130
- match & self . identifier {
3131
- Some ( s) => s,
3132
- None => & self . name ,
3139
+ fn write_identifier ( & self , printer : & mut Printer < ' _ , ' _ > ) -> Result < ( ) > {
3140
+ match & self . kind {
3141
+ NamingKind :: DollarName => {
3142
+ printer. result . write_str ( "$" ) ?;
3143
+ printer. result . write_str ( & self . name ) ?;
3144
+ }
3145
+ NamingKind :: DollarQuotedName => {
3146
+ printer. result . write_str ( "$\" " ) ?;
3147
+ printer. print_str_contents ( & self . name ) ?;
3148
+ printer. result . write_str ( "\" " ) ?;
3149
+ }
3150
+ NamingKind :: SyntheticPrefix ( prefix) => {
3151
+ printer. result . write_str ( "$\" " ) ?;
3152
+ printer. result . write_str ( & prefix) ?;
3153
+ printer. result . write_str ( " " ) ?;
3154
+ printer. print_str_contents ( & self . name ) ?;
3155
+ printer. result . write_str ( "\" " ) ?;
3156
+ }
3133
3157
}
3158
+ Ok ( ( ) )
3134
3159
}
3135
3160
3136
- fn write ( & self , dst : & mut dyn Print ) -> Result < ( ) > {
3137
- match & self . identifier {
3138
- Some ( alternate) => {
3139
- assert ! ( * alternate != self . name) ;
3140
- write ! ( dst, "${alternate} (@name \" " ) ?;
3141
- // https://webassembly.github.io/spec/core/text/values.html#text-string
3142
- for c in self . name . chars ( ) {
3143
- match c {
3144
- '\t' => write ! ( dst, "\\ t" ) ?,
3145
- '\n' => write ! ( dst, "\\ n" ) ?,
3146
- '\r' => write ! ( dst, "\\ r" ) ?,
3147
- '"' => write ! ( dst, "\\ \" " ) ?,
3148
- '\'' => write ! ( dst, "\\ '" ) ?,
3149
- '\\' => write ! ( dst, "\\ \\ " ) ?,
3150
- c if ( c as u32 ) < 0x20 || c as u32 == 0x7f => {
3151
- write ! ( dst, "\\ u{{{:x}}}" , c as u32 ) ?;
3152
- }
3153
- other => write ! ( dst, "{other}" ) ?,
3154
- }
3155
- }
3156
- write ! ( dst, "\" )" ) ?;
3157
- }
3158
- None => {
3159
- write ! ( dst, "${}" , self . name) ?;
3161
+ fn write ( & self , dst : & mut Printer < ' _ , ' _ > ) -> Result < ( ) > {
3162
+ self . write_identifier ( dst) ?;
3163
+ match & self . kind {
3164
+ NamingKind :: DollarName | NamingKind :: DollarQuotedName => { }
3165
+
3166
+ NamingKind :: SyntheticPrefix ( _) => {
3167
+ dst. result . write_str ( " " ) ?;
3168
+ dst. start_group ( "@name \" " ) ?;
3169
+ dst. print_str_contents ( & self . name ) ?;
3170
+ dst. result . write_str ( "\" " ) ?;
3171
+ dst. end_group ( ) ?;
3160
3172
}
3161
3173
}
3162
3174
Ok ( ( ) )
0 commit comments