Skip to content

Commit 20ccaae

Browse files
Yu-zhbobzhang
authored andcommitted
add docs for strconv
1 parent c4a6661 commit 20ccaae

File tree

5 files changed

+84
-6
lines changed

5 files changed

+84
-6
lines changed

strconv/decimal.mbt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,26 @@ pub fn Decimal::from_int64(v : Int64) -> Decimal {
5858
}
5959

6060
///|
61-
/// Create a decimal from number string.
61+
/// Parse a string into a decimal number. The string must contain at least one of:
62+
/// - An integer part (decimal digits)
63+
/// - A decimal point followed by a fractional part (decimal digits)
64+
/// - An exponent part ('e' or 'E' followed by an optional sign and decimal digits)
65+
///
66+
/// The string may optionally start with a sign ('+' or '-').
67+
/// For readability, underscores may appear between digits.
68+
///
69+
/// Examples:
70+
/// ```
71+
/// inspect!(parse_decimal!("123"), content="123")
72+
/// inspect!(parse_decimal!("12.34"), content="12.34")
73+
/// inspect!(parse_decimal!(".123"), content="0.123")
74+
/// inspect!(parse_decimal!("1e5"), content="100000")
75+
/// inspect!(parse_decimal!("1.2e-3"), content="0.0012")
76+
/// inspect!(parse_decimal!("1_234.5"), content="1234.5")
77+
/// ```
78+
///
79+
/// An exponent value exp scales the mantissa (significand) by 10^exp.
80+
/// For example, "1.23e2" represents 1.23 × 10² = 123.
6281
pub fn parse_decimal(str : String) -> Decimal!StrConvError {
6382
parse_decimal_from_view!(str[:])
6483
}
@@ -544,7 +563,7 @@ fn trim(self : Decimal) -> Unit {
544563
}
545564
546565
///|
547-
impl Show for Decimal with output(self, logger) {
566+
pub impl Show for Decimal with output(self, logger) {
548567
if self.digits_num == 0 {
549568
logger.write_char('0')
550569
return

strconv/double.mbt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ let max_exponent_disguised_fast_path : Int64 = 37L
4646
let max_mantissa_fast_path : UInt64 = 2UL << mantissa_explicit_bits
4747

4848
///|
49+
/// Parse a string into a double precision floating point number. The string
50+
/// must contain at least one of:
51+
/// - An integer part (decimal digits)
52+
/// - A decimal point followed by a fractional part (decimal digits)
53+
/// - An exponent part ('e' or 'E' followed by an optional sign and decimal digits)
54+
///
55+
/// The string may optionally start with a sign ('+' or '-').
56+
/// For readability, underscores may appear between digits.
57+
///
58+
/// Examples:
59+
/// ```
60+
/// inspect!(parse_double!("123"), content="123")
61+
/// inspect!(parse_double!("12.34"), content="12.34")
62+
/// inspect!(parse_double!(".123"), content="0.123")
63+
/// inspect!(parse_double!("1e5"), content="100000")
64+
/// inspect!(parse_double!("1.2e-3"), content="0.0012")
65+
/// inspect!(parse_double!("1_234.5"), content="1234.5")
66+
/// ```
67+
///
68+
/// An exponent value exp scales the mantissa (significand) by 10^exp.
69+
/// For example, "1.23e2" represents 1.23 × 10² = 123.
4970
pub fn parse_double(str : String) -> Double!StrConvError {
5071
if str.length() == 0 {
5172
syntax_err!()

strconv/int.mbt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,27 @@ fn check_and_consume_base(
5555
}
5656

5757
///|
58-
/// Parse a string in the given base (0, 2 to 36), return a Int64 number or an error.
59-
/// If the `~base` argument is 0, the base will be inferred by the prefix.
58+
/// Parses a string into an Int64 number using the specified base, or returns an error.
59+
/// The base must be 0 or between 2 and 36 (inclusive). If base is 0, it will be
60+
/// inferred from the string prefix:
61+
/// - "0x" or "0X" for base 16 (hex)
62+
/// - "0o" or "0O" for base 8 (octal)
63+
/// - "0b" or "0B" for base 2 (binary)
64+
/// - Default is base 10 (decimal)
65+
/// For readability, underscores may appear after base prefixes or between digits.
66+
/// These underscores do not affect the value.
67+
/// Examples:
68+
/// ```
69+
/// inspect!(parse_int64!("123"), content="123")
70+
/// inspect!(parse_int64!("0xff", base=0), content="255")
71+
/// inspect!(parse_int64!("0o10"), content="8")
72+
/// inspect!(parse_int64!("0b1010"), content="10")
73+
/// inspect!(parse_int64!("1_234"), content="1234")
74+
/// inspect!(parse_int64!("-123"), content="-123")
75+
/// inspect!(parse_int64!("ff", base=16), content="255")
76+
/// inspect!(parse_int64!("zz", base=36), content="1295")
77+
/// ```
78+
///
6079
pub fn parse_int64(str : String, base~ : Int = 0) -> Int64!StrConvError {
6180
guard str != "" else { syntax_err!() }
6281
let (neg, rest) = match str[:] {

strconv/strconv.mbti

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ impl Decimal {
2626
shift(Self, Int) -> Unit
2727
to_double(Self) -> Double!StrConvError
2828
}
29+
impl Show for Decimal
2930

3031
pub(all) type! StrConvError String
3132
impl Show for StrConvError

strconv/uint.mbt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,26 @@ const UINT_MAX : UInt = 0xffffffff
1919
const UINT64_MAX : UInt64 = 0xffffffffffffffffUL
2020

2121
///|
22-
/// Parse a string in the given base (0, 2 to 36), return an UInt64 number or an error.
23-
/// If the `~base` argument is 0, the base will be inferred by the prefix.
22+
/// Parses a string into a UInt64 number using the specified base, or returns an error.
23+
/// The base must be 0 or between 2 and 36 (inclusive). If base is 0, it will be
24+
/// inferred from the string prefix:
25+
/// - "0x" or "0X" for base 16 (hex)
26+
/// - "0o" or "0O" for base 8 (octal)
27+
/// - "0b" or "0B" for base 2 (binary)
28+
/// - Default is base 10 (decimal)
29+
/// For readability, underscores may appear after base prefixes or between digits.
30+
/// These underscores do not affect the value.
31+
/// Examples:
32+
/// ```
33+
/// inspect!(parse_uint64!("123"), content="123")
34+
/// inspect!(parse_uint64!("0xff", base=0), content="255")
35+
/// inspect!(parse_uint64!("0o10"), content="8")
36+
/// inspect!(parse_uint64!("0b1010"), content="10")
37+
/// inspect!(parse_uint64!("1_234"), content="1234")
38+
/// inspect!(parse_uint64!("ff", base=16), content="255")
39+
/// inspect!(parse_uint64!("zz", base=36), content="1295")
40+
/// ```
41+
///
2442
pub fn parse_uint64(str : String, base~ : Int = 0) -> UInt64!StrConvError {
2543
guard str != "" else { syntax_err!() }
2644
match str[:] {

0 commit comments

Comments
 (0)