/* * [TrimLeading.java] * * Summary: chop leading spaces from each line of the buffer. * * Copyright: (c) 2002-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 4.5 2009-02-26 add both Java string quoting and plain for Java search/regexes. */ package com.mindprod.quoter; import com.mindprod.common18.ST; import java.util.StringTokenizer; /** * chop leading spaces from each line of the buffer. * * @author Roedy Green, Canadian Mind Products * @version 4.5 2009-02-26 add both Java string quoting and plain for Java search/regexes. * @since 2002-06-19 */ final class TrimLeading extends TextProcessor { /** * Chops off leading spaces. * * @param raw text to be chopped * * @return String representing the cooked output */ String process( String raw ) { // break clipboard into lines delimited by \n if ( raw == null ) { return null; } else { StringBuilder stripped = new StringBuilder( raw.length() ); StringTokenizer st = new StringTokenizer( raw, "\n", true ); while ( st.hasMoreTokens() ) { String token = st.nextToken(); if ( token.equals( "\n" ) ) { stripped.append( "\n" ); } else { stripped.append( ST.trimLeading( token ) ); } } // end while return stripped.toString(); } } // end process }