
use mem

class ByteBufferStrRef {
    buffer: ByteBuffer
    offset: uint
    length: uint

    + static fn new(buffer: ByteBuffer, offset: uint, length: uint) ByteBufferStrRef {
        let b = ByteBufferStrRef {
            buffer: buffer
            offset: offset
            length: length
        }
        return b
    }

    + get data : ptr {
        return @offset(this.buffer.data, u8, this.offset)
    }

    + fn clear() {
        this.length = 0
    }

    + fn to_str() String $to {
        let len = this.length
        if len == 0 : return ""
        return this.buffer.part(this.offset, len)
    }

    + fn equals(cmp: String) bool $eq {
        let len = this.length
        if len != cmp.bytes : return false 
        if len == 0 : return true
        return mem:equal(@offset(this.buffer.data, u8, this.offset), cmp.data, len)
    }
}
