A buffer containing Byte-manipulation facilities

Uses Copy-On-Write when persisted

Introduced properties

fun add_char(c: Char)

core :: Bytes :: add_char

Adds the UTF-8 representation of c to self
fun append_ns(ns: CString, ln: Int)

core :: Bytes :: append_ns

Appends the ln first bytes of ns to self
fun append_ns_from(ns: CString, ln: Int, from: Int)

core :: Bytes :: append_ns_from

Appends ln bytes from ns starting at index from to self
fun append_text(str: Text)

core :: Bytes :: append_text

Appends the bytes of str to self
fun binarydigest: String

core :: Bytes :: binarydigest

Returns self as a stream of bits (0 and 1)
fun check_base64: nullable Error

core :: Bytes :: check_base64

Is self a well-formed Base64 entity ?
fun chexdigest: String

core :: Bytes :: chexdigest

Return self as a C hexadecimal digest where bytes are prefixed by \x
fun clone: Bytes

core :: Bytes :: clone

Returns a copy of self
fun decode_base64: Bytes

core :: Bytes :: decode_base64

Decodes the receiver string to base64 using a custom padding character.
init defaultinit(items: CString, length: Int, capacity: Int)

core :: Bytes :: defaultinit

fun deserialize_msgpack(static_type: nullable String): nullable Object

core :: Bytes :: deserialize_msgpack

Deserialize full Nit nullable Object from MessagePack formated data
init empty

core :: Bytes :: empty

var b = new Bytes.empty
fun encode_base64: Bytes

core :: Bytes :: encode_base64

Encodes the receiver string to base64 using a custom padding character.
fun from_percent_encoding: Bytes

core :: Bytes :: from_percent_encoding

Decode self from percent (or URL) encoding to a clear string
fun hamming_distance(other: SequenceRead[Int]): Int

core :: Bytes :: hamming_distance

Computes the edit/hamming distance of two sequences of bytes.
fun has_prefix(b: BytePattern): Bool

core :: Bytes :: has_prefix

Is b a prefix of self ?
fun has_suffix(b: BytePattern): Bool

core :: Bytes :: has_suffix

Is b a suffix of self ?
fun hexdigest: String

core :: Bytes :: hexdigest

Returns self as an hexadecimal digest.
fun is_base64: Bool

core :: Bytes :: is_base64

Is self a well-formed Base64 entity ?
fun items: CString

core :: Bytes :: items

A CString being a char*, it can be used as underlying representation here.
protected fun items=(items: CString)

core :: Bytes :: items=

A CString being a char*, it can be used as underlying representation here.
fun pkcs7(blocksize: Int): Bytes

core :: Bytes :: pkcs7

PKCS#7 padding.
fun replace(pattern: BytePattern, bytes: BytePattern): Bytes

core :: Bytes :: replace

Replaces all the occurences of this in self by by
fun reverse

core :: Bytes :: reverse

Reverse the byte array in place
fun slice(from: Int, count: Int): Bytes

core :: Bytes :: slice

Copy a subset of self starting at from and of count bytes
fun slice_from(from: Int): Bytes

core :: Bytes :: slice_from

Copy of self starting at from
fun split_once_on(b: BytePattern): Array[Bytes]

core :: Bytes :: split_once_on

Splits self in two parts at the first occurence of b
fun split_with(b: BytePattern): Array[Bytes]

core :: Bytes :: split_with

Splits the content on self when encountering b
fun to_i(signed: nullable Bool): Int

core :: Bytes :: to_i

Interprets self as a big-endian integer (unsigned by default)
fun trim: Bytes

core :: Bytes :: trim

Trims off the whitespaces at the beginning and the end of self
init with_capacity(cap: Int)

core :: Bytes :: with_capacity

Init a Bytes with capacity cap
fun xorcipher(key: Bytes): Bytes

core :: Bytes :: xorcipher

Returns self xored with key

Redefined properties

redef type SELF: Bytes

core $ Bytes :: SELF

Type of this instance, automatically specialized in every class
redef fun [](i: Int): Int

core $ Bytes :: []

var b = new Bytes.empty
redef fun []=(i: Int, v: Int)

core $ Bytes :: []=

var b = new Bytes.with_capacity(1)
redef fun accept_msgpack_serializer(v: MsgPackSerializer)

msgpack :: serialization_write $ Bytes :: accept_msgpack_serializer

Hook to customize the serialization of this class to MessagePack
redef fun add(c: Int)

core $ Bytes :: add

var b = new Bytes.empty
redef fun append(arr: Collection[Int])

core $ Bytes :: append

var b = new Bytes.empty
redef fun append_to(b: Sequence[Int])

core $ Bytes :: append_to

Appends self to b
redef fun clear

core $ Bytes :: clear

Remove all items
redef fun enlarge(sz: Int)

core $ Bytes :: enlarge

Force the capacity to be at least cap.
redef fun first_index_in_from(b: SequenceRead[Int], from: Int): Int

core $ Bytes :: first_index_in_from

Return the first occurence of self in b starting at from, or -1 if not found
redef fun has(c: nullable Object): Bool

core $ Bytes :: has

Is item in the collection ?
redef fun is_empty: Bool

core $ Bytes :: is_empty

Is there no item in the collection?
redef fun is_prefix(b: SequenceRead[Int]): Bool

core $ Bytes :: is_prefix

Is self a prefix for b ?
redef fun is_suffix(b: SequenceRead[Int]): Bool

