Module: Kward::NativeShell::Braces
- Defined in:
- lib/kward/shell/native_braces.rb
Overview
Brace expansion operates on source word parts, before any substitutions. Quoted braces and braces produced by variables remain literal.
Constant Summary collapse
- LIMIT =
4_096
Class Method Summary collapse
- .coalesce(parts) ⇒ Object
- .expand(word) ⇒ Object
- .expand_atoms(atoms, depth) ⇒ Object
- .literal?(part, value) ⇒ Boolean
- .sequence(parts) ⇒ Object
Class Method Details
.coalesce(parts) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/kward/shell/native_braces.rb', line 84 def coalesce(parts) parts.each_with_object([]) do |part, result| if part.kind == :literal && result.last&.kind == :literal && result.last.quoted == part.quoted previous = result.pop result << Part.new(kind: :literal, text: previous.text + part.text, quoted: part.quoted) else result << part end end end |
.expand(word) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/kward/shell/native_braces.rb', line 9 def (word) atoms = word.parts.flat_map do |part| if part.kind == :literal && !part.text.empty? part.text.chars.map { |text| Part.new(kind: :literal, text: text, quoted: part.quoted) } else [part] end end (atoms, 0).map { |parts| Word.new(parts: coalesce(parts)) } end |
.expand_atoms(atoms, depth) ⇒ Object
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 |
# File 'lib/kward/shell/native_braces.rb', line 24 def (atoms, depth) raise ExpansionError, "brace nesting exceeds 32 levels" if depth > 32 atoms.each_index do |start| next unless literal?(atoms[start], "{") level = 0 splits = [] finish = nil ((start + 1)...atoms.length).each do |index| if literal?(atoms[index], "{") level += 1 elsif literal?(atoms[index], "}") if level.zero? finish = index break end level -= 1 elsif level.zero? && literal?(atoms[index], ",") splits << index end end next unless finish alternatives = if splits.empty? sequence(atoms[(start + 1)...finish]) else ([start] + splits).zip(splits + [finish]).map { |left, right| atoms[(left + 1)...right] } end next unless alternatives result = [] alternatives.each do |alternative| result.concat((atoms[0...start] + alternative + atoms[(finish + 1)..], depth + 1)) raise ExpansionError, "brace expansion exceeds #{LIMIT} words" if result.length > LIMIT end return result end [atoms] end |
.literal?(part, value) ⇒ Boolean
20 21 22 |
# File 'lib/kward/shell/native_braces.rb', line 20 def literal?(part, value) part&.kind == :literal && !part.quoted && part.text == value end |
.sequence(parts) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/kward/shell/native_braces.rb', line 61 def sequence(parts) return unless parts.all? { |part| part.kind == :literal && !part.quoted } text = parts.map(&:text).join match = text.match(/\A(-?\d+|[a-zA-Z])\.\.(-?\d+|[a-zA-Z])(?:\.\.(-?\d+))?\z/) return unless match first, last, step = match.captures raise ExpansionError, "brace sequence exceeds numeric limit" if [first, last, step].compact.any? { |value| value.length > 128 } numeric = first.match?(/\A-?\d+\z/) && last.match?(/\A-?\d+\z/) return unless numeric || first.length == 1 && last.length == 1 && first.match?(/[a-zA-Z]/) && last.match?(/[a-zA-Z]/) from, to = numeric ? [first.to_i, last.to_i] : [first.ord, last.ord] stride = step ? step.to_i.abs : 1 stride = 1 if stride.zero? stride = -stride if from > to count = (to - from).abs / stride.abs + 1 raise ExpansionError, "brace expansion exceeds #{LIMIT} words" if count > LIMIT padded = numeric && [first, last].any? { |value| value.match?(/\A-?0\d/) } width = [first.length, last.length].max from.step(to, stride).map do |value| text = numeric ? (padded ? format("%0#{width}d", value) : value.to_s) : value.chr [Part.new(kind: :literal, text: text, quoted: false)] end end |