ä¿®æ£ç
ã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹ãã§ãã¯ãããã®ãã¸ãã¯ãã¹ããã£ãã®ã§ä¿®æ£ããçã
ãã®ä»ãã«ã¼ãé¨åãã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹é¨åã®ãã§ãã¯ã®è¨è¿°æ¹æ³ãå¤æ´ããã
def jis_couner(str) esc_flag = false esc_string = "" wide_byte_flag = false wide_first_byte_flag = false count = 0 # JIS ã§ã®æåæ°ã«ã¦ã³ã # æåæ°ã«ã¦ã³ãå¦ç(ãã¤ãã®é åã«ãã¦ã«ã¼ãããã) str.to_s.unpack('C*').each_index do |index| # æ¹è¡ã³ã¼ãã¯é¤å¤ next if str[index] == 0x0a # 2ãã¤ãæåãã©ã°ããã£ã¦ã1ãã¤ãç®ãééãã¦ããå ´å if wide_byte_flag && wide_first_byte_flag && (str[index].to_i >= 0x21 && str[index].to_i <= 0x7E) count += 1 wide_first_byte_flag = false elsif wide_byte_flag && (str[index].to_i >= 0x21 && str[index].to_i <= 0x7E) # 2ãã¤ãæåãã©ã°ããã£ã¦ã1ãã¤ãç®ãééãã¦ããªãå ´å wide_first_byte_flag = true # 1byte ç®ééãã©ã° on else count += 1 end # ã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹ãã§ã㯠if str[index] == 0x1b or esc_flag esc_flag = true esc_string << str[index] # 2ãã¤ãæåã«ã¦ã³ããã©ã°ã off wide_byte_flag = false end # ã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹ãï¼æåã«éããæç¹ã§è©ä¾¡ãã if esc_string.size == 3 # ã¨ã¹ã±ã¼ãæååããã§ãã¯ãã # 1ãã¤ãæå if esc_string =~ /(\x1b\x28\x42)|(\x1b\x28\x4A)|(\x1b\x28\x49)/ # 2ãã¤ãæåã«ã¦ã³ããã©ã°ã off wide_byte_flag = false # å¶å¾¡ã³ã¼ãåã®æåæ°ããã¤ãã¹ãã count -= 3 elsif esc_string =~ /(\x1b\x24\x40)|(\x1b\x24\x42)|(\x1b\x24\x44)/ # 2ãã¤ãæåã«ã¦ã³ããã©ã°ã on wide_byte_flag = true # å¶å¾¡ã³ã¼ãåã®æåæ°ããã¤ãã¹ãã count -= 3 end # åæå esc_string = "" esc_flag = false end end return count end file = [] File.open('./text.txt').each do |f| file << f end puts jis_couner(file.join("\n"))