core $ Bytes :: is_suffix

Is self a suffix for b ?
redef fun iterator: Iterator[Int]

core $ Bytes :: iterator

Get a new iterator on the collection.
redef fun last_index_in_from(b: SequenceRead[Int], from: Int): Int

core $ Bytes :: last_index_in_from

Return the last occurence of self in b, or -1 if not found
redef fun length: Int

core $ Bytes :: length

Number of bytes in the array
redef fun length=(length: Int)

core $ Bytes :: length=

Number of bytes in the array
redef fun pattern_length: Int

core $ Bytes :: pattern_length

Length of the pattern
redef fun pop: Int

core $ Bytes :: pop

var b = new Bytes.empty
redef fun search_all_in(b: SequenceRead[Int]): SequenceRead[Int]

core $ Bytes :: search_all_in

Returns the indexes of all the occurences of self in b
redef fun to_s: String

core $ Bytes :: to_s

User readable representation of self.
redef fun write_to(s: Writer)

core :: stream $ Bytes :: write_to

Write itself to a stream
redef fun write_to_string: String

core :: stream $ Bytes :: write_to_string

Like write_to but return a new String (may be quite large).

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type CONCURRENT: ConcurrentCollection[E]

core :: Collection :: CONCURRENT

Type of the concurrent variant of this collection
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
abstract fun [](index: Int): E

core :: SequenceRead :: []

Return the index-th element of the sequence.
abstract fun []=(index: Int, item: E)

core :: Sequence :: []=

Set the item at index.
protected fun accept_json_serializer(v: JsonSerializer)

serialization :: Serializable :: accept_json_serializer

Refinable service to customize the serialization of this class to JSON
protected fun accept_msgpack_attribute_counter(v: AttributeCounter)

serialization :: Serializable :: accept_msgpack_attribute_counter

Hook to customize the behavior of the AttributeCounter
protected fun accept_msgpack_serializer(v: MsgPackSerializer)

serialization :: Serializable :: accept_msgpack_serializer

Hook to customize the serialization of this class to MessagePack
abstract fun add(item: E)

core :: SimpleCollection :: add

Add item to this collection.
fun add_all(coll: Collection[E])

core :: SimpleCollection :: add_all

Add each item of coll.
fun add_char(c: Char)

core :: Bytes :: add_char

Adds the UTF-8 representation of c to self
protected fun add_to_bundle(bundle: NativeBundle, key: JavaString)

serialization :: Serializable :: add_to_bundle

Called by []= to dynamically choose the appropriate method according
fun append(coll: Collection[E])

core :: Sequence :: append

Add each item of coll after the last.
fun append_ns(ns: CString, ln: Int)

core :: Bytes :: append_ns

Appends the ln first bytes of ns to self
fun append_ns_from(ns: CString, ln: Int, from: Int)

core :: Bytes :: append_ns_from

Appends ln bytes from ns starting at index from to self
fun append_text(str: Text)

core :: Bytes :: append_text

Appends the bytes of str to self
abstract fun append_to(b: Sequence[Int])

core :: BytePattern :: append_to

Appends self to b
fun as_fifo: Queue[E]

core :: Sequence :: as_fifo

Return a FIFO proxy queue where result.take is shift.
fun as_lifo: Queue[E]

core :: Sequence :: as_lifo

Return a LIFO proxy queue (stack) where result.take is pop.
fun as_random: Queue[E]

core :: SimpleCollection :: as_random

Return a random proxy queue where result.take is random.
fun binarydigest: String

core :: Bytes :: binarydigest

Returns self as a stream of bits (0 and 1)
fun check_base64: nullable Error

core :: Bytes :: check_base64

Is self a well-formed Base64 entity ?
fun chexdigest: String

core :: Bytes :: chexdigest

Return self as a C hexadecimal digest where bytes are prefixed by \x
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
abstract fun clear

core :: RemovableCollection :: clear

Remove all items
fun clone: Bytes

core :: Bytes :: clone

Returns a copy of self
fun combinations(r: Int): Collection[SequenceRead[E]]

core :: Collection :: combinations

All r-length combinations on self (in same order) without repeated elements.
fun combinations_with_replacement(r: Int): Collection[SequenceRead[E]]

core :: Collection :: combinations_with_replacement

All r-length combination on self (in same order) with repeated elements.
fun copy_to(start: Int, len: Int, dest: AbstractArray[E], new_start: Int)

core :: AbstractArrayRead :: copy_to

Copy a portion of self to an other array.
fun core_serialize_to(serializer: Serializer)

serialization :: Serializable :: core_serialize_to

Actual serialization of self to serializer
fun count(item: nullable Object): Int

core :: Collection :: count

How many occurrences of item are in the collection?
fun decode_base64: Bytes

core :: Bytes :: decode_base64

Decodes the receiver string to base64 using a custom padding character.
init defaultinit(items: CString, length: Int, capacity: Int)

core :: Bytes :: defaultinit

fun deserialize_msgpack(static_type: nullable String): nullable Object

core :: Bytes :: deserialize_msgpack

Deserialize full Nit nullable Object from MessagePack formated data
init empty

core :: Bytes :: empty

var b = new Bytes.empty
fun encode_base64: Bytes

core :: Bytes :: encode_base64

Encodes the receiver string to base64 using a custom padding character.
abstract fun enlarge(cap: Int)

core :: AbstractArray :: enlarge

Force the capacity to be at least cap.
fun first: E

core :: Collection :: first

