Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions builtin/output.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
fn Bool::output(self : Bool, logger : &Logger) -> Unit {
if self {
logger.write_string("true")
} else {
logger.write_string("false")
}
}

///|
fn Int64::output(self : Int64, logger : &Logger, radix~ : Int = 10) -> Unit {
fn abs(n : Int64) -> Int64 {
if n < 0L {
0L - n
} else {
n
}
}

if self < 0L {
logger.write_char('-')
}
let radix : Int64 = radix.to_int64()
fn write_digits(num : Int64) {
let num2 = num / radix
if num2 != 0L {
write_digits(num2)
}
logger.write_char(ALPHABET[abs(num % radix).to_int()])
}

write_digits(abs(self))
}

///|
fn Int::output(self : Int, logger : &Logger, radix~ : Int = 10) -> Unit {
fn abs(n : Int) -> Int {
if n < 0 {
0 - n
} else {
n
}
}

if self < 0 {
logger.write_char('-')
}
fn write_digits(num : Int) {
let num2 = num / radix
if num2 != 0 {
write_digits(num2)
}
logger.write_char(ALPHABET[abs(num % radix)])
}

write_digits(abs(self))
}

///|
fn UInt::output(self : UInt, logger : &Logger, radix~ : Int = 10) -> Unit {
let radix : UInt = radix.reinterpret_as_uint()
fn write_digits(num : UInt) {
let num2 = num / radix
if num2 != 0U {
write_digits(num2)
}
logger.write_char(ALPHABET[(num % radix).reinterpret_as_int()])
}

write_digits(self)
}

///|
fn UInt64::output(self : UInt64, logger : &Logger, radix~ : Int = 10) -> Unit {
let radix : UInt64 = radix.to_uint64()
fn write_digits(num : UInt64) {
let num2 = num / radix
if num2 != 0UL {
write_digits(num2)
}
logger.write_char(ALPHABET[(num % radix).to_int()])
}

write_digits(self)
}

///|
fn Int64::output_size_hint(radix~ : Int = 10) -> Int {
match radix {
2..<7 => 70 // max length is 64, 70 is enough
8..<15 => 30 // max length is 23, 30 is enough
16..=36 => 20 // max length is 17, 20 is enough
_ => abort("radix must be between 2 and 36")
}
}

///|
fn Int::output_size_hint(radix~ : Int = 10) -> Int {
match radix {
2..<7 => 36 // max length is 32, 36 is enough
8..<15 => 18 // max length is 12, 18 is enough
16..=36 => 10 // max length is 8, 10 is enough
_ => abort("radix must be between 2 and 36")
}
}

///|
fn UInt::output_size_hint(radix~ : Int = 10) -> Int {
Int::output_size_hint(radix~)
}

///|
fn UInt64::output_size_hint(radix~ : Int = 10) -> Int {
Int64::output_size_hint(radix~)
}
10 changes: 5 additions & 5 deletions builtin/show.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ pub impl Show for Bool with output(self, logger) {

///|
pub impl Show for Int with output(self, logger) {
logger.write_string(self.to_string())
self.output(logger)
}

///|
pub impl Show for Int64 with output(self, logger) {
logger.write_string(self.to_string())
self.output(logger)
}

///|
pub impl Show for UInt with output(self, logger) {
logger.write_string(self.to_string())
self.output(logger)
}

///|
pub impl Show for UInt64 with output(self, logger) {
logger.write_string(self.to_string())
self.output(logger)
}

///|
Expand All @@ -54,7 +54,7 @@ pub impl Show for Int16 with output(self, logger) {

///|
pub impl Show for UInt16 with output(self, logger) {
logger.write_string(self.to_string())
self.to_int().output(logger)
}

///|
Expand Down
97 changes: 8 additions & 89 deletions builtin/to_string.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,8 @@ const ALPHABET : String = "0123456789abcdefghijklmnopqrstuvwxyz"

///|
pub fn Int64::to_string(self : Int64, radix~ : Int = 10) -> String {
fn abs(n : Int64) -> Int64 {
if n < 0L {
0L - n
} else {
n
}
}

let size_hint = match radix {
2..<7 => 70 // max length is 64, 70 is enough
8..<15 => 30 // max length is 23, 30 is enough
16..=36 => 20 // max length is 17, 20 is enough
_ => abort("radix must be between 2 and 36")
}
let buf = StringBuilder::new(size_hint~)
if self < 0L {
buf.write_char('-')
}
let radix : Int64 = radix.to_int64()
fn write_digits(num : Int64) {
let num2 = num / radix
if num2 != 0L {
write_digits(num2)
}
buf.write_char(ALPHABET[abs(num % radix).to_int()])
}

write_digits(abs(self))
let buf = StringBuilder::new(size_hint=Int64::output_size_hint(radix~))
self.output(buf, radix~)
buf.to_string()
}

Expand All @@ -64,33 +38,8 @@ pub impl Show for Int64 with to_string(self) {

///|
pub fn Int::to_string(self : Int, radix~ : Int = 10) -> String {
fn abs(n : Int) -> Int {
if n < 0 {
0 - n
} else {
n
}
}

let size_hint = match radix {
2..<7 => 36 // max length is 32, 36 is enough
8..<15 => 18 // max length is 12, 18 is enough
16..=36 => 10 // max length is 8, 10 is enough
_ => abort("radix must be between 2 and 36")
}
let buf = StringBuilder::new(size_hint~)
if self < 0 {
buf.write_char('-')
}
fn write_digits(num : Int) {
let num2 = num / radix
if num2 != 0 {
write_digits(num2)
}
buf.write_char(ALPHABET[abs(num % radix)])
}

write_digits(abs(self))
let buf = StringBuilder::new(size_hint=Int::output_size_hint(radix~))
self.output(buf, radix~)
buf.to_string()
}

Expand All @@ -101,23 +50,8 @@ pub impl Show for Int with to_string(self) {

///|
pub fn UInt::to_string(self : UInt, radix~ : Int = 10) -> String {
let size_hint = match radix {
2..<7 => 36 // max length is 32, 36 is enough
8..<15 => 18 // max length is 11, 18 is enough
16..=36 => 10 // max length is 8, 10 is enough
_ => abort("radix must be between 2 and 36")
}
let buf = StringBuilder::new(size_hint~)
let radix : UInt = radix.reinterpret_as_uint()
fn write_digits(num : UInt) {
let num2 = num / radix
if num2 != 0U {
write_digits(num2)
}
buf.write_char(ALPHABET[(num % radix).reinterpret_as_int()])
}

write_digits(self)
let buf = StringBuilder::new(size_hint=UInt::output_size_hint(radix~))
self.output(buf, radix~)
buf.to_string()
}

Expand All @@ -135,23 +69,8 @@ test "UInt::to_string" {

///|
pub fn UInt64::to_string(self : UInt64, radix~ : Int = 10) -> String {
let size_hint = match radix {
2..<7 => 70 // max length is 64, 70 is enough
8..<15 => 30 // max length is 23, 30 is enough
16..=36 => 20 // max length is 17, 20 is enough
_ => abort("radix must be between 2 and 36")
}
let buf = StringBuilder::new(size_hint~)
let radix : UInt64 = radix.to_uint64()
fn write_digits(num : UInt64) {
let num2 = num / radix
if num2 != 0UL {
write_digits(num2)
}
buf.write_char(ALPHABET[(num % radix).to_int()])
}

write_digits(self)
let buf = StringBuilder::new(size_hint=UInt64::output_size_hint(radix~))
self.output(buf, radix~)
buf.to_string()
}

Expand Down