Here you can find the source of trimIndent(String line, int indentsToRemove, int tabWidth, int indentWidth)
private static String trimIndent(String line, int indentsToRemove, int tabWidth, int indentWidth)
//package com.java2s; /******************************************************************************* * Copyright ? 2008, 2013 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from w ww. j av a 2 s . c o m*/ * IBM Corporation - initial API and implementation * *******************************************************************************/ import java.util.Arrays; public class Main { /** * Removes the given number of indents from the line. Asserts that the given line * has the requested number of indents. If <code>indentsToRemove <= 0</code> * the line is returned. * * @since 3.1 */ private static String trimIndent(String line, int indentsToRemove, int tabWidth, int indentWidth) { if (line == null || indentsToRemove <= 0) return line; final int spaceEquivalentsToRemove = indentsToRemove * indentWidth; int start = 0; int spaceEquivalents = 0; int size = line.length(); String prefix = null; for (int i = 0; i < size; i++) { char c = line.charAt(i); if (c == '\t') { int remainder = spaceEquivalents % tabWidth; spaceEquivalents += tabWidth - remainder; } else if (isIndentChar(c)) { spaceEquivalents++; } else { // Assert.isTrue(false, "Line does not have requested number of indents"); //$NON-NLS-1$ start = i; break; } if (spaceEquivalents == spaceEquivalentsToRemove) { start = i + 1; break; } if (spaceEquivalents > spaceEquivalentsToRemove) { // can happen if tabSize > indentSize, e.g tabsize==8, indent==4, indentsToRemove==1, line prefixed with one tab // this implements the third option start = i + 1; // remove the tab // and add the missing spaces char[] missing = new char[spaceEquivalents - spaceEquivalentsToRemove]; Arrays.fill(missing, ' '); prefix = new String(missing); break; } } String trimmed; if (start == size) trimmed = ""; //$NON-NLS-1$ else trimmed = line.substring(start); if (prefix == null) return trimmed; return prefix + trimmed; } /** * Indent char is a space char but not a line delimiters. * <code>== Character.isWhitespace(ch) && ch != '\n' && ch != '\r'</code> */ private static boolean isIndentChar(char ch) { return Character.isWhitespace(ch) && !isLineDelimiterChar(ch); } /** * Line delimiter chars are '\n' and '\r'. */ private static boolean isLineDelimiterChar(char ch) { return ch == '\n' || ch == '\r'; } }