Java Code Examples for java.text.StringCharacterIterator#first()

The following examples show how to use java.text.StringCharacterIterator#first() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JackOSClass.java    From nand2tetris-emu with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts a java string to a Jack Int according to the conversion
 * of the Jack OS API: converts until the first non-digit character in
 * the line (a - as a first character os allowed and is not considered
 * such a non-digit character).
 */
public static short javaStringToInt(String str) {
	StringCharacterIterator i = new StringCharacterIterator(str);
	i.first();
	boolean neg = false;
	if (i.current() == '-') {
		neg = true;
		i.next();
	}
	int value = 0;
	for (;i.current() != CharacterIterator.DONE; i.next()) {
		char c = i.current();
		if (i.current() < '0' || c > '9')
			break;
		value = value*10 + (c-'0');
	}
	if (neg) {
		return (short)-value;
	} else {
		return (short)value;
	}
}
 
Example 2
Source File: XMLOutput.java    From pegasus with Apache License 2.0 6 votes vote down vote up
/**
 * Escapes certain characters inappropriate for textual output.
 *
 * <p>Since this method does not hurt, and may be useful in other regards, it will be retained
 * for now.
 *
 * @param original is a string that needs to be quoted
 * @return a string that is "safe" to print.
 */
public static String escape(String original) {
    if (original == null) return null;
    StringBuilder result = new StringBuilder(2 * original.length());
    StringCharacterIterator i = new StringCharacterIterator(original);
    for (char ch = i.first(); ch != StringCharacterIterator.DONE; ch = i.next()) {
        if (ch == '\r') {
            result.append("\\r");
        } else if (ch == '\n') {
            result.append("\\n");
        } else if (ch == '\t') {
            result.append("\\t");
        } else {
            // DO NOT escape apostrophe. If apostrophe escaping is required,
            // do it beforehand.
            if (ch == '\"' || ch == '\\') result.append('\\');
            result.append(ch);
        }
    }

    return result.toString();
}
 
Example 3
Source File: JSONValidator.java    From wechat-pay-sdk with MIT License 6 votes vote down vote up
private boolean valid(String input) {
    if ("".equals(input)) return true;

    boolean ret = true;
    it = new StringCharacterIterator(input);
    c = it.first();
    col = 1;
    if (!value()) {
        ret = error("value", 1);
    } else {
        skipWhiteSpace();
        if (c != CharacterIterator.DONE) {
            ret = error("end", col);
        }
    }

    return ret;
}
 
Example 4
Source File: JSONValidator.java    From alipay-sdk with Apache License 2.0 6 votes vote down vote up
private boolean valid(String input) {
    if ("".equals(input)) return true;
    
    boolean ret = true;
    it = new StringCharacterIterator(input);
    c = it.first();
    col = 1;
    if (!value()) {
        ret = error("value", 1);
    } else {
        skipWhiteSpace();
        if (c != CharacterIterator.DONE) {
            ret = error("end", col);
        }
    }
    
    return ret;
}
 
Example 5
Source File: JSONValidator.java    From pay with Apache License 2.0 6 votes vote down vote up
private boolean valid(String input) {
    if ("".equals(input)) return true;
    
    boolean ret = true;
    it = new StringCharacterIterator(input);
    c = it.first();
    col = 1;
    if (!value()) {
        ret = error("value", 1);
    } else {
        skipWhiteSpace();
        if (c != CharacterIterator.DONE) {
            ret = error("end", col);
        }
    }
    
    return ret;
}
 
Example 6
Source File: JsonValidatorUtil.java    From xiaoV with GNU General Public License v3.0 6 votes vote down vote up
private boolean valid(String input) {
	if ("".equals(input))
		return true;

	boolean ret = true;
	it = new StringCharacterIterator(input);
	c = it.first();
	col = 1;
	if (!value()) {
		ret = error("value", 1);
	} else {
		skipWhiteSpace();
		if (c != CharacterIterator.DONE) {
			ret = error("end", col);
		}
	}

	return ret;
}
 
