Here you can find the source of normalizeIndentationFromWhitespace(String str, int tabSize, boolean insertSpaces)
private static String normalizeIndentationFromWhitespace(String str, int tabSize, boolean insertSpaces)
//package com.java2s; /**/*from ww w .jav a2s . c o m*/ * Copyright (c) 2015-2017 Angelo ZERR. * 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: * Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation */ public class Main { private static String normalizeIndentationFromWhitespace(String str, int tabSize, boolean insertSpaces) { int spacesCnt = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '\t') { spacesCnt += tabSize; } else { spacesCnt++; } } StringBuilder result = new StringBuilder(); if (!insertSpaces) { long tabsCnt = Math.round(Math.floor(spacesCnt / tabSize)); spacesCnt = spacesCnt % tabSize; for (int i = 0; i < tabsCnt; i++) { result.append('\t'); } } for (int i = 0; i < spacesCnt; i++) { result.append(' '); } return result.toString(); } }