Return the first item of the collection
fun first=(item: E)

core :: Sequence :: first=

Set the first item.
fun first_index_in(b: SequenceRead[Int]): Int

core :: BytePattern :: first_index_in

Return the first occurence of self in b, or -1 if not found
abstract fun first_index_in_from(b: SequenceRead[Int], from: Int): Int

core :: BytePattern :: first_index_in_from

Return the first occurence of self in b starting at from, or -1 if not found
init from_deserializer(deserializer: Deserializer)

serialization :: Serializable :: from_deserializer

Create an instance of this class from the deserializer
fun from_percent_encoding: Bytes

core :: Bytes :: from_percent_encoding

Decode self from percent (or URL) encoding to a clear string
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun get_or_default(index: Int, default: E): E

core :: SequenceRead :: get_or_default

Try to get an element, return default if the index is invalid.
fun get_or_null(index: Int): nullable E

core :: SequenceRead :: get_or_null

Try to get an element, return null if the index is invalid.
fun hamming_distance(other: SequenceRead[Int]): Int

core :: Bytes :: hamming_distance

Computes the edit/hamming distance of two sequences of bytes.
fun has(item: nullable Object): Bool

core :: Collection :: has

Is item in the collection ?
fun has_all(other: Collection[nullable Object]): Bool

core :: Collection :: has_all

Does the collection contain at least each element of other?
fun has_any(other: Collection[nullable Object]): Bool

core :: Collection :: has_any

Does the collection contain at least one element of other?
fun has_exactly(other: Collection[nullable Object]): Bool

core :: Collection :: has_exactly

Does the collection contain exactly all the elements of other?
fun has_only(item: nullable Object): Bool

core :: Collection :: has_only

Is the collection contain only item?
fun has_prefix(b: BytePattern): Bool

core :: Bytes :: has_prefix

Is b a prefix of self ?
fun has_suffix(b: BytePattern): Bool

core :: Bytes :: has_suffix

Is b a suffix of self ?
fun hash: Int

core :: Object :: hash

The hash code of the object.
fun hexdigest: String

core :: Bytes :: hexdigest

Returns self as an hexadecimal digest.
fun index_of(item: nullable Object): Int

core :: SequenceRead :: index_of

The index of the first occurrence of item.
fun index_of_from(item: nullable Object, pos: Int): Int

core :: SequenceRead :: index_of_from

The index of the first occurrence of item, starting from pos.
init init

core :: Object :: init

abstract fun insert(item: E, index: Int)

core :: Sequence :: insert

Insert an element at a given position, following elements are shifted.
fun insert_all(coll: Collection[E], index: Int)

core :: Sequence :: insert_all

Insert all elements at a given position, following elements are shifted.
fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
fun is_base64: Bool

core :: Bytes :: is_base64

Is self a well-formed Base64 entity ?
fun is_empty: Bool

core :: Collection :: is_empty

Is there no item in the collection?
abstract fun is_prefix(b: SequenceRead[Int]): Bool

core :: BytePattern :: is_prefix

Is self a prefix for b ?
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
abstract fun is_suffix(b: SequenceRead[Int]): Bool

core :: BytePattern :: is_suffix

Is self a suffix for b ?
fun items: CString

core :: Bytes :: items

A CString being a char*, it can be used as underlying representation here.
protected fun items=(items: CString)

core :: Bytes :: items=

A CString being a char*, it can be used as underlying representation here.
abstract fun iterator: Iterator[E]

core :: Collection :: iterator

Get a new iterator on the collection.
fun iterator_from(pos: Int): IndexedIterator[E]

core :: SequenceRead :: iterator_from

Gets a new Iterator starting at position pos
fun join(separator: nullable Text, last_separator: nullable Text): String

core :: Collection :: join

Concatenate and separate each elements with separator.
fun last: E

core :: SequenceRead :: last

Get the last item.
fun last=(item: E)

core :: Sequence :: last=

Set the last item.
fun last_index_in(b: SequenceRead[Int]): Int

core :: BytePattern :: last_index_in

Return the last occurence of self in b, or -1 if not found
abstract fun last_index_in_from(b: SequenceRead[Int], from: Int): Int

core :: BytePattern :: last_index_in_from

Return the last occurence of self in b, or -1 if not found
fun last_index_of(item: nullable Object): Int

core :: SequenceRead :: last_index_of

The index of the last occurrence of item.
fun last_index_of_from(item: nullable Object, pos: Int): Int

core :: SequenceRead :: last_index_of_from

The index of the last occurrence of item starting from pos and decrementing.
fun length: Int

core :: Collection :: length

Number of items in the collection.
protected fun length=(length: Int)

core :: AbstractArrayRead :: length=

fun modulo(index: Int): E

core :: SequenceRead :: modulo

Return the index-th element but wrap
fun modulo=(index: Int, value: E)

core :: Sequence :: modulo=

Set the index-th element but wrap
fun modulo_index(index: Int): Int

core :: SequenceRead :: modulo_index

Returns the real index for a modulo index.
protected fun msgpack_extra_array_items: Int

serialization :: Serializable :: msgpack_extra_array_items

Hook to request a larger than usual metadata array
fun not_empty: Bool

core :: Collection :: not_empty

Alias for not is_empty.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
abstract fun pattern_length: Int

core :: BytePattern :: pattern_length

Length of the pattern
fun permutations(r: Int): Collection[SequenceRead[E]]

core :: Collection :: permutations

