class ParserCsv extends ParserByLine {

	@Override
	public List<String> parseRow(String line) throws ParseException {
		int realPos = 0;
		int pos = 0;
		boolean isDelimiter = false;
		StringBuilder sb = new StringBuilder(line);
		List<String> row = new ArrayList<String>();
		
		while (pos < sb.length()) {
			char c = sb.charAt(pos);
			/*
			LOGGER.debug("Current buffer {}", sb);
			LOGGER.debug("pos {}", pos);
			LOGGER.debug("isDelimiter {}", isDelimiter);
			*/
			if (c == escapeChar) {
				realPos++;
				pos++;
				if (pos == sb.length()) {
					throw getParseException("Invalid escape char", realPos, line);
				}
				sb.delete(pos - 1, pos);
				realPos++;
			} else if (c == fieldTerminator) {
				realPos++;
				if (isDelimiter) {
					realPos++;
					pos++;
					if (pos == sb.length()) {
						sb.delete(pos - 1, pos);
						break;
					}
					if (sb.charAt(pos) == fieldSeparator) {
						row.add(sb.substring(0, pos - 1));
						sb.delete(0, pos + 1);
						pos = 0;
						isDelimiter = false;
						continue;
					}
					throw getParseException("Invalid field delimiter", realPos, line);
				}
				
				if (pos != 0) {
					throw getParseException("Unterminated field", realPos, line);
				}
				sb.delete(0, 1);
				isDelimiter = true;
				continue;
			} else if (c == fieldSeparator) {
				realPos++;
				if (isDelimiter) {
					pos++;
					continue;
				}
				row.add(sb.substring(0, pos));
				sb.delete(0, pos + 1);
				pos = 0;
			} else {
				realPos++;
				pos++;
			}
		}
		row.add(sb.toString());
		
		return row;
	}

}