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 /ext | |
| parent | 1cb0826d4b4071b9c5aac85927bbffd76b5d3dcf (diff) | |
play with pointers
Diffstat (limited to 'ext')
| -rw-r--r-- | ext/spandx/spandx.c | 44 |
1 files changed, 20 insertions, 24 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; |
