Class: Numerals::Format::TextNotation
- Defined in:
- lib/numerals/format/notations/text.rb
Defined Under Namespace
Classes: OutputSize
Instance Attribute Summary
Attributes inherited from Notation
Instance Method Summary collapse
Methods inherited from Notation
Constructor Details
This class inherits a constructor from Numerals::Format::Notation
Instance Method Details
#assemble(output, text_parts) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/numerals/format/notations/text.rb', line 7 def assemble(output, text_parts) if text_parts.special? output << text_parts.special else if format.symbols.padding.padded? output_size = OutputSize.new assemble_parts(output_size, text_parts) left_padding, internal_padding, right_padding = format.symbols.paddings(output_size.size) end assemble_parts(output, text_parts, left_padding, internal_padding, right_padding) end end |
#disassemble(text) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/numerals/format/notations/text.rb', line 20 def disassemble(text) text_parts = TextParts.new s = format.symbols special = / \A #{s.regexp(:plus, :minus, case_sensitivity: true)}? \s* #{s.regexp(:nan, :infinity, case_sensitivity: true)} \Z /x if match = special.match(text) valid = true text_parts.special = "#{match[1]}#{match[2]}" else valid = true base = format.significand_base # TODO: replace numbered groups by named variables ?<var> regular = / \A #{s.regexp(:fill, no_capture: true, optional: true, multiple: true)} #{s.regexp(:plus, :minus)}? \s* #{s.regexp(:fill, no_capture: true, optional: true, multiple: true)} #{s.regexp(:base_prefix, no_capture: true, optional: true)} (?: (?:(#{s.regexp(:grouped_digits, base: base, no_capture: true)}+)#{s.regexp(:point)}?) | #{s.regexp(:point)} # admit empty integer part, but then a point is needed ) (#{s.regexp(:digits, base: base, no_capture: true)}*) (?:#{s.regexp(:repeat_begin)}(#{s.regexp(:digits, base: base, no_capture: true)}+)#{s.regexp(:repeat_end)})? #{s.regexp(:repeat_suffix)}? #{s.regexp(:base_suffix, no_capture: true, optional: true)} (?:#{s.regexp(:exponent)}#{s.regexp(:plus, :minus)}?(\d+))? #{s.regexp(:fill, no_capture: true, optional: true, multiple: true)} \Z /x unless s.case_sensitive? regular = Regexp.new(regular.source, regular. | Regexp::IGNORECASE) end match = regular.match(text) if match.nil? valid = false else # TODO: we could avoid capturing point, point_with_no_integer_part sign = match[1] integer_part = match[2] point = match[3] point_with_no_integer_part = match[4] fractional_part = match[5] repeat_begin = match[6] repeat_part = match[7] repeat_end = match[8] repeat_suffix = match[9] exponent = match[10] exponent_sign = match[11] exponent_value = match[12] text_parts.sign = sign text_parts.integer = integer_part text_parts.fractional = fractional_part if repeat_begin if !repeat_part || !repeat_end || repeat_suffix valid = false end text_parts.repeat = repeat_part else if repeat_part || repeat_end valid = false end if repeat_suffix text_parts.detect_repeat = true end end text_parts.exponent_base = format.base if exponent if !exponent_value valid = false end text_parts.exponent = "#{exponent_sign}#{exponent_value}" text_parts.exponent_value = text_parts.exponent.to_i else if exponent_sign || exponent_value valid = false end end end end raise "Invalid text numeral" unless valid text_parts end |