Example 7
Source File: AliceAIM.java    From charliebot with GNU General Public License v2.0 6 votes vote down vote up
private StringBuffer imEscape(String s) {
    StringBuffer stringbuffer = new StringBuffer();
    StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
    for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next()) {
        switch (c) {
            case 34: // '"'
            case 36: // '$'
            case 40: // '('
            case 41: // ')'
            case 91: // '['
            case 92: // '\\'
            case 93: // ']'
            case 123: // '{'
            case 125: // '}'
                stringbuffer.append("\\");
                break;
        }
        stringbuffer.append(c);
    }

    stringbuffer.append("\"\0");
    return stringbuffer;
}
 
Example 8
Source File: Toolkit.java    From charliebot with GNU General Public License v2.0 6 votes vote down vote up
public static ArrayList wordSplit(String s) {
    ArrayList arraylist = new ArrayList();
    int i = s.length();
    if (i == 0) {
        arraylist.add("");
        return arraylist;
    }
    int j = 0;
    StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
    for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next())
        if (c == ' ') {
            int k = stringcharacteriterator.getIndex();
            arraylist.add(s.substring(j, k));
            j = k + 1;
        }

    if (j < s.length())
        arraylist.add(s.substring(j));
    return arraylist;
}
 
Example 9
Source File: HackController.java    From nand2tetris-emu with GNU General Public License v2.0 5 votes vote down vote up
private static boolean compareLineWithTemplate(String out, String cmp) {
    if (out.length() != cmp.length()) {
        return false;
    }
    StringCharacterIterator outi = new StringCharacterIterator(out);
    StringCharacterIterator cmpi = new StringCharacterIterator(cmp);
    for (outi.first(), cmpi.first();
         outi.current() != CharacterIterator.DONE;
         outi.next(), cmpi.next()) {
        if (cmpi.current() != '*' && outi.current() != cmpi.current()) {
            return false;
        }
    }
    return true;
}
 
Example 10
Source File: AssemblyLineTokenizer.java    From nand2tetris-emu with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes space characters from the given string.
 */
private static String removeSpaces(String line) {
	StringBuffer nospc = new StringBuffer();
       StringCharacterIterator i = new StringCharacterIterator(line);
       for (i.first(); i.current() != CharacterIterator.DONE; i.next()) {
		if (i.current() != ' ') {
			nospc.append(i.current());
		}
	}
	return nospc.toString();
}
 
Example 11
Source File: TrieTest.java    From staash with Apache License 2.0 5 votes vote down vote up
public boolean addWord(String word) {
    word = word.toUpperCase();
    
    StringCharacterIterator iter = new StringCharacterIterator(word);
    TrieNode current = root;
    for (Character ch = iter.first(); ch != CharacterIterator.DONE; ch = iter.next()) {
        current = current.getOrCreateChild(ch);
    }
    current.setIsWord(true);
    return true;
}
 
Example 12
Source File: InputNormalizer.java    From charliebot with GNU General Public License v2.0 5 votes vote down vote up
public static String patternFitIgnoreCase(String s) {
    s = Toolkit.removeMarkup(s);
    StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
    StringBuffer stringbuffer = new StringBuffer(s.length());
    for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next())
        if (!Character.isLetterOrDigit(c))
            stringbuffer.append(' ');
        else
            stringbuffer.append(c);

    return Toolkit.filterWhitespace(stringbuffer.toString());
}
 
Example 13
Source File: InputNormalizer.java    From charliebot with GNU General Public License v2.0 5 votes vote down vote up
public static String patternFit(String s) {
    s = Toolkit.removeMarkup(s);
    StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
    StringBuffer stringbuffer = new StringBuffer(s.length());
    for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next())
        if (!Character.isLetterOrDigit(c) && c != '*' && c != '_')
            stringbuffer.append(' ');
        else
            stringbuffer.append(Character.toUpperCase(c));

    return Toolkit.filterWhitespace(stringbuffer.toString());
}
 
