/* * [StripNewlines.java] * * Summary: Flows text by stripping newlines ( actually replacing them with spaces ). * * 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: * 1.0 2002-06-19 */ package com.mindprod.quoter; /** * Flows text by stripping newlines ( actually replacing them with spaces ). * * @author Roedy Green, Canadian Mind Products * @version 1.0 2011-11-15 initial version * @since 2011-11-15 */ final class StripNewlines extends TextProcessor { /** * Chops off leading and trailing spaces from each line. * * @param raw text to be chopped * * @return String representing the cooked output */ String process( String raw ) { if ( raw == null ) { return null; } // we don't actually remove newlines, we convert them to blanks final StringBuilder sb = new StringBuilder( raw.length() ); boolean pendingSpace = false; for ( int i = 0; i < raw.length(); i++ ) { final char c = raw.charAt( i ); if ( c == '\r' || c == '\n' ) { pendingSpace = true; } else { if ( pendingSpace ) { sb.append( ' ' ); pendingSpace = false; } sb.append( c ); } } return sb.toString(); } }