public static String extractIndentString(String line,int tabWidth,int indentWidth){
  if (tabWidth < 0 || indentWidth < 0 || line == null) {
    throw new IllegalArgumentException();
  }
  int size=line.length();
  int end=0;
  int spaceEquivs=0;
  int characters=0;
  for (int i=0; i < size; i++) {
    char c=line.charAt(i);
    if (c == '\t') {
      spaceEquivs=calculateSpaceEquivalents(tabWidth,spaceEquivs);
      characters++;
    }
 else     if (isIndentChar(c)) {
      spaceEquivs++;
      characters++;
    }
 else {
      break;
    }
    if (spaceEquivs >= indentWidth) {
      end+=characters;
      characters=0;
      if (indentWidth == 0) {
        spaceEquivs=0;
      }
 else {
        spaceEquivs=spaceEquivs % indentWidth;
      }
    }
  }
  if (end == 0) {
    return Util.EMPTY_STRING;
  }
 else   if (end == size) {
    return line;
  }
 else {
    return line.substring(0,end);
  }
}

public static String extractIndentString ( String line , int tabWidth , int indentWidth ) { if ( tabWidth < 0 || indentWidth < 0 || line == null ) { throw new IllegalArgumentException ( ) ; } int size = line . length ( ) ; int end = 0 ; int spaceEquivs = 0 ; int characters = 0 ; for ( int i = 0 ; i < size ; i ++ ) { char c = line . charAt ( i ) ; if ( c == '\t' ) { spaceEquivs = calculateSpaceEquivalents ( tabWidth , spaceEquivs ) ; characters ++ ; } else if ( isIndentChar ( c ) ) { spaceEquivs ++ ; characters ++ ; } else { break ; } if ( spaceEquivs >= indentWidth ) { end += characters ; characters = 0 ; if ( indentWidth == 0 ) { spaceEquivs = 0 ; } else { spaceEquivs = spaceEquivs % indentWidth ; } } } if ( end == 0 ) { return Util . EMPTY_STRING ; } else if ( end == size ) { return line ; } else { return line . substring ( 0 , end ) ; } }
returns the leading indentation string of the given line . note that the returned string need not be equal to the leading whitespace as odd spaces are not considered part of the indentation .