Example 14
Source File: XMLOutput.java    From pegasus with Apache License 2.0 5 votes vote down vote up
/**
 * Escapes certain characters inappropriate for XML content output.
 *
 * <p>According to the <a href="http://www.w3.org/TR/2008/REC-xml-20081126/#NT-AttValue">XML 1.0
 * Specification</a>, an attribute cannot contain ampersand, percent, nor the character that was
 * used to quote the attribute value.
 *
 * @param original is a string that needs to be quoted
 * @param isAttribute denotes an attributes value, if set to true. If false, it denotes regular
 *     XML content outside of attributes.
 * @return a string that is "safe" to print as XML.
 */
public static String quote(String original, boolean isAttribute) {
    if (original == null) return null;
    StringBuilder result = new StringBuilder(2 * original.length());
    StringCharacterIterator i = new StringCharacterIterator(original);
    for (char ch = i.first(); ch != StringCharacterIterator.DONE; ch = i.next()) {
        switch (ch) {
            case '<':
                if (isAttribute) result.append("&#60;");
                else result.append("&lt;");
                break;
            case '&':
                if (isAttribute) result.append("&#38;");
                else result.append("&amp;");
                break;
            case '>':
                // greater-than does not need to be attribute-escaped,
                // but standard does not say we must not do it, either.
                if (isAttribute) result.append("&#62;");
                else result.append("&gt;");
                break;
            case '\'':
                if (isAttribute) result.append("&#39;");
                else result.append("&apos;");
                break;
            case '\"':
                if (isAttribute) result.append("&#34;");
                else result.append("&quot;");
                break;
            default:
                result.append(ch);
                break;
        }
    }

    return result.toString();
}
 
Example 15
Source File: BpmnXMLUtil.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
public static List<String> parseDelimitedList(String s) {
    List<String> result = new ArrayList<>();
    if (StringUtils.isNotEmpty(s)) {

        StringCharacterIterator iterator = new StringCharacterIterator(s);
        char c = iterator.first();

        StringBuilder strb = new StringBuilder();
        boolean insideExpression = false;

        while (c != StringCharacterIterator.DONE) {
            if (c == '{' || c == '$') {
                insideExpression = true;
            } else if (c == '}') {
                insideExpression = false;
            } else if (c == ',' && !insideExpression) {
                result.add(strb.toString().trim());
                strb.delete(0, strb.length());
            }

            if (c != ',' || insideExpression) {
                strb.append(c);
            }

            c = iterator.next();
        }

        if (strb.length() > 0) {
            result.add(strb.toString().trim());
        }

    }
    return result;
}
 
Example 16
Source File: CmmnXmlUtil.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
public static List<String> parseDelimitedList(String s) {
    List<String> result = new ArrayList<>();
    if (StringUtils.isNotEmpty(s)) {

        StringCharacterIterator iterator = new StringCharacterIterator(s);
        char c = iterator.first();

        StringBuilder strb = new StringBuilder();
        boolean insideExpression = false;

        while (c != StringCharacterIterator.DONE) {
            if (c == '{' || c == '$') {
                insideExpression = true;
            } else if (c == '}') {
                insideExpression = false;
            } else if (c == ',' && !insideExpression) {
                result.add(strb.toString().trim());
                strb.delete(0, strb.length());
            }

            if (c != ',' || insideExpression) {
                strb.append(c);
            }

            c = iterator.next();
        }

        if (strb.length() > 0) {
            result.add(strb.toString().trim());
        }

    }
    return result;
}
 
