Returns the next comma (not inside a quote) in the specified array. @param c array to search @param index offset at which to start looking @return index of the comma, or -1 if line ended inside an unclosed quote
()
| 649 | * @return index of the comma, or -1 if line ended inside an unclosed quote |
| 650 | */ |
| 651 | protected boolean ingest() { |
| 652 | boolean hasEscapedQuotes = false; |
| 653 | // not possible |
| 654 | // if (index == c.length) { // we're already at the end |
| 655 | // return c.length; |
| 656 | // } |
| 657 | boolean quoted = c[start] == '\"'; |
| 658 | if (quoted) { |
| 659 | start++; // step over the quote |
| 660 | } |
| 661 | int i = start; |
| 662 | while (i < c.length) { |
| 663 | // PApplet.println(c[i] + " i=" + i); |
| 664 | if (c[i] == '\"') { |
| 665 | // if this fella started with a quote |
| 666 | if (quoted) { |
| 667 | if (i == c.length-1) { |
| 668 | // closing quote for field; last field on the line |
| 669 | addPiece(start, i, hasEscapedQuotes); |
| 670 | start = c.length; |
| 671 | return true; |
| 672 | |
| 673 | } else if (c[i+1] == '\"') { |
| 674 | // an escaped quote inside a quoted field, step over it |
| 675 | hasEscapedQuotes = true; |
| 676 | i += 2; |
| 677 | |
| 678 | } else if (c[i+1] == ',') { |
| 679 | // that was our closing quote, get outta here |
| 680 | addPiece(start, i, hasEscapedQuotes); |
| 681 | start = i+2; |
| 682 | return true; |
| 683 | |
| 684 | } else { |
| 685 | // This is a lone-wolf quote, occasionally seen in exports. |
| 686 | // It's a single quote in the middle of some other text, |
| 687 | // and not escaped properly. Pray for the best! |
| 688 | i++; |
| 689 | } |
| 690 | |
| 691 | } else { // not a quoted line |
| 692 | if (i == c.length-1) { |
| 693 | // we're at the end of the line, can't have an unescaped quote |
| 694 | throw new RuntimeException("Unterminated quote at end of line"); |
| 695 | |
| 696 | } else if (c[i+1] == '\"') { |
| 697 | // step over this crummy quote escape |
| 698 | hasEscapedQuotes = true; |
| 699 | i += 2; |
| 700 | |
| 701 | } else { |
| 702 | throw new RuntimeException("Unterminated quoted field mid-line"); |
| 703 | } |
| 704 | } |
| 705 | } else if (!quoted && c[i] == ',') { |
| 706 | addPiece(start, i, hasEscapedQuotes); |
| 707 | start = i+1; |
| 708 | return true; |