Something went wrong. Try again.
An experiment in static, refcounted, procedural language with ownership
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108use std.option.Optionuse std.slice.(?)use std.str.StrBufconst I8-MIN*: i8 = -128const I8-MAX*: i8 = 127const I16-MIN*: i16 = -32_768const I16-MAX*: i16 = 32_767const I32-MIN*: i32 = -2_147_483_648const I32-MAX*: i32 = 2_147_483_647const I64-MIN*: i64 = -9_223_372_036_854_775_808const I64-MAX*: i64 = 9_223_372_036_854_775_807// TODO: Consider 32bit platforms!const INT-MIN*: int = I64-MINconst INT-MAX*: int = I64-MAXconst U8-MIN*: u8 = 0const U8-MAX*: u8 = 255const U16-MIN*: u16 = 0const U16-MAX*: u16 = 65_535const U32-MIN*: u32 = 0const U32-MAX*: u32 = 4_294_967_295const U64-MIN*: u64 = 0const U64-MAX*: u64 = 18_446_744_073_709_551_615// TODO: Consider 32bit platforms!const UINT-MIN*: uint = U64-MINconst UINT-MAX*: uint = U64-MAXfn 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 StrBuffn 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}