<commit_before>package taursus.commands;

public class CommandParser implements ICommandParser {
	protected static class CommandParserLoader {
        private static final CommandParser INSTANCE = new CommandParser();
    }

	protected CommandParser() {
        if (CommandParserLoader.INSTANCE != null) {
            throw new IllegalStateException("Already instantiated");
        }
    }

    public static CommandParser getInstance() {
        return CommandParserLoader.INSTANCE;
    }
    
    protected boolean endsCmdNameParsing(char c) {
    	return c == ' ';
    }
    
    protected boolean startsCmdParamNameParsing(char c) {
    	return c == '-';
    }
    
    protected boolean endsCmdParamNameParsing(char c) {
        return c == ' ';
    }
    
    protected boolean endsCmdParamValueParsing(char c, boolean withinQuotes) {
    	if(withinQuotes) {
    		return c == '"';
    	} else {
    		return c == ' ' || startsCmdParamNameParsing(c);
    	}
    }
	
    protected String readCmdName(String str) {
    	String name = "";
    	int length = str.length();
    	
    	for(int i=0; i<length; i++){
    		char c = str.charAt(i);
    		if(endsCmdNameParsing(c)) {
    			break;
    		}
    		name += c;
    	}
    	
    	return name;
    }
    
    protected String readCmdParamName(String str) {
    	String name = "";
    	int length = str.length();
    	
    	for(int i=0; i<length; i++){
    		char c = str.charAt(i);
    		if(endsCmdParamNameParsing(c)) {
    			break;
    		}
    		name += c;
    	}
    	
    	return name;
    }
    
    protected String readCmdParamValue(String str, boolean withinQuotes) {
    	String name = "";
    	int length = str.length();
 
    	for(int i=0; i<length; i++){
    		char c = str.charAt(i);
    		if(endsCmdParamValueParsing(c, withinQuotes)) {
    			break;
    		}
    		name += c;
    	}
    	
    	return name;
    }
    
    protected String skipToNextParam(String str) {
    	int skipLength = 0;
    	int length = str.length();
    	
    	for(int i=0; i<length; i++){
    		char c = str.charAt(i);
    		skipLength++;
    		if(startsCmdParamNameParsing(c)) {
    			break;
    		}
    	}
    	
    	return str.substring(skipLength);
    }
    
    protected String skipToNextNonWhiteChar(String str) {
    	int skipLength = 0;
    	int length = str.length();
    	
    	for(int i=0; i<length; i++){
    		char c = str.charAt(i);
    		if(c != ' ') {
    			break;
    		}
    		skipLength++;
    	}
    	
    	return str.substring(skipLength);
    }
    
	@Override
	public ICommand parse(String str, ICommandsRepository repository) {
		String name = readCmdName(str);
		
		ICommand command = repository.getCommand(name);
		if(command != null) {
		    command.setName(name);
	        str = str.substring(name.length());
	        
	        while(str.length() > 0) {
	            str = skipToNextParam(str);
	            
	            String paramName = readCmdParamName(str);
	            str = str.substring(paramName.length());
	            str = skipToNextNonWhiteChar(str);
	            
	            boolean withinQuotes = false;
	            if(str.length() > 0 && str.charAt(0) == '"') {
	                withinQuotes = true;
	                str = str.substring(1);
	            }
	            
	            String paramValue = readCmdParamValue(str, withinQuotes);
	            command.addParam(paramName, paramValue);
	            str = str.substring(paramValue.length());
	        }
		}
		
		return command;
	}
}<commit_msg>refactored CommandParser so it uses read string reader and read strategies<commit_after>package taursus.commands;

public class CommandParser implements ICommandParser {
	protected static class CommandParserLoader {
        private static final CommandParser INSTANCE = new CommandParser();
    }

	protected CommandParser() {
        if (CommandParserLoader.INSTANCE != null) {
            throw new IllegalStateException("Already instantiated");
        }
    }

    public static CommandParser getInstance() {
        return CommandParserLoader.INSTANCE;
    }
    
	@Override
	public ICommand parse(String str, ICommandsRepository repository) {
	    StringReader reader = new StringReader();
	    StringIterator it = new StringIterator(str);
		String name = reader.read(it, new CommandNameReadStrategy());
		
		ICommand command = repository.getCommand(name);
		
		if(command == null) {
		    return null;
		}
		
	    command.setName(name);
        
        while(it.hasNext()) {
            reader.read(it, new CommandSkipToNextParamReadStrategy()); //Skip to next param
            
            String paramName = reader.read(it, new CommandParamNameReadStrategy());
            it.skipToNextNonWhiteChar();
            
            String paramValue = "";
            
            if(determineIfParamValueExists(it)) {
                boolean isWithinQuotes = determineIfParamValueIsWithinQuotes(it);
                paramValue = reader.read(it, new CommandParamValueReadStrategy(isWithinQuotes));
            }
            
            command.addParam(paramName, paramValue);
        }
		
		return command;
	}

    private boolean determineIfParamValueExists(StringIterator it) {
        if(it.hasNext()) {
            char c = it.next();
            it.rewind();
            
            if(c != '-') {
                return true;
            }
        }
        
        return false;
    }

    private boolean determineIfParamValueIsWithinQuotes(StringIterator it) {
        if(it.hasNext()) {
            boolean withinQuotes = it.next() == '"';
            it.rewind();
            return withinQuotes;
        }
        
        return false;
    }
}
{'fraction_non_alphanumeric': 0.10303458009880029, 'fraction_numerical': 0.0019407198306280876, 'mean_word_length': 1.9852553975776726, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2125321', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1658, 'n_words': 352}