/jEdit/tags/after_bsh-2-0b4/org/gjt/sp/util/StandardUtilities.java
Java | 256 lines | 133 code | 19 blank | 104 comment | 20 complexity | 8903e0328d8ad55c5ddc62402ce2f7a4 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
- /*
- * StandardUtilities.java - Various miscallaneous utility functions
- * :tabSize=8:indentSize=8:noTabs=false:
- * :folding=explicit:collapseFolds=1:
- *
- * Copyright (C) 1999, 2006 Matthieu Casanova, Slava Pestov
- * Portions copyright (C) 2000 Richard S. Hall
- * Portions copyright (C) 2001 Dirk Moebius
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- package org.gjt.sp.util;
- //{{{ Imports
- import javax.swing.text.Segment;
- //}}}
- /**
- * Several tools that depends on JDK only.
- *
- * @author Matthieu Casanova
- * @version $Id: StandardUtilities.java 5486 2006-06-23 22:31:58Z kpouer $
- * @since 4.3pre5
- */
- public class StandardUtilities
- {
-
- //{{{ Text methods
- //{{{ getLeadingWhiteSpace() method
- /**
- * Returns the number of leading white space characters in the
- * specified string.
- * @param str The string
- */
- public static int getLeadingWhiteSpace(String str)
- {
- int whitespace = 0;
- loop: for(;whitespace < str.length();)
- {
- switch(str.charAt(whitespace))
- {
- case ' ':
- case '\t':
- whitespace++;
- break;
- default:
- break loop;
- }
- }
- return whitespace;
- } //}}}
- //{{{ getTrailingWhiteSpace() method
- /**
- * Returns the number of trailing whitespace characters in the
- * specified string.
- * @param str The string
- */
- public static int getTrailingWhiteSpace(String str)
- {
- int whitespace = 0;
- loop: for(int i = str.length() - 1; i >= 0; i--)
- {
- switch(str.charAt(i))
- {
- case ' ':
- case '\t':
- whitespace++;
- break;
- default:
- break loop;
- }
- }
- return whitespace;
- } //}}}
-
- //{{{ getLeadingWhiteSpaceWidth() method
- /**
- * Returns the width of the leading white space in the specified
- * string.
- * @param str The string
- * @param tabSize The tab size
- */
- public static int getLeadingWhiteSpaceWidth(String str, int tabSize)
- {
- int whitespace = 0;
- loop: for(int i = 0; i < str.length(); i++)
- {
- switch(str.charAt(i))
- {
- case ' ':
- whitespace++;
- break;
- case '\t':
- whitespace += tabSize -
- whitespace % tabSize;
- break;
- default:
- break loop;
- }
- }
- return whitespace;
- } //}}}
- //{{{ createWhiteSpace() method
- /**
- * Creates a string of white space with the specified length.<p>
- *
- * To get a whitespace string tuned to the current buffer's
- * settings, call this method as follows:
- *
- * <pre>myWhitespace = MiscUtilities.createWhiteSpace(myLength,
- * (buffer.getBooleanProperty("noTabs") ? 0
- * : buffer.getTabSize()));</pre>
- *
- * @param len The length
- * @param tabSize The tab size, or 0 if tabs are not to be used
- */
- public static String createWhiteSpace(int len, int tabSize)
- {
- return createWhiteSpace(len,tabSize,0);
- } //}}}
- //{{{ createWhiteSpace() method
- /**
- * Creates a string of white space with the specified length.<p>
- *
- * To get a whitespace string tuned to the current buffer's
- * settings, call this method as follows:
- *
- * <pre>myWhitespace = MiscUtilities.createWhiteSpace(myLength,
- * (buffer.getBooleanProperty("noTabs") ? 0
- * : buffer.getTabSize()));</pre>
- *
- * @param len The length
- * @param tabSize The tab size, or 0 if tabs are not to be used
- * @param start The start offset, for tab alignment
- */
- public static String createWhiteSpace(int len, int tabSize, int start)
- {
- StringBuffer buf = new StringBuffer();
- if(tabSize == 0)
- {
- while(len-- > 0)
- buf.append(' ');
- }
- else if(len == 1)
- buf.append(' ');
- else
- {
- int count = (len + start % tabSize) / tabSize;
- if(count != 0)
- len += start;
- while(count-- > 0)
- buf.append('\t');
- count = len % tabSize;
- while(count-- > 0)
- buf.append(' ');
- }
- return buf.toString();
- } //}}}
- //{{{ getVirtualWidth() method
- /**
- * Returns the virtual column number (taking tabs into account) of the
- * specified offset in the segment.
- *
- * @param seg The segment
- * @param tabSize The tab size
- */
- public static int getVirtualWidth(Segment seg, int tabSize)
- {
- int virtualPosition = 0;
- for (int i = 0; i < seg.count; i++)
- {
- char ch = seg.array[seg.offset + i];
- if (ch == '\t')
- {
- virtualPosition += tabSize
- - virtualPosition % tabSize;
- }
- else
- {
- ++virtualPosition;
- }
- }
- return virtualPosition;
- } //}}}
- //{{{ getOffsetOfVirtualColumn() method
- /**
- * Returns the array offset of a virtual column number (taking tabs
- * into account) in the segment.
- *
- * @param seg The segment
- * @param tabSize The tab size
- * @param column The virtual column number
- * @param totalVirtualWidth If this array is non-null, the total
- * virtual width will be stored in its first location if this method
- * returns -1.
- *
- * @return -1 if the column is out of bounds
- */
- public static int getOffsetOfVirtualColumn(Segment seg, int tabSize,
- int column, int[] totalVirtualWidth)
- {
- int virtualPosition = 0;
- for (int i = 0; i < seg.count; i++)
- {
- char ch = seg.array[seg.offset + i];
- if (ch == '\t')
- {
- int tabWidth = tabSize
- - virtualPosition % tabSize;
- if(virtualPosition >= column)
- return i;
- else
- virtualPosition += tabWidth;
- }
- else
- {
- if(virtualPosition >= column)
- return i;
- else
- ++virtualPosition;
- }
- }
- if(totalVirtualWidth != null)
- totalVirtualWidth[0] = virtualPosition;
- return -1;
- } //}}}
- //}}}
- private StandardUtilities(){}
- }