Class: Antlr4::Runtime::Utils
- Inherits:
-
Object
- Object
- Antlr4::Runtime::Utils
- Defined in:
- lib/antlr4/runtime/utils.rb
Class Method Summary collapse
- .count(s, x) ⇒ Object
- .escape_whitespace(s, escape_spaces) ⇒ Object
- .expand_tabs(s, tab_size) ⇒ Object
- .newlines(n) ⇒ Object
- .num_non_nil(data) ⇒ Object
- .read_file(file_name, _encoding = nil) ⇒ Object
- .remove_all_elements(data, value) ⇒ Object
- .sequence(n, s) ⇒ Object
- .spaces(n) ⇒ Object
- .write_file(file_name, content, _encoding = nil) ⇒ Object
Class Method Details
.count(s, x) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/antlr4/runtime/utils.rb', line 107 def self.count(s, x) n = 0 i = 0 while i < s.length n += 1 if s[i] == x i += 1 end n end |
.escape_whitespace(s, escape_spaces) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/antlr4/runtime/utils.rb', line 22 def self.escape_whitespace(s, escape_spaces) buf = '' s.each_char do |c| buf << if c == ' ' && escape_spaces '\u00B7' elsif c == '\t' '\\t' elsif c == '\n' '\\n' elsif c == '\r' '\\r' else c end end buf end |
.expand_tabs(s, tab_size) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/antlr4/runtime/utils.rb', line 64 def self.(s, tab_size) return nil if s.nil? buf = '' col = 0 i = 0 while i < s.length c = s[i] case c when '\n' col = 0 buf << c when '\t' n = tab_size - col % tab_size col += n buf << spaces(n) else col += 1 buf << c end i += 1 end buf end |
.newlines(n) ⇒ Object
93 94 95 |
# File 'lib/antlr4/runtime/utils.rb', line 93 def self.newlines(n) sequence(n, "\n") end |
.num_non_nil(data) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/antlr4/runtime/utils.rb', line 3 def self.num_non_nil(data) n = 0 return n if data.nil? i = 0 while i < data.length o = datap[i] n += 1 unless o.nil? i += 1 end n end |
.read_file(file_name, _encoding = nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/antlr4/runtime/utils.rb', line 50 def self.read_file(file_name, _encoding = nil) f = File.new(file_name, 'r') size = File.size(file_name) begin data = Array.new(size) f.read(nil, data) ensure f.close end data end |
.remove_all_elements(data, value) ⇒ Object
16 17 18 19 20 |
# File 'lib/antlr4/runtime/utils.rb', line 16 def self.remove_all_elements(data, value) return if data.nil? data.remove(value) while data.contains(value) end |
.sequence(n, s) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/antlr4/runtime/utils.rb', line 97 def self.sequence(n, s) buf = '' sp = 1 while sp <= n buf << s sp += 1 end buf end |
.spaces(n) ⇒ Object
89 90 91 |
# File 'lib/antlr4/runtime/utils.rb', line 89 def self.spaces(n) sequence(n, ' ') end |
.write_file(file_name, content, _encoding = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/antlr4/runtime/utils.rb', line 40 def self.write_file(file_name, content, _encoding = nil) f = File.new(file_name, 'w') begin f << content ensure f.close end end |