Module: Spandx::Core::CsvParser

Defined in:
ext/spandx/spandx.c

Class Method Summary collapse

Class Method Details

.parse(line) ⇒ Object

“name”,“version”,“”rn



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'ext/spandx/spandx.c', line 14

VALUE parse(VALUE self, VALUE line)
{
  if (NIL_P(line)) return Qnil;

  char *p;

  p = RSTRING_PTR(line);
  if (*p != '"') return Qnil;

  const VALUE items = rb_ary_new2(3);
  const char *s, *n;
  const long len = RSTRING_LEN(line);
  enum { open, closed } state = closed;

  for (int i = 0; i < len && *p; i++) {
    if (*p == '"') {
      n = p;
      if (i < (len - 1)) *n++;

      if (state == closed) {
        s = n;
        state = open;
      } else if (state == open) {
        if (!*n || n == p || *n == ',' || *n == NEWLINE) {
          rb_ary_push(items, rb_str_new(s, p - s));
          state = closed;
        }
      }
    }
    *(p++);
  }

  return items;
}