All r-length permutations on self (all possible ordering) without repeated elements.
fun pkcs7(blocksize: Int): Bytes

core :: Bytes :: pkcs7

PKCS#7 padding.
fun plain_to_s: String

core :: Collection :: plain_to_s

Concatenate elements without separators
abstract fun pop: E

core :: Sequence :: pop

Remove the last item.
fun prepend(coll: Collection[E])

core :: Sequence :: prepend

Add all items of coll before the first one.
fun product(r: Int): Collection[SequenceRead[E]]

core :: Collection :: product

Cartesian product, over r times self.
abstract fun push(e: E)

core :: Sequence :: push

Add an item after the last one.
fun rand: E

core :: Collection :: rand

Return a random element form the collection
abstract fun remove(item: nullable Object)

core :: RemovableCollection :: remove

Remove an occurrence of item
fun remove_all(item: nullable Object)

core :: RemovableCollection :: remove_all

Remove all occurrences of item
abstract fun remove_at(index: Int)

core :: Sequence :: remove_at

Remove the item at index and shift all following elements
fun replace(pattern: BytePattern, bytes: BytePattern): Bytes

core :: Bytes :: replace

Replaces all the occurences of this in self by by
fun reverse

core :: Bytes :: reverse

Reverse the byte array in place
abstract fun reverse_iterator: IndexedIterator[E]

core :: SequenceRead :: reverse_iterator

Gets an iterator starting at the end and going backwards
fun reverse_iterator_from(pos: Int): IndexedIterator[E]

core :: SequenceRead :: reverse_iterator_from

Gets an iterator on the chars of self starting from pos
fun reversed: Array[E]

core :: AbstractArrayRead :: reversed

Return a new array that is the reverse of self
fun rotate_left

core :: Sequence :: rotate_left

Rotates the elements of self once to the left
fun rotate_right

core :: Sequence :: rotate_right

Rotates the elements of self once to the right
fun sample(length: Int): Array[E]

core :: Collection :: sample

Return a new array made of (at most) length elements randomly chosen.
abstract fun search_all_in(b: SequenceRead[Int]): SequenceRead[Int]

core :: BytePattern :: search_all_in

Returns the indexes of all the occurences of self in b
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun serialize_msgpack(plain: nullable Bool): Bytes

serialization :: Serializable :: serialize_msgpack

Serialize self to MessagePack bytes
fun serialize_to(serializer: Serializer)

serialization :: Serializable :: serialize_to

Serialize self to serializer
fun serialize_to_json(plain: nullable Bool, pretty: nullable Bool): String

serialization :: Serializable :: serialize_to_json

Serialize self to JSON
abstract fun shift: E

core :: Sequence :: shift

Remove the first item.
fun shuffle

core :: AbstractArray :: shuffle

Reorder randomly the elements in self.
fun slice(from: Int, count: Int): Bytes

core :: Bytes :: slice

Copy a subset of self starting at from and of count bytes
fun slice_from(from: Int): Bytes

core :: Bytes :: slice_from

Copy of self starting at from
fun split_once_on(b: BytePattern): Array[Bytes]

core :: Bytes :: split_once_on

Splits self in two parts at the first occurence of b
fun split_with(b: BytePattern): Array[Bytes]

core :: Bytes :: split_with

Splits the content on self when encountering b
fun sub(from: Int, count: Int): Array[E]

core :: AbstractArrayRead :: sub

Returns a sub-array containing count elements starting from from.
fun subarray(start: Int, len: Int): Array[E]

core :: Sequence :: subarray

Copy the content of self between start and len to a new Array.
fun swap_at(a: Int, b: Int)

core :: AbstractArray :: swap_at

Invert two elements in the array
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun to_a: Array[E]

core :: Collection :: to_a

Build a new array from a collection
abstract fun to_concurrent: CONCURRENT

core :: Collection :: to_concurrent

Wraps self in a thread-safe collection
fun to_counter: Counter[E]

core :: Collection :: to_counter

