PageRenderTime 100ms CodeModel.GetById 89ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/util/StandardUtilities.java

#
Java | 256 lines | 133 code | 19 blank | 104 comment | 20 complexity | 8903e0328d8ad55c5ddc62402ce2f7a4 MD5 | raw file
  1/*
  2 * StandardUtilities.java - Various miscallaneous utility functions
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 2006 Matthieu Casanova, Slava Pestov
  7 * Portions copyright (C) 2000 Richard S. Hall
  8 * Portions copyright (C) 2001 Dirk Moebius
  9 *
 10 * This program is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU General Public License
 12 * as published by the Free Software Foundation; either version 2
 13 * of the License, or any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 23 */
 24
 25package org.gjt.sp.util;
 26
 27
 28//{{{ Imports
 29import javax.swing.text.Segment;
 30//}}}
 31
 32/**
 33 * Several tools that depends on JDK only.
 34 *
 35 * @author Matthieu Casanova
 36 * @version $Id: StandardUtilities.java 5486 2006-06-23 22:31:58Z kpouer $
 37 * @since 4.3pre5
 38 */
 39public class StandardUtilities
 40{
 41	
 42	//{{{ Text methods
 43
 44	//{{{ getLeadingWhiteSpace() method
 45	/**
 46	 * Returns the number of leading white space characters in the
 47	 * specified string.
 48	 * @param str The string
 49	 */
 50	public static int getLeadingWhiteSpace(String str)
 51	{
 52		int whitespace = 0;
 53loop:		for(;whitespace < str.length();)
 54		{
 55			switch(str.charAt(whitespace))
 56			{
 57			case ' ': 
 58			case '\t':
 59				whitespace++;
 60				break;
 61			default:
 62				break loop;
 63			}
 64		}
 65		return whitespace;
 66	} //}}}
 67
 68	//{{{ getTrailingWhiteSpace() method
 69	/**
 70	 * Returns the number of trailing whitespace characters in the
 71	 * specified string.
 72	 * @param str The string
 73	 */
 74	public static int getTrailingWhiteSpace(String str)
 75	{
 76		int whitespace = 0;
 77loop:		for(int i = str.length() - 1; i >= 0; i--)
 78		{
 79			switch(str.charAt(i))
 80			{
 81				case ' ': 
 82				case '\t':
 83					whitespace++;
 84					break;
 85				default:
 86					break loop;
 87			}
 88		}
 89		return whitespace;
 90	} //}}}
 91	
 92	//{{{ getLeadingWhiteSpaceWidth() method
 93	/**
 94	 * Returns the width of the leading white space in the specified
 95	 * string.
 96	 * @param str The string
 97	 * @param tabSize The tab size
 98	 */
 99	public static int getLeadingWhiteSpaceWidth(String str, int tabSize)
100	{
101		int whitespace = 0;
102loop:		for(int i = 0; i < str.length(); i++)
103		{
104			switch(str.charAt(i))
105			{
106				case ' ':
107					whitespace++;
108					break;
109				case '\t':
110					whitespace += tabSize - 
111						whitespace % tabSize;
112					break;
113				default:
114					break loop;
115			}
116		}
117		return whitespace;
118	} //}}}
119
120	//{{{ createWhiteSpace() method
121	/**
122	 * Creates a string of white space with the specified length.<p>
123	 *
124	 * To get a whitespace string tuned to the current buffer's
125	 * settings, call this method as follows:
126	 *
127	 * <pre>myWhitespace = MiscUtilities.createWhiteSpace(myLength,
128	 *     (buffer.getBooleanProperty("noTabs") ? 0
129	 *     : buffer.getTabSize()));</pre>
130	 *
131	 * @param len The length
132	 * @param tabSize The tab size, or 0 if tabs are not to be used
133	 */
134	public static String createWhiteSpace(int len, int tabSize)
135	{
136		return createWhiteSpace(len,tabSize,0);
137	} //}}}
138
139	//{{{ createWhiteSpace() method
140	/**
141	 * Creates a string of white space with the specified length.<p>
142	 *
143	 * To get a whitespace string tuned to the current buffer's
144	 * settings, call this method as follows:
145	 *
146	 * <pre>myWhitespace = MiscUtilities.createWhiteSpace(myLength,
147	 *     (buffer.getBooleanProperty("noTabs") ? 0
148	 *     : buffer.getTabSize()));</pre>
149	 *
150	 * @param len The length
151	 * @param tabSize The tab size, or 0 if tabs are not to be used
152	 * @param start The start offset, for tab alignment
153	 */
154	public static String createWhiteSpace(int len, int tabSize, int start)
155	{
156		StringBuffer buf = new StringBuffer();
157		if(tabSize == 0)
158		{
159			while(len-- > 0)
160				buf.append(' ');
161		}
162		else if(len == 1)
163			buf.append(' ');
164		else
165		{
166			int count = (len + start % tabSize) / tabSize;
167			if(count != 0)
168				len += start;
169			while(count-- > 0)
170				buf.append('\t');
171			count = len % tabSize;
172			while(count-- > 0)
173				buf.append(' ');
174		}
175		return buf.toString();
176	} //}}}
177
178	//{{{ getVirtualWidth() method
179	/**
180	 * Returns the virtual column number (taking tabs into account) of the
181	 * specified offset in the segment.
182	 *
183	 * @param seg The segment
184	 * @param tabSize The tab size
185	 */
186	public static int getVirtualWidth(Segment seg, int tabSize)
187	{
188		int virtualPosition = 0;
189
190		for (int i = 0; i < seg.count; i++)
191		{
192			char ch = seg.array[seg.offset + i];
193
194			if (ch == '\t')
195			{
196				virtualPosition += tabSize
197					- virtualPosition % tabSize;
198			}
199			else
200			{
201				++virtualPosition;
202			}
203		}
204
205		return virtualPosition;
206	} //}}}
207
208	//{{{ getOffsetOfVirtualColumn() method
209	/**
210	 * Returns the array offset of a virtual column number (taking tabs
211	 * into account) in the segment.
212	 *
213	 * @param seg The segment
214	 * @param tabSize The tab size
215	 * @param column The virtual column number
216	 * @param totalVirtualWidth If this array is non-null, the total
217	 * virtual width will be stored in its first location if this method
218	 * returns -1.
219	 *
220	 * @return -1 if the column is out of bounds
221	 */
222	public static int getOffsetOfVirtualColumn(Segment seg, int tabSize, 
223					    int column, int[] totalVirtualWidth)
224	{
225		int virtualPosition = 0;
226
227		for (int i = 0; i < seg.count; i++)
228		{
229			char ch = seg.array[seg.offset + i];
230
231			if (ch == '\t')
232			{
233				int tabWidth = tabSize
234					- virtualPosition % tabSize;
235				if(virtualPosition >= column)
236					return i;
237				else
238					virtualPosition += tabWidth;
239			}
240			else
241			{
242				if(virtualPosition >= column)
243					return i;
244				else
245					++virtualPosition;
246			}
247		}
248
249		if(totalVirtualWidth != null)
250			totalVirtualWidth[0] = virtualPosition;
251		return -1;
252	} //}}}
253
254	//}}}
255	private StandardUtilities(){}
256}