MCPcopy Create free account
hub / github.com/SpartanJ/eepp / strip_ansi_avx2

Function strip_ansi_avx2

src/eepp/core/string.cpp:2630–2686  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2628// Force AVX2 generation for this function on GCC/Clang
2629#if defined( COMPILER_GCC_CLANG )
2630__attribute__( ( target( "avx2" ) ) )
2631#endif
2632void strip_ansi_avx2( std::string& str ) {
2633 if ( str.empty() )
2634 return;
2635
2636 char* data = &str[0];
2637 const size_t len = str.size();
2638 size_t read_idx = 0;
2639 size_t write_idx = 0;
2640
2641 const __m256i esc_vec = _mm256_set1_epi8( '\x1B' );
2642
2643 // Process 32-byte chunks
2644 while ( read_idx + 32 <= len ) {
2645 __m256i chunk = _mm256_loadu_si256( reinterpret_cast<const __m256i*>( data + read_idx ) );
2646 __m256i cmp = _mm256_cmpeq_epi8( chunk, esc_vec );
2647 int mask = _mm256_movemask_epi8( cmp );
2648
2649 if ( likely( mask == 0 ) ) {
2650 if ( read_idx != write_idx ) {
2651 _mm256_storeu_si256( reinterpret_cast<__m256i*>( data + write_idx ), chunk );
2652 }
2653 read_idx += 32;
2654 write_idx += 32;
2655 } else {
2656 // Found ESC. Let the scalar loop handle the complexity of finding EXACT location
2657 // and parsing the variable length ANSI code.
2658 break;
2659 }
2660 }
2661
2662 // Finish remaining with Scalar Logic
2663 while ( read_idx < len ) {
2664 if ( unlikely( data[read_idx] == '\x1B' ) ) {
2665 if ( read_idx + 1 < len && data[read_idx + 1] == '[' ) {
2666 size_t scan = read_idx + 2;
2667 while ( scan < len ) {
2668 unsigned char c = static_cast<unsigned char>( data[scan] );
2669 if ( c >= 0x40 && c <= 0x7E ) {
2670 scan++;
2671 break;
2672 }
2673 scan++;
2674 }
2675 read_idx = scan;
2676 continue;
2677 }
2678 }
2679 if ( read_idx != write_idx ) {
2680 data[write_idx] = data[read_idx];
2681 }
2682 write_idx++;
2683 read_idx++;
2684 }
2685 str.resize( write_idx );
2686}
2687#endif // ARCH_X86

Callers

nothing calls this directly

Calls 3

emptyMethod · 0.45
sizeMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected