parse(text: string, radix: int) - > Option<Big Int>- 수신자
- 해당 없음
- 최소 인자 수
- 2
- 최대 인자 수
- 2
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 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.
관련 규칙 · Read and write integers in an explicit radix →결과
The declared result type is Option<Big.
효과
실패
Result and effects
링크Radices from 2 through 36 interpret every ASCII digit. Invalid spelling or radix returns None without changing the input.
Resolved contracts
링크Related entries
링크Read and write integers in an explicit radix
링크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
링크import std.bigint { BigInt }import std.testimport std.iolet number = test.assertSome(BigInt.parse("ff", 16))io.writeLine(number.toString(2))io.writeLine(number.toString(10))io.writeLine(number.toString(16))io.writeLine(test.assertSome(BigInt.parse("z", 36)).toString(10))io.writeLine(test.assertSome(BigInt.parse("-00015", 10)).toString(16))io.writeLine(test.assertSome(BigInt.parse("-000", 10)).toString(36))io.writeLine(test.assertSome(BigInt.parse("123456789012345678901234567890", 10)).toString(10))
11111111
255
ff
35
-f
0
123456789012345678901234567890
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
11111111255ff35-f0123456789012345678901234567890
Return None for malformed or out of radix digits
링크import std.bigint { BigInt }import std.iofor text in (["", "-", "1_0", "0x10", "g", "12", "1\n2"]) {match (BigInt.parse(text, 16)) {case None() => io.writeLine("rejected")case Some(_) => io.writeLine("accepted")}}match (BigInt.parse("2", 2)) {case None() => io.writeLine("digit rejected")case Some(_) => io.writeLine("accepted")}
rejected
rejected
rejected
rejected
rejected
rejected
rejected
digit rejected
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
rejectedrejectedrejectedrejectedrejectedrejectedrejecteddigit rejected
Reject parse radices outside two through thirty six
링크import std.bigint { BigInt }import std.iofor radix in ([-1, 0, 1, 37, 9223372036854775807]) {match (BigInt.parse("10", radix)) {case None() => io.writeLine("radix rejected")case Some(_) => io.writeLine("accepted")}}
radix rejected
radix rejected
radix rejected
radix rejected
radix rejected
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
radix rejectedradix rejectedradix rejectedradix rejectedradix rejected
Reject surrounding whitespace in integer text
링크import std.bigint { BigInt }import std.iomatch (BigInt.parse(" 10 ", 10)) {case None() => io.writeLine("whitespace rejected")case Some(_) => io.writeLine("accepted")}
whitespace rejected
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
whitespace rejected
Reject a leading plus sign in integer text
링크import std.bigint { BigInt }import std.iomatch (BigInt.parse("+10", 10)) {case None() => io.writeLine("plus rejected")case Some(_) => io.writeLine("accepted")}
plus rejected
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
plus rejected
Reject uppercase letters in integer text
링크import std.bigint { BigInt }import std.iomatch (BigInt.parse("FF", 16)) {case None() => io.writeLine("uppercase rejected")case Some(_) => io.writeLine("accepted")}
uppercase rejected
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
uppercase rejected
Evaluate parse arguments before validating their values
링크import std.bigint { BigInt }import std.iofunction text() -> string {io.writeLine("text")return "bad"}function radix() -> int {io.writeLine("radix")return 10}match (BigInt.parse(text(), radix())) {case None() => io.writeLine("rejected")case Some(_) => io.writeLine("accepted")}
text
radix
rejected
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
textradixrejected
Reject a formatting radix outside the valid range
링크import std.bigint { BigInt }import std.testimport std.iolet number: BigInt = test.assertSome(BigInt.parse("1", 10))number.toString(1)io.writeLine("after")
표준 출력 없음.기대 결과
Reject a formatting radix outside the valid range
링크import std.bigint { BigInt }import std.testimport std.iolet number: BigInt = test.assertSome(BigInt.parse("1", 10))number.toString(37)io.writeLine("after")
표준 출력 없음.