Class: Pcut::LineParser
- Inherits:
-
Object
- Object
- Pcut::LineParser
- Defined in:
- lib/pcut/line_parser.rb
Constant Summary collapse
- DOUBLE_QUOTE =
"\""
- SINGLE_QUOTE =
"'"
- QUOTES =
{ DOUBLE_QUOTE => [DOUBLE_QUOTE, DOUBLE_QUOTE], SINGLE_QUOTE => [SINGLE_QUOTE, SINGLE_QUOTE], "D" => [DOUBLE_QUOTE, DOUBLE_QUOTE], # shortcut "S" => [SINGLE_QUOTE, SINGLE_QUOTE], # shortcut "[" => ["[", "]"], "(" => ["(", ")"], "<" => ["<", ">"] }
- DELIMITER =
"\t"
Instance Attribute Summary collapse
-
#keep_quotes ⇒ Object
Returns the value of attribute keep_quotes.
-
#quote_guard ⇒ Object
Returns the value of attribute quote_guard.
-
#skip_continuous_delimiters ⇒ Object
Returns the value of attribute skip_continuous_delimiters.
Instance Method Summary collapse
-
#initialize ⇒ LineParser
constructor
A new instance of LineParser.
- #parse(str) ⇒ Object
- #set_delimiter(char) ⇒ Object
- #set_quote_guard(quote) ⇒ Object
Constructor Details
#initialize ⇒ LineParser
Returns a new instance of LineParser.
25 26 27 28 29 30 31 32 |
# File 'lib/pcut/line_parser.rb', line 25 def initialize @quote_guard = {} @delimiter = DELIMITER # do not delete quoting chars @keep_quotes = false # treat continuous delimiters as one delimiters @skip_continuous_delimiters = false end |
Instance Attribute Details
#keep_quotes ⇒ Object
Returns the value of attribute keep_quotes.
20 21 22 |
# File 'lib/pcut/line_parser.rb', line 20 def keep_quotes @keep_quotes end |
#quote_guard ⇒ Object
Returns the value of attribute quote_guard.
20 21 22 |
# File 'lib/pcut/line_parser.rb', line 20 def quote_guard @quote_guard end |
#skip_continuous_delimiters ⇒ Object
Returns the value of attribute skip_continuous_delimiters.
20 21 22 |
# File 'lib/pcut/line_parser.rb', line 20 def skip_continuous_delimiters @skip_continuous_delimiters end |
Instance Method Details
#parse(str) ⇒ Object
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 |
# File 'lib/pcut/line_parser.rb', line 48 def parse(str) buffer = nil previous_char = nil current_quote = nil finalizer = nil partials = [] str.each_char do |c| # skip continuous delimiters if skip_continuous_delimiters && @delimiter == c && @delimiter == previous_char next end # check starting quote if @quote_guard.include?(c) && previous_char != "\\" # not escaped starter = @quote_guard[c].first if !current_quote && c == starter current_quote = c finalizer = @quote_guard[c].last if @keep_quotes buffer ||= "" buffer += c end next end end # check end of quote if current_quote && c == finalizer current_quote = nil finalizer = nil if @keep_quotes buffer ||= "" buffer += c end next end if !current_quote && c == @delimiter partials << buffer buffer = nil elsif current_quote || c != @delimiter buffer ||= "" buffer += c end previous_char = c end partials << buffer if buffer partials end |
#set_delimiter(char) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/pcut/line_parser.rb', line 41 def set_delimiter(char) if !char.is_a?(String) || char.size != 1 raise ArgumentError, "invalid delimiter: #{char.to_s}" end @delimiter = char end |