/** Check if we are still in the number */ @Override public boolean isWordPart(char c) { // ok, we have to test for scientific notation e.g.: 10.9e10 if ((c == 'x' || c == 'X') && buffer.length() == 1 && buffer.charAt(0) == '0') { // it is an hexadecimal buffer.append(c); isInHexa = true; return true; } else { buffer.append(c); } if (isInHexa) { return Character.isDigit(c) || c == 'a' || c == 'A' || c == 'b' || c == 'B' || c == 'c' || c == 'C' || c == 'd' || c == 'D' || c == 'e' || c == 'E' || c == 'f' || c == 'F'; } else { return Character.isDigit(c) || c == 'e' || c == '.'; } }
/** * Adds spaces after the '#' according to the configured settings. The first char of the buffer * passed (which is also the output) should always start with a '#'. */ public static void formatComment(FormatStd std, FastStringBuffer bufWithComment) { if (std.spacesInStartComment > 0) { Assert.isTrue(bufWithComment.charAt(0) == '#'); int len = bufWithComment.length(); char firstCharFound = '\n'; String bufAsString = bufWithComment.toString(); // handle cases where the code-formatting should not take place if (FileUtils.isPythonShebangLine(bufAsString)) { return; } int spacesFound = 0; String remainingStringContent = ""; for (int j = 1; j < len; j++) { // start at 1 because 0 should always be '#' if ((firstCharFound = bufWithComment.charAt(j)) != ' ') { remainingStringContent = bufAsString.substring(j).trim(); break; } spacesFound += 1; } if (firstCharFound != '\r' && firstCharFound != '\n') { // Only add spaces if it wasn't an empty line. // handle cases where the code-formatting should not take place for (String s : BLOCK_COMMENT_SKIPS) { if (remainingStringContent.endsWith(s) || remainingStringContent.startsWith(s)) { return; } } int diff = std.spacesInStartComment - spacesFound; if (diff > 0) { bufWithComment.insertN(1, ' ', diff); } } } }
public static Map<Integer, String> loadDictFrom( FastBufferedReader reader, FastStringBuffer buf, ObjectsPoolMap objectsPoolMap) throws IOException { int size = StringUtils.parsePositiveInt(reader.readLine()); HashMap<Integer, String> map = new HashMap<Integer, String>(size + 5); FastStringBuffer line; int val = 0; while (true) { line = reader.readLine(); if (line == null) { return map; } else if (line.startsWith("-- ")) { if (line.startsWith("-- END DICTIONARY")) { return map; } throw new RuntimeException("Unexpected line: " + line); } else { int length = line.length(); // line is str=int for (int i = 0; i < length; i++) { char c = line.charAt(i); if (c == '=') { val = StringUtils.parsePositiveInt(buf); buf.clear(); } else { buf.appendResizeOnExc(c); } } String bufStr = buf.toString(); String interned = objectsPoolMap.get(bufStr); if (interned == null) { interned = bufStr; objectsPoolMap.put(bufStr, bufStr); } map.put(val, interned); buf.clear(); } } }
/** * Formats the contents for when a parenthesis is found (so, go until the closing parens and * format it accordingly) * * @param throwSyntaxError * @throws SyntaxErrorException */ private int formatForPar( final ParsingUtils parsingUtils, final char[] cs, final int i, final FormatStd std, final FastStringBuffer buf, final int parensLevel, final String delimiter, boolean throwSyntaxError) throws SyntaxErrorException { char c = ' '; FastStringBuffer locBuf = new FastStringBuffer(); int j = i + 1; int start = j; int end = start; while (j < cs.length && (c = cs[j]) != ')') { j++; if (c == '\'' || c == '"') { // ignore comments or multiline comments... j = parsingUtils.eatLiterals(null, j - 1, std.trimMultilineLiterals) + 1; end = j; } else if (c == '#') { j = parsingUtils.eatComments(null, j - 1) + 1; end = j; } else if (c == '(') { // open another par. if (end > start) { locBuf.append(cs, start, end - start); start = end; } j = formatForPar( parsingUtils, cs, j - 1, std, locBuf, parensLevel + 1, delimiter, throwSyntaxError) + 1; start = j; } else { end = j; } } if (end > start) { locBuf.append(cs, start, end - start); start = end; } if (c == ')') { // Now, when a closing parens is found, let's see the contents of the line where that parens // was found // and if it's only whitespaces, add all those whitespaces (to handle the following case: // a(a, // b // ) <-- we don't want to change this one. char c1; FastStringBuffer buf1 = new FastStringBuffer(); if (locBuf.indexOf('\n') != -1 || locBuf.indexOf('\r') != -1) { for (int k = locBuf.length(); k > 0 && (c1 = locBuf.charAt(k - 1)) != '\n' && c1 != '\r'; k--) { buf1.insert(0, c1); } } String formatStr = formatStr(trim(locBuf).toString(), std, parensLevel, delimiter, throwSyntaxError); FastStringBuffer formatStrBuf = trim(new FastStringBuffer(formatStr, 10)); String closing = ")"; if (buf1.length() > 0 && PySelection.containsOnlyWhitespaces(buf1.toString())) { formatStrBuf.append(buf1); } else if (std.parametersWithSpace) { closing = " )"; } if (std.parametersWithSpace) { if (formatStrBuf.length() == 0) { buf.append("()"); } else { buf.append("( "); buf.append(formatStrBuf); buf.append(closing); } } else { buf.append('('); buf.append(formatStrBuf); buf.append(closing); } return j; } else { if (throwSyntaxError) { throw new SyntaxErrorException("No closing ')' found."); } // we found no closing parens but we finished looking already, so, let's just add anything // without // more formatting... buf.append('('); buf.append(locBuf); return j; } }
/** * This method formats a string given some standard. * * @param str the string to be formatted * @param std the standard to be used * @param parensLevel the level of the parenthesis available. * @return a new (formatted) string * @throws SyntaxErrorException */ private String formatStr( String str, FormatStd std, int parensLevel, String delimiter, boolean throwSyntaxError) throws SyntaxErrorException { char[] cs = str.toCharArray(); FastStringBuffer buf = new FastStringBuffer(); // Temporary buffer for some operations. Must always be cleared before it's used. FastStringBuffer tempBuf = new FastStringBuffer(); ParsingUtils parsingUtils = ParsingUtils.create(cs, throwSyntaxError); char lastChar = '\0'; for (int i = 0; i < cs.length; i++) { char c = cs[i]; switch (c) { case '\'': case '"': // ignore literals and multi-line literals, including comments... i = parsingUtils.eatLiterals(buf, i, std.trimMultilineLiterals); break; case '#': i = handleComment(std, cs, buf, tempBuf, parsingUtils, i); break; case ',': i = formatForComma(std, cs, buf, i, tempBuf); break; case '(': i = formatForPar( parsingUtils, cs, i, std, buf, parensLevel + 1, delimiter, throwSyntaxError); break; // Things to treat: // +, -, *, /, % // ** // << >> // <, >, !=, <>, <=, >=, //=, *=, /=, // & ^ ~ | case '*': // for *, we also need to treat when it's used in varargs, kwargs and list expansion boolean isOperator = false; for (int j = buf.length() - 1; j >= 0; j--) { char localC = buf.charAt(j); if (Character.isWhitespace(localC)) { continue; } if (localC == '(' || localC == ',') { // it's not an operator, but vararg. kwarg or list expansion } if (Character.isJavaIdentifierPart(localC)) { // ok, there's a chance that it can be an operator, but we still have to check // the chance that it's a wild import tempBuf.clear(); while (Character.isJavaIdentifierPart(localC)) { tempBuf.append(localC); j--; if (j < 0) { break; // break while } localC = buf.charAt(j); } String reversed = tempBuf.reverse().toString(); if (!reversed.equals("import") && !reversed.equals("lambda")) { isOperator = true; } } if (localC == '\'' || localC == ')' || localC == ']') { isOperator = true; } // If it got here (i.e.: not whitespace), get out of the for loop. break; } if (!isOperator) { buf.append('*'); break; // break switch } // Otherwise, FALLTHROUGH case '+': case '-': if (c == '-' || c == '+') { // could also be * // handle exponentials correctly: e.g.: 1e-6 cannot have a space tempBuf.clear(); boolean started = false; for (int j = buf.length() - 1; ; j--) { if (j < 0) { break; } char localC = buf.charAt(j); if (localC == ' ' || localC == '\t') { if (!started) { continue; } else { break; } } started = true; if (Character.isJavaIdentifierPart(localC) || localC == '.') { tempBuf.append(localC); } else { break; // break for } } boolean isExponential = true; String partialNumber = tempBuf.reverse().toString(); int partialLen = partialNumber.length(); if (partialLen < 2 || !Character.isDigit(partialNumber.charAt(0))) { // at least 2 chars: the number and the 'e' isExponential = false; } else { // first char checked... now, if the last is an 'e', we must leave it together no // matter what if (partialNumber.charAt(partialLen - 1) != 'e' && partialNumber.charAt(partialLen - 1) != 'E') { isExponential = false; } } if (isExponential) { buf.rightTrim(); buf.append(c); // skip the next whitespaces from the buffer int initial = i; do { i++; } while (i < cs.length && (c = cs[i]) == ' ' || c == '\t'); if (i > initial) { i--; // backup 1 because we walked 1 too much. } break; // break switch } // Otherwise, FALLTHROUGH } case '/': case '%': case '<': case '>': case '!': case '&': case '^': case '~': case '|': i = handleOperator(std, cs, buf, parsingUtils, i, c); c = cs[i]; break; // check for = and == (other cases that have an = as the operator should already be // treated) case '=': if (i < cs.length - 1 && cs[i + 1] == '=') { // if == handle as if a regular operator i = handleOperator(std, cs, buf, parsingUtils, i, c); c = cs[i]; break; } while (buf.length() > 0 && buf.lastChar() == ' ') { buf.deleteLast(); } boolean surroundWithSpaces = std.operatorsWithSpace; if (parensLevel > 0) { surroundWithSpaces = std.assignWithSpaceInsideParens; } // add space before if (surroundWithSpaces) { buf.append(' '); } // add the operator and the '=' buf.append('='); // add space after if (surroundWithSpaces) { buf.append(' '); } i = parsingUtils.eatWhitespaces(null, i + 1); break; default: if (c == '\r' || c == '\n') { if (lastChar == ',' && std.spaceAfterComma && buf.lastChar() == ' ') { buf.deleteLast(); } if (std.trimLines) { buf.rightTrim(); } } buf.append(c); } lastChar = c; } if (parensLevel == 0 && std.trimLines) { buf.rightTrim(); } return buf.toString(); }