📄 stringvalue.java
字号:
TempBuffer.free(tBuf); } } /** * Exports the value. */ @Override public void varExport(StringBuilder sb) { sb.append("'"); String value = toString(); int len = value.length(); for (int i = 0; i < len; i++) { char ch = value.charAt(i); switch (ch) { case '\'': sb.append("\\'"); break; case '\\': sb.append("\\\\"); break; default: sb.append(ch); } } sb.append("'"); } /** * Interns the string. */ public StringValue intern(Quercus quercus) { return quercus.intern(toString()); } // // CharSequence // /** * Returns the length of the string. */ public int length() { return toString().length(); } /** * Returns the character at a particular location */ public char charAt(int index) { return toString().charAt(index); } /** * Returns a subsequence */ public CharSequence subSequence(int start, int end) { return new StringBuilderValue(toString().substring(start, end)); } // // java.lang.String methods // /** * Returns the first index of the match string, starting from the head. */ public final int indexOf(CharSequence match) { return indexOf(match, 0); } /** * Returns the first index of the match string, starting from the head. */ 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 (charAt(head) != first) continue; for (int i = 1; i < matchLength; i++) { if (charAt(head + i) != match.charAt(i)) continue loop; } return head; } return -1; } /** * Returns the last index of the match string, starting from the head. */ public int indexOf(char match) { return indexOf(match, 0); } /** * Returns the last index of the match string, starting from the head. */ public int indexOf(char match, int head) { int length = length(); for (; head < length; head++) { if (charAt(head) == match) return head; } return -1; } /** * Returns the last index of the match string, starting from the head. */ public final int lastIndexOf(char match) { return lastIndexOf(match, Integer.MAX_VALUE); } /** * Returns the last index of the match string, starting from the head. */ public int lastIndexOf(char match, int tail) { int length = length(); if (tail >= length) tail = length - 1; for (; tail >= 0; tail--) { if (charAt(tail) == match) return tail; } return -1; } /** * Returns the last index of the match string, starting from the tail. */ public int lastIndexOf(CharSequence match) { return lastIndexOf(match, Integer.MAX_VALUE); } /** * Returns the last index of the match string, starting from the tail. */ public int lastIndexOf(CharSequence match, int tail) { int length = length(); int matchLength = match.length(); if (matchLength <= 0) return -1; if (tail < 0) return -1; if (tail > length - matchLength) tail = length - matchLength; char first = match.charAt(0); loop: for (; tail >= 0; tail--) { if (charAt(tail) != first) continue; for (int i = 1; i < matchLength; i++) { if (charAt(tail + i) != match.charAt(i)) continue loop; } return tail; } return -1; } /** * Returns true if the region matches */ public boolean regionMatches(int offset, char []mBuffer, int mOffset, int mLength) { int length = length(); if (length < offset + mLength) return false; for (int i = 0; i < mLength; i++) { if (charAt(offset + i) != mBuffer[mOffset + i]) return false; } return true; } /** * Returns true if the region matches */ public boolean regionMatches(int offset, StringValue match, int mOffset, int mLength) { int length = length(); if (length < offset + mLength) return false; for (int i = 0; i < mLength; i++) { if (charAt(offset + i) != match.charAt(mOffset + i)) return false; } return true; } /** * Returns true if the region matches */ public boolean regionMatchesIgnoreCase(int offset, char []match, int mOffset, int mLength) { int length = length(); if (length < offset + mLength) return false; for (int i = 0; i < mLength; i++) { char a = Character.toLowerCase(charAt(offset + i)); char b = Character.toLowerCase(match[mOffset + i]); if (a != b) return false; } return true; } /** * Returns true if the string ends with another string. */ public boolean endsWith(StringValue tail) { int len = length(); int tailLen = tail.length(); int offset = len - tailLen; if (offset < 0) return false; for (int i = 0; i < tailLen; i++) { if (charAt(offset + i) != tail.charAt(i)) return false; } return true; } /** * Returns a StringValue substring. */ public StringValue substring(int head) { return (StringValue) subSequence(head, length()); } /** * Returns a StringValue substring. */ public StringValue substring(int begin, int end) { return (StringValue) subSequence(begin, end); } /** * Returns a character array */ public char []toCharArray() { int length = length(); char []array = new char[length()]; getChars(0, array, 0, length); return array; } public char []getRawCharArray() { return toCharArray(); } /** * Copies the chars */ public void getChars(int stringOffset, char []buffer, int offset, int length) { for (int i = 0; i < length; i++) buffer[offset + i] = charAt(stringOffset + i); } /** * Convert to lower case. */ public StringValue toLowerCase() { int length = length(); UnicodeBuilderValue string = new UnicodeBuilderValue(length); char []buffer = string.getBuffer(); getChars(0, buffer, 0, length); for (int i = 0; i < length; i++) { char ch = buffer[i]; if ('A' <= ch && ch <= 'Z') buffer[i] = (char) (ch + 'a' - 'A'); else if (ch < 0x80) { } else if (Character.isUpperCase(ch)) buffer[i] = Character.toLowerCase(ch); } string.setOffset(length); return string; } /** * Convert to lower case. */ public StringValue toUpperCase() { int length = length(); UnicodeBuilderValue string = new UnicodeBuilderValue(length); char []buffer = string.getBuffer(); getChars(0, buffer, 0, length); for (int i = 0; i < length; i++) { char ch = buffer[i]; if ('a' <= ch && ch <= 'z') buffer[i] = (char) (ch + 'A' - 'a'); else if (ch < 0x80) { } else if (Character.isLowerCase(ch)) buffer[i] = Character.toUpperCase(ch); } string.setOffset(length); return string; } /** * Returns a byteArrayInputStream for the value. * See TempBufferStringValue for how this can be overriden * * @return InputStream */ public InputStream toInputStream() { try { //XXX: refactor so that env is passed in return toInputStream(Env.getInstance().getRuntimeEncoding()); } catch (UnsupportedEncodingException e) { throw new QuercusRuntimeException(e); } //return new StringValueInputStream(); } /** * Returns a byte stream of chars. * @param charset to encode chars to */ public InputStream toInputStream(String charset) throws UnsupportedEncodingException { return new ByteArrayInputStream(toString().getBytes(charset)); } /** * Returns a char stream. * XXX: when decoding fails * * @param charset to decode bytes by */ public Reader toReader(String charset) throws UnsupportedEncodingException { byte []bytes = toBytes(); return new InputStreamReader(new ByteArrayInputStream(bytes), charset); } /** * Converts to a BinaryValue in desired charset. * * @param env * @param charset */ public StringValue toBinaryValue(Env env, String charset) { TempBuffer tb = TempBuffer.allocate(); byte[] buffer = tb.getBuffer(); try { InputStream in = toInputStream(charset); TempStream out = new TempStream(); int sublen = in.read(buffer, 0, buffer.length); while (sublen >= 0) { out.write(buffer, 0, sublen, false); sublen = in.read(buffer, 0, buffer.length); } out.flush(); StringValue result = env.createBinaryBuilder(); for (TempBuffer ptr = out.getHead(); ptr != null; ptr = ptr.getNext()) { result.append(ptr.getBuffer(), 0, ptr.getLength()); } TempBuffer.free(out.getHead()); return result; } catch (IOException e) { throw new QuercusModuleException(e.getMessage()); } finally { TempBuffer.free(tb); } } public byte []toBytes() { throw new UnsupportedOperationException(); } /** * Decodes from charset and returns UnicodeValue. * * @param env * @param charset */ public StringValue toUnicodeValue(Env env, String charset) { StringValue sb = env.createUnicodeBuilder(); TempCharBuffer tb = TempCharBuffer.allocate(); char[] charBuf = tb.getBuffer(); try { Reader in = toReader(charset); int sublen; while ((sublen = in.read(charBuf, 0, charBuf.length)) >= 0) { sb.append(charBuf, 0, sublen); } } catch (IOException e) { throw new QuercusModuleException(e.getMessage()); } finally { TempCharBuffer.free(tb); } return sb; } /** * Decodes from charset and returns UnicodeValue. * * @param env * @param charset */ public StringValue convertToUnicode(Env env, String charset) { UnicodeBuilderValue sb = new UnicodeBuilderValue(); TempCharBuffer tb = TempCharBuffer.allocate(); char[] charBuf = tb.getBuffer(); try { Reader in = toReader(charset); int sublen; while ((sublen = in.read(charBuf, 0, charBuf.length)) >= 0) { sb.append(charBuf, 0, sublen); } } catch (IOException e) { throw new QuercusModuleException(e.getMessage()); } finally { TempCharBuffer.free(tb); } return sb; } /** * Converts to a string builder */ @Override public StringValue toStringBuilder(Env env) { return createStringBuilder().append(this); } /** * Writes to a stream */ public void writeTo(OutputStream os) { try { int len = length(); for (int i = 0; i < len; i++) os.write(charAt(i)); } catch (IOException e) { throw new QuercusModuleException(e); } } // // java.lang.Object methods // /** * Returns the hash code. */ public int hashCode() { int hash = 37; int length = length(); for (int i = 0; i < length; i++) { hash = 65521 * hash + charAt(i); } return hash; } /** * Test for equality */ public boolean equals(Object o) { if (this == o) return true; else if (! (o instanceof StringValue)) return false; StringValue s = (StringValue) o; if (s.isUnicode() != isUnicode()) return false; int aLength = length(); int bLength = s.length(); if (aLength != bLength) return false; for (int i = aLength - 1; i >= 0; i--) { if (charAt(i) != s.charAt(i)) return false; } return true; } @Override abstract public String toDebugString(); @Override abstract public void varDumpImpl(Env env, WriteStream out, int depth, IdentityHashMap<Value, String> valueSet) throws IOException; class StringValueInputStream extends java.io.InputStream { private final int _length; private int _index; StringValueInputStream() { _length = length(); } /** * Reads the next byte. */ public int read() { if (_index < _length) return charAt(_index++); else return -1; } /** * Reads into a buffer. */ public int read(byte []buffer, int offset, int length) { int sublen = _length - _index; if (length < sublen) sublen = length; if (sublen <= 0) return -1; int index = _index; for (int i = 0; i < sublen; i++) buffer[offset + i] = (byte) charAt(index + i); _index += sublen; return sublen; } } static { // XXX: need to update for unicode CHAR_STRINGS = new StringValue[256]; for (int i = 0; i < CHAR_STRINGS.length; i++) CHAR_STRINGS[i] = new StringBuilderValue(String.valueOf((char) i)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -