All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sfm.csv.cell.StringCellValueReader Maven / Gradle / Ivy

package org.sfm.csv.cell;

import org.sfm.csv.CellValueReader;
import org.sfm.csv.ParsingContext;


public class StringCellValueReader implements CellValueReader {

	final static byte QUOTE = '"';
	final static char CQUOTE = '"';
	
	@Override
	public String read(char[] chars, int offset, int length, ParsingContext parsingContext) {
		return readString(chars, offset, length);
	}
	
	public static String readString(char[] chars, int offset, int length) {
		if (chars[offset] == CQUOTE) {
			return unescape(chars, offset, length);
		}
		
		return new String(chars, offset, length);	
	}

	private static String unescape(char[] chars, int offset, int length) {
		char[] newChars = new char[length];
		boolean lastWasQuote = false;
		int j = 0;
		for(int i = offset + 1; i < offset + length -1; i++) {
			if (chars[i] != CQUOTE || lastWasQuote) {
				newChars[j++] = chars[i];
				lastWasQuote = false;
			} else {
				lastWasQuote = true;
			}
		}
		if (chars[offset + length -1] != CQUOTE) {
			newChars[j++] = chars[offset + length -1];
		}
		return new String(newChars, 0, j);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy