ParseInt64 v0.1.0
Parses a signed integer from decimal text.
Package: Format
Signature
func ParseInt64(
str: Slice<char8>
) -> Result<int64, ParseError>;
func ParseInt64(
str: String
) -> Result<int64, ParseError>;
Parameters
| Name | Type | Description |
|---|---|---|
str | Slice<char8> / String | The text to parse. |
Returns
A Result: Success holds the parsed int64, and
Error holds a ParseError saying why the text was rejected.
The accepted form is an optional + or - followed by one or more decimal
digits, and nothing else — the whole string has to be a number. There is no
whitespace to trim, no other base, and no digit separators. The three failures
are:
Empty— the input has no bytes.InvalidCharacter(index)— a byte that is not a digit, or a sign with no digits after it;indexis where it sits.Overflow— the value is outside theint64range. The negative limit is one larger than the positive one, so the most negativeint64,-9223372036854775808, parses rather than overflowing.
Example
import Format::ParseInt64;
import Io::PrintLine;
func Main() -> int {
match ParseInt64("42") {
.Success(n) => PrintLine(n), // 42
.Error(_) => PrintLine("not an integer")
}
match ParseInt64("-9223372036854775808") {
.Success(n) => PrintLine(n), // int64::Min, parsed exactly
.Error(_) => {}
}
match ParseInt64("12x") {
.Success(_) => {},
.Error(e) => {} // InvalidCharacter(2)
}
return 0;
}
See also
Format— the package overviewTryParseInt64— the same parse, into an out-parameter, without aResultParseFloat64— parse a floating-point value insteadParseError— the reason a parse failedToString— the inverse, a value to its text