public int charat(int x) {
            int l = 0, r = text.length() + 1;
            while (true) {
                int p = (l + r) / 2;
                int a = advance(p);
                if ((a < x) && (l < p)) {
                    l = p;
                } else if ((a > x) && (r > p)) {
                    r = p;
                } else {
                    return (p);
                }
            }
        }