@@ -4,7 +4,7 @@ use std::sync::mpsc::{Receiver, Sender, TryRecvError};
44use std:: thread:: { self , JoinHandle } ;
55
66use super :: messages:: * ;
7- use crate :: core:: calc_engine;
7+ use crate :: core:: { bit_ops , calc_engine} ;
88
99/// Backend processor that handles all computation requests.
1010pub struct BackendWorker ;
@@ -229,105 +229,80 @@ impl BackendWorker {
229229 match req. operation {
230230 BitViewerOperation :: ParseHex => {
231231 let hex_input = req. hex_input . unwrap_or_default ( ) ;
232- let clean_hex: String = hex_input
233- . chars ( )
234- . filter ( |c| c. is_ascii_hexdigit ( ) )
235- . collect :: < String > ( )
236- . to_uppercase ( ) ;
237-
238- if clean_hex. is_empty ( ) {
239- return BitViewerResponse {
232+ match bit_ops:: parse_hex_input ( & hex_input) {
233+ Ok ( parsed) => BitViewerResponse {
240234 id : req. id ,
241- hex_input : String :: new ( ) ,
242- binary_bits : Vec :: new ( ) ,
243- error : Some ( "输入为空" . to_string ( ) ) ,
244- } ;
245- }
246-
247- // Validate hex characters
248- if !clean_hex. chars ( ) . all ( |c| c. is_ascii_hexdigit ( ) ) {
249- return BitViewerResponse {
235+ hex_input : parsed. normalized_hex ,
236+ binary_bits : bit_ops:: bit_string_to_bits ( & parsed. bit_string )
237+ . unwrap_or_default ( ) ,
238+ error : None ,
239+ } ,
240+ Err ( error) => BitViewerResponse {
250241 id : req. id ,
251242 hex_input,
252243 binary_bits : Vec :: new ( ) ,
253- error : Some ( "无效的十六进制字符" . to_string ( ) ) ,
254- } ;
255- }
256-
257- let mut binary_bits = Vec :: new ( ) ;
258- for hex_char in clean_hex. chars ( ) {
259- if let Some ( digit) = hex_char. to_digit ( 16 ) {
260- let digit = digit as u8 ;
261- for i in ( 0 ..4 ) . rev ( ) {
262- binary_bits. push ( ( digit & ( 1 << i) ) != 0 ) ;
263- }
264- }
244+ error : Some ( error) ,
245+ } ,
265246 }
247+ }
248+ BitViewerOperation :: ToggleBit ( index) => {
249+ let current_bits = req. current_bits . unwrap_or_default ( ) ;
250+ let bit_string = bit_ops:: bits_to_bit_string ( & current_bits) ;
251+ let updated_bit_string =
252+ bit_ops:: toggle_bit ( & bit_string, index) . unwrap_or ( bit_string) ;
266253
267254 BitViewerResponse {
268255 id : req. id ,
269- hex_input : clean_hex,
270- binary_bits,
256+ hex_input : bit_ops:: bit_string_to_hex ( & updated_bit_string) . unwrap_or_default ( ) ,
257+ binary_bits : bit_ops:: bit_string_to_bits ( & updated_bit_string)
258+ . unwrap_or_default ( ) ,
271259 error : None ,
272260 }
273261 }
274- BitViewerOperation :: ToggleBit ( index) => {
275- let mut bits = req. current_bits . unwrap_or_default ( ) ;
276- if index < bits. len ( ) {
277- bits[ index] = !bits[ index] ;
278- }
279- let hex_input = Self :: bits_to_hex ( & bits) ;
262+ BitViewerOperation :: InvertAll => {
263+ let current_bits = req. current_bits . unwrap_or_default ( ) ;
264+ let bit_string = bit_ops:: bits_to_bit_string ( & current_bits) ;
265+ let updated_bit_string = bit_ops:: invert_all ( & bit_string) . unwrap_or ( bit_string) ;
280266
281267 BitViewerResponse {
282268 id : req. id ,
283- hex_input,
284- binary_bits : bits,
269+ hex_input : bit_ops:: bit_string_to_hex ( & updated_bit_string) . unwrap_or_default ( ) ,
270+ binary_bits : bit_ops:: bit_string_to_bits ( & updated_bit_string)
271+ . unwrap_or_default ( ) ,
285272 error : None ,
286273 }
287274 }
288- BitViewerOperation :: InvertAll => {
289- let mut bits = req. current_bits . unwrap_or_default ( ) ;
290- for bit in & mut bits {
291- * bit = ! * bit ;
292- }
293- let hex_input = Self :: bits_to_hex ( & bits ) ;
275+ BitViewerOperation :: ShiftLeft ( count ) => {
276+ let current_bits = req. current_bits . unwrap_or_default ( ) ;
277+ let bit_string = bit_ops :: bits_to_bit_string ( & current_bits ) ;
278+ let updated_bit_string =
279+ bit_ops :: shift_left ( & bit_string , count , bit_ops :: ShiftMode :: ZeroFill )
280+ . unwrap_or ( bit_string ) ;
294281
295282 BitViewerResponse {
296283 id : req. id ,
297- hex_input,
298- binary_bits : bits,
284+ hex_input : bit_ops:: bit_string_to_hex ( & updated_bit_string) . unwrap_or_default ( ) ,
285+ binary_bits : bit_ops:: bit_string_to_bits ( & updated_bit_string)
286+ . unwrap_or_default ( ) ,
299287 error : None ,
300288 }
301289 }
302- }
303- }
304-
305- fn bits_to_hex ( bits : & [ bool ] ) -> String {
306- if bits. is_empty ( ) {
307- return String :: new ( ) ;
308- }
309-
310- let mut hex_string = String :: new ( ) ;
311- let mut current_nibble = 0u8 ;
290+ BitViewerOperation :: ShiftRight ( count) => {
291+ let current_bits = req. current_bits . unwrap_or_default ( ) ;
292+ let bit_string = bit_ops:: bits_to_bit_string ( & current_bits) ;
293+ let updated_bit_string =
294+ bit_ops:: shift_right ( & bit_string, count, bit_ops:: ShiftMode :: ZeroFill )
295+ . unwrap_or ( bit_string) ;
312296
313- for ( i, & bit) in bits. iter ( ) . enumerate ( ) {
314- let bit_pos = 3 - ( i % 4 ) ;
315- if bit {
316- current_nibble |= 1 << bit_pos;
317- }
318-
319- if ( i + 1 ) % 4 == 0 {
320- hex_string. push_str ( & format ! ( "{:X}" , current_nibble) ) ;
321- current_nibble = 0 ;
297+ BitViewerResponse {
298+ id : req. id ,
299+ hex_input : bit_ops:: bit_string_to_hex ( & updated_bit_string) . unwrap_or_default ( ) ,
300+ binary_bits : bit_ops:: bit_string_to_bits ( & updated_bit_string)
301+ . unwrap_or_default ( ) ,
302+ error : None ,
303+ }
322304 }
323305 }
324-
325- // Handle incomplete last nibble
326- if !bits. len ( ) . is_multiple_of ( 4 ) {
327- hex_string. push_str ( & format ! ( "{:X}" , current_nibble) ) ;
328- }
329-
330- hex_string
331306 }
332307
333308 fn handle_calculator ( req : CalculatorRequest ) -> CalculatorResponse {
0 commit comments