/plugins/SuperAbbrevs/tags/version0.25/abbrev_functions.bsh

# · Unknown · 164 lines · 143 code · 21 blank · 0 comment · 0 complexity · df301cc847c2db0fc9e0846d99460250 MD5 · raw file

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. public String firstUp(String s){
  4. StringBuffer res = new StringBuffer(s);
  5. if(0 < res.length()){
  6. char first = res.charAt(0);
  7. res.setCharAt(0,Character.toUpperCase(first));
  8. }
  9. return res.toString();
  10. }
  11. public String firstDown(String s){
  12. StringBuffer res = new StringBuffer(s);
  13. if(0 < res.length()){
  14. char first = res.charAt(0);
  15. res.setCharAt(0,Character.toLowerCase(first));
  16. }
  17. return res.toString();
  18. }
  19. public String choose(String s, String regexp, String match, String noMatch){
  20. Pattern p = Pattern.compile(regexp);
  21. Matcher m = p.matcher(s);
  22. return m.matches()?match:noMatch;
  23. }
  24. public String replace(String s, String regexp, String replacement, String noMatch){
  25. Pattern p = Pattern.compile(regexp);
  26. Matcher m = p.matcher(s);
  27. if(m.find()){
  28. return m.replaceAll(replacement);
  29. } else {
  30. return noMatch;
  31. }
  32. }
  33. public String replace(String s, String regexp, String replacement){
  34. replace(s,regex,replacement,s);
  35. }
  36. public String foreach(String s, String regexp, String replacement){
  37. Pattern rp = Pattern.compile("\\$(\\d)");
  38. Matcher rm = rp.matcher(replacement);
  39. StringBuffer res = new StringBuffer();
  40. Pattern p = Pattern.compile(regexp);
  41. Matcher m = p.matcher(s);
  42. int groupCount = m.groupCount();
  43. int end = 0;
  44. while(m.find()){
  45. while(rm.find(end)){
  46. res.append(replacement.substring(end,rm.start()));
  47. int g = Integer.parseInt(rm.group(1));
  48. if(0 <= g && g <= groupCount){
  49. res.append(m.group(g));
  50. }
  51. end = rm.end();
  52. }
  53. res.append(replacement.substring(end));
  54. end = 0;
  55. }
  56. return res.toString();
  57. }
  58. public String ifTrue(boolean condition, String output){
  59. return condition?output:"";
  60. }
  61. public String ifEmpty(String s,String emptyValue){
  62. return s.trim().equals("")?emptyValue:s;
  63. }
  64. public String ifNotEmpty(String s,String notEmptyValue){
  65. return s.trim().equals("")?s:notEmptyValue;
  66. }
  67. // use this instead of choose if possible
  68. public String ifEquals(String s,String pattern, String match, String noMatch){
  69. return s.equals(pattern)?match:noMatch;
  70. }
  71. public String repeat(String s, int times){
  72. StringBuffer res = new StringBuffer();
  73. for (int i=0; i<times; i++) {
  74. res.append(s);
  75. }
  76. return res.toString();
  77. }
  78. public String substring(String s, int from, int length){
  79. if(0 <= from && 0 <= length && from + length <= s.length()){
  80. return s.substring(from,length);
  81. } else {
  82. return "";
  83. }
  84. }
  85. public String indent(String whitespace, String s){
  86. return s.replaceAll("\n","\n"+indent+whitespace);
  87. }
  88. public String substring(String s, int from){
  89. if(from < 0){
  90. return "";
  91. } else if(s.length() <= from){
  92. return s;
  93. } else {
  94. return s.substring(from);
  95. }
  96. }
  97. /**
  98. * Match the string s against a comma seperated list of words, if s is a unique
  99. * prefix of one of the words, the words except the prefix will be returned.
  100. *
  101. * ex. complete("Arr","ArrayList,Hashtable,LinkedList") will return "ayList"
  102. *
  103. *@param s the string to check against the words
  104. *@param words a comma seperated list of words to complete against
  105. */
  106. public String complete(String s, String words){
  107. complete(s,words.split(","));
  108. }
  109. /**
  110. * Match the string s against a comma seperated list of words, if s is a unique
  111. * prefix of one of the words, the words except the prefix will be returned.
  112. *
  113. * ex.
  114. * String[] words = {"ArrayList","Hashtable","LinkedList"};
  115. * complete("Arr",words) will return "ayList"
  116. *
  117. *@param s the string to check against the words
  118. *@param words a list of words to complete against
  119. */
  120. public String complete(String s, String[] words){
  121. Arrays.sort(words);
  122. int longestPrefix = 0;
  123. int longestPrefixIndex = -1;
  124. for (int i=0; i<words.length; i++) {
  125. // if s is longer than the word, we can't use it for completion
  126. if(words[i].length() < s.length()) continue;
  127. // find prefix length
  128. int j = 0;
  129. while(j < s.length() && s.charAt(j)==words[i].charAt(j)) j++;
  130. //check if the newly found prefix is the longest
  131. if(longestPrefix < j) {
  132. longestPrefix = j;
  133. longestPrefixIndex = i;
  134. }
  135. //If the prefix is the same length as s, then we stop searching because
  136. //words[i] must be the longest prefix.
  137. if(longestPrefix == s.length()) break;
  138. }
  139. if(0 < longestPrefix)
  140. return words[longestPrefixIndex].substring(longestPrefix);
  141. else
  142. return "";
  143. }