Method: RubyRTF::Parser#parse_control

Defined in:
lib/ruby-rtf/parser.rb

#parse_control(src, current_pos = 0) ⇒ String, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses a control switch

Parameters:

  • src (String)

    The fragment to parse

  • current_pos (Integer) (defaults to: 0)

    The position in string the control starts at (after the )

Returns:

  • (String, String|Integer, Integer)

    The name, optional control value and the new current position



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ruby-rtf/parser.rb', line 76

def parse_control(src, current_pos = 0)
  ctrl = ''
  val = nil

  max_len = src.length
  start = current_pos

  # handle hex special
  if src[current_pos] == "'"
    val = src[(current_pos + 1), 2].hex.chr
    current_pos += 3
    return [:hex, val, current_pos]
  end

  while (true)
    break if current_pos >= max_len
    break if STOP_CHARS.include?(src[current_pos])

    current_pos += 1
  end
  return [src[current_pos].to_sym, nil, current_pos + 1] if start == current_pos

  contents = src[start, current_pos - start]
  m = contents.match(/([\*a-z]+)(\-?\d+)?\*?/)
  ctrl = m[1].to_sym
  val = m[2].to_i unless m[2].nil?

  # we advance past the optional space if present
  current_pos += 1 if src[current_pos] == ' '

  [ctrl, val, current_pos]
end