Skip to content

Commit 5a17272

Browse files
tonyfettesbobzhang
authored andcommitted
take apart builtin/console.mbt
1 parent 888ed7d commit 5a17272

File tree

4 files changed

+188
-162
lines changed

4 files changed

+188
-162
lines changed

builtin/console.mbt

Lines changed: 0 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -42,168 +42,6 @@ pub fn dump[T](t : T, name? : String, loc~ : SourceLoc = _) -> T {
4242
t
4343
}
4444

45-
///|
46-
pub fn Bool::to_string(self : Bool) -> String {
47-
if self {
48-
"true"
49-
} else {
50-
"false"
51-
}
52-
}
53-
54-
///|
55-
pub fn Int64::to_string(self : Int64) -> String {
56-
fn abs(n : Int64) -> Int64 {
57-
if n < 0L {
58-
0L - n
59-
} else {
60-
n
61-
}
62-
}
63-
64-
// The min and max value of i64 are -9223372036854775808 and 9223372036854775807,
65-
// so max=20 is enough.
66-
67-
let buf = StringBuilder::new(size_hint=20)
68-
if self < 0L {
69-
buf.write_char('-')
70-
}
71-
fn write_digits(num) {
72-
let num2 = num / 10L
73-
if num2 != 0L {
74-
write_digits(num2)
75-
}
76-
buf.write_char(Char::from_int(abs(num % 10L).to_int() + 48))
77-
}
78-
79-
write_digits(self)
80-
buf.to_string()
81-
}
82-
83-
///|
84-
pub fn Int::to_string(self : Int) -> String {
85-
fn abs(n : Int) -> Int {
86-
if n < 0 {
87-
0 - n
88-
} else {
89-
n
90-
}
91-
}
92-
93-
// The min and max value of i32 are -2147483648 and 2147483647,
94-
// so max=11 is enough.
95-
96-
let buf = StringBuilder::new()
97-
if self < 0 {
98-
buf.write_char('-')
99-
}
100-
fn write_digits(num) {
101-
let num2 = num / 10
102-
if num2 != 0 {
103-
write_digits(num2)
104-
}
105-
buf.write_char(Char::from_int(abs(num % 10) + 48))
106-
}
107-
108-
write_digits(self)
109-
buf.to_string()
110-
}
111-
112-
///|
113-
pub fn UInt::to_string(self : UInt) -> String {
114-
let buf = StringBuilder::new()
115-
fn write_digits(num) {
116-
let num2 = num / 10U
117-
if num2 != 0U {
118-
write_digits(num2)
119-
}
120-
buf.write_char(Char::from_int((num % 10U).reinterpret_as_int() + 48))
121-
}
122-
123-
write_digits(self)
124-
buf.to_string()
125-
}
126-
127-
test "UInt::to_string" {
128-
inspect!(0U, content="0")
129-
inspect!(17U, content="17")
130-
inspect!(4294967295U, content="4294967295")
131-
}
132-
133-
///|
134-
pub fn UInt64::to_string(self : UInt64) -> String {
135-
let buf = StringBuilder::new()
136-
fn write_digits(num : UInt64) {
137-
let num2 = num / 10UL
138-
if num2 != 0UL {
139-
write_digits(num2)
140-
}
141-
buf.write_char(
142-
Char::from_int((num % 10UL).reinterpret_as_int64().to_int() + 48),
143-
)
144-
}
145-
146-
write_digits(self)
147-
buf.to_string()
148-
}
149-
150-
///|
151-
pub fn Int16::to_string(self : Int16) -> String {
152-
self.to_int().to_string()
153-
}
154-
155-
///|
156-
pub fn UInt16::to_string(self : UInt16) -> String {
157-
self.to_int().to_string()
158-
}
159-
160-
///|
161-
/// @coverage.skip
162-
pub fn op_lt[T : Compare](self_ : T, other : T) -> Bool {
163-
self_.compare(other).is_neg()
164-
}
165-
166-
///|
167-
/// @coverage.skip
168-
pub fn op_gt[T : Compare](self_ : T, other : T) -> Bool {
169-
self_.compare(other).is_pos()
170-
}
171-
172-
///|
173-
/// @coverage.skip
174-
pub fn op_le[T : Compare](self_ : T, other : T) -> Bool {
175-
self_.compare(other).is_non_pos()
176-
}
177-
178-
///|
179-
/// @coverage.skip
180-
pub fn op_ge[T : Compare](self_ : T, other : T) -> Bool {
181-
self_.compare(other).is_non_neg()
182-
}
183-
184-
///|
185-
/// @coverage.skip
186-
pub fn op_notequal[T : Eq](x : T, y : T) -> Bool {
187-
not(x == y)
188-
}
189-
190-
///|
191-
fn unsafe_make_string(length : Int, value : Char) -> String = "$moonbit.unsafe_make_string"
192-
193-
///|
194-
/// Create new string of `length`, where each character is `value`
195-
///
196-
/// ```
197-
/// assert_eq!(String::make(5,'S'), "SSSSS")
198-
/// ```
199-
pub fn String::make(length : Int, value : Char) -> String {
200-
if length < 0 {
201-
abort("invalid length")
202-
} else {
203-
unsafe_make_string(length, value)
204-
}
205-
}
206-
20745
///|
20846
pub(all) type! InspectError String
20947

