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

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

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

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

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

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

    BigInt.toString

    Format an exact integer as lowercase digits in the selected radix.

    toString(radix: int) -> string
    Модуль
    std.bigint →
    Получатель
    BigInt
    Минимальное число аргументов
    1
    Максимальное число аргументов
    1

    Declaration and parameters

    Ссылка

    Доступность

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

    Import std.bigint in language mode 5.21.

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

    Получатель

    BigInt

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

    BigInt represents signed integers without a fixed bit width. Its text operations preserve the exact integer value and do not use a locale.

    BigInt.parse takes a radix from 2 through 36. Text consists of an optional minus sign followed by at least one ASCII digit or lowercase ASCII letter. Letters a through z have digit values 10 through 35. Every digit must be smaller than the radix.

    Leading zero digits are accepted. Negative zero produces the same zero value as 0. Parsing does not trim whitespace, accept a plus sign, accept uppercase letters, accept separators, or interpret a radix prefix. Characters are interpreted as ordinary digits of the supplied radix.

    Valid text returns Some containing the exact integer. Empty text, a sign without digits, an invalid digit, any extra character, or a radix outside the allowed range returns None(). These input failures do not raise a runtime fault.

    BigInt.toString takes a radix from 2 through 36 and returns lowercase ASCII digits in that radix. A negative nonzero value starts with a minus sign. The output has no radix prefix or leading zero digits. Zero is written as 0.

    An invalid BigInt.toString radix raises TPZ5001 during execution. It does not select a default radix or return empty text.

    Formatting a value and parsing the result with the same radix recovers that value. Parsing and formatting accepted text produces its canonical spelling in the selected radix.

    The receiver and positional arguments are evaluated once in source order before the operation validates their values. An earlier fault prevents later evaluation. The operations do not change their inputs or perform external effects.

    Результат

    The declared result type is string.

    Эффекты

    Ошибки

    Result and effects

    Ссылка

    Radices from 2 through 36 produce a canonical sign and digit sequence. An invalid radix raises TPZ5001.

    Resolved contracts

    Ссылка

    Related entries

    Ссылка

    std.bigint →

    Read and write integers in an explicit radix

    Ссылка
    Правилоtopaz.stdlib.contract.bigint-text.rule

    Read and write integers in an explicit radix

    BigInt represents signed integers without a fixed bit width. Its text operations preserve the exact integer value and do not use a locale.

    BigInt.parse takes a radix from 2 through 36. Text consists of an optional minus sign followed by at least one ASCII digit or lowercase ASCII letter. Letters a through z have digit values 10 through 35. Every digit must be smaller than the radix.

    Leading zero digits are accepted. Negative zero produces the same zero value as 0. Parsing does not trim whitespace, accept a plus sign, accept uppercase letters, accept separators, or interpret a radix prefix. Characters are interpreted as ordinary digits of the supplied radix.

    Valid text returns Some containing the exact integer. Empty text, a sign without digits, an invalid digit, any extra character, or a radix outside the allowed range returns None(). These input failures do not raise a runtime fault.

    BigInt.toString takes a radix from 2 through 36 and returns lowercase ASCII digits in that radix. A negative nonzero value starts with a minus sign. The output has no radix prefix or leading zero digits. Zero is written as 0.

    An invalid BigInt.toString radix raises TPZ5001 during execution. It does not select a default radix or return empty text.

    Formatting a value and parsing the result with the same radix recovers that value. Parsing and formatting accepted text produces its canonical spelling in the selected radix.

    The receiver and positional arguments are evaluated once in source order before the operation validates their values. An earlier fault prevents later evaluation. The operations do not change their inputs or perform external effects.

    Причина

    An explicit radix gives each digit one value. Strict input spelling makes accidental whitespace and alternate signs visible to the caller. Canonical output gives stored integer text a predictable form without a locale dependency.

    Parse and format exact integers in explicit radices

    Ссылка
    main.tpzParse and format exact integers in explicit radicesОткрыть песочницу →
    1. import std.bigint { BigInt }
    2. import std.test
    3. import std.io
    4. let number = test.assertSome(BigInt.parse("ff", 16))
    5. io.writeLine(number.toString(2))
    6. io.writeLine(number.toString(10))
    7. io.writeLine(number.toString(16))
    8. io.writeLine(test.assertSome(BigInt.parse("z", 36)).toString(10))
    9. io.writeLine(test.assertSome(BigInt.parse("-00015", 10)).toString(16))
    10. io.writeLine(test.assertSome(BigInt.parse("-000", 10)).toString(36))
    11. io.writeLine(test.assertSome(BigInt.parse("123456789012345678901234567890", 10)).toString(10))
    Ожидаемый выводКод завершения 0
    11111111
    255
    ff
    35
    -f
    0
    123456789012345678901234567890
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

    Ожидаемый стандартный вывод
    1. 11111111
    2. 255
    3. ff
    4. 35
    5. -f
    6. 0
    7. 123456789012345678901234567890

    Return None for malformed or out of radix digits

    Ссылка
    main.tpzReturn None for malformed or out of radix digitsОткрыть песочницу →
    1. import std.bigint { BigInt }
    2. import std.io
    3. for text in (["", "-", "1_0", "0x10", "g", "12", "1\n2"]) {
    4. match (BigInt.parse(text, 16)) {
    5. case None() => io.writeLine("rejected")
    6. case Some(_) => io.writeLine("accepted")
    7. }
    8. }
    9. match (BigInt.parse("2", 2)) {
    10. case None() => io.writeLine("digit rejected")
    11. case Some(_) => io.writeLine("accepted")
    12. }
    Ожидаемый выводКод завершения 0
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    rejected
    digit rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

    Ожидаемый стандартный вывод
    1. rejected
    2. rejected
    3. rejected
    4. rejected
    5. rejected
    6. rejected
    7. rejected
    8. digit rejected

    Reject parse radices outside two through thirty six

    Ссылка
    main.tpzReject parse radices outside two through thirty sixОткрыть песочницу →
    1. import std.bigint { BigInt }
    2. import std.io
    3. for radix in ([-1, 0, 1, 37, 9223372036854775807]) {
    4. match (BigInt.parse("10", radix)) {
    5. case None() => io.writeLine("radix rejected")
    6. case Some(_) => io.writeLine("accepted")
    7. }
    8. }
    Ожидаемый выводКод завершения 0
    radix rejected
    radix rejected
    radix rejected
    radix rejected
    radix rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

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

    Reject surrounding whitespace in integer text

    Ссылка
    main.tpzReject surrounding whitespace in integer textОткрыть песочницу →
    1. import std.bigint { BigInt }
    2. import std.io
    3. match (BigInt.parse(" 10 ", 10)) {
    4. case None() => io.writeLine("whitespace rejected")
    5. case Some(_) => io.writeLine("accepted")
    6. }
    Ожидаемый выводКод завершения 0
    whitespace rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

    Ожидаемый стандартный вывод
    1. whitespace rejected

    Reject a leading plus sign in integer text

    Ссылка
    main.tpzReject a leading plus sign in integer textОткрыть песочницу →
    1. import std.bigint { BigInt }
    2. import std.io
    3. match (BigInt.parse("+10", 10)) {
    4. case None() => io.writeLine("plus rejected")
    5. case Some(_) => io.writeLine("accepted")
    6. }
    Ожидаемый выводКод завершения 0
    plus rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

    Ожидаемый стандартный вывод
    1. plus rejected

    Reject uppercase letters in integer text

    Ссылка
    main.tpzReject uppercase letters in integer textОткрыть песочницу →
    1. import std.bigint { BigInt }
    2. import std.io
    3. match (BigInt.parse("FF", 16)) {
    4. case None() => io.writeLine("uppercase rejected")
    5. case Some(_) => io.writeLine("accepted")
    6. }
    Ожидаемый выводКод завершения 0
    uppercase rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

    Ожидаемый стандартный вывод
    1. uppercase rejected

    Evaluate parse arguments before validating their values

    Ссылка
    main.tpzEvaluate parse arguments before validating their valuesОткрыть песочницу →
    1. import std.bigint { BigInt }
    2. import std.io
    3. function text() -> string {
    4. io.writeLine("text")
    5. return "bad"
    6. }
    7. function radix() -> int {
    8. io.writeLine("radix")
    9. return 10
    10. }
    11. match (BigInt.parse(text(), radix())) {
    12. case None() => io.writeLine("rejected")
    13. case Some(_) => io.writeLine("accepted")
    14. }
    Ожидаемый выводКод завершения 0
    text
    radix
    rejected
    
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

    Ожидаемый стандартный вывод
    1. text
    2. radix
    3. rejected

    Reject a formatting radix outside the valid range

    Ссылка
    main.tpzReject a formatting radix outside the valid rangeОткрыть песочницу →
    1. import std.bigint { BigInt }
    2. import std.test
    3. import std.io
    4. let number: BigInt = test.assertSome(BigInt.parse("1", 10))
    5. number.toString(1)
    6. io.writeLine("after")
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

    Reject a formatting radix outside the valid range

    Ссылка
    main.tpzReject a formatting radix outside the valid rangeОткрыть песочницу →
    1. import std.bigint { BigInt }
    2. import std.test
    3. import std.io
    4. let number: BigInt = test.assertSome(BigInt.parse("1", 10))
    5. number.toString(37)
    6. io.writeLine("after")
    Ожидаемый выводНенулевой код завершения
    Нет стандартного вывода.
    $ topaz run main.tpz · Целевой продукт 5.21.3
    Ожидаемый результат

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

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

    Diagnostics

    Ссылка

    Runtime value guard →

    Меню

    Загрузка →