// tokenizes a string for PBM and PGM headers and plain PBM and PGM data. If oneChar is true, // then // rather than parsing a whole string, a single character is read (not including whitespace or // comments) static String tokenizeString(InputStream stream, boolean oneChar) throws IOException { final int EOF = -1; StringBuilder b = new StringBuilder(); int c; boolean inComment = false; while (true) { c = stream.read(); if (c == EOF) throw new IOException( "Stream ended prematurely, before table reading was completed."); // no more tokens else if (inComment) { if (c == '\r' || c == '\n') // escape the comment inComment = false; else { } // do nothing } else if (Character.isWhitespace((char) c)) { } // do nothing else if (c == '#') // start of a comment { inComment = true; } else // start of a string { b.append((char) c); break; } } if (oneChar) return b.toString(); // at this point we have a valid string. We read until whitespace or a # while (true) { c = stream.read(); if (c == EOF) break; else if (c == '#') // start of comment, read until a '\n' { while (true) { c = stream.read(); // could hit EOF, which is fine if (c == EOF) break; else if (c == '\r' || c == '\n') break; } // break; // comments are not delimiters } else if (Character.isWhitespace((char) c)) break; else b.append((char) c); } return b.toString(); }
public void mouseMoved(MouseEvent e) { int k = html.viewToModel(e.getPoint()); if (html.hasFocus() && html.getSelectionStart() <= k && k < html.getSelectionEnd()) { setMessage("(on selection)", MOVE); return; } String s = text.getText(); // ""; int m = s.length(); // html.getDocument().getLength(); /*try { s = html.getText(0, m); } catch (BadLocationException x) { setMessage("BadLocation "+m, TEXT); return; } */ if (!Character.isLetter(s.charAt(k))) { setMessage("(not a letter)", TEXT); return; } selB = k; selE = k + 1; while (!Character.isWhitespace(s.charAt(selB - 1))) selB--; while (!Character.isWhitespace(s.charAt(selE))) selE++; setMessage(selB + "-" + selE, HAND); word = ""; for (int i = selB; i < selE; i++) if (Character.isLetter(s.charAt(i))) word += s.charAt(i); html.setToolTipText(word); }
private boolean isToStopAt(char toCheck, char former) { if (isInStopAtArray(former) || isInStopAtArray(toCheck)) { return true; } else if (false == Character.isWhitespace(former) && Character.isWhitespace(toCheck) || Character.isWhitespace(former) && false == Character.isWhitespace(toCheck)) // else if(Character.isWhitespace(former) && false == Character.isWhitespace(toCheck)) { return true; } return false; }
String cleanupSource(String source) { if (source.isEmpty()) { return source.concat("\n"); } int lastChIdx = source.length() - 1; if (source.charAt(lastChIdx) != '\n') { // Append a newline at the end source = source.concat("\n"); } else { // Remove all newlines at the end but one while (lastChIdx >= 1) { Character ch1 = source.charAt(lastChIdx); if (ch1 == '\n' || Character.isWhitespace(ch1)) { source = source.substring(0, lastChIdx--); } else { break; } } source = source.concat("\n"); } return source; }
private boolean needToShiftWhiteSpaces(final DocumentEvent e) { if (!CharArrayUtil.containsOnlyWhiteSpaces(e.getNewFragment()) || CharArrayUtil.containLineBreaks(e.getNewFragment())) return e.getOldLength() > 0; if (e.getOffset() == 0) return false; final char charBefore = myEditor.getDocument().getCharsSequence().charAt(e.getOffset() - 1); // final char charAfter = myEditor.getDocument().getCharsSequence().charAt(e.getOffset() + // e.getNewLength()); return Character.isWhitespace(charBefore) /* || !Character.isWhitespace(charAfter)*/; }
public static int[] findspaces(String text) { java.util.List<Integer> l = new ArrayList<Integer>(); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); if (Character.isWhitespace(c)) l.add(i); } int[] ret = new int[l.size()]; for (int i = 0; i < ret.length; i++) ret[i] = l.get(i); return (ret); }
/** * This is the write() method of the stream. All Writer subclasses implement this. All other * versions of write() are variants of this one */ public void write(char[] buffer, int index, int len) { synchronized (this.lock) { // Loop through all the characters passed to us for (int i = index; i < index + len; i++) { // If we haven't begun a page (or a new page), do that now. if (page == null) newpage(); // If the character is a line terminator, then begin new line, // unless it is a \n immediately after a \r. if (buffer[i] == '\n') { if (!last_char_was_return) newline(); continue; } if (buffer[i] == '\r') { newline(); last_char_was_return = true; continue; } else last_char_was_return = false; // If it some other non-printing character, ignore it. if (Character.isWhitespace(buffer[i]) && !Character.isSpaceChar(buffer[i]) && (buffer[i] != '\t')) continue; // If no more characters will fit on the line, start a new line. if (charnum >= chars_per_line) { newline(); if (page == null) newpage(); // and start a new page, if necessary } // Now print the character: // If it is a space, skip one space, without output. // If it is a tab, skip the necessary number of spaces. // Otherwise, print the character. // It is inefficient to draw only one character at a time, but // because our FontMetrics don't match up exactly to what the // printer uses we need to position each character individually. if (Character.isSpaceChar(buffer[i])) charnum++; else if (buffer[i] == '\t') charnum += 8 - (charnum % 8); else { page.drawChars( buffer, i, 1, x0 + charnum * charwidth, y0 + (linenum * lineheight) + lineascent); charnum++; } } } }
public String ucWords(String wordsToConvert) { wordsToConvert = wordsToConvert.toLowerCase(); Boolean ucChar = false; String convertedString = ""; for (int i = 0; i < wordsToConvert.length(); i++) { if (i == 0 | ucChar) { convertedString += Character.toUpperCase(wordsToConvert.charAt(i)); } else { convertedString += Character.toString(wordsToConvert.charAt(i)); } if (Character.isWhitespace(wordsToConvert.charAt(i))) { ucChar = true; } else { ucChar = false; } } return convertedString; }
protected static String parsePhrase(final String lineText) { boolean overloading = false; { // Check if we can provide suggestions for this phrase ending String trimmedLineText = lineText.trim(); if (trimmedLineText.length() == 0) return null; char lastChar = trimmedLineText.charAt(trimmedLineText.length() - 1); if (lastChar == '.') { trimmedLineText = trimmedLineText.substring(0, trimmedLineText.length() - 1).trim(); if (trimmedLineText.length() == 0) return null; lastChar = trimmedLineText.charAt(trimmedLineText.length() - 1); switch (lastChar) { case ')': case ']': case '"': break; // We can suggest for these default: if (!Character.isJavaIdentifierPart(lastChar)) { return null; // Not something we can suggest } break; } } else if (lastChar == '(') { overloading = true; // We can suggest overloaded methods } else if (!Character.isJavaIdentifierPart(lastChar)) { return null; // Not something we can suggest } } final int currentCharIndex = lineText.length() - 1; { // Check if the caret is in the comment int commentStart = lineText.indexOf("//", 0); if (commentStart >= 0 && currentCharIndex > commentStart) { return null; } } // Index the line BitSet isInLiteral = new BitSet(lineText.length()); BitSet isInBrackets = new BitSet(lineText.length()); { // Mark parts in literals boolean inString = false; boolean inChar = false; boolean inEscaped = false; for (int i = 0; i < lineText.length(); i++) { if (!inEscaped) { switch (lineText.charAt(i)) { case '\"': if (!inChar) inString = !inString; break; case '\'': if (!inString) inChar = !inChar; break; case '\\': if (inString || inChar) { inEscaped = true; } break; } } else { inEscaped = false; } isInLiteral.set(i, inString || inChar); } } if (isInLiteral.get(currentCharIndex)) return null; { // Mark parts in top level brackets int depth = overloading ? 1 : 0; int bracketStart = overloading ? lineText.length() : 0; int squareDepth = 0; int squareBracketStart = 0; bracketLoop: for (int i = lineText.length() - 1; i >= 0; i--) { if (!isInLiteral.get(i)) { switch (lineText.charAt(i)) { case ')': if (depth == 0) bracketStart = i; depth++; break; case '(': depth--; if (depth == 0) { isInBrackets.set(i, bracketStart); } else if (depth < 0) { break bracketLoop; } break; case ']': if (squareDepth == 0) squareBracketStart = i; squareDepth++; break; case '[': squareDepth--; if (squareDepth == 0) { isInBrackets.set(i, squareBracketStart); } else if (squareDepth < 0) { break bracketLoop; } break; } } } if (depth > 0) isInBrackets.set(0, bracketStart); if (squareDepth > 0) isInBrackets.set(0, squareBracketStart); } // Walk the line from the end while it makes sense int position = currentCharIndex; parseLoop: while (position >= 0) { int currChar = lineText.charAt(position); switch (currChar) { case '.': // Grab it position--; break; case '[': break parseLoop; // End of scope case ']': // Grab the whole region in square brackets position = isInBrackets.previousClearBit(position - 1); break; case '(': if (isInBrackets.get(position)) { position--; // This checks for first bracket while overloading break; } break parseLoop; // End of scope case ')': // Grab the whole region in brackets position = isInBrackets.previousClearBit(position - 1); break; case '"': // Grab the whole literal and quit position = isInLiteral.previousClearBit(position - 1); break parseLoop; default: if (Character.isJavaIdentifierPart(currChar)) { position--; // Grab the identifier } else if (Character.isWhitespace(currChar)) { position--; // Grab whitespace too } else { break parseLoop; // Got a char ending the phrase } break; } } position++; // Extract phrase String phrase = lineText.substring(position, lineText.length()).trim(); Messages.log(phrase); if (phrase.length() == 0 || Character.isDigit(phrase.charAt(0))) { return null; // Can't suggest for numbers or empty phrases } return phrase; }
public void keyTyped(KeyEvent e) { if (debug) { System.out.println( "--- RecordingModule: key typed = " + e + "\n > Key char->int = " + (int) e.getKeyChar()); System.out.println(" -- isActionKey() = " + e.isActionKey()); System.out.println(" -- isISOControl() = " + Character.isISOControl(e.getKeyChar())); System.out.println(" -- isWhitespace() = " + Character.isWhitespace(e.getKeyChar())); } if (isKeyReserved(e)) { return; } if (enabled && !readOnly && lastKeyPressEvent != null) { if (enableKeyboard) { boolean replace = false; String text = ""; if (isControl(e)) { if (lastKeyPressEvent.getKeyCode() == KeyEvent.VK_ENTER) { // Change the Type cmd prior to Typeline if the delay from the last type key is less // than 1 sec if (useTypeline && e.getModifiers() == 0 && lastElement != null) { String s = DocumentUtils.getElementText(lastElement); if (s.startsWith("Type ") && (System.currentTimeMillis() - lastInsertTime) < typelineDelay) { replace = true; text = s.replaceFirst("Type", "Typeline"); } } } if ("".equals(text)) { int count = 1; KeyEvent e2; long lastEventTime = e.getWhen(); // We go through the vector of events and check whether there are events corresponding // to a typed text. for (int i = 0; i < events.size() && events.get(i) instanceof KeyEvent; i++) { e2 = (KeyEvent) events.get(i); if (e.getID() == e2.getID() && e.getKeyChar() == e2.getKeyChar() && e.getKeyCode() == e2.getKeyCode() && e.getModifiers() == e2.getModifiers() && (lastEventTime - e2.getWhen() < keyMutiDelay)) { count++; replace = true; lastEventTime = e2.getWhen(); } else { break; } } text = "Press "; // String modifiers = KeyEvent.getKeyModifiersText(e.getModifiers()); String modifiers = parser.modifiersToString(e.getModifiers()); if (!"".equals(modifiers)) { text += modifiers + "+"; } String charText = KeyEvent.getKeyText(lastKeyPressEvent.getKeyCode()); if (charText == null) { charText = "<unknown>"; } text += charText; if (count > 1) { text += " " + PressCommand.PARAM_COUNT + "=" + count; } if (debug) { System.out.println("--- RecordingModule: Inserting '" + text + "'"); } } } else { text = "" + e.getKeyChar(); KeyEvent e2; // We go through the vector of events and check whether there are events corresponding to // a typed text. for (int i = 0; i < events.size() && events.get(i) instanceof KeyEvent; i++) { e2 = (KeyEvent) events.get(i); if (!isControl(e2) && !e2.isActionKey()) { text = e2.getKeyChar() + text; replace = true; } else { break; } } text = "Type \"" + Utils.escapeUnescapedDoubleQuotes(text) + "\""; } // Insert the command to the current editor insertLine(text, replace, true, false); } insertEvent(e); } }
@Override protected void initChildren(Element eThis) throws OmException { Node nPrevious = eThis.getPreviousSibling(); if (nPrevious != null && nPrevious instanceof Text) { String sText = ((Text) nPrevious).getData(); if (sText.length() > 0 && Character.isWhitespace(sText.charAt(sText.length() - 1))) bSpaceBefore = true; } Node nAfter = eThis.getNextSibling(); if (nAfter != null && nAfter instanceof Text) { String sText = ((Text) nAfter).getData(); if (sText.length() > 0 && Character.isWhitespace(sText.charAt(0))) bSpaceAfter = true; } List<Place> lPlaces = new LinkedList<Place>(); int iPlace = 0; StringBuffer sbText = new StringBuffer(); for (Node n = eThis.getFirstChild(); n != null; n = n.getNextSibling()) { if (n instanceof Element) { Element eplace = (Element) n; if (!eplace.getTagName().equals("eplace")) throw new OmFormatException("<equation> may only contain text and <eplace> tags"); Element[] aeChildren = XML.getChildren(eplace); QComponent qcChild; boolean bImplicit = false; if (aeChildren.length != 1) // Treats more than one child as inside <t> { qcChild = getQDocument().build(this, eplace, "t"); bImplicit = true; } else // Treats single child as specific component (auto-sizing works) qcChild = getQDocument().build(this, aeChildren[0], null); addChild(qcChild); // Must be stored in standard child array so it // can be found etc. // See if width/height is specified int iWidth, iHeight; if (eplace.hasAttribute("width") && eplace.hasAttribute("height")) { try { iWidth = Integer.parseInt(eplace.getAttribute("width")); iHeight = Integer.parseInt(eplace.getAttribute("height")); } catch (NumberFormatException nfe) { throw new OmFormatException("<equation> <eplace>: width= and height= must be integers"); } } else { Dimension d = qcChild.getApproximatePixelSize(); if (d == null) throw new OmFormatException( "<equation> <eplace>: Except for components that support automatic " + "size estimation and fixing, <eplace> must include width= and height="); iWidth = d.width; iHeight = d.height; } Place p = new Place(); p.sID = "p" + (iPlace++); p.qc = qcChild; p.iWidth = iWidth; p.iHeight = iHeight; p.bImplicit = bImplicit; if (!eplace.hasAttribute("label")) throw new OmFormatException("<equation> <eplace>: Must include label="); if (eplace.hasAttribute("label")) p.sLabel = eplace.getAttribute("label"); else p.sLabel = null; if (eplace.hasAttribute("for")) p.sLabelFor = eplace.getAttribute("for"); else if (qcChild instanceof Labelable) p.sLabelFor = qcChild.getID(); lPlaces.add(p); // Add in the equation format text representing the placeholder sbText.append("\\placeholder{" + p.sID + "}{" + p.iWidth + "," + p.iHeight + "}"); } else if (n instanceof Text) { sbText.append(n.getNodeValue()); } } sEquation = sbText.toString(); apPlaces = lPlaces.toArray(new Place[0]); }
/** * Checks whether the character is a delimiter. * * @param character the character to check * @return true if a delimiter */ public boolean isDelimiter(String character) { return Character.isWhitespace(character.charAt(0)) || (m_Delimiters.indexOf(character.charAt(0)) > -1); }