builtin/op.mbt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2025 International Digital Economy Academy
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
///|
16+
/// @coverage.skip
17+
pub fn op_lt[T : Compare](self_ : T, other : T) -> Bool {
18+
self_.compare(other).is_neg()
19+
}
20+
21+
///|
22+
/// @coverage.skip
23+
pub fn op_gt[T : Compare](self_ : T, other : T) -> Bool {
24+
self_.compare(other).is_pos()
25+
}
26+
27+
///|
28+
/// @coverage.skip
29+
pub fn op_le[T : Compare](self_ : T, other : T) -> Bool {
30+
self_.compare(other).is_non_pos()
31+
}
32+
33+
///|
34+
/// @coverage.skip
35+
pub fn op_ge[T : Compare](self_ : T, other : T) -> Bool {
36+
self_.compare(other).is_non_neg()
37+
}
38+
39+
///|
40+
/// @coverage.skip
41+
pub fn op_notequal[T : Eq](x : T, y : T) -> Bool {
42+
not(x == y)
43+
}

builtin/string.mbt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
///|
16+
fn unsafe_make_string(length : Int, value : Char) -> String = "$moonbit.unsafe_make_string"
17+
18+
///|
19+
/// Create new string of `length`, where each character is `value`
20+
///
21+
/// ```
22+
/// assert_eq!(String::make(5,'S'), "SSSSS")
23+
/// ```
24+
pub fn String::make(length : Int, value : Char) -> String {
25+
if length < 0 {
26+
abort("invalid length")
27+
} else {
28+
unsafe_make_string(length, value)
29+
}
30+
}
31+
1532
///|
1633
let min_leading_surrogate = 0xD800
1734

builtin/to_string.mbt

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Copyright 2025 International Digital Economy Academy
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
///|
16+
pub fn Bool::to_string(self : Bool) -> String {
17+
if self {
18+
"true"
19+
} else {
20+
"false"
21+
}
22+
}
23+
24+
///|
25+
pub fn Int64::to_string(self : Int64) -> String {
26+
fn abs(n : Int64) -> Int64 {
27+
if n < 0L {
28+
0L - n
29+
} else {
30+
n
31+
}
32+
}
33+
34+
// The min and max value of i64 are -9223372036854775808 and 9223372036854775807,
35+
// so max=20 is enough.
36+
37+
let buf = StringBuilder::new(size_hint=20)
38+
if self < 0L {
39+
buf.write_char('-')
40+
}
41+
fn write_digits(num) {
42+
let num2 = num / 10L
43+
if num2 != 0L {
44+
write_digits(num2)
45+
}
46+
buf.write_char(Char::from_int(abs(num % 10L).to_int() + 48))
47+
}
48+
49+
write_digits(self)
50+
buf.to_string()
51+
}
52+
53+
///|
54+
pub fn Int::to_string(self : Int) -> String {
55+
fn abs(n : Int) -> Int {
56+
if n < 0 {
57+
0 - n
58+
} else {
59+
n
60+
}
61+
}
62+
63+
// The min and max value of i32 are -2147483648 and 2147483647,
64+
// so max=11 is enough.
65+
66+
let buf = StringBuilder::new()
67+
if self < 0 {
68+
buf.write_char('-')
69+
}
70+
fn write_digits(num) {
71+
let num2 = num / 10
72+
if num2 != 0 {
73+
write_digits(num2)
74+
}
75+
buf.write_char(Char::from_int(abs(num % 10) + 48))
76+
}
77+
78+
write_digits(self)
79+
buf.to_string()
80+
}
81+
82+
///|
83+
pub fn UInt::to_string(self : UInt) -> String {
84+
let buf = StringBuilder::new()
85+
fn write_digits(num) {
86+
let num2 = num / 10U
87+
if num2 != 0U {
88+
write_digits(num2)
89+
}
90+
buf.write_char(Char::from_int((num % 10U).reinterpret_as_int() + 48))
91+
}
92+
93+
write_digits(self)
94+
buf.to_string()
95+
}
96+
97+
test "UInt::to_string" {
98+
inspect!(0U, content="0")
99+
inspect!(17U, content="17")
100+
inspect!(4294967295U, content="4294967295")
101+
}
102+
103+
///|
104+
pub fn UInt64::to_string(self : UInt64) -> String {
105+
let buf = StringBuilder::new()
106+
fn write_digits(num : UInt64) {
107+
let num2 = num / 10UL
108+
if num2 != 0UL {
109+
write_digits(num2)
110+
}
111+
buf.write_char(
112+
Char::from_int((num % 10UL).reinterpret_as_int64().to_int() + 48),
113+
)
114+
}
115+
116+
write_digits(self)
117+
buf.to_string()
118+
}
119+
120+
///|
121+
pub fn Int16::to_string(self : Int16) -> String {
122+
self.to_int().to_string()
123+
}
124+
125+
///|
126+
pub fn UInt16::to_string(self : UInt16) -> String {
127+
self.to_int().to_string()
128+
}

0 commit comments

Comments
 (0)