use std.option.Option use std.slice.(?) use std.str.StrBuf const I8-MIN*: i8 = -128 const I8-MAX*: i8 = 127 const I16-MIN*: i16 = -32_768 const I16-MAX*: i16 = 32_767 const I32-MIN*: i32 = -2_147_483_648 const I32-MAX*: i32 = 2_147_483_647 const I64-MIN*: i64 = -9_223_372_036_854_775_808 const I64-MAX*: i64 = 9_223_372_036_854_775_807 // TODO: Consider 32bit platforms! const INT-MIN*: int = I64-MIN const INT-MAX*: int = I64-MAX const U8-MIN*: u8 = 0 const U8-MAX*: u8 = 255 const U16-MIN*: u16 = 0 const U16-MAX*: u16 = 65_535 const U32-MIN*: u32 = 0 const U32-MAX*: u32 = 4_294_967_295 const U64-MIN*: u64 = 0 const U64-MAX*: u64 = 18_446_744_073_709_551_615 // TODO: Consider 32bit platforms! const UINT-MIN*: uint = U64-MIN const UINT-MAX*: uint = U64-MAX fn abs*(x: i8) i8 = if x > 0 { x } else { -x } fn abs*(x: i16) i16 = if x > 0 { x } else { -x } fn abs*(x: i32) i32 = if x > 0 { x } else { -x } fn abs*(x: i64) i64 = if x > 0 { x } else { -x } fn abs*(x: int) int = if x > 0 { x } else { -x } fn to-str*(x: i8) str = fmt-impl(uint(x.abs()), x < 0, 10) fn to-str*(x: i8, base: u32) str = fmt-impl(uint(x.abs()), x < 0, base) fn fmt*(x: &i8, buf: &mut StrBuf) unit = buf.push(x.to-str(10)) fn to-str*(x: i16) str = fmt-impl(uint(x.abs()), x < 0, 10) fn to-str*(x: i16, base: u32) str = fmt-impl(uint(x.abs()), x < 0, base) fn fmt*(x: &i16, buf: &mut StrBuf) unit = buf.push(x.to-str(10)) fn to-str*(x: i32) str = fmt-impl(uint(x.abs()), x < 0, 10) fn to-str*(x: i32, base: u32) str = fmt-impl(uint(x.abs()), x < 0, base) fn fmt*(x: &i32, buf: &mut StrBuf) unit = buf.push(x.to-str(10)) fn to-str*(x: i64) str = fmt-impl(uint(x.abs()), x < 0, 10) fn to-str*(x: i64, base: u32) str = fmt-impl(uint(x.abs()), x < 0, base) fn fmt*(x: &i64, buf: &mut StrBuf) unit = buf.push(x.to-str(10)) fn to-str*(x: int) str = fmt-impl(uint(x.abs()), x < 0, 10) fn to-str*(x: int, base: u32) str = fmt-impl(uint(x.abs()), x < 0, base) fn fmt*(x: &int, buf: &mut StrBuf) unit = buf.push(x.to-str(10)) fn to-str*(x: u8) str = fmt-impl(x, false, 10) fn to-str*(x: u8, base: u32) str = fmt-impl(x, false, base) fn fmt*(x: &u8, buf: &mut StrBuf) unit = buf.push(x.to-str(10)) fn to-str*(x: u16) str = fmt-impl(x, false, 10) fn to-str*(x: u16, base: u32) str = fmt-impl(x, false, base) fn fmt*(x: &u16, buf: &mut StrBuf) unit = buf.push(x.to-str(10)) fn to-str*(x: u32) str = fmt-impl(x, false, 10) fn to-str*(x: u32, base: u32) str = fmt-impl(x, false, base) fn fmt*(x: &u32, buf: &mut StrBuf) unit = buf.push(x.to-str(10)) fn to-str*(x: u64) str = fmt-impl(x, false, 10) fn to-str*(x: u64, base: u32) str = fmt-impl(x, false, base) fn fmt*(x: &u64, buf: &mut StrBuf) unit = buf.push(x.to-str(10)) fn to-str*(x: uint) str = fmt-impl(x, false, 10) fn to-str*(x: uint, base: u32) str = fmt-impl(x, false, base) fn fmt*(x: &uint, buf: &mut StrBuf) unit = buf.push(x.to-str(10)) // TODO: We can avoid this allocation if we add stack allocated arrays to the language, // and stack allocate an uninitialized [u8; 22] instead of using a StrBuf fn fmt-impl(mut num: uint, is-neg: bool, base: u32) str = { let mut buf = StrBuf.with-cap(22) // TODO: We should be able to use an early return here, // but the ownership check dictates that `buf` is `MaybeMoved`, hence it can't be used afterwards. if num == 0 { buf.push('0') } else { for if num > 0 { let ch = char.from-digit(u32(num % base), u32(base)).or-panic() buf.push(ch) num /= base } if is-neg { buf.push('-') } } let mut s = buf.take() s.as-bytes-mut().reverse() s }