Example 17
Source File: RestfulMockSortingUtils.java    From smockin with Apache License 2.0 5 votes vote down vote up
private String leftPad(String stringToPad, String padder, Integer size) {

        final StringBuilder sb = new StringBuilder(size.intValue());
        final StringCharacterIterator sci = new StringCharacterIterator(padder);

        while (sb.length() < (size.intValue() - stringToPad.length())) {
            for (char ch = sci.first(); ch != CharacterIterator.DONE; ch = sci.next()) {
                if (sb.length() < (size.intValue() - stringToPad.length())) {
                    sb.insert(sb.length(), String.valueOf(ch));
                }
            }
        }

        return sb.append(stringToPad).toString();
    }
 
Example 18
Source File: JackOSClass.java    From nand2tetris-emu with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts a java string to a Jack String by using whatever implementation
 * of the Jack class String is available (String.vm if available, else
 * built-in): Constructs a string using String.new and then fills it
 * using String.appendChar.
 * Returns the VM address to the Jack String.
 */
public static short javaStringToJackStringUsingVM(String javaStr) throws TerminateVMProgramThrowable {
	if (javaStr.length() == 0) {
		return callFunction("String.new", 1);
	}
	short jackStr = callFunction("String.new", javaStr.length());
       StringCharacterIterator i = new StringCharacterIterator(javaStr);
       for (i.first(); i.current() != CharacterIterator.DONE; i.next()) {
		callFunction("String.appendChar", jackStr, i.current());
	}
	return jackStr;
}
 
Example 19
Source File: TrieTest.java    From staash with Apache License 2.0 5 votes vote down vote up
public boolean containsWord(String word) {
    word = word.toUpperCase();
    StringCharacterIterator iter = new StringCharacterIterator(word);
    TrieNode current = root;
    for (Character ch = iter.first(); ch != CharacterIterator.DONE; ch = iter.next()) {
        current = current.getChild(ch);
        if (current == null)
            return false;
    }
    return current.isWord();
}
 
Example 20
Source File: PatternArbiter.java    From charliebot with GNU General Public License v2.0 4 votes vote down vote up
public static void checkAIMLPattern(String s, boolean flag)
        throws NotAnAIMLPatternException {
    StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
    boolean flag1 = true;
    int j = 1;
    for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next()) {
        int i;
        if (!Character.isLetterOrDigit(c)) {
            i = 32;
            if (Character.isWhitespace(c)) {
                i |= 0x10;
                if (c == ' ')
                    i |= 8;
                else
                    throw new NotAnAIMLPatternException("The only allowed whitespace is a space ( ).", s);
            }
            if (c == '*' || c == '_') {
                i |= 2;
                if (j != 1 && (j == 4 || (j & 2) == 2))
                    throw new NotAnAIMLPatternException("A wildcard cannot be preceded by a wildcard, a letter or a digit.", s);
            }
            if (c == '<') {
                int k = stringcharacteriterator.getIndex();
                if (s.regionMatches(false, k, "<bot name=\"", 0, 11)) {
                    stringcharacteriterator.setIndex(k + 11);
                    for (c = stringcharacteriterator.next(); c != '\uFFFF' && c != '"' && (Character.isLetterOrDigit(c) || c == ' ' || c == '_'); c = stringcharacteriterator.next())
                        ;
                    k = stringcharacteriterator.getIndex();
                    if (!s.regionMatches(false, k, "\"/>", 0, 3))
                        throw new NotAnAIMLPatternException("Invalid or malformed <bot/> element.", s);
                    stringcharacteriterator.setIndex(k + 3);
                } else {
                    throw new NotAnAIMLPatternException("Invalid or malformed inner element.", s);
                }
            }
        } else {
            i = 4;
            if (!flag && Character.toUpperCase(c) != c)
                throw new NotAnAIMLPatternException("Characters with case mappings must be uppercase.", s);
            if (j != 1 && (j & 2) == 2)
                throw new NotAnAIMLPatternException("A letter or digit may not be preceded by a wildcard.", s);
        }
        j = i;
    }

}