PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms 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
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
  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. package org.gjt.sp.util;
  25. //{{{ Imports
  26. import javax.swing.text.Segment;
  27. //}}}
  28. /**
  29. * Several tools that depends on JDK only.
  30. *
  31. * @author Matthieu Casanova
  32. * @version $Id: StandardUtilities.java 5486 2006-06-23 22:31:58Z kpouer $
  33. * @since 4.3pre5
  34. */
  35. public class StandardUtilities
  36. {
  37. //{{{ Text methods
  38. //{{{ getLeadingWhiteSpace() method
  39. /**
  40. * Returns the number of leading white space characters in the
  41. * specified string.
  42. * @param str The string
  43. */
  44. public static int getLeadingWhiteSpace(String str)
  45. {
  46. int whitespace = 0;
  47. loop: for(;whitespace < str.length();)
  48. {
  49. switch(str.charAt(whitespace))
  50. {
  51. case ' ':
  52. case '\t':
  53. whitespace++;
  54. break;
  55. default:
  56. break loop;
  57. }
  58. }
  59. return whitespace;
  60. } //}}}
  61. //{{{ getTrailingWhiteSpace() method
  62. /**
  63. * Returns the number of trailing whitespace characters in the
  64. * specified string.
  65. * @param str The string
  66. */
  67. public static int getTrailingWhiteSpace(String str)
  68. {
  69. int whitespace = 0;
  70. loop: for(int i = str.length() - 1; i >= 0; i--)
  71. {
  72. switch(str.charAt(i))
  73. {
  74. case ' ':
  75. case '\t':
  76. whitespace++;
  77. break;
  78. default:
  79. break loop;
  80. }
  81. }
  82. return whitespace;
  83. } //}}}
  84. //{{{ getLeadingWhiteSpaceWidth() method
  85. /**
  86. * Returns the width of the leading white space in the specified
  87. * string.
  88. * @param str The string
  89. * @param tabSize The tab size
  90. */
  91. public static int getLeadingWhiteSpaceWidth(String str, int tabSize)
  92. {
  93. int whitespace = 0;
  94. loop: for(int i = 0; i < str.length(); i++)
  95. {
  96. switch(str.charAt(i))
  97. {
  98. case ' ':
  99. whitespace++;
  100. break;
  101. case '\t':
  102. whitespace += tabSize -
  103. whitespace % tabSize;
  104. break;
  105. default:
  106. break loop;
  107. }
  108. }
  109. return whitespace;
  110. } //}}}
  111. //{{{ createWhiteSpace() method
  112. /**
  113. * Creates a string of white space with the specified length.<p>
  114. *
  115. * To get a whitespace string tuned to the current buffer's
  116. * settings, call this method as follows:
  117. *
  118. * <pre>myWhitespace = MiscUtilities.createWhiteSpace(myLength,
  119. * (buffer.getBooleanProperty("noTabs") ? 0
  120. * : buffer.getTabSize()));</pre>
  121. *
  122. * @param len The length
  123. * @param tabSize The tab size, or 0 if tabs are not to be used
  124. */
  125. public static String createWhiteSpace(int len, int tabSize)
  126. {
  127. return createWhiteSpace(len,tabSize,0);
  128. } //}}}
  129. //{{{ createWhiteSpace() method
  130. /**
  131. * Creates a string of white space with the specified length.<p>
  132. *
  133. * To get a whitespace string tuned to the current buffer's
  134. * settings, call this method as follows:
  135. *
  136. * <pre>myWhitespace = MiscUtilities.createWhiteSpace(myLength,
  137. * (buffer.getBooleanProperty("noTabs") ? 0
  138. * : buffer.getTabSize()));</pre>
  139. *
  140. * @param len The length
  141. * @param tabSize The tab size, or 0 if tabs are not to be used
  142. * @param start The start offset, for tab alignment
  143. */
  144. public static String createWhiteSpace(int len, int tabSize, int start)
  145. {
  146. StringBuffer buf = new StringBuffer();
  147. if(tabSize == 0)
  148. {
  149. while(len-- > 0)
  150. buf.append(' ');
  151. }
  152. else if(len == 1)
  153. buf.append(' ');
  154. else
  155. {
  156. int count = (len + start % tabSize) / tabSize;
  157. if(count != 0)
  158. len += start;
  159. while(count-- > 0)
  160. buf.append('\t');
  161. count = len % tabSize;
  162. while(count-- > 0)
  163. buf.append(' ');
  164. }
  165. return buf.toString();
  166. } //}}}
  167. //{{{ getVirtualWidth() method
  168. /**
  169. * Returns the virtual column number (taking tabs into account) of the
  170. * specified offset in the segment.
  171. *
  172. * @param seg The segment
  173. * @param tabSize The tab size
  174. */
  175. public static int getVirtualWidth(Segment seg, int tabSize)
  176. {
  177. int virtualPosition = 0;
  178. for (int i = 0; i < seg.count; i++)
  179. {
  180. char ch = seg.array[seg.offset + i];
  181. if (ch == '\t')
  182. {
  183. virtualPosition += tabSize
  184. - virtualPosition % tabSize;
  185. }
  186. else
  187. {
  188. ++virtualPosition;
  189. }
  190. }
  191. return virtualPosition;
  192. } //}}}
  193. //{{{ getOffsetOfVirtualColumn() method
  194. /**
  195. * Returns the array offset of a virtual column number (taking tabs
  196. * into account) in the segment.
  197. *
  198. * @param seg The segment
  199. * @param tabSize The tab size
  200. * @param column The virtual column number
  201. * @param totalVirtualWidth If this array is non-null, the total
  202. * virtual width will be stored in its first location if this method
  203. * returns -1.
  204. *
  205. * @return -1 if the column is out of bounds
  206. */
  207. public static int getOffsetOfVirtualColumn(Segment seg, int tabSize,
  208. int column, int[] totalVirtualWidth)
  209. {
  210. int virtualPosition = 0;
  211. for (int i = 0; i < seg.count; i++)
  212. {
  213. char ch = seg.array[seg.offset + i];
  214. if (ch == '\t')
  215. {
  216. int tabWidth = tabSize
  217. - virtualPosition % tabSize;
  218. if(virtualPosition >= column)
  219. return i;
  220. else
  221. virtualPosition += tabWidth;
  222. }
  223. else
  224. {
  225. if(virtualPosition >= column)
  226. return i;
  227. else
  228. ++virtualPosition;
  229. }
  230. }
  231. if(totalVirtualWidth != null)
  232. totalVirtualWidth[0] = virtualPosition;
  233. return -1;
  234. } //}}}
  235. //}}}
  236. private StandardUtilities(){}
  237. }