コード例 #1
1
ファイル: Util.java プロジェクト: w7cook/batch-javac
 /**
  * Given a string, replace all tabs with the appropriate number of spaces.
  *
  * @param tabLength the length of each tab.
  * @param s the String to scan.
  */
 public static void replaceTabs(int tabLength, StringBuilder s) {
   if (whitespace == null || whitespace.length() < tabLength)
     whitespace = String.format("%" + tabLength + "s", " ");
   int index = 0;
   while ((index = s.indexOf("\t", index)) != -1) {
     int spaceCount = tabLength - index % tabLength;
     s.replace(index, index + 1, whitespace.substring(0, spaceCount));
     index += spaceCount;
   }
 }
コード例 #2
1
ファイル: Util.java プロジェクト: subxiang/jdk-source-code
 /**
  * Given a string, return an array of tokens. The separator can be escaped with the '\' character.
  * The '\' character may also be escaped by the '\' character.
  *
  * @param s the string to tokenize.
  * @param separator the separator char.
  * @param maxTokens the maxmimum number of tokens returned. If the max is reached, the remaining
  *     part of s is appended to the end of the last token.
  * @return an array of tokens.
  */
 public static String[] tokenize(String s, char separator, int maxTokens) {
   List tokens = new ArrayList();
   StringBuilder token = new StringBuilder();
   boolean prevIsEscapeChar = false;
   for (int i = 0; i < s.length(); i += Character.charCount(i)) {
     int currentChar = s.codePointAt(i);
     if (prevIsEscapeChar) {
       // Case 1:  escaped character
       token.appendCodePoint(currentChar);
       prevIsEscapeChar = false;
     } else if (currentChar == separator && tokens.size() < maxTokens - 1) {
       // Case 2:  separator
       tokens.add(token.toString());
       token = new StringBuilder();
     } else if (currentChar == '\\') {
       // Case 3:  escape character
       prevIsEscapeChar = true;
     } else {
       // Case 4:  regular character
       token.appendCodePoint(currentChar);
     }
   }
   if (token.length() > 0) {
     tokens.add(token.toString());
   }
   return (String[]) tokens.toArray(new String[] {});
 }
コード例 #3
0
 protected String makeSpace(int len) {
   if (len <= 0) {
     return "";
   }
   StringBuilder sb = new StringBuilder(len);
   for (int i = 0; i < len; i++) {
     sb.append(' ');
   }
   return sb.toString();
 }
コード例 #4
0
 /**
  * Convert the name to a valid HTML name.
  *
  * @param name the name that needs to be converted to valid HTML name.
  * @return a valid HTML name string.
  */
 public String getName(String name) {
   StringBuilder sb = new StringBuilder();
   char ch;
   /* The HTML 4 spec at http://www.w3.org/TR/html4/types.html#h-6.2 mentions
    * that the name/id should begin with a letter followed by other valid characters.
    * The HTML 5 spec (draft) is more permissive on names/ids where the only restriction
    * is that it should be at least one character long and should not contain spaces.
    * The spec draft is @ http://www.w3.org/html/wg/drafts/html/master/dom.html#the-id-attribute.
    *
    * For HTML 4, we need to check for non-characters at the beginning of the name and
    * substitute it accordingly, "_" and "$" can appear at the beginning of a member name.
    * The method substitutes "$" with "Z:Z:D" and will prefix "_" with "Z:Z".
    */
   for (int i = 0; i < name.length(); i++) {
     ch = name.charAt(i);
     switch (ch) {
       case '(':
       case ')':
       case '<':
       case '>':
       case ',':
         sb.append('-');
         break;
       case ' ':
       case '[':
         break;
       case ']':
         sb.append(":A");
         break;
         // Any appearance of $ needs to be substituted with ":D" and not with hyphen
         // since a field name "P$$ and a method P(), both valid member names, can end
         // up as "P--". A member name beginning with $ needs to be substituted with
         // "Z:Z:D".
       case '$':
         if (i == 0) sb.append("Z:Z");
         sb.append(":D");
         break;
         // A member name beginning with _ needs to be prefixed with "Z:Z" since valid anchor
         // names can only begin with a letter.
       case '_':
         if (i == 0) sb.append("Z:Z");
         sb.append(ch);
         break;
       default:
         sb.append(ch);
     }
   }
   return sb.toString();
 }