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
- #extract_delim(str) ⇒ Object
-
#normalize_fmt(str) ⇒ Object
1) &npsp;, fill-width space -> half width space 2) j->i,j for nested {}.
- #normalize_name(s) ⇒ Object
- #normalize_names(names) ⇒ Object
- #normalize_size(container, size, ix0) ⇒ Object
-
#size_array(container, size) ⇒ Object
split size by container dimension.
- #split_size(str) ⇒ Object
Instance Method Details
#extract_delim(str) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/at_coder_friends/parser/input_format.rb', line 82 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 |
# 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('&', '&') .gsub('>', '>') .gsub('<', '<') .gsub('\\ ', ' ') .gsub(/\\hspace\{\d+pt\}/, ' ') .gsub('\\(', '') .gsub('\\)', '') .gsub('\\lvert', '|') .gsub('\\rvert', '|') .gsub('\\mathit', '') .gsub('\\mathrm', '') .gsub('\\times', '*') .gsub(/\\begin(\{[^{}]*\})*/, '') .gsub(/\\end(\{[^{}]*\})*/, '') .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
90 91 92 |
# File 'lib/at_coder_friends/parser/input_format.rb', line 90 def normalize_name(s) s.delete('{},').gsub(/(\A_+|_+\z)/, '') end |
#normalize_names(names) ⇒ Object
94 95 96 |
# File 'lib/at_coder_friends/parser/input_format.rb', line 94 def normalize_names(names) names.map { |nm| normalize_name(nm) } end |
#normalize_size(container, size, ix0) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/at_coder_friends/parser/input_format.rb', line 98 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
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/at_coder_friends/parser/input_format.rb', line 113 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/at_coder_friends/parser/input_format.rb', line 126 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..-1] || '_'] end |