Example usage for com.google.gwt.user.server.rpc.impl CharVector getSize

List of usage examples for com.google.gwt.user.server.rpc.impl CharVector getSize

Introduction

In this page you can find the example usage for com.google.gwt.user.server.rpc.impl CharVector getSize.

Prototype

public int getSize() 

Source Link

Usage

From source file:com.foo.server.rpc230.ServerSerializationStreamWriterSenasa.java

License:Apache License

/**
 * This method takes a string and outputs a JavaScript string literal. The
 * data is surrounded with quotes, and any contained characters that need to
 * be escaped are mapped onto their escape sequence.
 * //from   w  ww.  j av a 2  s .  c  o  m
 * Assumptions: We are targeting a version of JavaScript that that is later
 * than 1.3 that supports unicode strings.
 */
public static String escapeString(String toEscape) {
    // make output big enough to escape every character (plus the quotes)
    char[] input = toEscape.toCharArray();
    CharVector charVector = new CharVector(input.length * 2 + 2, input.length);

    charVector.add(JS_QUOTE_CHAR);

    for (int i = 0, n = input.length; i < n; ++i) {
        char c = input[i];
        if (needsUnicodeEscape(c)) {
            unicodeEscape(c, charVector);
        } else {
            charVector.add(c);
        }
    }

    charVector.add(JS_QUOTE_CHAR);
    return String.valueOf(charVector.asArray(), 0, charVector.getSize());
}