Перейти к содержимому
Топаз6.0 · До релиза
Загрузка →

Поиск в документации

Спецификация 6.0

Выберите результат стрелками и нажмите Enter, чтобы открыть его.

Введите имя API, номер диагностики, правило или тему проекта.

    Библиотека · std.bytes

    ByteBuffer.allocate

    Creates a mutable byte buffer with the supplied length and initial value.

    allocate(length: int, value: int = 0) -> ByteBuffer
    Модуль
    std.bytes →
    Получатель
    Неприменимо
    Минимальное число аргументов
    1
    Максимальное число аргументов
    2

    Declaration and parameters

    Ссылка

    Доступность

    Включено по умолчанию · Не определено

    Import std.bytes in language mode 5.21.

    The current compiler selects std/bytes.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    Получатель

    Ограничения параметров

    ByteBuffer.allocate accepts a nonnegative int length and an int initial value from 0 through 255. The initial value defaults to zero.

    The new buffer has exactly that many copies of the initial byte. Zero length is valid and still requires a valid initial byte.

    A negative length produces TPZ4001 at runtime. An invalid initial byte or an allocation that cannot be satisfied produces TPZ5001 at runtime.

    ByteBuffer.fromBytes converts an immutable Bytes value into a mutable ByteBuffer value containing the same bytes.

    ByteBuffer.toBytes converts the current buffer contents into an immutable Bytes value.

    ByteBuffer.length is a read-only byte count property. Buffer operations never change its length.

    Assignment, argument passing, return, capture, and aggregate storage follow collection value semantics.

    Supplied expressions evaluate once from left to right before allocation or conversion. These operations have no I/O effects.

    Результат

    The declared result type is ByteBuffer.

    Эффекты

    Ошибки

    Result and effects

    Ссылка

    Allocate a fixed buffer with a nonnegative byte count and a byte value from 0 through 255. The initial value defaults to zero.

    Resolved contracts

    Ссылка

    Related entries

    Ссылка

    std.bytes →

    Allocate buffers and convert byte values

    Ссылка
    Правилоtopaz.stdlib.contract.byte-buffer-storage.rule

    Allocate buffers and convert byte values

    ByteBuffer.allocate accepts a nonnegative int length and an int initial value from 0 through 255. The initial value defaults to zero.

    The new buffer has exactly that many copies of the initial byte. Zero length is valid and still requires a valid initial byte.

    A negative length produces TPZ4001 at runtime. An invalid initial byte or an allocation that cannot be satisfied produces TPZ5001 at runtime.

    ByteBuffer.fromBytes converts an immutable Bytes value into a mutable ByteBuffer value containing the same bytes.

    ByteBuffer.toBytes converts the current buffer contents into an immutable Bytes value.

    ByteBuffer.length is a read-only byte count property. Buffer operations never change its length.

    Assignment, argument passing, return, capture, and aggregate storage follow collection value semantics.

    Supplied expressions evaluate once from left to right before allocation or conversion. These operations have no I/O effects.

    Причина

    A fixed length gives binary algorithms stable byte positions. Explicit conversion distinguishes mutable construction from immutable byte data.

    Allocate zero-filled and initialized buffers

    Ссылка
    main.tpzAllocate zero-filled and initialized buffersОткрыть песочницу →
    1. import std.bytes { ByteBuffer }
    2. import std.io
    3. io.writeLine(ByteBuffer.allocate(0).toBytes().toHex())
    4. io.writeLine(ByteBuffer.allocate(3).toBytes().toHex())
    5. io.writeLine(ByteBuffer.allocate(2, 255).toBytes().toHex())
    Ожидаемый выводКод завершения 0
    
    000000
    ffff
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. 000000
    2. ffff

    Keep the original buffer and converted bytes unchanged

    Ссылка
    main.tpzKeep the original buffer and converted bytes unchangedОткрыть песочницу →
    1. import std.bytes { ByteBuffer }
    2. import std.io
    3. let source = "ab".encodeUtf8()
    4. let buffer = ByteBuffer.fromBytes(source)
    5. let independent = ByteBuffer.fromBytes(source)
    6. let before = buffer.toBytes()
    7. let mut alias = buffer
    8. alias.set(0, 99)
    9. io.writeLine(source.toHex())
    10. io.writeLine(before.toHex())
    11. io.writeLine(independent.toBytes().toHex())
    12. io.writeLine(buffer.toBytes().toHex())
    Ожидаемый выводКод завершения 0
    6162
    6162
    6162
    6162
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Выполнение завершается. Код завершения 0.

    Ожидаемый стандартный вывод
    1. 6162
    2. 6162
    3. 6162
    4. 6162

    Reject a negative buffer length

    Ссылка
    main.tpzReject a negative buffer lengthОткрыть песочницу →
    1. import std.bytes { ByteBuffer }
    2. ByteBuffer.allocate(-1)
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения run. Ненулевой код завершения.

    Validate the initial byte of an empty buffer

    Ссылка
    main.tpzValidate the initial byte of an empty bufferОткрыть песочницу →
    1. import std.bytes { ByteBuffer }
    2. import std.io
    3. ByteBuffer.allocate(0, -1)
    4. io.writeLine("after")
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения run. Ненулевой код завершения.

    Validate the initial byte of an empty buffer

    Ссылка
    main.tpzValidate the initial byte of an empty bufferОткрыть песочницу →
    1. import std.bytes { ByteBuffer }
    2. import std.io
    3. ByteBuffer.allocate(0, 256)
    4. io.writeLine("after")
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

    Ожидаемый результат

    Этап отклонения run. Ненулевой код завершения.

    Diagnostics

    Ссылка

    Index out of bounds →

    Runtime value guard →

    Меню

    Загрузка →