Module: AtCoderFriends::Parser::InputFormatUtils

Includes:
InputFormatConstants
Included in:
InputFormat, InputFormatMatcher
Defined in:
lib/at_coder_friends/parser/input_format.rb

Overview

utilities for input format parser

Constant Summary

Constants included from InputFormatConstants

AtCoderFriends::Parser::InputFormatConstants::ADD_TAG, AtCoderFriends::Parser::InputFormatConstants::DELIMS, AtCoderFriends::Parser::InputFormatConstants::DIMENSION_TBL, AtCoderFriends::Parser::InputFormatConstants::RE_0, AtCoderFriends::Parser::InputFormatConstants::RE_00, AtCoderFriends::Parser::InputFormatConstants::RE_99, AtCoderFriends::Parser::InputFormatConstants::RE_BLOCK, AtCoderFriends::Parser::InputFormatConstants::RE_ITEM, AtCoderFriends::Parser::InputFormatConstants::RE_IX, AtCoderFriends::Parser::InputFormatConstants::RE_IX_0, AtCoderFriends::Parser::InputFormatConstants::RE_IX_00, AtCoderFriends::Parser::InputFormatConstants::RE_IX_99, AtCoderFriends::Parser::InputFormatConstants::RE_SINGLE, AtCoderFriends::Parser::InputFormatConstants::RE_SZ, AtCoderFriends::Parser::InputFormatConstants::RE_SZ2_0, AtCoderFriends::Parser::InputFormatConstants::RE_SZ2_REF, AtCoderFriends::Parser::InputFormatConstants::RE_SZ_0, AtCoderFriends::Parser::InputFormatConstants::RE_SZ_00, AtCoderFriends::Parser::InputFormatConstants::RE_SZ_99, AtCoderFriends::Parser::InputFormatConstants::RE_SZ_REF, AtCoderFriends::Parser::InputFormatConstants::SECTIONS, AtCoderFriends::Parser::InputFormatConstants::TO_SUFFIX, AtCoderFriends::Parser::InputFormatConstants::TO_SUFFIX_STR

Instance Method Summary collapse

Instance Method Details

#extract_delim(str) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/at_coder_friends/parser/input_format.rb', line 86

def extract_delim(str)
  # a-b, a/b, a:b -> a b
  str = str.dup
  dlms =
    DELIMS.select { |c| str.gsub!(/#{c}(#{RE_SINGLE})/, ' \1') }.join
  [str, dlms]
end

#normalize_fmt(str) ⇒ Object

1) &npsp;, fill-width space -> half width space 2) j->i,j for nested {}



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/at_coder_friends/parser/input_format.rb', line 46

def normalize_fmt(str)
  str
    .tr('0-9A-Za-z', '0-9A-Za-z')
    .gsub(/[[:space:]]/) { |c| c.gsub(/[^\n]/, ' ') } # 1)
    .gsub(%r{<var>([^<>]+)</var>}i, '\1') # <sub><var>N</var></sub>
    .gsub(%r{<sup>([^<>]+)</sup>}i, '^\1')
    .gsub(%r{<sub>([^<>]+)</sub>}i, '_{\1}')
    .gsub(%r{<sub>([^<>]+)</sub>}i, '_{\1}') # for nested<sub>
    .gsub(/<("[^"]*"|'[^']*'|[^'"<>])*>/, '')
    .gsub('&amp;', '&')
    .gsub('&gt;', '>')
    .gsub('&lt;', '<')
    .gsub('\\ ', ' ')
    .gsub('\\(', '')
    .gsub('\\)', '')
    .gsub('\\lvert', '|')
    .gsub('\\rvert', '|')
    .gsub('\\times', '*')
    .gsub(/\\begin(\{[^{}]*\})*/, '')
    .gsub(/\\end(\{[^{}]*\})*/, '')
    .gsub(/\\hspace(\{[^{}]*\})*/, ' ')
    .gsub(/\\mathrm\{([^{}]*)\}/, '\1')
    .gsub(/\\mathit\{([^{}]*)\}/, '\1')
    .gsub(/\\textrm\{([^{}]*)\}/, '\1')
    .gsub(/\\text\{([^{}]*)\}/, '\1')
    .gsub(/\\rm\{([^{}]*)\}/, '\1')
    .gsub('\\rm ', ' ')
    .gsub(/\\[cdlv]?dots/, '..')
    .gsub(/\{\}/, ' ')
    .gsub('−', '-') # full width hyphen
    .gsub(/[・.:‥⋮︙…]+/, '..')
    .gsub(/[\\$']/, '') # s' -> s
    .gsub(/[&~|]/, ' ') # |S| -> S
    .gsub(/^\s*[.:][\s.:]*$/, '..')
    .tr('()', '{}')
    .gsub(/#{RE_BLOCK}/) { |w| w.delete(' ') } # 2)
    .split("\n")
    .map(&:strip)
end

#normalize_name(s) ⇒ Object



94
95
96
# File 'lib/at_coder_friends/parser/input_format.rb', line 94

def normalize_name(s)
  s.delete('{},').gsub(/(\A_+|_+\z)/, '')
end

#normalize_names(names) ⇒ Object



98
99
100
# File 'lib/at_coder_friends/parser/input_format.rb', line 98

def normalize_names(names)
  names.map { |nm| normalize_name(nm) }
end

#normalize_size(container, size, ix0) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/at_coder_friends/parser/input_format.rb', line 102

def normalize_size(container, size, ix0)
  sz = size_array(container, size)
  sz0 = size_array(container, ix0)

  sz.map.with_index do |s, i|
    if sz0[i] == '0'
      # 0 -> 1,  N-1 -> N, N-2 -> N-1 if 0 origin
      s.gsub(/\A0\z/, '1').gsub(/-1\z/, '').gsub(/-2\z/, '-1')
    else
      s
    end
  end
end

#size_array(container, size) ⇒ Object

split size by container dimension



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/at_coder_friends/parser/input_format.rb', line 117

def size_array(container, size)
  (
    case DIMENSION_TBL[container]
    when 2
      split_size(size)
    when 1
      [size]
    when 0
      []
    end
  ).map { |s| normalize_name(s) }
end

#split_size(str) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/at_coder_friends/parser/input_format.rb', line 130

def split_size(str)
  str = str.gsub(/(\A\{|\}\z)/, '') while str =~ /\A#{RE_BLOCK}\z/

  sz = str.split(',')
  return sz if sz.size == 2

  sz = str.scan(/(?<nbl>[^{}]+)|#{RE_BLOCK}/).flatten.compact
  return sz if sz.size == 2

  str = str.delete('{},')

  sz = str.scan(/[^_](?:_[^_])?/)
  return sz if sz.size == 2

  sz = str.split('_')
  return sz if sz.size == 2

  [str[0] || '_', str[1..] || '_']
end