/fstmerge/examples/jEdit/rev9022-9462/right-branch-9462/org/gjt/sp/util/StandardUtilities.java

https://github.com/RoDaniel/featurehouse · Java · 388 lines · 310 code · 78 blank · 0 comment · 64 complexity · bc50142f9a8fd159a39d869009d67cba MD5 · raw file

  1. package org.gjt.sp.util;
  2. import javax.swing.text.Segment;
  3. import java.util.Comparator;
  4. import java.util.Stack;
  5. public class StandardUtilities
  6. {
  7. public static int getLeadingWhiteSpace(String str)
  8. {
  9. return getLeadingWhiteSpace((CharSequence)str);
  10. }
  11. public static int getLeadingWhiteSpace(CharSequence str)
  12. {
  13. int whitespace = 0;
  14. loop: for(;whitespace < str.length();)
  15. {
  16. switch(str.charAt(whitespace))
  17. {
  18. case ' ':
  19. case '\t':
  20. whitespace++;
  21. break;
  22. default:
  23. break loop;
  24. }
  25. }
  26. return whitespace;
  27. }
  28. public static int getTrailingWhiteSpace(String str)
  29. {
  30. int whitespace = 0;
  31. loop: for(int i = str.length() - 1; i >= 0; i--)
  32. {
  33. switch(str.charAt(i))
  34. {
  35. case ' ':
  36. case '\t':
  37. whitespace++;
  38. break;
  39. default:
  40. break loop;
  41. }
  42. }
  43. return whitespace;
  44. }
  45. public static int getLeadingWhiteSpaceWidth(String str, int tabSize)
  46. {
  47. return getLeadingWhiteSpaceWidth((CharSequence)str, tabSize);
  48. }
  49. public static int getLeadingWhiteSpaceWidth(CharSequence str, int tabSize)
  50. {
  51. int whitespace = 0;
  52. loop: for(int i = 0; i < str.length(); i++)
  53. {
  54. switch(str.charAt(i))
  55. {
  56. case ' ':
  57. whitespace++;
  58. break;
  59. case '\t':
  60. whitespace += tabSize -
  61. whitespace % tabSize;
  62. break;
  63. default:
  64. break loop;
  65. }
  66. }
  67. return whitespace;
  68. }
  69. public static int getLastIndexOf(CharSequence seq, char ch)
  70. {
  71. return getLastIndexOf(seq, ch, seq.length());
  72. }
  73. public static int getLastIndexOf(CharSequence seq, char ch, int pos)
  74. {
  75. assert (pos <= seq.length()) : "invalid start position";
  76. for (int i = pos - 1; i >= 0; i--)
  77. if (seq.charAt(i) == ch)
  78. return i;
  79. return -1;
  80. }
  81. public static String createWhiteSpace(int len, int tabSize)
  82. {
  83. return createWhiteSpace(len,tabSize,0);
  84. }
  85. public static String createWhiteSpace(int len, int tabSize, int start)
  86. {
  87. StringBuilder buf = new StringBuilder();
  88. if(tabSize == 0)
  89. {
  90. while(len-- > 0)
  91. buf.append(' ');
  92. }
  93. else if(len == 1)
  94. buf.append(' ');
  95. else
  96. {
  97. int count = (len + start % tabSize) / tabSize;
  98. if(count != 0)
  99. len += start;
  100. while(count-- > 0)
  101. buf.append('\t');
  102. count = len % tabSize;
  103. while(count-- > 0)
  104. buf.append(' ');
  105. }
  106. return buf.toString();
  107. }
  108. public static int getVirtualWidth(Segment seg, int tabSize)
  109. {
  110. int virtualPosition = 0;
  111. for (int i = 0; i < seg.count; i++)
  112. {
  113. char ch = seg.array[seg.offset + i];
  114. if (ch == '\t')
  115. {
  116. virtualPosition += tabSize
  117. - virtualPosition % tabSize;
  118. }
  119. else
  120. {
  121. ++virtualPosition;
  122. }
  123. }
  124. return virtualPosition;
  125. }
  126. public static int getOffsetOfVirtualColumn(Segment seg, int tabSize,
  127. int column, int[] totalVirtualWidth)
  128. {
  129. int virtualPosition = 0;
  130. for (int i = 0; i < seg.count; i++)
  131. {
  132. char ch = seg.array[seg.offset + i];
  133. if (ch == '\t')
  134. {
  135. int tabWidth = tabSize
  136. - virtualPosition % tabSize;
  137. if(virtualPosition >= column)
  138. return i;
  139. else
  140. virtualPosition += tabWidth;
  141. }
  142. else
  143. {
  144. if(virtualPosition >= column)
  145. return i;
  146. else
  147. ++virtualPosition;
  148. }
  149. }
  150. if(totalVirtualWidth != null)
  151. totalVirtualWidth[0] = virtualPosition;
  152. return -1;
  153. }
  154. public static int compareStrings(String str1, String str2, boolean ignoreCase)
  155. {
  156. char[] char1 = str1.toCharArray();
  157. char[] char2 = str2.toCharArray();
  158. int len = Math.min(char1.length,char2.length);
  159. for(int i = 0, j = 0; i < len && j < len; i++, j++)
  160. {
  161. char ch1 = char1[i];
  162. char ch2 = char2[j];
  163. if(Character.isDigit(ch1) && Character.isDigit(ch2)
  164. && ch1 != '0' && ch2 != '0')
  165. {
  166. int _i = i + 1;
  167. int _j = j + 1;
  168. for(; _i < char1.length; _i++)
  169. {
  170. if(!Character.isDigit(char1[_i]))
  171. {
  172. break;
  173. }
  174. }
  175. for(; _j < char2.length; _j++)
  176. {
  177. if(!Character.isDigit(char2[_j]))
  178. {
  179. break;
  180. }
  181. }
  182. int len1 = _i - i;
  183. int len2 = _j - j;
  184. if(len1 > len2)
  185. return 1;
  186. else if(len1 < len2)
  187. return -1;
  188. else
  189. {
  190. for(int k = 0; k < len1; k++)
  191. {
  192. ch1 = char1[i + k];
  193. ch2 = char2[j + k];
  194. if(ch1 != ch2)
  195. return ch1 - ch2;
  196. }
  197. }
  198. i = _i - 1;
  199. j = _j - 1;
  200. }
  201. else
  202. {
  203. if(ignoreCase)
  204. {
  205. ch1 = Character.toLowerCase(ch1);
  206. ch2 = Character.toLowerCase(ch2);
  207. }
  208. if(ch1 != ch2)
  209. return ch1 - ch2;
  210. }
  211. }
  212. return char1.length - char2.length;
  213. }
  214. public static class StringCompare implements Comparator
  215. {
  216. public int compare(Object obj1, Object obj2)
  217. {
  218. return compareStrings(obj1.toString(),
  219. obj2.toString(),false);
  220. }
  221. }
  222. public static boolean objectsEqual(Object o1, Object o2)
  223. {
  224. if(o1 == null)
  225. {
  226. if(o2 == null)
  227. return true;
  228. else
  229. return false;
  230. }
  231. else if(o2 == null)
  232. return false;
  233. else
  234. return o1.equals(o2);
  235. }
  236. public static String globToRE(String glob)
  237. {
  238. final Object NEG = new Object();
  239. final Object GROUP = new Object();
  240. Stack state = new Stack();
  241. StringBuffer buf = new StringBuffer();
  242. boolean backslash = false;
  243. for(int i = 0; i < glob.length(); i++)
  244. {
  245. char c = glob.charAt(i);
  246. if(backslash)
  247. {
  248. buf.append('\\');
  249. buf.append(c);
  250. backslash = false;
  251. continue;
  252. }
  253. switch(c)
  254. {
  255. case '\\':
  256. backslash = true;
  257. break;
  258. case '?':
  259. buf.append('.');
  260. break;
  261. case '.':
  262. case '+':
  263. case '(':
  264. case ')':
  265. buf.append('\\');
  266. buf.append(c);
  267. break;
  268. case '*':
  269. buf.append(".*");
  270. break;
  271. case '|':
  272. if(backslash)
  273. buf.append("\\|");
  274. else
  275. buf.append('|');
  276. break;
  277. case '{':
  278. buf.append('(');
  279. if(i + 1 != glob.length() && glob.charAt(i + 1) == '!')
  280. {
  281. buf.append('?');
  282. state.push(NEG);
  283. }
  284. else
  285. state.push(GROUP);
  286. break;
  287. case ',':
  288. if(!state.isEmpty() && state.peek() == GROUP)
  289. buf.append('|');
  290. else
  291. buf.append(',');
  292. break;
  293. case '}':
  294. if(!state.isEmpty())
  295. {
  296. buf.append(")");
  297. if(state.pop() == NEG)
  298. buf.append(".*");
  299. }
  300. else
  301. buf.append('}');
  302. break;
  303. default:
  304. buf.append(c);
  305. }
  306. }
  307. return buf.toString();
  308. }
  309. private StandardUtilities(){}
  310. }