@@ -167,22 +167,9 @@ def history_file
167167    def  read_history_file 
168168      if  history  && File . exist? ( path  =  history_file ) 
169169        f  =  ( [ '' ,  'DAI-' ,  'CHU-' ,  'SHO-' ] . map { |e | e +'KICHI' } +[ 'KYO' ] ) . sample 
170-         begin 
171-           # Force UTF-8 encoding to handle history files with Unicode characters 
172-           lines  =  File . readlines ( path ,  encoding : 'UTF-8' ) 
173-           [ "#{ FH } #{ f }  " . dup ]  + lines 
174-         rescue  ArgumentError ,  Encoding ::UndefinedConversionError 
175-           # If UTF-8 reading fails, try with binary mode and force UTF-8 
176-           begin 
177-             lines  =  File . readlines ( path ,  mode : 'rb' ) . map  do  |line |
178-               line . force_encoding ( 'UTF-8' ) . scrub ( '?' ) 
179-             end 
180-             [ "#{ FH } #{ f }  " . dup ]  + lines 
181-           rescue 
182-             # If all encoding attempts fail, return empty history to avoid crash 
183-             [ "#{ FH } #{ f }  " . dup ] 
184-           end 
185-         end 
170+         # Read history file and scrub invalid characters to prevent encoding errors 
171+         lines  =  File . readlines ( path ) . map ( &:scrub ) 
172+         [ "#{ FH } #{ f }  " . dup ]  + lines 
186173      else 
187174        [ ] 
188175      end 
@@ -206,20 +193,12 @@ def deactivate
206193
207194        if  !added_records . empty?  && !path . empty? 
208195          orig_records  =  read_history_file 
209-           open ( history_file ,  'w' ,   encoding :  'UTF-8' ) { |f |
196+           open ( history_file ,  'w' ) { |f |
210197            ( orig_records  + added_records ) . last ( max ) . each { |line |
211-               begin 
212-                 # Ensure proper encoding before calling strip 
213-                 if  line . encoding  != Encoding ::UTF_8 
214-                   line  =  line . encode ( 'UTF-8' ,  invalid : :replace ,  undef : :replace ) 
215-                 end 
216-                 stripped_line  =  line . strip 
217-                 if  !line . start_with? ( FH )  && !stripped_line . empty? 
218-                   f . puts  stripped_line 
219-                 end 
220-               rescue  Encoding ::CompatibilityError ,  ArgumentError 
221-                 # Skip lines that cannot be properly encoded to avoid crashes 
222-                 next 
198+               # Use scrub to handle encoding issues gracefully 
199+               scrubbed_line  =  line . scrub . strip 
200+               if  !line . start_with? ( FH )  && !scrubbed_line . empty? 
201+                 f . puts  scrubbed_line 
223202              end 
224203            } 
225204          } 
@@ -229,17 +208,9 @@ def deactivate
229208
230209    def  load_history 
231210      read_history_file . each { |line |
232-         begin 
233-           # Ensure proper encoding before calling strip! 
234-           if  line . encoding  != Encoding ::UTF_8 
235-             line  =  line . encode ( 'UTF-8' ,  invalid : :replace ,  undef : :replace ) 
236-           end 
237-           line . strip! 
238-           history  << line  unless  line . empty? 
239-         rescue  Encoding ::CompatibilityError ,  ArgumentError 
240-           # Skip lines that cannot be properly encoded to avoid crashes 
241-           next 
242-         end 
211+         # Use scrub to handle encoding issues gracefully, then strip 
212+         line . scrub . strip! 
213+         history  << line  unless  line . empty? 
243214      }  if  history . empty? 
244215      history . count 
245216    end 
0 commit comments