5
5
#![ allow( clippy:: nonminimal_bool) ]
6
6
#![ allow( non_local_definitions) ]
7
7
8
- use std:: { borrow:: Cow , fmt:: Write , io, str} ;
8
+ use std:: { borrow:: Cow , fmt:: Write , io, ops :: Deref , str} ;
9
9
10
10
use ascii:: AsciiChar ;
11
+ use compact_str:: { format_compact, CompactString } ;
11
12
use memchr:: memmem:: Finder ;
12
13
use once_cell:: sync:: Lazy ;
13
14
use swc_allocator:: maybe:: vec:: Vec ;
@@ -108,7 +109,23 @@ where
108
109
pub wr : W ,
109
110
}
110
111
111
- fn replace_close_inline_script ( raw : & str ) -> Cow < str > {
112
+ enum CowStr < ' a > {
113
+ Borrowed ( & ' a str ) ,
114
+ Owned ( CompactString ) ,
115
+ }
116
+
117
+ impl Deref for CowStr < ' _ > {
118
+ type Target = str ;
119
+
120
+ fn deref ( & self ) -> & str {
121
+ match self {
122
+ CowStr :: Borrowed ( s) => s,
123
+ CowStr :: Owned ( s) => s. as_str ( ) ,
124
+ }
125
+ }
126
+ }
127
+
128
+ fn replace_close_inline_script ( raw : & str ) -> CowStr {
112
129
let chars = raw. as_bytes ( ) ;
113
130
let pattern_len = 8 ; // </script>
114
131
@@ -128,16 +145,16 @@ fn replace_close_inline_script(raw: &str) -> Cow<str> {
128
145
. peekable ( ) ;
129
146
130
147
if matched_indexes. peek ( ) . is_none ( ) {
131
- return Cow :: Borrowed ( raw) ;
148
+ return CowStr :: Borrowed ( raw) ;
132
149
}
133
150
134
- let mut result = String :: from ( raw) ;
151
+ let mut result = CompactString :: new ( raw) ;
135
152
136
153
for ( offset, i) in matched_indexes. enumerate ( ) {
137
154
result. insert ( i + 1 + offset, '\\' ) ;
138
155
}
139
156
140
- Cow :: Owned ( result)
157
+ CowStr :: Owned ( result)
141
158
}
142
159
143
160
static NEW_LINE_TPL_REGEX : Lazy < regex:: Regex > = Lazy :: new ( || regex:: Regex :: new ( r"\\n|\n" ) . unwrap ( ) ) ;
@@ -684,10 +701,11 @@ where
684
701
let ( quote_char, mut value) = get_quoted_utf16 ( & node. value , self . cfg . ascii_only , target) ;
685
702
686
703
if self . cfg . inline_script {
687
- value = Cow :: Owned (
704
+ value = CowStr :: Owned (
688
705
replace_close_inline_script ( & value)
689
706
. replace ( "\x3c !--" , "\\ x3c!--" )
690
- . replace ( "--\x3e " , "--\\ x3e" ) ,
707
+ . replace ( "--\x3e " , "--\\ x3e" )
708
+ . into ( ) ,
691
709
) ;
692
710
}
693
711
@@ -3965,13 +3983,13 @@ fn get_template_element_from_raw(s: &str, ascii_only: bool) -> String {
3965
3983
buf
3966
3984
}
3967
3985
3968
- fn get_ascii_only_ident ( sym : & str , may_need_quote : bool , target : EsVersion ) -> Cow < str > {
3986
+ fn get_ascii_only_ident ( sym : & str , may_need_quote : bool , target : EsVersion ) -> CowStr {
3969
3987
if sym. is_ascii ( ) {
3970
- return Cow :: Borrowed ( sym) ;
3988
+ return CowStr :: Borrowed ( sym) ;
3971
3989
}
3972
3990
3973
3991
let mut first = true ;
3974
- let mut buf = String :: with_capacity ( sym. len ( ) + 8 ) ;
3992
+ let mut buf = CompactString :: with_capacity ( sym. len ( ) + 8 ) ;
3975
3993
let mut iter = sym. chars ( ) . peekable ( ) ;
3976
3994
let mut need_quote = false ;
3977
3995
@@ -4133,14 +4151,14 @@ fn get_ascii_only_ident(sym: &str, may_need_quote: bool, target: EsVersion) -> C
4133
4151
}
4134
4152
4135
4153
if need_quote {
4136
- Cow :: Owned ( format ! ( "\" {}\" " , buf) )
4154
+ CowStr :: Owned ( format_compact ! ( "\" {}\" " , buf) )
4137
4155
} else {
4138
- Cow :: Owned ( buf)
4156
+ CowStr :: Owned ( buf)
4139
4157
}
4140
4158
}
4141
4159
4142
4160
/// Returns `(quote_char, value)`
4143
- fn get_quoted_utf16 ( v : & str , ascii_only : bool , target : EsVersion ) -> ( AsciiChar , Cow < str > ) {
4161
+ fn get_quoted_utf16 ( v : & str , ascii_only : bool , target : EsVersion ) -> ( AsciiChar , CowStr ) {
4144
4162
// Fast path: If the string is ASCII and doesn't need escaping, we can avoid
4145
4163
// allocation
4146
4164
if v. is_ascii ( ) {
@@ -4172,7 +4190,7 @@ fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> (AsciiChar,
4172
4190
if ( quote_char == AsciiChar :: Apostrophe && single_quote_count == 0 )
4173
4191
|| ( quote_char == AsciiChar :: Quotation && double_quote_count == 0 )
4174
4192
{
4175
- return ( quote_char, Cow :: Borrowed ( v) ) ;
4193
+ return ( quote_char, CowStr :: Borrowed ( v) ) ;
4176
4194
}
4177
4195
}
4178
4196
}
@@ -4207,7 +4225,7 @@ fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> (AsciiChar,
4207
4225
4208
4226
// Add 1 for each escaped quote
4209
4227
let capacity = v. len ( ) + escape_count;
4210
- let mut buf = String :: with_capacity ( capacity) ;
4228
+ let mut buf = CompactString :: with_capacity ( capacity) ;
4211
4229
4212
4230
let mut iter = v. chars ( ) . peekable ( ) ;
4213
4231
while let Some ( c) = iter. next ( ) {
@@ -4354,7 +4372,7 @@ fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> (AsciiChar,
4354
4372
}
4355
4373
}
4356
4374
4357
- ( quote_char, Cow :: Owned ( buf) )
4375
+ ( quote_char, CowStr :: Owned ( buf) )
4358
4376
}
4359
4377
4360
4378
fn handle_invalid_unicodes ( s : & str ) -> Cow < str > {
0 commit comments