diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2021-06-21 23:47:23 -0600 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2021-06-22 01:47:53 -0600 |
| commit | 384181b120e3cfce85f3b9254cd067e109063ffd (patch) | |
| tree | e92ce468add8ac7c737929caf67377a3944e7cc3 /lib | |
| parent | 90292213d092b6abea578eed94923e63ee17d925 (diff) | |
store quote in local variable when parsing CSV to avoid unnecessary string allocations
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/asciidoctor/table.rb | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/lib/asciidoctor/table.rb b/lib/asciidoctor/table.rb index bd580eb2..87b81c2b 100644 --- a/lib/asciidoctor/table.rb +++ b/lib/asciidoctor/table.rb @@ -535,12 +535,13 @@ class Table::ParserContext # # returns true if the buffer has unclosed quotes, false if it doesn't or it # isn't quoted data - def buffer_has_unclosed_quotes? append = nil - if (record = append ? (@buffer + append).strip : @buffer.strip) == '"' + def buffer_has_unclosed_quotes? append = nil, q = '"' + if (record = append ? (@buffer + append).strip : @buffer.strip) == q true - elsif record.start_with? '"' - if ((trailing_quote = record.end_with? '"') && (record.end_with? '""')) || (record.start_with? '""') - ((record = record.gsub '""', '').start_with? '"') && !(record.end_with? '"') + elsif record.start_with? q + qq = q + q + if ((trailing_quote = record.end_with? q) && (record.end_with? qq)) || (record.start_with? qq) + ((record = record.gsub qq, '').start_with? q) && !(record.end_with? q) else !trailing_quote end @@ -633,20 +634,20 @@ class Table::ParserContext @buffer = '' cellspec = nil repeat = 1 - if @format == 'csv' && !cell_text.empty? && cell_text.include?('"') + if @format == 'csv' && !cell_text.empty? && (cell_text.include? (q = '"')) # this may not be perfect logic, but it hits the 99% - if cell_text.start_with?('"') && cell_text.end_with?('"') + if (cell_text.start_with? q) && (cell_text.end_with? q) # unquote if (cell_text = cell_text.slice(1, cell_text.length - 2)) # trim whitespace and collapse escaped quotes - cell_text = cell_text.strip.squeeze('"') + cell_text = cell_text.strip.squeeze q else logger.error message_with_context 'unclosed quote in CSV data; setting cell to empty', source_location: @reader.cursor_at_prev_line cell_text = '' end else # collapse escaped quotes - cell_text = cell_text.squeeze('"') + cell_text = cell_text.squeeze q end end end |