Create and fill up a counter with the elements of `self.
fun to_curlslist: CURLSList

core :: Collection :: to_curlslist

Convert Collection[String] to CURLSList
fun to_i(signed: nullable Bool): Int

core :: Bytes :: to_i

Interprets self as a big-endian integer (unsigned by default)
fun to_json: String

serialization :: Serializable :: to_json

Serialize self to plain JSON
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_pretty_json: String

serialization :: Serializable :: to_pretty_json

Serialize self to plain pretty JSON
fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun to_shuffle: Array[E]

core :: Collection :: to_shuffle

Return a new array made of elements in a random order.
fun trim: Bytes

core :: Bytes :: trim

Trims off the whitespaces at the beginning and the end of self
abstract fun unshift(e: E)

core :: Sequence :: unshift

Add an item before the first one.
init with_capacity(cap: Int)

core :: Bytes :: with_capacity

Init a Bytes with capacity cap
abstract fun write_to(stream: Writer)

core :: Writable :: write_to

Write itself to a stream
fun write_to_bytes: Bytes

core :: Writable :: write_to_bytes

Like write_to but return a new Bytes (may be quite large)
fun write_to_file(filepath: String)

core :: Writable :: write_to_file

Like write_to but take care of creating the file
fun write_to_string: String

core :: Writable :: write_to_string

Like write_to but return a new String (may be quite large).
fun xorcipher(key: Bytes): Bytes

core :: Bytes :: xorcipher

Returns self xored with key
package_diagram core::Bytes Bytes core::Writable Writable core::Bytes->core::Writable core::AbstractArray AbstractArray core::Bytes->core::AbstractArray core::BytePattern BytePattern core::Bytes->core::BytePattern core::Object Object core::Writable->core::Object core::AbstractArrayRead AbstractArrayRead core::AbstractArray->core::AbstractArrayRead core::Sequence Sequence core::AbstractArray->core::Sequence core::BytePattern->core::Object ...core::Object ... ...core::Object->core::Object ...core::AbstractArrayRead ... ...core::AbstractArrayRead->core::AbstractArrayRead ...core::Sequence ... ...core::Sequence->core::Sequence

Ancestors

abstract class AbstractArrayRead[E: nullable Object]

core :: AbstractArrayRead

One dimension array of objects.
interface Collection[E: nullable Object]

core :: Collection

The root of the collection hierarchy.
interface Object

core :: Object

The root of the class hierarchy.
interface RemovableCollection[E: nullable Object]

core :: RemovableCollection

Items can be removed from this collection
interface Sequence[E: nullable Object]

core :: Sequence

Sequence are indexed collection.
interface SequenceRead[E: nullable Object]

core :: SequenceRead

Sequences are indexed collections.
interface Serializable

serialization :: Serializable

Instances of this class can be passed to Serializer::serialize
interface SimpleCollection[E: nullable Object]

core :: SimpleCollection

Items can be added to these collections.

Parents

abstract class AbstractArray[E: nullable Object]

core :: AbstractArray

Resizable one dimension array of objects.
interface BytePattern

core :: BytePattern

Any kind of entity which can be searched for in a Sequence of Byte
interface Writable

core :: Writable

Things that can be efficienlty written to a Writer

Class definitions

core $ Bytes
# A buffer containing Byte-manipulation facilities
#
# Uses Copy-On-Write when persisted
class Bytes
	super AbstractArray[Int]
	super BytePattern

	# A CString being a char*, it can be used as underlying representation here.
	var items: CString

	# Number of bytes in the array
	redef var length

	# Capacity of the array
	private var capacity: Int

	# Has this buffer been persisted (to_s'd)?
	#
	# Used for Copy-On-Write
	private var persisted = false

	#     var b = new Bytes.empty
	#     assert b.to_s == ""
	init empty do
		var ns = new CString(0)
		init(ns, 0, 0)
	end

	# Init a `Bytes` with capacity `cap`
	init with_capacity(cap: Int) do
		var ns = new CString(cap)
		init(ns, 0, cap)
	end

	redef fun pattern_length do return length

	redef fun is_empty do return length == 0

	#     var b = new Bytes.empty
	#     b.add 101
	#     assert b[0] == 101
	redef fun [](i) do
		assert i >= 0
		assert i < length
		return items[i]
	end

	# Returns a copy of `self`
	fun clone: Bytes do
		var b = new Bytes.with_capacity(length)
		b.append(self)
		return b
	end

	# Trims off the whitespaces at the beginning and the end of `self`
	#
	#     var b = "102041426E6F1020" .hexdigest_to_bytes
	#     assert b.trim.hexdigest == "41426E6F"
	#
	# NOTE: A whitespace is defined here as a byte whose value is <= 0x20
	fun trim: Bytes do
		var st = 0
		while st < length do
			if self[st] > 0x20 then break
			st += 1
		end
		if st >= length then return new Bytes.empty
		var ed = length - 1
		while ed > 0 do
			if self[ed] > 0x20 then break
			ed -= 1
		end
		return slice(st, ed - st + 1)
	end

	# Copy a subset of `self` starting at `from` and of `count` bytes
	#
	#     var b = "abcd".to_bytes
	#     assert b.slice(1, 2).hexdigest == "6263"
	#     assert b.slice(-1, 2).hexdigest == "61"
	#     assert b.slice(1, 0).hexdigest == ""
	#     assert b.slice(2, 5).hexdigest == "6364"
	fun slice(from, count: Int): Bytes do
		if count <= 0 then return new Bytes.empty

		if from < 0 then
			count += from
			if count < 0 then count = 0
			from = 0
		end

		if (count + from) > length then count = length - from
		if count <= 0 then return new Bytes.empty

		var ret = new Bytes.with_capacity(count)

		ret.append_ns(items.fast_cstring(from), count)
		return ret
	end

	# Copy of `self` starting at `from`
	#
	#     var b = "abcd".to_bytes
	#     assert b.slice_from(1).hexdigest  == "626364"
	#     assert b.slice_from(-1).hexdigest == "61626364"
	#     assert b.slice_from(2).hexdigest  == "6364"
	fun slice_from(from: Int): Bytes do
		if from >= length then return new Bytes.empty
		if from < 0 then from = 0
		return slice(from, length)
	end

	# Reverse the byte array in place
	#
	#     var b = "abcd".to_bytes
	#     b.reverse
	#     assert b.to_s == "dcba"
	fun reverse
	do
		var l = length
		for i in [0..l/2[ do
			var tmp = self[i]
			self[i] = self[l-i-1]
			self[l-i-1] = tmp
		end
	end

	# Returns self as an hexadecimal digest.
	#
	# Also known as plain hexdump or postscript hexdump.
	#
	# ~~~
	# var b = "abcd".to_bytes
	# assert b.hexdigest == "61626364"
	# assert b.hexdigest.hexdigest_to_bytes == b
	# ~~~
	fun hexdigest: String do
		var elen = length * 2
		var ns = new CString(elen)
		var i = 0
		var oi = 0
		while i < length do
			self[i].add_digest_at(ns, oi)
			i += 1
			oi += 2
		end
		return new FlatString.full(ns, elen, 0, elen)
	end

	# Return self as a C hexadecimal digest where bytes are prefixed by `\x`
	#
	# The output is compatible with literal stream of bytes for most languages
	# including C and Nit.
	#
	# ~~~
	# var b = "abcd".to_bytes
	# assert b.chexdigest == "\\x61\\x62\\x63\\x64"
	# assert b.chexdigest.unescape_to_bytes == b
	# ~~~
	fun chexdigest: String do
		var elen = length * 4
		var ns = new CString(elen)
		var i = 0
		var oi = 0
		while i < length do
			ns[oi] = u'\\'
			ns[oi+1] = u'x'
			self[i].add_digest_at(ns, oi+2)
			i += 1
			oi += 4
		end
		return new FlatString.full(ns, elen, 0, elen)
	end


	# Returns self as a stream of bits (0 and 1)
	#
	# ~~~
	# var b = "abcd".to_bytes
	# assert b.binarydigest == "01100001011000100110001101100100"
	# assert b.binarydigest.binarydigest_to_bytes == b
	# ~~~
	fun binarydigest: String do
		var elen = length * 8
		var ns = new CString(elen)
		var i = 0
		var oi = 0
		while i < length do
			var c = self[i]
			var b = 128
			while b > 0 do
				if c & b == 0 then
					ns[oi] = u'0'
				else
					ns[oi] = u'1'
				end
				oi += 1
				b = b >> 1
			end
			i += 1
		end
		return new FlatString.full(ns, elen, 0, elen)
	end

	# Interprets `self` as a big-endian integer (unsigned by default)
	#
	# ~~~
	# var b = "0102".hexdigest_to_bytes
	# assert b.to_i == 258
	#
	# assert   "01".hexdigest_to_bytes.to_i == 1
	# assert   "FF".hexdigest_to_bytes.to_i == 255
	# assert "0000".hexdigest_to_bytes.to_i == 0
	# ~~~
	#
	# If `self.is_empty`, 0 is returned.
	#
	# ~~~
	# assert "".hexdigest_to_bytes.to_i == 0
	# ~~~
	#
	# If `signed == true`, the bytes are read as a signed integer.
	# As usual, the sign bit is the left most bit, no matter the
	# `length` of `self`.
	#
	# ~~~
	# assert     "01".hexdigest_to_bytes.to_i(true) ==      1
	# assert     "FF".hexdigest_to_bytes.to_i(true) ==     -1
	# assert   "00FF".hexdigest_to_bytes.to_i(true) ==    255
	# assert     "E0".hexdigest_to_bytes.to_i(true) ==    -32
	# assert   "FE00".hexdigest_to_bytes.to_i(true) ==   -512
	# assert "FEFEFE".hexdigest_to_bytes.to_i(true) == -65794
	# ~~~
	#
	# `Int::to_bytes` is a loosely reverse method.
	#
	# ~~~
	# assert b.to_i.to_bytes == b
	# assert (b.to_i + 1).to_bytes.hexdigest == "0103"
	# assert "0001".hexdigest_to_bytes.to_i.to_bytes.hexdigest == "01"
	#
	# assert (-32).to_bytes.to_i(true) == -32
	# ~~~
	#
	# Warning: `Int` might overflow for bytes with more than 60 bits.
	fun to_i(signed: nullable Bool): Int do
		var res = 0
		var i = 0
		while i < length do
			res *= 256
			res += self[i].to_i
			i += 1
		end

		# Two's complement is `signed`
		if signed == true and not_empty and first > 0x80 then
			var ff = 0
			for j in [0..length[ do
				ff *= 0x100
				ff += 0xFF
			end

			res = -((res ^ ff) + 1)
		end

		return res
	end

	#     var b = new Bytes.with_capacity(1)
	#     b[0] = 101
	#     assert b.to_s == "e"
	redef fun []=(i, v) do
		if persisted then regen
		assert i >= 0
		assert i <= length
		if i == length then add(v)
		items[i] = v
	end

	#     var b = new Bytes.empty
	#     b.add 101
	#     assert b.to_s == "e"
	redef fun add(c) do
		if persisted then regen
		if length >= capacity then
			enlarge(length)
		end
		items[length] = c
		length += 1
	end

	# Adds the UTF-8 representation of `c` to `self`
	#
	#     var b = new Bytes.empty
	#     b.add_char('A')
	#     b.add_char('キ')
	#     assert b.hexdigest == "41E382AD"
	fun add_char(c: Char) do
		if persisted then regen
		var cln = c.u8char_len
		var ln = length
		enlarge(ln + cln)
		items.set_char_at(length, c)
		length += cln
	end

	redef fun has(c)
	do
		if not c isa Int then return false
		return super(c&255)
	end

	#     var b = new Bytes.empty
	#     b.append([104, 101, 108, 108, 111])
	#     assert b.to_s == "hello"
	redef fun append(arr) do
		if arr isa Bytes then
			append_ns(arr.items, arr.length)
		else
			for i in arr do add i
		end
	end

	#     var b = new Bytes.empty
	#     b.append([0x41, 0x41, 0x18])
	#     b.pop
	#     assert b.to_s == "AA"
	redef fun pop do
		assert length >= 1
		length -= 1
		return items[length]
	end

	redef fun clear do length = 0

	# Regenerates the buffer, necessary when it was persisted
	private fun regen do
		var nns = new CString(capacity)
		items.copy_to(nns, length, 0, 0)
		persisted = false
	end

	# Appends the `ln` first bytes of `ns` to self
	fun append_ns(ns: CString, ln: Int) do
		if persisted then regen
		var nlen = length + ln
		if nlen > capacity then enlarge(nlen)
		ns.copy_to(items, ln, 0, length)
		length += ln
	end

	# Appends `ln` bytes from `ns` starting at index `from` to self
	fun append_ns_from(ns: CString, ln, from: Int) do
		if persisted then regen
		var nlen = length + ln
		if nlen > capacity then enlarge(nlen)
		ns.copy_to(items, ln, from, length)
		length += ln
	end

	# Appends the bytes of `str` to `self`
	fun append_text(str: Text) do str.append_to_bytes self

	redef fun append_to(b) do b.append self

	redef fun enlarge(sz) do
		if capacity >= sz then return
		persisted = false
		if capacity < 16 then capacity = 16
		while capacity < sz do capacity = capacity * 2 + 2
		var ns = new CString(capacity)
		items.copy_to(ns, length, 0, 0)
		items = ns
	end

	redef fun to_s do
		persisted = true
		var b = self
		var r = b.items.to_s_unsafe(length, copy=false)
		if r != items then persisted = false
		return r
	end

	redef fun iterator do return new BytesIterator.with_buffer(self)

	redef fun first_index_in_from(b, from) do
		if is_empty then return -1
		var fst = self[0]
		var bpos = fst.first_index_in_from(self, from)
		for i in [0 .. length[ do
			if self[i] != b[bpos] then return first_index_in_from(b, bpos + 1)
			bpos += 1
		end
		return bpos
	end

	redef fun last_index_in_from(b, from) do
		if is_empty then return -1
		var lst = self[length - 1]
		var bpos = lst.last_index_in_from(b, from)
		for i in [0 .. length[.step(-1) do
			if self[i] != b[bpos] then return last_index_in_from(b, bpos - 1)
			bpos -= 1
		end
		return bpos
	end

	redef fun search_all_in(b) do
		var ret = new Array[Int]
		var pos = first_index_in_from(b, 0)
		if pos == -1 then return ret
		pos = pos + 1
		ret.add pos
		loop
			pos = first_index_in_from(b, pos)
			if pos == -1 then return ret
			ret.add pos
			pos += length
		end
	end

	# Splits the content on self when encountering `b`
	#
	#     var a = "String is string".to_bytes.split_with(u's')
	#     assert a.length == 3
	#     assert a[0].hexdigest == "537472696E672069"
	#     assert a[1].hexdigest == "20"
	#     assert a[2].hexdigest == "7472696E67"
	fun split_with(b: BytePattern): Array[Bytes] do
		var fst = b.search_all_in(self)
		if fst.is_empty then return [clone]
		var retarr = new Array[Bytes]
		var prev = 0
		for i in fst do
			retarr.add(slice(prev, i - prev))
			prev = i + b.pattern_length
		end
		retarr.add slice_from(prev)
		return retarr
	end

	# Splits `self` in two parts at the first occurence of `b`
	#
	#     var a = "String is string".to_bytes.split_once_on(u's')
	#     assert a[0].hexdigest == "537472696E672069"
	#     assert a[1].hexdigest == "20737472696E67"
	fun split_once_on(b: BytePattern): Array[Bytes] do
		var spl = b.first_index_in(self)
		if spl == -1 then return [clone]
		var ret = new Array[Bytes].with_capacity(2)
		ret.add(slice(0, spl))
		ret.add(slice_from(spl + b.pattern_length))
		return ret
	end

	# Replaces all the occurences of `this` in `self` by `by`
	#
	#     var b = "String is string".to_bytes.replace(0x20, 0x41)
	#     assert b.hexdigest == "537472696E6741697341737472696E67"
	fun replace(pattern: BytePattern, bytes: BytePattern): Bytes do
		if is_empty then return new Bytes.empty
		var pos = pattern.search_all_in(self)
		if pos.is_empty then return clone
		var ret = new Bytes.with_capacity(length)
		var prev = 0
		for i in pos do
			ret.append_ns(items.fast_cstring(prev), i - prev)
			bytes.append_to ret
			prev = i + pattern.pattern_length
		end
		ret.append(slice_from(pos.last + pattern.pattern_length))
		return ret
	end

	# Decode `self` from percent (or URL) encoding to a clear string
	#
	# Invalid '%' are not decoded.
	#
	#     assert "aBc09-._~".to_bytes.from_percent_encoding == "aBc09-._~".to_bytes
	#     assert "%25%28%29%3c%20%3e".to_bytes.from_percent_encoding == "%()< >".to_bytes
	#     assert ".com%2fpost%3fe%3dasdf%26f%3d123".to_bytes.from_percent_encoding == ".com/post?e=asdf&f=123".to_bytes
	#     assert "%25%28%29%3C%20%3E".to_bytes.from_percent_encoding == "%()< >".to_bytes
	#     assert "incomplete %".to_bytes.from_percent_encoding == "incomplete %".to_bytes
	#     assert "invalid % usage".to_bytes.from_percent_encoding == "invalid % usage".to_bytes
	#     assert "%c3%a9%e3%81%82%e3%81%84%e3%81%86".to_bytes.from_percent_encoding == "éあいう".to_bytes
	#     assert "%1 %A %C3%A9A9".to_bytes.from_percent_encoding == "%1 %A éA9".to_bytes
	fun from_percent_encoding: Bytes do
		var tmp = new Bytes.with_capacity(length)
		var pos = 0
		while pos < length do
			var b = self[pos]
			if b != u'%' then
				tmp.add b
				pos += 1
				continue
			end
			if length - pos < 2 then
				tmp.add u'%'
				pos += 1
				continue
			end
			var bn = self[pos + 1]
			var bnn = self[pos + 2]
			if not bn.is_valid_hexdigit or not bnn.is_valid_hexdigit then
				tmp.add u'%'
				pos += 1
				continue
			end
			tmp.add((bn.hexdigit_to_byteval << 4) + bnn.hexdigit_to_byteval)
			pos += 3
		end
		return tmp
	end

	# Is `b` a prefix of `self` ?
	fun has_prefix(b: BytePattern): Bool do return b.is_prefix(self)

	# Is `b` a suffix of `self` ?
	fun has_suffix(b: BytePattern): Bool do return b.is_suffix(self)

	redef fun is_suffix(b) do
		if length > b.length then return false
		var j = b.length - 1
		var i = length - 1
		while i > 0 do
			if self[i] != b[j] then return false
			i -= 1
			j -= 1
		end
		return true
	end

	redef fun is_prefix(b) do
		if length > b.length then return false
		for i in [0 .. length[ do if self[i] != b[i] then return false
		return true
	end
end
lib/core/bytes.nit:235,1--772,3

core :: stream $ Bytes
redef class Bytes
	super Writable
	redef fun write_to(s) do s.write_bytes(self)

	redef fun write_to_string do return to_s
end
lib/core/stream.nit:586,1--591,3

base64 :: base64 $ Bytes
redef class Bytes

	# Encodes the receiver string to base64 using a custom padding character.
	#
	# If using the default padding character `=`, see `encode_base64`.
	fun encode_base64: Bytes do return items.encode_base64(length)

	# Decodes the receiver string to base64 using a custom padding character.
	#
	# Default padding character `=`
	fun decode_base64: Bytes do return items.decode_base64(length)

	# Is `self` a well-formed Base64 entity ?
	fun is_base64: Bool do return items.is_base64(length)

	# Is `self` a well-formed Base64 entity ?
	#
	# Will return an Error otherwise with info on which part is erroneous.
	fun check_base64: nullable Error do return items.check_base64(length)
end
lib/base64/base64.nit:200,1--219,3

crypto :: basic_ciphers $ Bytes
redef class Bytes
	# Computes the edit/hamming distance of two sequences of bytes.
	#
	#     assert "this is a test".to_bytes.hamming_distance("wokka wokka!!!".bytes) == 37
	#     assert "this is a test".to_bytes.hamming_distance("this is a test".bytes) == 0
	#
	fun hamming_distance(other: SequenceRead[Int]): Int do
		var diff = 0
		for idx in self.length.times do
			var res_byte = self[idx] ^ other[idx]
			for bit in [0..8[ do
				if res_byte & 1 == 1 then diff += 1
				res_byte = res_byte >> 1
			end
		end
		return diff
	end
end
lib/crypto/basic_ciphers.nit:209,1--226,3

crypto :: bytes $ Bytes
redef class Bytes
	# PKCS#7 padding.
	#
	# Extends `self` by appending the number of bytes of padding to the end of the block.
	#
	#     assert "YELLOW SUBMARINE".to_bytes.pkcs7(20) == "YELLOW SUBMARINE\x04\x04\x04\x04".to_bytes
	fun pkcs7(blocksize: Int): Bytes
	do
		var mod = length % blocksize
		if mod == 0 then return self
		var pad = blocksize - mod
		var byte = pad
		for i in [0..pad[ do add byte
		return self
	end
end
lib/crypto/bytes.nit:18,1--33,3

crypto :: xor_ciphers $ Bytes
redef class Bytes
	# Returns `self` xored with `key`
	#
	# The key is cycled through until the `self` has been completely xored.
	#
	#     assert "goodmorning".to_bytes.xorcipher(" ".to_bytes) == "GOODMORNING".bytes
	fun xorcipher(key: Bytes): Bytes do
		var xored = new Bytes.with_capacity(self.length)

		for i in self.length.times do
			xored.add(self[i] ^ key[i % key.length])
		end

		return xored
	end
end
lib/crypto/xor_ciphers.nit:18,1--33,3

msgpack :: serialization_write $ Bytes
redef class Bytes
	redef fun accept_msgpack_serializer(v) do v.stream.write_msgpack_bin self
end
lib/msgpack/serialization_write.nit:289,1--291,3

msgpack :: serialization_read $ Bytes
redef class Bytes

	# Deserialize full Nit `nullable Object` from MessagePack formated data
	#
	# The dynamic type of the deserialized object can be limited to `static_type`.
	#
	# Warning: Deserialization errors are reported with `print_error`,
	# the returned object may be partial or fall back on `null`.
	# To handle the errors programmatically, use a `MsgPackDeserializer`.
	fun deserialize_msgpack(static_type: nullable String): nullable Object
	do
		var stream = new BytesReader(self)
		var res = stream.deserialize_msgpack(static_type)
		stream.close
		return res
	end
end
lib/msgpack/serialization_read.nit:33,1--49,3