📄 binarybuildervalue.java
字号:
int sublen = s.length(); if (_buffer.length < _length + sublen) ensureCapacity(_length + sublen); for (int i = 0; i < sublen; i++) { _buffer[_length++] = (byte) s.charAt(i); } return this; } /** * Append a Java string to the value. */ @Override public final StringValue appendUnicode(String s) { UnicodeBuilderValue sb = new UnicodeBuilderValue(); appendTo(sb); sb.append(s); return sb; } /** * Append a Java string to the value. */ @Override public final StringValue appendUnicode(String s, int start, int end) { UnicodeBuilderValue sb = new UnicodeBuilderValue(); appendTo(sb); sb.append(s, start, end); return sb; } /** * Append a value to the value. */ @Override public final StringValue appendUnicode(Value value) { value = value.toValue(); if (value instanceof BinaryBuilderValue) { append((BinaryBuilderValue) value); return this; } else if (value.isString()) { UnicodeBuilderValue sb = new UnicodeBuilderValue(); appendTo(sb); sb.append(value); return sb; } else return value.appendTo(this); } /** * Append a Java char to the value. */ @Override public final StringValue appendUnicode(char ch) { UnicodeBuilderValue sb = new UnicodeBuilderValue(); appendTo(sb); sb.append(ch); return sb; } /** * Append a Java boolean to the value. */ @Override public final StringValue appendUnicode(boolean v) { return append(v ? "true" : "false"); } /** * Append a Java long to the value. */ @Override public StringValue appendUnicode(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 appendUnicode(double v) { return append(String.valueOf(v)); } /** * Append a Java object to the value. */ @Override public StringValue appendUnicode(Object v) { if (v instanceof String) return appendUnicode(v.toString()); else return append(v.toString()); } /** * 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++] = (byte) v; return this; } /** * Append to a string builder. */ public StringValue appendTo(UnicodeBuilderValue sb) { if (length() == 0) return sb; Env env = Env.getInstance(); try { Reader reader = env.getRuntimeEncodingFactory().create(toInputStream()); if (reader != null) { sb.append(reader); reader.close(); } return sb; } catch (IOException e) { throw new QuercusRuntimeException(e); } } /** * Returns the buffer. */ public byte []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.write(_buffer, 0, _length); } /** * Prints the value. * @param env */ public void print(Env env, WriteStream out) { try { out.write(_buffer, 0, _length); } catch (IOException e) { throw new QuercusRuntimeException(e); } } /** * Serializes the value. */ public void serialize(StringBuilder sb) { sb.append("s:"); sb.append(_length); sb.append(":\""); sb.append(toString()); sb.append("\";"); } /** * Returns an OutputStream. */ public OutputStream getOutputStream() { return new BuilderOutputStream(); } private void ensureCapacity(int newCapacity) { if (newCapacity <= _buffer.length) return; else if (newCapacity < 4096) newCapacity = 4 * newCapacity; else newCapacity = newCapacity + 4096; byte []buffer = new byte[newCapacity]; System.arraycopy(_buffer, 0, buffer, 0, _length); _buffer = buffer; } /** * Returns the hash code. */ @Override public int hashCode() { int hash = 37; int length = _length; byte []buffer = _buffer; for (int i = 0; i < length; i++) { hash = 65521 * hash + (buffer[i] & 0xff); } return hash; } /** * Returns true for equality */ @Override public boolean eq(Value rValue) { ValueType typeA = getValueType(); 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(); } else if (typeA.isNumberCmp() && typeB.isNumberCmp()) { double l = toDouble(); double r = rValue.toDouble(); return l == r; } rValue = rValue.toValue(); if (rValue instanceof BinaryBuilderValue) { BinaryBuilderValue value = (BinaryBuilderValue) rValue; int length = _length; if (length != value._length) return false; byte []bufferA = _buffer; byte []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 BinaryBuilderValue) { BinaryBuilderValue value = (BinaryBuilderValue) o; int length = _length; if (length != value._length) return false; byte []bufferA = _buffer; byte []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 BinaryBuilderValue) { BinaryBuilderValue value = (BinaryBuilderValue) o; int length = _length; if (length != value._length) return false; byte []bufferA = _buffer; byte []bufferB = value._buffer; for (int i = length - 1; i >= 0; i--) { if (bufferA[i] != bufferB[i]) return false; } return true; } else return false; } @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; // QA needs to distinguish php5 string from php6 binary if (Alarm.isTest()) out.print("binary"); else out.print("string"); out.print("("); out.print(length); out.print(") \""); for (int i = 0; i < length; i++) { char ch = charAt(i); if (0x20 <= ch && ch <= 0x7f || ch == '\t' || ch == '\r' || ch == '\n') out.print(ch); else if (ch <= 0xff) out.print("\\x" + Integer.toHexString(ch / 16) + Integer.toHexString(ch % 16)); else { out.print("\\u" + Integer.toHexString((ch >> 12) & 0xf) + Integer.toHexString((ch >> 8) & 0xf) + Integer.toHexString((ch >> 4) & 0xf) + Integer.toHexString((ch) & 0xf)); } } out.print("\""); } // // 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 BinaryBuilderValue(\""); printJavaString(out, this); out.print("\")"); } // // Java serialization code // private void writeObject(ObjectOutputStream out) throws IOException { out.writeInt(_length); out.write(_buffer, 0, _length); } private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException { _length = in.readInt(); _buffer = new byte[_length]; in.read(_buffer, 0, _length); } // // static helper functions // // // static helper functions // public static ValueType getValueType(byte []buffer, int offset, int len) { if (len == 0) return ValueType.LONG_ADD; int i = offset; int ch = 0; boolean hasPoint = false; if (i < len && ((ch = buffer[i]) == '+' || ch == '-')) { i++; } if (len <= i) return ValueType.STRING; ch = buffer[i]; if (ch == '.') { for (i++; i < len && '0' <= (ch = buffer[i]) && ch <= '9'; i++) { return ValueType.DOUBLE_CMP; } return ValueType.STRING; } else if (! ('0' <= ch && ch <= '9')) return ValueType.STRING; for (; i < len && '0' <= (ch = buffer[i]) && ch <= '9'; i++) { } if (len <= i) return ValueType.LONG_EQ; else if (ch == '.' || ch == 'e' || ch == 'E') { for (i++; i < len && ('0' <= (ch = buffer[i]) && ch <= '9' || ch == '+' || ch == '-' || ch == 'e' || ch == 'E'); i++) { } if (i < len) return ValueType.STRING; else return ValueType.DOUBLE_CMP; } else return ValueType.STRING; } public static int getNumericType(byte []buffer, int offset, int len) { if (len == 0) return IS_STRING; int i = offset; int ch = 0; boolean hasPoint = false; if (i < len && ((ch = buffer[i]) == '+' || ch == '-')) { i++; } if (len <= i) return IS_STRING; ch = buffer[i]; if (ch == '.') { for (i++; i < len && '0' <= (ch = buffer[i]) && ch <= '9'; i++) { return IS_DOUBLE; } return IS_STRING; } else if (! ('0' <= ch && ch <= '9')) return IS_STRING; for (; i < len && '0' <= (ch = buffer[i]) && ch <= '9'; i++) { } if (len <= i) return IS_LONG; else if (ch == '.' || ch == 'e' || ch == 'E') { for (i++; i < len && ('0' <= (ch = buffer[i]) && ch <= '9' || ch == '+' || ch == '-' || ch == 'e' || ch == 'E'); i++) { } if (i < len) return IS_STRING; else return IS_DOUBLE; } else return IS_STRING; } public static double toDouble(byte []buffer, int offset, int len) { int i = offset; int ch = 0; if (i < len && ((ch = buffer[i]) == '+' || ch == '-')) { i++; } for (; i < len && '0' <= (ch = buffer[i]) && ch <= '9'; i++) { } if (ch == '.') { for (i++; i < len && '0' <= (ch = buffer[i]) && ch <= '9'; i++) { } if (i == 1) return 0; } if (ch == 'e' || ch == 'E') { int e = i++; if (i < len && (ch = buffer[i]) == '+' || ch == '-') { i++; } for (; i < len && '0' <= (ch = buffer[i]) && ch <= '9'; i++) { } if (i == e + 1) i = e; } if (i == 0) return 0; try { return Double.parseDouble(new String(buffer, 0, i)); } catch (NumberFormatException e) { return 0; } } 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; System.arraycopy(_buffer, _index, buffer, offset, sublen); _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 BinaryBuilderValue[256]; for (int i = 0; i < CHAR_STRINGS.length; i++) CHAR_STRINGS[i] = new BinaryBuilderValue((byte) i); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -