@JRubyMethod public IRubyObject hash(ThreadContext context) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); for (IRubyObject key : table.keySet()) { digest.update((byte) key.hashCode()); digest.update((byte) table.get(key).hashCode()); } return context.runtime.newString(new ByteList(digest.digest())); } catch (NoSuchAlgorithmException ignore) { return context.runtime.newFixnum(System.identityHashCode(table)); } }
/** * This looks deceptively like tokadd_mbchar(int, ByteList) but it differs in that it uses * the bytelists encoding and the first parameter is a full codepoint and not the first byte * of a mbc sequence. */ public void tokaddmbc(int codepoint, ByteList buffer) { Encoding encoding = buffer.getEncoding(); int length = encoding.codeToMbcLength(codepoint); buffer.ensure(buffer.getRealSize() + length); encoding.codeToMbc(codepoint, buffer.getUnsafeBytes(), buffer.begin() + buffer.getRealSize()); buffer.setRealSize(buffer.getRealSize() + length); }
public StrNode(ISourcePosition position, StrNode head, StrNode tail) { super(position, false); ByteList headBL = head.getValue(); ByteList tailBL = tail.getValue(); ByteList myValue = new ByteList(headBL.getRealSize() + tailBL.getRealSize()); myValue.setEncoding(headBL.getEncoding()); myValue.append(headBL); myValue.append(tailBL); frozen = head.isFrozen() && tail.isFrozen(); value = myValue; codeRange = StringSupport.codeRangeScan(value.getEncoding(), value); }
public static ByteList addByteLists(ByteList value1, ByteList value2) { ByteList result = new ByteList(value1.getRealSize() + value2.getRealSize()); result.setRealSize(value1.getRealSize() + value2.getRealSize()); System.arraycopy(value1.getUnsafeBytes(), value1.getBegin(), result.getUnsafeBytes(), 0, value1.getRealSize()); System.arraycopy(value2.getUnsafeBytes(), value2.getBegin(), result.getUnsafeBytes(), value1.getRealSize(), value2.getRealSize()); return result; }
public static void strBufCat(Ruby runtime, RubyString str, ByteList ptr) { int total, off = -1; // termlen is not relevant since we have no termination sequence // missing: if ptr string is inside str, off = ptr start minus str start str.modify(); if (ptr.length() == 0) return; // much logic is missing here, since we don't manually manage the ByteList buffer total = str.size() + ptr.length(); str.getByteList().ensure(total); str.getByteList().append(ptr); }
public static void strBufCat(ByteList str, byte[] ptrBytes, int ptr, int len) { int total, off = -1; // termlen is not relevant since we have no termination sequence // missing: if ptr string is inside str, off = ptr start minus str start // str.modify(); if (len == 0) return; // much logic is missing here, since we don't manually manage the ByteList buffer total = str.getRealSize() + len; str.ensure(total); str.append(ptrBytes, ptr, len); }
private void view(byte[] bytes, boolean copy) { modifyCheck(); value = new ByteList(bytes, copy); shareLevel = SHARE_LEVEL_NONE; value.invalidate(); }
protected void flush_string_content(Encoding encoding) { if (delayed != null) { int len = lex_p - tokp; if (len > 0) { delayed.setEncoding(encoding); delayed.append(lexb.makeShared(tokp, len)); } dispatchDelayedToken(RipperParser.tSTRING_CONTENT); tokp = lex_p; } }
/** rb_str_cmp * */ public final int op_cmp(RubyString other) { int ret = value.cmp(other.value); if (ret == 0 && !StringSupport.areComparable(this, other)) { return value.getEncoding().getIndex() > other.value.getEncoding().getIndex() ? 1 : -1; } return ret; }
/** * Does this ByteList equal the other ByteList? * * @param other is the bytelist to compare with * @return true is this ByteList is the same */ @Override public boolean equals(Object other) { if (other instanceof ByteList) return equal((ByteList)other); return false; }
public XStrNode(ISourcePosition position, ByteList value, int coderange) { // FIXME: Shouldn't this have codeRange like StrNode? super(position, false); this.value = (value == null ? ByteList.create("") : value); this.coderange = coderange; }
@Override public int resize(ByteList destination, int len, int new_len) { destination.setRealSize(len); destination.ensure(new_len); return destination.getBegin(); } };
public Encoding getEncodingFromString(String string) { if (string == null) return null; ByteList name = new ByteList(ByteList.plain(string), false); checkAsciiEncodingName(name); SpecialEncoding special = SpecialEncoding.valueOf(name); if (special != null) { return special.toEncoding(runtime); } return findEncodingWithError(name); }
@Override public ByteList gets() { int length = completeSource.length(); if (offset >= length) return null; // At end of source/eof int end = completeSource.indexOf('\n', offset) + 1; if (end == 0) end = length; ByteList line = completeSource.makeShared(offset, end - offset); offset = end; if (scriptLines != null) scriptLines.append(RubyString.newString(scriptLines.getRuntime(), line)); return line; }
@Override public void visit(ThreadContext context, RubyHash self, IRubyObject key, IRubyObject value, int index, RubyString str) { RubyString keyStr = inspect(context, key); RubyString valStr = inspect(context, value); final ByteList bytes = str.getByteList(); bytes.ensure(2 + keyStr.size() + 2 + valStr.size()); if (index > 0) bytes.append((byte) ',').append((byte) ' '); str.cat19(keyStr); bytes.append((byte) '=').append((byte) '>'); str.cat19(valStr); } };