본문으로 건너뛰기
토파즈6.0 · 출시 전
다운로드 →

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    라이브러리 · std.bytes

    Bytes.length

    Returns the number of bytes in this sequence.

    length: int
    수신자
    Bytes
    최소 인자 수
    0
    최대 인자 수
    0

    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.

    수신자

    Bytes

    매개변수 제약

    Bytes.length and ByteBuffer.length are read-only int properties that report the number of bytes.

    JSONValue.length is an Option<int> property. Arrays and objects return Some(count) for their element or member count. Other JSON kinds return None().

    JSONValue.keys and JSONValue.values are properties returning Option<Array<string>> and Option<Array<JSONValue>>. Objects return Some(keys) or Some(values). Other JSON kinds return None().

    Read these properties without parentheses, as with Array.length, Map.length, Map.keys, Map.values, and Set.length. Calling a property value is rejected as a non-callable value.

    Test whether a Bytes, Map, or Set value is empty by comparing its length property with zero. The comparison is true exactly when the collection has no bytes, entries, or elements.

    Bytes, Map, and Set do not expose an isEmpty member. Read the length property and use the ordinary equality operator.

    A Bytes value is an immutable ordered sequence of octets. Each octet is an int from 0 through 255.

    Bytes.empty returns the empty sequence. Bytes.fromArray accepts an Array<int> and returns an independent snapshot in Ok.

    Bytes.fromArray returns Err with a string identifying the first element outside 0 through 255. It does not wrap, clamp, or return a partial sequence.

    Bytes.toArray returns a new Array<int> in byte order. Mutating that array or the original input array does not change an existing Bytes value.

    Bytes.concat returns the receiver bytes followed by the argument bytes. Either input may be empty and both inputs retain their contents.

    Bytes.get uses a zero-based byte index. It returns Some(byte) within the sequence and None() for every negative or past-end index.

    Bytes.length is the number of bytes and is read as a property. Compare it with zero to test emptiness.

    Receivers and supplied arguments evaluate once from left to right before the operation begins. These operations perform no I/O or text normalization.

    A correctly typed lookup or concatenation has no data-dependent Result failure. Allocation exhaustion is an operational failure and is not a decoded data error.

    결과

    The declared result type is int.

    효과

    실패

    Result and effects

    링크

    Select this field from a value of its named type without call parentheses.

    Read the number of bytes as a read-only int property. Compare it with zero to test emptiness.

    Why

    링크

    Collection observations use the same property syntax as Array, Map, and Set. Parentheses identify calls rather than stored or derived values.

    Related entries

    링크

    std.bytes →

    Read collection sizes, keys, and values as properties

    링크
    규칙topaz.stdlib.contract.collection-properties.rule

    Read collection sizes, keys, and values as properties

    Bytes.length and ByteBuffer.length are read-only int properties that report the number of bytes.

    JSONValue.length is an Option<int> property. Arrays and objects return Some(count) for their element or member count. Other JSON kinds return None().

    JSONValue.keys and JSONValue.values are properties returning Option<Array<string>> and Option<Array<JSONValue>>. Objects return Some(keys) or Some(values). Other JSON kinds return None().

    Read these properties without parentheses, as with Array.length, Map.length, Map.keys, Map.values, and Set.length. Calling a property value is rejected as a non-callable value.

    이유

    The same collection observation should use the same syntax across core collections and library values. Keeping JSONValue results optional preserves its distinction between collections and scalar values.

    Read the same properties on core collections

    링크
    main.tpzRead the same properties on core collections플레이그라운드 열기 →
    1. import std.io
    2. let array = [2, 3]
    3. let entries = map { "name": "Topaz" }
    4. let values = set { 2, 3 }
    5. io.writeLine("{array.length}:{entries.length}:{values.length}")
    6. io.writeLine(entries.keys[0])
    7. io.writeLine(entries.values[0])
    기대 출력종료 코드 0
    2:1:2
    name
    Topaz
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 2:1:2
    2. name
    3. Topaz

    Read a byte sequence size

    링크
    main.tpzRead a byte sequence size플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.io
    3. let bytes = Bytes.empty()
    4. let size: int = bytes.length
    5. io.writeLine("{size}")
    기대 출력종료 코드 0
    0
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 0

    Read a byte buffer size

    링크
    main.tpzRead a byte buffer size플레이그라운드 열기 →
    1. import std.bytes { ByteBuffer }
    2. import std.io
    3. let buffer = ByteBuffer.allocate(3)
    4. let size: int = buffer.length
    5. io.writeLine("{size}")
    기대 출력종료 코드 0
    3
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 3

    Read JSON sizes and preserve optional results

    링크
    main.tpzRead JSON sizes and preserve optional results플레이그라운드 열기 →
    1. import std.json
    2. import std.test
    3. import std.io
    4. let array = test.assertOk(json.parse("[1,2]"))
    5. let object = test.assertOk(json.parse("\{\"name\":\"Topaz\"\}"))
    6. let scalar = test.assertOk(json.parse("42"))
    7. let arraySize: Option<int> = array.length
    8. let objectSize: Option<int> = object.length
    9. let scalarSize: Option<int> = scalar.length
    10. test.assertEq(test.assertSome(arraySize), 2)
    11. test.assertEq(test.assertSome(objectSize), 1)
    12. test.assertNone(scalarSize)
    13. io.writeLine("JSON sizes read")
    기대 출력종료 코드 0
    JSON sizes read
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. JSON sizes read

    Read JSON object keys

    링크
    main.tpzRead JSON object keys플레이그라운드 열기 →
    1. import std.json
    2. import std.test
    3. import std.io
    4. let object = test.assertOk(json.parse("\{\"name\":\"Topaz\"\}"))
    5. let array = test.assertOk(json.parse("[]"))
    6. let scalar = test.assertOk(json.parse("42"))
    7. let names: Option<Array<string>> = object.keys
    8. test.assertEq(test.assertSome(names)[0], "name")
    9. test.assertNone(array.keys)
    10. test.assertNone(scalar.keys)
    11. io.writeLine("JSON keys read")
    기대 출력종료 코드 0
    JSON keys read
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. JSON keys read

    Read JSON object values

    링크
    main.tpzRead JSON object values플레이그라운드 열기 →
    1. import std.json
    2. import std.test
    3. import std.io
    4. let object = test.assertOk(json.parse("\{\"name\":\"Topaz\"\}"))
    5. let array = test.assertOk(json.parse("[]"))
    6. let scalar = test.assertOk(json.parse("42"))
    7. let values = test.assertSome(object.values)
    8. test.assertEq(test.assertSome(values[0].asString()), "Topaz")
    9. test.assertNone(array.values)
    10. test.assertNone(scalar.values)
    11. io.writeLine("JSON values read")
    기대 출력종료 코드 0
    JSON values read
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. JSON values read

    Reject calling Bytes.length

    링크
    main.tpzReject calling Bytes.length플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. function inspect(value: Bytes) -> () {
    3. let result = value.length()
    4. return ()
    5. }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    • TPZ5005 →

      stdlib-property-not-callable at main.tpz:3:22

    Reject calling ByteBuffer.length

    링크
    main.tpzReject calling ByteBuffer.length플레이그라운드 열기 →
    1. import std.bytes { ByteBuffer }
    2. function inspect(value: ByteBuffer) -> () {
    3. let result = value.length()
    4. return ()
    5. }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    • TPZ5005 →

      stdlib-property-not-callable at main.tpz:3:22

    Reject calling JSONValue.length

    링크
    main.tpzReject calling JSONValue.length플레이그라운드 열기 →
    1. import std.json { JSONValue }
    2. function inspect(value: JSONValue) -> () {
    3. let result = value.length()
    4. return ()
    5. }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    • TPZ5005 →

      stdlib-property-not-callable at main.tpz:3:22

    Reject calling JSONValue.keys

    링크
    main.tpzReject calling JSONValue.keys플레이그라운드 열기 →
    1. import std.json { JSONValue }
    2. function inspect(value: JSONValue) -> () {
    3. let result = value.keys()
    4. return ()
    5. }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    • TPZ5005 →

      stdlib-property-not-callable at main.tpz:3:22

    Reject calling JSONValue.values

    링크
    main.tpzReject calling JSONValue.values플레이그라운드 열기 →
    1. import std.json { JSONValue }
    2. function inspect(value: JSONValue) -> () {
    3. let result = value.values()
    4. return ()
    5. }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    • TPZ5005 →

      stdlib-property-not-callable at main.tpz:3:22

    Diagnostics

    링크

    Value is not callable →

    Test collection emptiness with length

    링크
    규칙topaz.stdlib.contract.collection-emptiness.rule

    Test collection emptiness with length

    Test whether a Bytes, Map, or Set value is empty by comparing its length property with zero. The comparison is true exactly when the collection has no bytes, entries, or elements.

    Bytes, Map, and Set do not expose an isEmpty member. Read the length property and use the ordinary equality operator.

    이유

    A collection already exposes its count through length. Comparing that value with zero expresses emptiness for every collection without another query name or a type-specific method.

    Test empty and nonempty maps with length

    링크
    main.tpzTest empty and nonempty maps with length플레이그라운드 열기 →
    1. import std.io
    2. let empty: Map<string, int> = map {}
    3. let filled = map { "one": 1 }
    4. io.writeLine("{empty.length == 0}:{filled.length == 0}")
    기대 출력종료 코드 0
    true:false
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. true:false

    Test empty and nonempty sets with length

    링크
    main.tpzTest empty and nonempty sets with length플레이그라운드 열기 →
    1. import std.io
    2. let empty: Set<int> = set {}
    3. let filled = set { 1 }
    4. io.writeLine("{empty.length == 0}:{filled.length == 0}")
    기대 출력종료 코드 0
    true:false
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. true:false

    Test empty and nonempty byte sequences with length

    링크
    main.tpzTest empty and nonempty byte sequences with length플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.test
    3. import std.io
    4. let empty = Bytes.empty()
    5. let filled = test.assertOk(Bytes.fromArray([1]))
    6. io.writeLine("{empty.length == 0}:{filled.length == 0}")
    기대 출력종료 코드 0
    true:false
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. true:false

    Reject Bytes.isEmpty

    링크
    main.tpzReject Bytes.isEmpty플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. function inspect(value: Bytes) -> () {
    3. let result = value.isEmpty()
    4. return ()
    5. }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    Reject Map.isEmpty

    링크
    main.tpzReject Map.isEmpty플레이그라운드 열기 →
    1. function inspect(value: Map<string, int>) -> () {
    2. let result = value.isEmpty()
    3. return ()
    4. }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    Reject Set.isEmpty

    링크
    main.tpzReject Set.isEmpty플레이그라운드 열기 →
    1. function inspect(value: Set<int>) -> () {
    2. let result = value.isEmpty()
    3. return ()
    4. }
    기대 출력0이 아닌 종료 코드
    표준 출력 없음.
    $ topaz check main.tpz --format json · 대상 제품 5.21.3
    기대 결과

    기대 결과

    거절 단계 check. 종료 코드가 0이 아닙니다.

    Diagnostics

    링크

    Unknown member or field →

    Construct and inspect immutable byte sequences

    링크
    규칙topaz.stdlib.contract.bytes-values.rule

    Construct and inspect immutable byte sequences

    A Bytes value is an immutable ordered sequence of octets. Each octet is an int from 0 through 255.

    Bytes.empty returns the empty sequence. Bytes.fromArray accepts an Array<int> and returns an independent snapshot in Ok.

    Bytes.fromArray returns Err with a string identifying the first element outside 0 through 255. It does not wrap, clamp, or return a partial sequence.

    Bytes.toArray returns a new Array<int> in byte order. Mutating that array or the original input array does not change an existing Bytes value.

    Bytes.concat returns the receiver bytes followed by the argument bytes. Either input may be empty and both inputs retain their contents.

    Bytes.get uses a zero-based byte index. It returns Some(byte) within the sequence and None() for every negative or past-end index.

    Bytes.length is the number of bytes and is read as a property. Compare it with zero to test emptiness.

    Receivers and supplied arguments evaluate once from left to right before the operation begins. These operations perform no I/O or text normalization.

    A correctly typed lookup or concatenation has no data-dependent Result failure. Allocation exhaustion is an operational failure and is not a decoded data error.

    이유

    Exact octets keep binary payloads independent of text encoding. Snapshot conversion prevents later array edits from silently changing a payload.

    Preserve empty values and byte order

    링크
    main.tpzPreserve empty values and byte order플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. let empty = test.assertOk(Bytes.fromArray([]))
    5. let value = test.assertOk(Bytes.fromArray([0, 128, 255]))
    6. io.writeLine(Bytes.empty().toHex())
    7. io.writeLine(empty.concat(value).toHex())
    8. io.writeLine(value.concat(test.assertOk(Bytes.fromArray([1]))).toHex())
    기대 출력종료 코드 0
    
    0080ff
    0080ff01
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 0080ff
    2. 0080ff01

    Reject out-of-range array elements

    링크
    main.tpzReject out-of-range array elements플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.io
    3. for values in ([[0, -1, 255], [0, 256, 255]]) {
    4. match (Bytes.fromArray(values)) {
    5. case Err(_) => io.writeLine("invalid byte")
    6. case Ok(_) => io.writeLine("accepted")
    7. }
    8. }
    기대 출력종료 코드 0
    invalid byte
    invalid byte
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. invalid byte
    2. invalid byte

    Read optional bytes at both boundaries

    링크
    main.tpzRead optional bytes at both boundaries플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. let value = test.assertOk(Bytes.fromArray([0, 128, 255]))
    5. for index in ([-1, 0, 2, 3]) {
    6. match (value.get(index)) {
    7. case Some(byte) => io.writeLine("{byte}")
    8. case None() => io.writeLine("absent")
    9. }
    10. }
    기대 출력종료 코드 0
    absent
    0
    255
    absent
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. absent
    2. 0
    3. 255
    4. absent

    Keep array edits independent from bytes

    링크
    main.tpzKeep array edits independent from bytes플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.io
    3. import std.test
    4. let mut input = [0, 255]
    5. let value = test.assertOk(Bytes.fromArray(input))
    6. let mut output = value.toArray()
    7. input[0] = 7
    8. output[1] = 8
    9. io.writeLine(value.toHex())
    10. io.writeLine("{input[0]},{output[1]}")
    기대 출력종료 코드 0
    00ff
    7,8
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. 00ff
    2. 7,8

    Evaluate the receiver before its argument

    링크
    main.tpzEvaluate the receiver before its argument플레이그라운드 열기 →
    1. import std.bytes { Bytes }
    2. import std.io
    3. function receiver() -> Bytes {
    4. io.writeLine("receiver")
    5. return "a".encodeUtf8()
    6. }
    7. function argument() -> Bytes {
    8. io.writeLine("argument")
    9. return "b".encodeUtf8()
    10. }
    11. io.writeLine(receiver().concat(argument()).toHex())
    기대 출력종료 코드 0
    receiver
    argument
    6162
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. receiver
    2. argument
    3. 6162

    메뉴

    다운로드 →