@@ -31,15 +31,9 @@ fn sub(a: *const u8, b: *const u8) -> usize {
3131
3232#[ target_feature( enable = "avx512f" , enable = "avx512bw" ) ]
3333#[ inline]
34- pub unsafe fn escape_avx512 < S : AsRef < str > > ( input : S ) -> String {
35- let s = input. as_ref ( ) ;
36- let bytes = s. as_bytes ( ) ;
34+ pub unsafe fn escape_avx512 ( bytes : & [ u8 ] , mut result : Vec < u8 > ) -> String {
3735 let len = bytes. len ( ) ;
3836
39- // Pre-allocate with estimated capacity
40- let estimated_capacity = len + len / 2 + 2 ;
41- let mut result = Vec :: with_capacity ( estimated_capacity) ;
42-
4337 result. push ( b'"' ) ;
4438
4539 let start_ptr = bytes. as_ptr ( ) ;
@@ -237,7 +231,7 @@ pub unsafe fn escape_avx512<S: AsRef<str>>(input: S) -> String {
237231 }
238232 } else {
239233 // Fall back to AVX2 for small strings
240- return escape_avx2 ( input ) ;
234+ return escape_avx2 ( bytes , result ) ;
241235 }
242236
243237 // Copy any remaining bytes
@@ -251,15 +245,9 @@ pub unsafe fn escape_avx512<S: AsRef<str>>(input: S) -> String {
251245
252246#[ target_feature( enable = "avx2" ) ]
253247#[ inline]
254- pub unsafe fn escape_avx2 < S : AsRef < str > > ( input : S ) -> String {
255- let s = input. as_ref ( ) ;
256- let bytes = s. as_bytes ( ) ;
248+ pub unsafe fn escape_avx2 ( bytes : & [ u8 ] , mut result : Vec < u8 > ) -> String {
257249 let len = bytes. len ( ) ;
258250
259- // Pre-allocate with estimated capacity
260- let estimated_capacity = len + len / 2 + 2 ;
261- let mut result = Vec :: with_capacity ( estimated_capacity) ;
262-
263251 result. push ( b'"' ) ;
264252
265253 let start_ptr = bytes. as_ptr ( ) ;
@@ -477,7 +465,7 @@ pub unsafe fn escape_avx2<S: AsRef<str>>(input: S) -> String {
477465 }
478466 } else {
479467 // Fall back to SSE2 for small strings
480- return escape_sse2 ( input ) ;
468+ return escape_sse2 ( bytes , result ) ;
481469 }
482470
483471 // Copy any remaining bytes
@@ -491,14 +479,9 @@ pub unsafe fn escape_avx2<S: AsRef<str>>(input: S) -> String {
491479
492480#[ target_feature( enable = "sse2" ) ]
493481#[ inline]
494- pub unsafe fn escape_sse2 < S : AsRef < str > > ( input : S ) -> String {
495- let s = input. as_ref ( ) ;
496- let bytes = s. as_bytes ( ) ;
482+ pub unsafe fn escape_sse2 ( bytes : & [ u8 ] , mut result : Vec < u8 > ) -> String {
497483 let len = bytes. len ( ) ;
498484
499- let estimated_capacity = len + len / 2 + 2 ;
500- let mut result = Vec :: with_capacity ( estimated_capacity) ;
501-
502485 result. push ( b'"' ) ;
503486
504487 let start_ptr = bytes. as_ptr ( ) ;
0 commit comments