📄 stringbuildervalue.java
字号:
for (int i = 0; i < sublen; i++) { _buffer[_length++] = s.charAt(i); } return this; } /** * Append a Java string to the value. */ @Override public final StringValue append(String s, int start, int end) { int sublen = end - start; if (_buffer.length < _length + sublen) ensureCapacity(_length + sublen); char []buffer = _buffer; int length = _length; for (; start < end; start++) buffer[length++] = s.charAt(start); _length = length; return this; } /** * Append a Java char to the value. */ @Override public final StringValue append(char ch) { if (_buffer.length < _length + 1) ensureCapacity(_length + 1); _buffer[_length++] = ch; return this; } /** * Append a Java buffer to the value. */ @Override public final StringValue append(char []buf, int offset, int length) { int end = _length + length; if (_buffer.length < end) ensureCapacity(end); char []buffer = _buffer; int bufferLength = _length; while (--length >= 0) buffer[bufferLength + length] = buf[offset + length]; _length = end; return this; } /** * Append a Java buffer to the value. */ @Override public final StringValue appendUnicode(char []buf, int offset, int length) { if (_buffer.length < _length + length) ensureCapacity(_length + length); char []buffer = _buffer; int bufferLength = _length; for (; length > 0; length--) buffer[bufferLength++] = buf[offset++]; _buffer = buffer; _length = bufferLength; return this; } /** * Append a Java buffer to the value. */ @Override public final StringValue append(char []buf) { int length = buf.length; if (_buffer.length < _length + length) ensureCapacity(_length + length); char []buffer = _buffer; int bufferLength = _length; _length = bufferLength + length; for (length--; length >= 0; length--) buffer[bufferLength + length] = buf[length]; _buffer = buffer; return this; } /** * Append a Java buffer to the value. */ @Override public final StringValue appendUnicode(char []buf) { int length = buf.length; if (_buffer.length < _length + length) ensureCapacity(_length + length); char []buffer = _buffer; int bufferLength = _length; _length = bufferLength + length; for (length--; length >= 0; length--) buffer[bufferLength + length] = buf[length]; _buffer = buffer; return this; } /** * Append a Java buffer to the value. */ @Override public final StringValue append(CharSequence buf, int head, int tail) { int length = tail - head; if (_buffer.length < _length + length) ensureCapacity(_length + length); if (buf instanceof StringBuilderValue) { StringBuilderValue sb = (StringBuilderValue) buf; System.arraycopy(sb._buffer, head, _buffer, _length, tail - head); _length += tail - head; return this; } else { char []buffer = _buffer; int bufferLength = _length; for (; head < tail; head++) { buffer[bufferLength++] = buf.charAt(head); } _length = bufferLength; return this; } } /** * Append a Java buffer to the value. */ // @Override public final StringValue append(StringBuilderValue sb, int head, int tail) { int length = tail - head; if (_buffer.length < _length + length) ensureCapacity(_length + length); System.arraycopy(sb._buffer, head, _buffer, _length, tail - head); _length += tail - head; return this; } /** * Append a Java value to the value. */ @Override public final StringValue append(Value v) { /* if (v.length() == 0) return this; else { // php/033a v.appendTo(this); return this; } */ v.appendTo(this); return this; } /** * Returns the first index of the match string, starting from the head. */ @Override public int indexOf(CharSequence match, int head) { int length = length(); int matchLength = match.length(); if (matchLength <= 0) return -1; else if (head < 0) return -1; int end = length - matchLength; char first = match.charAt(0); loop: for (; head <= end; head++) { if (_buffer[head] != first) continue; for (int i = 1; i < matchLength; i++) { if (_buffer[head + i] != match.charAt(i)) continue loop; } return head; } return -1; } /** * Append a Java value to the value. */ @Override public StringValue appendUnicode(Value v) { v.appendTo(this); return this; } /** * Append a Java value to the value. */ @Override public StringValue appendUnicode(Value v1, Value v2) { v1.appendTo(this); v2.appendTo(this); return this; } /** * Append a buffer to the value. */ public StringValue append(byte []buf, int offset, int length) { int end = _length + length; if (_buffer.length < end) ensureCapacity(end); char []charBuffer = _buffer; int charLength = _length; for (int i = length - 1; i >= 0; i--) { charBuffer[charLength + i] = (char) (buf[offset + i] & 0xff); } _length = end; return this; } /** * Append a double to the value. */ public final StringValue append(byte []buf) { return append(buf, 0, buf.length); } /** * Append a Java byte to the value without conversions. */ @Override public final StringValue appendByte(int v) { int length = _length + 1; if (_buffer.length < length) ensureCapacity(length); _buffer[_length++] = (char) v; return this; } /** * Append a Java boolean to the value. */ @Override public final StringValue append(boolean v) { return append(v ? "true" : "false"); } /** * Append a Java long to the value. */ @Override public StringValue append(long v) { // XXX: this probably is frequent enough to special-case return append(String.valueOf(v)); } /** * Append a Java double to the value. */ @Override public StringValue append(double v) { return append(String.valueOf(v)); } /** * Append a bytes to the value. */ @Override public StringValue appendBytes(String s) { int sublen = s.length(); if (_buffer.length < _length + sublen) ensureCapacity(_length + sublen); for (int i = 0; i < sublen; i++) { _buffer[_length++] = s.charAt(i); } return this; } @Override public StringValue append(Reader reader, long length) throws IOException { // php/4407 - oracle clob callback passes very long length int sublen = (int) Math.min(8192L, length); try { while (length > 0) { ensureAppendCapacity(sublen); int count = reader.read(_buffer, _length, sublen); if (count <= 0) break; length -= count; _length += count; } } catch (IOException e) { throw new QuercusModuleException(e); } return this; } /** * Returns the buffer. */ public char []getBuffer() { return _buffer; } /** * Returns the offset. */ public int getOffset() { return _length; } /** * Sets the offset. */ public void setOffset(int offset) { _length = offset; } /** * Returns the current capacity. */ public int getLength() { return _buffer.length; } // // Java generator code // /** * Prints the value. * @param env */ public void print(Env env) { env.print(_buffer, 0, _length); } /** * Prints the value. * @param env */ public void print(Env env, WriteStream out) { try { out.print(_buffer, 0, _length); } catch (IOException e) { throw new QuercusModuleException(e); } } /** * Serializes the value. */ public void serialize(StringBuilder sb) { sb.append("s:"); sb.append(_length); sb.append(":\""); sb.append(toString()); sb.append("\";"); } @Override public String toDebugString() { StringBuilder sb = new StringBuilder(); int length = length(); sb.append("binary("); sb.append(length); sb.append(") \""); int appendLength = length > 256 ? 256 : length; for (int i = 0; i < appendLength; i++) sb.append(charAt(i)); if (length > 256) sb.append(" ..."); sb.append('"'); return sb.toString(); } @Override public void varDumpImpl(Env env, WriteStream out, int depth, IdentityHashMap<Value, String> valueSet) throws IOException { int length = length(); if (length < 0) length = 0; out.print("string("); out.print(length); out.print(") \""); for (int i = 0; i < length; i++) { int ch = charAt(i); out.print((char) ch); } out.print("\""); } /** * Returns an OutputStream. */ public OutputStream getOutputStream() { return new BuilderOutputStream(); } public void ensureAppendCapacity(int newCapacity) { ensureCapacity(_length + newCapacity); } protected void ensureCapacity(int newCapacity) { if (newCapacity <= _buffer.length) return; else if (newCapacity < 4096) newCapacity = 4 * newCapacity; else newCapacity = newCapacity + 4096; assert newCapacity > _buffer.length : "cannot set new capacity to " + newCapacity; char []buffer = new char[newCapacity]; System.arraycopy(_buffer, 0, buffer, 0, _length); _buffer = buffer; } /** * Returns the hash code. */ @Override public int hashCode() { int hash = _hashCode; if (hash != 0) return hash; hash = 37; int length = _length; char []buffer = _buffer; if (length > 256) { for (int i = 127; i >= 0; i--) { hash = 65521 * hash + buffer[i]; } for (int i = length - 128; i < length; i++) { hash = 65521 * hash + buffer[i]; } _hashCode = hash; return hash; } for (int i = length - 1; i >= 0; i--) { hash = 65521 * hash + buffer[i]; } _hashCode = hash; return hash; } /** * Returns true for equality */ @Override public boolean eq(Value rValue) { ValueType typeB = rValue.getValueType(); if (typeB.isNumber()) { double l = toDouble(); double r = rValue.toDouble(); return l == r; } else if (typeB.isBoolean()) { return toBoolean() == rValue.toBoolean(); } ValueType typeA = getValueType(); if (typeA.isNumberCmp() && typeB.isNumberCmp()) { double l = toDouble(); double r = rValue.toDouble(); return l == r; } rValue = rValue.toValue(); if (rValue instanceof StringBuilderValue) { StringBuilderValue value = (StringBuilderValue) rValue; int length = _length; if (length != value._length) return false; char []bufferA = _buffer; char []bufferB = value._buffer; for (int i = length - 1; i >= 0; i--) { if (bufferA[i] != bufferB[i]) return false; } return true; } else { return toString().equals(rValue.toString()); } } @Override public boolean equals(Object o) { if (o instanceof StringBuilderValue) { StringBuilderValue value = (StringBuilderValue) o; int length = _length; if (length != value._length) return false; char []bufferA = _buffer; char []bufferB = value._buffer; for (int i = length - 1; i >= 0; i--) { if (bufferA[i] != bufferB[i]) return false; } return true; } /* else if (o instanceof UnicodeValue) { UnicodeValue value = (UnicodeValue)o; return value.equals(this); } */ else return false; } @Override public boolean eql(Value o) { o = o.toValue(); if (o instanceof StringBuilderValue) { StringBuilderValue value = (StringBuilderValue) o; int length = _length; if (length != value._length) return false; char []bufferA = _buffer; char []bufferB = value._buffer; for (int i = length - 1; i >= 0; i--) { if (bufferA[i] != bufferB[i]) return false; } return true; } else return false; } // // Java generator code // /** * Generates code to recreate the expression. * * @param out the writer to the Java source code. */ @Override public void generate(PrintWriter out) throws IOException { out.print("new StringBuilderValue(\""); printJavaString(out, this); out.print("\")"); } // // Java serialization code // private void writeObject(ObjectOutputStream out) throws IOException { out.writeInt(_length); for (int i = 0; i < _length; i++) out.write(_buffer[i]); } private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException { _length = in.readInt(); _buffer = new char[_length]; for (int i = 0; i < _length; i++) _buffer[i] = (char) in.read(); } class BinaryInputStream extends InputStream { private int _offset; /** * Reads the next byte. */ @Override public int read() { if (_offset < _length) return _buffer[_offset++]; else return -1; } /** * Reads into a buffer. */ @Override public int read(byte []buffer, int offset, int length) { int sublen = _length - _offset; if (length < sublen) sublen = length; if (sublen <= 0) return -1; System.arraycopy(_buffer, _offset, buffer, offset, sublen); _offset += sublen; return sublen; } } class BuilderInputStream extends InputStream { private int _index; /** * Reads the next byte. */ @Override public int read() { if (_index < _length) return _buffer[_index++] & 0xff; else return -1; } /** * Reads into a buffer. */ @Override public int read(byte []buffer, int offset, int length) { int sublen = _length - _index; if (length < sublen) sublen = length; if (sublen <= 0) return -1; for (int i = 0; i < sublen; i++) buffer[offset + i] = (byte) _buffer[_index + i]; _index += sublen; return sublen; } } class BuilderOutputStream extends OutputStream { /** * Writes the next byte. */ @Override public void write(int ch) { append(ch); } /** * Reads into a buffer. */ @Override public void write(byte []buffer, int offset, int length) { append(buffer, offset, length); } } static { CHAR_STRINGS = new StringBuilderValue[256]; for (int i = 0; i < CHAR_STRINGS.length; i++) CHAR_STRINGS[i] = new StringBuilderValue((char) i); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -