2
2
3
3
use std:: { cell:: RefCell , char, iter:: FusedIterator , mem:: transmute, rc:: Rc } ;
4
4
5
+ use arrayvec:: ArrayVec ;
5
6
use either:: Either :: { Left , Right } ;
6
- use smallvec:: { smallvec, SmallVec } ;
7
7
use swc_atoms:: { Atom , AtomStoreCell } ;
8
8
use swc_common:: {
9
9
comments:: Comments ,
@@ -52,7 +52,7 @@ impl From<u32> for Char {
52
52
}
53
53
}
54
54
55
- pub ( crate ) struct CharIter ( SmallVec < [ char ; 7 ] > ) ;
55
+ pub ( crate ) struct CharIter ( ArrayVec < char , 12 > ) ;
56
56
57
57
/// Ported from https://github.com/web-infra-dev/oxc/blob/99a4816ce7b6132b2667257984f9d92ae3768f03/crates/oxc_parser/src/lexer/mod.rs#L1349-L1374
58
58
impl IntoIterator for Char {
@@ -67,9 +67,16 @@ impl IntoIterator for Char {
67
67
// }
68
68
69
69
CharIter ( match char:: from_u32 ( self . 0 ) {
70
- Some ( c) => smallvec ! [ c] ,
70
+ Some ( c) => {
71
+ let mut buf = ArrayVec :: new ( ) ;
72
+ // Safety: we can make sure that `buf` has enough capacity
73
+ unsafe {
74
+ buf. push_unchecked ( c) ;
75
+ }
76
+ buf
77
+ }
71
78
None => {
72
- let mut buf = smallvec ! [ ] ;
79
+ let mut buf = ArrayVec :: new ( ) ;
73
80
74
81
let high = self . 0 & 0xffff0000 >> 16 ;
75
82
@@ -78,19 +85,31 @@ impl IntoIterator for Char {
78
85
// The second code unit of a surrogate pair is always in the range from 0xDC00
79
86
// to 0xDFFF, and is called a low surrogate or a trail surrogate.
80
87
if !( 0xdc00 ..=0xdfff ) . contains ( & low) {
81
- buf. push ( '\\' ) ;
82
- buf. push ( 'u' ) ;
83
- buf. extend ( format ! ( "{high:x}" ) . chars ( ) ) ;
84
- buf. push ( '\\' ) ;
85
- buf. push ( 'u' ) ;
86
- buf. extend ( format ! ( "{low:x}" ) . chars ( ) ) ;
88
+ // Safety: we can make sure that `buf` has enough capacity
89
+ unsafe {
90
+ buf. push_unchecked ( '\\' ) ;
91
+ buf. push_unchecked ( 'u' ) ;
92
+ for c in format ! ( "{high:x}" ) . chars ( ) {
93
+ buf. push_unchecked ( c) ;
94
+ }
95
+ buf. push_unchecked ( '\\' ) ;
96
+ buf. push_unchecked ( 'u' ) ;
97
+ for c in format ! ( "{low:x}" ) . chars ( ) {
98
+ buf. push_unchecked ( c) ;
99
+ }
100
+ }
87
101
} else {
88
102
// `https://tc39.es/ecma262/#sec-utf16decodesurrogatepair`
89
103
let astral_code_point = ( high - 0xd800 ) * 0x400 + low - 0xdc00 + 0x10000 ;
90
104
91
- buf. push ( '\\' ) ;
92
- buf. push ( 'u' ) ;
93
- buf. extend ( format ! ( "{astral_code_point:x}" ) . chars ( ) ) ;
105
+ // Safety: we can make sure that `buf` has enough capacity
106
+ unsafe {
107
+ buf. push_unchecked ( '\\' ) ;
108
+ buf. push_unchecked ( 'u' ) ;
109
+ for c in format ! ( "{astral_code_point:x}" ) . chars ( ) {
110
+ buf. push_unchecked ( c) ;
111
+ }
112
+ }
94
113
}
95
114
96
115
buf
0 commit comments