Class: FileCharLicker::MbLicker

Inherits:
Licker
  • Object
show all
Defined in:
lib/file_char_licker/licker/mb_licker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Licker

#backward_lines, #current_line, #forward_lines, #seek_contiguous_max, #seek_contiguous_min

Constructor Details

#initialize(file, encoding = 'utf-8') ⇒ MbLicker

Returns a new instance of MbLicker.



7
8
9
10
11
12
13
# File 'lib/file_char_licker/licker/mb_licker.rb', line 7

def initialize(file, encoding = 'utf-8')

  super(file)
  init_encoding_variables(encoding)

  @mb_bytesize_max = 6
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



5
6
7
# File 'lib/file_char_licker/licker/mb_licker.rb', line 5

def encoding
  @encoding
end

#kconv_checkerObject (readonly)

Returns the value of attribute kconv_checker.



5
6
7
# File 'lib/file_char_licker/licker/mb_licker.rb', line 5

def kconv_checker
  @kconv_checker
end

#mb_bytesize_maxObject

Returns the value of attribute mb_bytesize_max.



4
5
6
# File 'lib/file_char_licker/licker/mb_licker.rb', line 4

def mb_bytesize_max
  @mb_bytesize_max
end

#nkf_optionObject (readonly)

Returns the value of attribute nkf_option.



5
6
7
# File 'lib/file_char_licker/licker/mb_licker.rb', line 5

def nkf_option
  @nkf_option
end

Instance Method Details

#around_lines(*args) ⇒ Object

get lines around for passed file#pos



16
17
18
19
20
# File 'lib/file_char_licker/licker/mb_licker.rb', line 16

def around_lines(*args)

  lines = super(*args)
  NKF.nkf(@nkf_option, lines)
end

#backward_charObject

get a backword character from file#pos

instance variables

mb_bytesize_max ... max bytesize for multibyte character

args

update_flag     ... if true, update file#pos for backward character's head

returner

String object ... exists
          nil ... not exists


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/file_char_licker/licker/mb_licker.rb', line 33

def backward_char

  file    = @file
  pos_max = file.pos - 1
  pos_min = pos_max - @mb_bytesize_max

  pos_max.downto(pos_min) do |pos|

    break if pos < 0

    file.seek(pos)
    char = file.getc

    # return file#getc character
    # - when that is regular for multibyte character
    return char if check_mb(char)
  end

  nil
end

#seek_line_headObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/file_char_licker/licker/mb_licker.rb', line 54

def seek_line_head

  file = @file

  # loop
  # - move pointer until reach to EOL of before line.
  while file.pos > 0

    char = backward_char

    # break
    # - if did not get backward character
    # - if got character is a line break character
    break if char.nil? || char.match(/[\r\n]/)

    # backward pos as bytesize of char
    file.seek(-(char.bytesize), IO::SEEK_CUR)
  end

  file.pos
end