diff options
| author | mo khan <mo.khan@gmail.com> | 2020-05-14 21:09:15 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-05-14 21:09:15 -0600 |
| commit | ee021d5bdb9b35e19eb386e17e81f054a00397e9 (patch) | |
| tree | 6cf9408b9445b69253005f49341cad6db957abe2 | |
| parent | 1cb0826d4b4071b9c5aac85927bbffd76b5d3dcf (diff) | |
play with pointers
| -rw-r--r-- | ext/spandx/spandx.c | 44 | ||||
| -rw-r--r-- | lib/spandx/core/index_file.rb | 6 | ||||
| -rw-r--r-- | lib/spandx/core/relation.rb | 2 | ||||
| -rw-r--r-- | spec/unit/core/csv_parser_spec.rb | 7 |
4 files changed, 29 insertions, 30 deletions
diff --git a/ext/spandx/spandx.c b/ext/spandx/spandx.c index f4cd539..f767570 100644 --- a/ext/spandx/spandx.c +++ b/ext/spandx/spandx.c @@ -11,36 +11,32 @@ VALUE rb_mCsvParser; // "name","version",""\r\n static VALUE parse(VALUE self, VALUE line) { + if (NIL_P(line)) return Qnil; + + char *p = RSTRING_PTR(line); + if (*p != '"') return Qnil; + const VALUE items = rb_ary_new2(3); - const char *p, *s, *t; - const int line_length = RSTRING_LEN(line); + const char *s, *n; + const int len = RSTRING_LEN(line); enum { open, closed } state = closed; - p = RSTRING_PTR(line); - - if (*p != '"') return Qnil; + for (int i = 0; i < len && *p; i++) { + if (*p == '"') { + n = p; + if (i < (len - 1)) *n++; - for (int i = 0; i < line_length; i++) { - switch(*p) { - case '"': - if (state == closed) { - state = open; - p++; - s = p; - } else { - t = p; - t++; - if (!*t || *t == 10 || *t == 13 || *t == ',') { - rb_ary_push(items, rb_str_new(s, p - s)); - p = t; - state = closed; - } + if (state == closed) { + s = n; + state = open; + } else if (state == open) { + if (!*n || n == p || *n == ',' || *n == 10) { + rb_ary_push(items, rb_str_new(s, p - s)); + state = closed; } - break; - default: - p++; - break; + } } + *p++; } return items; diff --git a/lib/spandx/core/index_file.rb b/lib/spandx/core/index_file.rb index 29c66f5..ecb15fa 100644 --- a/lib/spandx/core/index_file.rb +++ b/lib/spandx/core/index_file.rb @@ -82,14 +82,14 @@ module Spandx end def load - return load_index_from_data_file unless path.exist? + return build_index_from_data_file unless path.exist? load_index_from_gzip_index_file rescue Zlib::GzipFile::Error - load_index_from_data_file + build_index_from_data_file end - def load_index_from_data_file + def build_index_from_data_file data_file.open_file { |io| lines_in(io) } end diff --git a/lib/spandx/core/relation.rb b/lib/spandx/core/relation.rb index 6d385fb..a8bd2fe 100644 --- a/lib/spandx/core/relation.rb +++ b/lib/spandx/core/relation.rb @@ -28,7 +28,7 @@ module Spandx private def parse_row(line) - CsvParser.parse(line) # || CSV.parse(line)[0] + CsvParser.parse(line) end end end diff --git a/spec/unit/core/csv_parser_spec.rb b/spec/unit/core/csv_parser_spec.rb index 84edad7..8aad98f 100644 --- a/spec/unit/core/csv_parser_spec.rb +++ b/spec/unit/core/csv_parser_spec.rb @@ -35,9 +35,12 @@ RSpec.describe Spandx::Core::CsvParser do end context 'when parsing an invalid line of csv' do + specify { expect(described_class.parse(nil)).to be_nil } + specify { expect(described_class.parse('"","",""')).to eql(['', '', '']) } + specify { expect(described_class.parse('"","0.0.0",""')).to eql(['', '0.0.0', '']) } + specify { expect(described_class.parse('"hello O"world","0.1.0"')).to eql(['hello O"world', '0.1.0']) } + specify { expect(described_class.parse('"hello O"world","0.1.0",""')).to eql(['hello O"world', '0.1.0', '']) } specify { expect(described_class.parse('invalid","3.3.8.12",""')).to be_nil } - specify { expect(described_class.parse('"hello O\"world","0.1.0"')).to be_nil } - specify { expect(described_class.parse('"hello O\"world","0.1.0",""')).to be_nil } end end end |
