/** * Sets the output text. * * @param t output text * @param s text size */ public final void setText(final byte[] t, final int s) { // remove invalid characters and compare old with new string int ns = 0; final int ts = text.size(); final byte[] tt = text.text(); boolean eq = true; for (int r = 0; r < s; ++r) { final byte b = t[r]; // support characters, highlighting codes, tabs and newlines if (b >= ' ' || b <= TokenBuilder.MARK || b == 0x09 || b == 0x0A) { t[ns++] = t[r]; } eq &= ns < ts && ns < s && t[ns] == tt[ns]; } eq &= ns == ts; // new text is different... if (!eq) { text = new BaseXTextTokens(Arrays.copyOf(t, ns)); rend.setText(text); scroll.pos(0); } if (undo != null) undo.store(t.length != ns ? Arrays.copyOf(t, ns) : t, 0); SwingUtilities.invokeLater(calc); }
/** Finishes the index creation. */ void finish() { if (reorder == null) return; for (int i = 1; i < reorder.size(); i++) { if (reorder.get(i)) Arrays.sort(idsList.get(i), 0, lenList.get(i)); } reorder = null; }
@Override public void rehash() { super.rehash(); final int s = size << 1; ids = Array.copyOf(ids, s); len = Arrays.copyOf(len, s); }
/** * Adds part of a byte array to the token. * * @param value the byte array to be added * @param start start position * @param end end position * @return self reference */ public TokenBuilder add(final byte[] value, final int start, final int end) { byte[] chrs = chars; final int cl = chrs.length, l = end - start, s = size, ns = s + l; if (ns > cl) chrs = Arrays.copyOf(chrs, Array.newSize(ns)); System.arraycopy(value, start, chrs, s, l); chars = chrs; size = ns; return this; }
/** * Adds a single byte to the token. * * @param value the byte to be added * @return self reference */ public TokenBuilder addByte(final byte value) { byte[] chrs = chars; int s = size; if (s == chrs.length) chrs = Arrays.copyOf(chrs, Array.newSize(s)); chrs[s++] = value; chars = chrs; size = s; return this; }
/** * Scans an external ID. * * @param f full flag * @param r root flag * @return id * @throws IOException I/O exception */ private byte[] externalID(final boolean f, final boolean r) throws IOException { byte[] cont = null; final boolean pub = consume(PUBLIC); if (pub || consume(SYSTEM)) { checkS(); if (pub) { pubidLit(); if (f) checkS(); } final int qu = consume(); // [11] if (qu == '\'' || qu == '"') { int ch; final TokenBuilder tok = new TokenBuilder(); while ((ch = nextChar()) != qu) tok.add(ch); if (!f) return null; final String name = string(tok.finish()); if (!dtd && r) return cont; final XMLInput tin = input; try { final IO file = input.io().merge(name); cont = file.read(); } catch (final IOException ex) { Util.debug(ex); // skip unknown DTDs/entities cont = new byte[] {'?'}; } input = new XMLInput(new IOContent(cont, name)); if (consume(XDECL)) { check(XML); s(); if (version()) checkS(); s(); if (encoding() == null) error(TEXTENC); ch = nextChar(); if (s(ch)) ch = nextChar(); if (ch != '?') error(WRONGCHAR, '?', ch); ch = nextChar(); if (ch != '>') error(WRONGCHAR, '>', ch); cont = Arrays.copyOfRange(cont, input.pos(), cont.length); } s(); if (r) { extSubsetDecl(); if (!consume((char) 0)) error(INVEND); } input = tin; } else { if (f) error(SCANQUOTE, (char) qu); prev(1); } } return cont; }
/** * Sets the output text. * * @param text output text * @param size text size */ public final void setText(final byte[] text, final int size) { byte[] txt = text; if (Token.contains(text, '\r')) { // remove carriage returns int ns = 0; for (int r = 0; r < size; ++r) { final byte b = text[r]; if (b != '\r') text[ns++] = b; } // new text is different... txt = Arrays.copyOf(text, ns); } else if (text.length != size) { txt = Arrays.copyOf(text, size); } if (editor.text(txt)) { if (hist != null) hist.store(txt, editor.pos(), 0); } if (isShowing()) resizeCode.invokeLater(); }
/** * Inserts the specified UTF8 character. * * @param pos insertion position * @param cp the character to be added * @return self reference */ public TokenBuilder insert(final int pos, final int cp) { final int s = size; final int cl = chars.length; final int l = cp <= 0x7F ? 1 : cp <= 0x7FF ? 2 : cp <= 0xFFF ? 3 : 4; if (s + l > cl) chars = Arrays.copyOf(chars, Math.max(s + l, (int) (cl * Array.RESIZE))); Array.move(chars, pos, l, size - pos); size = pos; add(cp); size = s + l; return this; }
/** * Indexes the specified keys and values. * * @param key key * @param id id value * @return index position */ public int index(final byte[] key, final int id) { int i = add(key); if (i > 0) { ids[i] = new int[] {id}; } else { i = -i; final int l = len[i]; if (l == ids[i].length) ids[i] = Arrays.copyOf(ids[i], l << 1); ids[i][l] = id; } len[i]++; return i; }
/** * Adds values to the index. * * @param key key to be indexed * @param vals sorted values */ void add(final byte[] key, final int... vals) { // token index: add values. otherwise, reference existing values final int id = type == IndexType.TOKEN ? values.put(key) : values.id(key), vl = vals.length; // updatable index: if required, resize existing arrays while (idsList.size() < id + 1) idsList.add(null); if (lenList.size() < id + 1) lenList.set(id, 0); final int len = lenList.get(id), size = len + vl; int[] ids = idsList.get(id); if (ids == null) { ids = vals; } else { if (ids.length < size) ids = Arrays.copyOf(ids, Array.newSize(size)); System.arraycopy(vals, 0, ids, len, vl); if (ids[len - 1] > vals[0]) { if (reorder == null) reorder = new BoolList(values.size()); reorder.set(id, true); } } idsList.set(id, ids); lenList.set(id, size); }
@Override protected void rehash() { super.rehash(); funcs = Arrays.copyOf(funcs, size << 1); }
/** * Returns the byte buffer. * * @return byte buffer */ private byte[] buffer() { final byte[] bb = bp == b.length ? b : Arrays.copyOf(b, bp); bp = 0; return bb; }
@Override protected byte[] stem(final byte[] str) { te = str.length; tok = str; return s() ? Arrays.copyOf(str, te) : str; }
/** * Returns the token as byte array, and invalidates the internal array. Warning: the function must * only be called if the builder is discarded afterwards. * * @return token */ public byte[] finish() { final byte[] chrs = chars; chars = null; final int s = size; return s == 0 ? EMPTY : s == chrs.length ? chrs : Arrays.copyOf(chrs, s); }
/** * Returns the token as byte array and resets the token buffer. The call of this function is * identical to calling {@link #toArray} and {@link #reset}. * * @return token */ public byte[] next() { final int s = size; if (s == 0) return EMPTY; size = 0; return Arrays.copyOf(chars, s); }
/** * Returns the token as byte array. * * @return token */ public byte[] toArray() { final int s = size; return s == 0 ? EMPTY : Arrays.copyOf(chars, s); }