codecin 7.22.10__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codecin/__init__.py +27 -0
- codecin/aot.py +1057 -0
- codecin/assembler.py +619 -0
- codecin/cbackend.py +3024 -0
- codecin/cin.py +4265 -0
- codecin/cli.py +670 -0
- codecin/codecin_native.dll +0 -0
- codecin/config.py +50 -0
- codecin/console.py +183 -0
- codecin/cpu.py +312 -0
- codecin/crom.py +449 -0
- codecin/cvm.py +708 -0
- codecin/disasm.py +149 -0
- codecin/environment.py +176 -0
- codecin/errors.py +54 -0
- codecin/isa.py +560 -0
- codecin/lib/array/pic.toml +10 -0
- codecin/lib/array/src/array.cin +103 -0
- codecin/lib/bigint/pic.toml +10 -0
- codecin/lib/bigint/src/bigint.cin +589 -0
- codecin/lib/bits/pic.toml +10 -0
- codecin/lib/bits/src/bits.cin +143 -0
- codecin/lib/bitset/pic.toml +10 -0
- codecin/lib/bitset/src/bitset.cin +374 -0
- codecin/lib/codec/pic.toml +10 -0
- codecin/lib/codec/src/codec.cin +563 -0
- codecin/lib/combin/pic.toml +10 -0
- codecin/lib/combin/src/combin.cin +414 -0
- codecin/lib/conv/pic.toml +10 -0
- codecin/lib/conv/src/conv.cin +151 -0
- codecin/lib/cppstd/pic.toml +10 -0
- codecin/lib/cppstd/src/cppstd.cin +418 -0
- codecin/lib/cstd/pic.toml +10 -0
- codecin/lib/cstd/src/cstd.cin +424 -0
- codecin/lib/csv/pic.toml +10 -0
- codecin/lib/csv/src/csv.cin +405 -0
- codecin/lib/dp/pic.toml +10 -0
- codecin/lib/dp/src/dp.cin +325 -0
- codecin/lib/ffi/pic.toml +10 -0
- codecin/lib/ffi/src/ffi.cin +163 -0
- codecin/lib/fmt/pic.toml +10 -0
- codecin/lib/fmt/src/fmt.cin +325 -0
- codecin/lib/frac/pic.toml +10 -0
- codecin/lib/frac/src/frac.cin +252 -0
- codecin/lib/gostd/pic.toml +10 -0
- codecin/lib/gostd/src/gostd.cin +415 -0
- codecin/lib/graph/pic.toml +10 -0
- codecin/lib/graph/src/graph.cin +367 -0
- codecin/lib/gui/pic.toml +10 -0
- codecin/lib/gui/src/gui.cin +218 -0
- codecin/lib/hash/pic.toml +10 -0
- codecin/lib/hash/src/hash.cin +67 -0
- codecin/lib/heap/pic.toml +10 -0
- codecin/lib/heap/src/heap.cin +199 -0
- codecin/lib/io/pic.toml +10 -0
- codecin/lib/io/src/io.cin +141 -0
- codecin/lib/json/pic.toml +10 -0
- codecin/lib/json/src/json.cin +84 -0
- codecin/lib/key/pic.toml +10 -0
- codecin/lib/key/src/key.cin +78 -0
- codecin/lib/math/pic.toml +10 -0
- codecin/lib/math/src/math.cin +50 -0
- codecin/lib/matrix/pic.toml +10 -0
- codecin/lib/matrix/src/matrix.cin +155 -0
- codecin/lib/net/pic.toml +10 -0
- codecin/lib/net/src/net.cin +171 -0
- codecin/lib/path/pic.toml +10 -0
- codecin/lib/path/src/path.cin +372 -0
- codecin/lib/queue/pic.toml +10 -0
- codecin/lib/queue/src/queue.cin +112 -0
- codecin/lib/rand/pic.toml +10 -0
- codecin/lib/rand/src/rand.cin +46 -0
- codecin/lib/set/pic.toml +10 -0
- codecin/lib/set/src/set.cin +269 -0
- codecin/lib/sort/pic.toml +10 -0
- codecin/lib/sort/src/sort.cin +93 -0
- codecin/lib/stat/pic.toml +10 -0
- codecin/lib/stat/src/stat.cin +138 -0
- codecin/lib/str/pic.toml +10 -0
- codecin/lib/str/src/str.cin +54 -0
- codecin/lib/termux/pic.toml +10 -0
- codecin/lib/termux/src/termux.cin +118 -0
- codecin/lib/test/pic.toml +10 -0
- codecin/lib/test/src/test.cin +74 -0
- codecin/lib/text/pic.toml +10 -0
- codecin/lib/text/src/text.cin +323 -0
- codecin/lib/time/pic.toml +10 -0
- codecin/lib/time/src/time.cin +54 -0
- codecin/lib/token/pic.toml +10 -0
- codecin/lib/token/src/token.cin +183 -0
- codecin/lib/tree/pic.toml +10 -0
- codecin/lib/tree/src/tree.cin +256 -0
- codecin/lib/unionfind/pic.toml +10 -0
- codecin/lib/unionfind/src/unionfind.cin +115 -0
- codecin/lib/validate/pic.toml +10 -0
- codecin/lib/validate/src/validate.cin +148 -0
- codecin/lib/vec/pic.toml +10 -0
- codecin/lib/vec/src/vec.cin +88 -0
- codecin/logger.py +145 -0
- codecin/memory.py +306 -0
- codecin/mkn.py +916 -0
- codecin/native/aot/aot.go +114 -0
- codecin/native/aot/stub_main.go.txt +23 -0
- codecin/native/build.ps1 +33 -0
- codecin/native/build.sh +48 -0
- codecin/native/cmd/cincc/main.go +66 -0
- codecin/native/compiler/codegen.go +2238 -0
- codecin/native/compiler/compile_test.go +1069 -0
- codecin/native/compiler/parser.go +1331 -0
- codecin/native/compiler/syscalls.go +166 -0
- codecin/native/compiler/tokenizer.go +394 -0
- codecin/native/compiler/types.go +217 -0
- codecin/native/engine/args.go +37 -0
- codecin/native/engine/audio.go +402 -0
- codecin/native/engine/audio_other.go +105 -0
- codecin/native/engine/audio_test.go +279 -0
- codecin/native/engine/audio_windows.go +190 -0
- codecin/native/engine/canvas.go +271 -0
- codecin/native/engine/console_other.go +9 -0
- codecin/native/engine/console_windows.go +43 -0
- codecin/native/engine/crom.go +86 -0
- codecin/native/engine/encode.go +93 -0
- codecin/native/engine/engine_test.go +198 -0
- codecin/native/engine/ffi_nocgo.go +20 -0
- codecin/native/engine/ffi_registry.go +69 -0
- codecin/native/engine/ffi_unix.go +109 -0
- codecin/native/engine/ffi_windows.go +117 -0
- codecin/native/engine/gui.go +209 -0
- codecin/native/engine/gui_stub.go +13 -0
- codecin/native/engine/gui_test.go +148 -0
- codecin/native/engine/gui_windows.go +604 -0
- codecin/native/engine/gui_x11.go +723 -0
- codecin/native/engine/host_test.go +139 -0
- codecin/native/engine/hostcrypto.go +30 -0
- codecin/native/engine/hostdesktop.go +173 -0
- codecin/native/engine/hostnet.go +127 -0
- codecin/native/engine/isa_gen.go +689 -0
- codecin/native/engine/keyboard.go +432 -0
- codecin/native/engine/keyboard_other.go +94 -0
- codecin/native/engine/keyboard_stub.go +17 -0
- codecin/native/engine/keyboard_termios_darwin.go +9 -0
- codecin/native/engine/keyboard_termios_linux.go +9 -0
- codecin/native/engine/keyboard_test.go +246 -0
- codecin/native/engine/keyboard_windows.go +208 -0
- codecin/native/engine/keyboard_windows_test.go +123 -0
- codecin/native/engine/lineedit.go +262 -0
- codecin/native/engine/lineedit_test.go +105 -0
- codecin/native/engine/lineint_unix.go +17 -0
- codecin/native/engine/lineint_windows.go +19 -0
- codecin/native/engine/mem_other.go +37 -0
- codecin/native/engine/mem_windows.go +33 -0
- codecin/native/engine/native.go +640 -0
- codecin/native/engine/netsock.go +321 -0
- codecin/native/engine/ops_ext.go +515 -0
- codecin/native/engine/stdin.go +72 -0
- codecin/native/engine/stdin_other.go +9 -0
- codecin/native/engine/stdin_windows.go +60 -0
- codecin/native/engine/system.go +313 -0
- codecin/native/engine/termux.go +180 -0
- codecin/native/engine/version_gen.go +7 -0
- codecin/native/engine/vkb_darwin.go +62 -0
- codecin/native/engine/vkb_linux.go +95 -0
- codecin/native/engine/vkb_stub.go +11 -0
- codecin/native/engine/vm.go +1677 -0
- codecin/native/go.mod +3 -0
- codecin/native/ir/ir.go +48 -0
- codecin/native/main.go +448 -0
- codecin/native/mc/aarch64.go +165 -0
- codecin/native/mc/aarch64_test.go +30 -0
- codecin/native/mc/elf.go +88 -0
- codecin/native/mc/print_test.go +52 -0
- codecin/native/mc/x86_64.go +1262 -0
- codecin/native/mc/x86_64_test.go +102 -0
- codecin/native.py +432 -0
- codecin/native_codegen.py +987 -0
- codecin/nativegen.py +786 -0
- codecin/object.py +539 -0
- codecin/optimizer.py +236 -0
- codecin/packaging/__init__.py +46 -0
- codecin/packaging/archive.py +177 -0
- codecin/packaging/model.py +519 -0
- codecin/packaging/spec.py +193 -0
- codecin/packaging/store.py +372 -0
- codecin/packaging/toml.py +541 -0
- codecin/packaging/vcs.py +215 -0
- codecin/pic.py +1917 -0
- codecin/pic_api.py +374 -0
- codecin/pic_login.py +519 -0
- codecin/pic_registry.py +515 -0
- codecin/registers.py +130 -0
- codecin/sdgcc.py +493 -0
- codecin/stats.py +230 -0
- codecin/stdlib.py +138 -0
- codecin/toolchain.py +396 -0
- codecin/toolkit.py +26 -0
- codecin/version.py +260 -0
- codecin-7.22.10.dist-info/METADATA +1077 -0
- codecin-7.22.10.dist-info/RECORD +202 -0
- codecin-7.22.10.dist-info/WHEEL +5 -0
- codecin-7.22.10.dist-info/entry_points.txt +5 -0
- codecin-7.22.10.dist-info/licenses/LICENSE +7 -0
- codecin-7.22.10.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 排版与格式化输出 (fmt)
|
|
2
|
+
// 用法: import "fmt.cin"
|
|
3
|
+
//
|
|
4
|
+
// 纯 CIN 实现, 只使用非宿主能力内建 (strlen / substr / strcmp / indexof /
|
|
5
|
+
// int_to_str / float_to_str / floor / idiv / trim), 不依赖任何宿主能力,
|
|
6
|
+
// 在解释器与 Go 原生 VM 两条路径上行为一致。
|
|
7
|
+
//
|
|
8
|
+
// 与 conv.cin 的分工: conv 提供进制转换与基础填充 (c_pad_left / c_pad_right /
|
|
9
|
+
// c_pad_int / c_to_hex / c_to_bin), 本库面向"排版"再包一层: 定点小数、千位分隔、
|
|
10
|
+
// 对齐/居中、比例条、表格单元格与边框。两者可同时导入, 互不依赖。
|
|
11
|
+
//
|
|
12
|
+
// 全局数据段: 本库**没有全局变量**, 不占用数据区。
|
|
13
|
+
//
|
|
14
|
+
// 统一约定 (重要, 也写在 .libdocs/fmt.md 里):
|
|
15
|
+
// * 所有函数返回新字符串 (新堆块, 不回收), 因此不要在无界循环里对超长串反复调用;
|
|
16
|
+
// * 字符串长度一律是**字节**语义 (strlen / substr / s[i] 均按字节), 非 ASCII
|
|
17
|
+
// (如 UTF-8 中文) 按字节计数与切断, 一个汉字算 3 个字节、宽度算 3 列;
|
|
18
|
+
// * 计数类参数统一钳制到 [0, 1024] (FMT_REPEAT_LIMIT), 宽度类参数各自钳制,
|
|
19
|
+
// 避免一次调用就把 64 KiB 堆撑爆;
|
|
20
|
+
// * fmt_float_dec / fmt_float_pad 的 digits 钳制到 [0, 9];
|
|
21
|
+
// * fmt_float_dec 的 |v| >= 1e9 时退化为 float_to_str(v) (定点整数部分会溢出
|
|
22
|
+
// 64 位整数槽), 该退化行为不做补零;
|
|
23
|
+
// * 局部标量变量不自动清零, 本库所有局部变量都显式赋值后再使用。
|
|
24
|
+
|
|
25
|
+
// 重复次数上限 (fmt_repeat / fmt_repeat_char / fmt_bar 的 width)
|
|
26
|
+
function fmt_repeat_limit() -> int {
|
|
27
|
+
return 1024
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// ---------------- 内部辅助 ----------------
|
|
31
|
+
|
|
32
|
+
// 内部: 可打印 ASCII 表, 下标 i 对应编码 32 + i (即 32..126), 共 95 字符
|
|
33
|
+
function fmt_ascii_table() -> string {
|
|
34
|
+
return " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 编码 -> 单字符字符串
|
|
38
|
+
// 支持 32..126 (可打印 ASCII) 与 9 / 10 / 13 (制表/换行/回车);
|
|
39
|
+
// 其余编码返回 "" (含 0 与 > 255, 本库不做多字节编码)
|
|
40
|
+
function fmt_chr(int ch) -> string {
|
|
41
|
+
if (ch == 9) { return "\t" }
|
|
42
|
+
if (ch == 10) { return "\n" }
|
|
43
|
+
if (ch == 13) { return "\r" }
|
|
44
|
+
if (ch < 32 || ch > 126) { return "" }
|
|
45
|
+
return substr(fmt_ascii_table(), ch - 32, 1)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 内部: 把 s 重复 n 次; n 钳制到 [0, 1024], s 为空返回 ""
|
|
49
|
+
// 用倍增法构造 (O(log n) 次拼接), 避免逐次拼接时 O(n^2) 的堆分配:
|
|
50
|
+
// n = 1024 时逐次拼接要写约 50 万字节临时块, 足以撑爆默认 64 KiB 堆。
|
|
51
|
+
function fmt_rep(string s, int n) -> string {
|
|
52
|
+
if (n <= 0) { return "" }
|
|
53
|
+
if (strlen(s) == 0) { return "" }
|
|
54
|
+
if (n > 1024) { n = 1024 }
|
|
55
|
+
string block = s
|
|
56
|
+
int reps = 1
|
|
57
|
+
while (reps * 2 <= n) {
|
|
58
|
+
block = block + block
|
|
59
|
+
reps = reps * 2
|
|
60
|
+
}
|
|
61
|
+
string out = ""
|
|
62
|
+
int left = n
|
|
63
|
+
while (left > 0) {
|
|
64
|
+
if (left >= reps) {
|
|
65
|
+
out = out + block
|
|
66
|
+
left = left - reps
|
|
67
|
+
} else {
|
|
68
|
+
reps = idiv(reps, 2)
|
|
69
|
+
block = substr(block, 0, idiv(strlen(block), 2))
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return out
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ---------------- 整数排版 ----------------
|
|
76
|
+
|
|
77
|
+
// 整数右对齐到 width 列 (左侧补空格); width 不足时原样返回, 负数保留 '-' 号
|
|
78
|
+
// width <= 0 时原样返回
|
|
79
|
+
function fmt_int_width(int v, int width) -> string {
|
|
80
|
+
string s = int_to_str(v)
|
|
81
|
+
int n = strlen(s)
|
|
82
|
+
if (width <= n) { return s }
|
|
83
|
+
return fmt_rep(" ", width - n) + s
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 整数补前导 0 到 width 列 (含符号, 即总宽度); width 不足时原样返回
|
|
87
|
+
// fmt_int_zero(-42, 5) -> "-0042"
|
|
88
|
+
function fmt_int_zero(int v, int width) -> string {
|
|
89
|
+
string s = int_to_str(v)
|
|
90
|
+
int n = strlen(s)
|
|
91
|
+
if (width <= n) { return s }
|
|
92
|
+
if (v < 0) {
|
|
93
|
+
return "-" + fmt_rep("0", width - n) + substr(s, 1, n - 1)
|
|
94
|
+
}
|
|
95
|
+
return fmt_rep("0", width - n) + s
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 千位分隔 (每 3 位一个逗号), 负数保留 '-' 号
|
|
99
|
+
// fmt_thousands(1234567) -> "1,234,567"; 0 -> "0"; 100 -> "100"; -1000 -> "-1,000"
|
|
100
|
+
function fmt_thousands(int v) -> string {
|
|
101
|
+
string s = int_to_str(v)
|
|
102
|
+
string sign = ""
|
|
103
|
+
if (s[0] == 45) {
|
|
104
|
+
sign = "-"
|
|
105
|
+
s = substr(s, 1, strlen(s) - 1)
|
|
106
|
+
}
|
|
107
|
+
int n = strlen(s)
|
|
108
|
+
string out = ""
|
|
109
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
110
|
+
if (i > 0 && (n - i) % 3 == 0) {
|
|
111
|
+
out = out + ","
|
|
112
|
+
}
|
|
113
|
+
out = out + substr(s, i, 1)
|
|
114
|
+
}
|
|
115
|
+
return sign + out
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ---------------- 浮点排版 ----------------
|
|
119
|
+
|
|
120
|
+
// 定点小数: 四舍五入到 digits 位并补足尾随 0, digits 钳制到 [0, 9]
|
|
121
|
+
// fmt_float_dec(3.14159, 2) -> "3.14"; fmt_float_dec(2.0, 3) -> "2.000"
|
|
122
|
+
// fmt_float_dec(9.999, 2) -> "10.00" (进位贯穿整数部分)
|
|
123
|
+
// fmt_float_dec(3.14159, 0) -> "3" (digits == 0 时不含小数点)
|
|
124
|
+
// 负号在圆整前剥掉, 因此按"绝对值四舍五入"再补符号 (-0.005, 2 -> "-0.01")
|
|
125
|
+
// |v| >= 1e9 时退化为 float_to_str(v), 不做定点补零
|
|
126
|
+
function fmt_float_dec(float v, int digits) -> string {
|
|
127
|
+
if (digits < 0) { digits = 0 }
|
|
128
|
+
if (digits > 9) { digits = 9 }
|
|
129
|
+
string sign = ""
|
|
130
|
+
float mag = v
|
|
131
|
+
if (v < 0.0) {
|
|
132
|
+
sign = "-"
|
|
133
|
+
mag = -v
|
|
134
|
+
}
|
|
135
|
+
if (mag >= 1000000000.0) {
|
|
136
|
+
return float_to_str(v)
|
|
137
|
+
}
|
|
138
|
+
int scale = 1
|
|
139
|
+
for (int i = 0; i < digits; i = i + 1) {
|
|
140
|
+
scale = scale * 10
|
|
141
|
+
}
|
|
142
|
+
int scaled = floor(mag * scale + 0.5)
|
|
143
|
+
int ip = idiv(scaled, scale)
|
|
144
|
+
int fp = scaled % scale
|
|
145
|
+
string out = sign + int_to_str(ip)
|
|
146
|
+
if (digits > 0) {
|
|
147
|
+
string fs = int_to_str(fp)
|
|
148
|
+
out = out + "." + fmt_rep("0", digits - strlen(fs)) + fs
|
|
149
|
+
}
|
|
150
|
+
return out
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// 定点小数并把**整数部分**补前导 0 到 int_width 位 (不含符号与小数部分)
|
|
154
|
+
// fmt_float_pad(3.14159, 6, 2) -> "000003.14"
|
|
155
|
+
// fmt_float_pad(-3.5, 4, 1) -> "-0003.5"
|
|
156
|
+
// dec_digits 钳制到 [0, 9]; int_width <= 数字位数时不补
|
|
157
|
+
function fmt_float_pad(float v, int int_width, int dec_digits) -> string {
|
|
158
|
+
string fixed = fmt_float_dec(v, dec_digits)
|
|
159
|
+
string sign = ""
|
|
160
|
+
string body = fixed
|
|
161
|
+
if (fixed[0] == 45) {
|
|
162
|
+
sign = "-"
|
|
163
|
+
body = substr(fixed, 1, strlen(fixed) - 1)
|
|
164
|
+
}
|
|
165
|
+
string ipart = body
|
|
166
|
+
string rest = ""
|
|
167
|
+
int dot = indexof(body, ".")
|
|
168
|
+
if (dot >= 0) {
|
|
169
|
+
ipart = substr(body, 0, dot)
|
|
170
|
+
rest = substr(body, dot, strlen(body) - dot)
|
|
171
|
+
}
|
|
172
|
+
return sign + fmt_rep("0", int_width - strlen(ipart)) + ipart + rest
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// 比例 -> 百分数字符串 (四舍五入到 digits 位小数, digits 钳制到 [0, 9])
|
|
176
|
+
// fmt_percent(0.1234, 1) -> "12.3%"; fmt_percent(1.0, 0) -> "100%"
|
|
177
|
+
// fmt_percent(-0.25, 1) -> "-25.0%"
|
|
178
|
+
function fmt_percent(float ratio, int digits) -> string {
|
|
179
|
+
return fmt_float_dec(ratio * 100.0, digits) + "%"
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ---------------- 对齐与重复 ----------------
|
|
183
|
+
|
|
184
|
+
// 对齐到 width 列: mode 0 左对齐 / 1 右对齐 / 2 居中;
|
|
185
|
+
// 其余取值 (含负数) 一律按左对齐处理; 居中时余出的 1 列补在右侧
|
|
186
|
+
// 字符串已 >= width 时原样返回
|
|
187
|
+
function fmt_align(string s, int width, int mode) -> string {
|
|
188
|
+
int n = strlen(s)
|
|
189
|
+
if (width <= n) { return s }
|
|
190
|
+
int pad = width - n
|
|
191
|
+
if (mode == 1) {
|
|
192
|
+
return fmt_rep(" ", pad) + s
|
|
193
|
+
}
|
|
194
|
+
if (mode == 2) {
|
|
195
|
+
int left = idiv(pad, 2)
|
|
196
|
+
return fmt_rep(" ", left) + s + fmt_rep(" ", pad - left)
|
|
197
|
+
}
|
|
198
|
+
return s + fmt_rep(" ", pad)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 居中到 width 列 (等价于 fmt_align(s, width, 2))
|
|
202
|
+
function fmt_center(string s, int width) -> string {
|
|
203
|
+
return fmt_align(s, width, 2)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// 字符串重复 n 次; n 钳制到 [0, 1024], n <= 0 或 s 为空返回 ""
|
|
207
|
+
function fmt_repeat(string s, int n) -> string {
|
|
208
|
+
return fmt_rep(s, n)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// 同一个字符重复 n 次; ch 见 fmt_chr (非法编码返回 "")
|
|
212
|
+
function fmt_repeat_char(int ch, int n) -> string {
|
|
213
|
+
return fmt_rep(fmt_chr(ch), n)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// ---------------- 图形化 ----------------
|
|
217
|
+
|
|
218
|
+
// 比例条: 用 '#' 填充已用部分, '-' 填充剩余部分, 共 width 列
|
|
219
|
+
// ratio 钳制到 [0, 1]; width <= 0 返回 ""; width > 1024 钳制为 1024
|
|
220
|
+
// 填充长度 = round(ratio * width) (四舍五入)
|
|
221
|
+
// fmt_bar(0.5, 10) -> "#####-----"
|
|
222
|
+
function fmt_bar(float ratio, int width) -> string {
|
|
223
|
+
if (width <= 0) { return "" }
|
|
224
|
+
if (width > 1024) { width = 1024 }
|
|
225
|
+
if (ratio < 0.0) { ratio = 0.0 }
|
|
226
|
+
if (ratio > 1.0) { ratio = 1.0 }
|
|
227
|
+
int filled = floor(ratio * width + 0.5)
|
|
228
|
+
if (filled < 0) { filled = 0 }
|
|
229
|
+
if (filled > width) { filled = width }
|
|
230
|
+
return fmt_rep("#", filled) + fmt_rep("-", width - filled)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// ---------------- 表格 ----------------
|
|
234
|
+
|
|
235
|
+
// 表格单元格: 正好 width 列
|
|
236
|
+
// 内容不足右补空格; 内容 >= width 时**按字节截断**到 width (不做省略号提示)
|
|
237
|
+
// width <= 0 返回 ""
|
|
238
|
+
function fmt_cell(string s, int width) -> string {
|
|
239
|
+
if (width <= 0) { return "" }
|
|
240
|
+
int n = strlen(s)
|
|
241
|
+
if (n >= width) { return substr(s, 0, width) }
|
|
242
|
+
return s + fmt_rep(" ", width - n)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// 水平分隔线: width 个 '-' 字符; width <= 0 返回 ""
|
|
246
|
+
function fmt_sep(int width) -> string {
|
|
247
|
+
return fmt_rep("-", width)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// 表格边框线: cols 列、每列 col_width 宽, 形如 "+----+----+"
|
|
251
|
+
// cols <= 0 返回 ""; cols 与 col_width 各自钳制到 [0, 256]
|
|
252
|
+
// fmt_border(3, 4) -> "+----+----+----+"
|
|
253
|
+
function fmt_border(int cols, int col_width) -> string {
|
|
254
|
+
if (cols <= 0) { return "" }
|
|
255
|
+
if (cols > 256) { cols = 256 }
|
|
256
|
+
if (col_width < 0) { col_width = 0 }
|
|
257
|
+
if (col_width > 256) { col_width = 256 }
|
|
258
|
+
string out = "+"
|
|
259
|
+
for (int i = 0; i < cols; i = i + 1) {
|
|
260
|
+
out = out + fmt_rep("-", col_width) + "+"
|
|
261
|
+
}
|
|
262
|
+
return out
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// ---------------- 进制与布尔 ----------------
|
|
266
|
+
|
|
267
|
+
// 整数 -> 大写十六进制字符串, 前导 0 补到 width 位
|
|
268
|
+
// v == 0 输出 "0"; 负数按 64 位补码展开 (16 位十六进制)
|
|
269
|
+
// width 钳制到 [0, 64]; width 不超过实际位数时不补
|
|
270
|
+
// fmt_hex(255, 4) -> "00FF"; fmt_hex(-1, 0) -> "FFFFFFFFFFFFFFFF"
|
|
271
|
+
function fmt_hex(int v, int width) -> string {
|
|
272
|
+
if (width < 0) { width = 0 }
|
|
273
|
+
if (width > 64) { width = 64 }
|
|
274
|
+
string out = ""
|
|
275
|
+
if (v == 0) {
|
|
276
|
+
out = "0"
|
|
277
|
+
} else {
|
|
278
|
+
string digits = "0123456789ABCDEF"
|
|
279
|
+
int x = v
|
|
280
|
+
while (x != 0) {
|
|
281
|
+
int d = x & 15
|
|
282
|
+
out = substr(digits, d, 1) + out
|
|
283
|
+
x = (x >> 4) & 0x0FFFFFFFFFFFFFFF
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (width > strlen(out)) {
|
|
287
|
+
out = fmt_rep("0", width - strlen(out)) + out
|
|
288
|
+
}
|
|
289
|
+
return out
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// 整数 -> 二进制字符串, 前导 0 补到 width 位
|
|
293
|
+
// v == 0 输出 "0"; 负数按 64 位补码展开 (64 位二进制)
|
|
294
|
+
// width 钳制到 [0, 64]
|
|
295
|
+
// fmt_bin(10, 8) -> "00001010"
|
|
296
|
+
function fmt_bin(int v, int width) -> string {
|
|
297
|
+
if (width < 0) { width = 0 }
|
|
298
|
+
if (width > 64) { width = 64 }
|
|
299
|
+
string out = ""
|
|
300
|
+
if (v == 0) {
|
|
301
|
+
out = "0"
|
|
302
|
+
} else {
|
|
303
|
+
int x = v
|
|
304
|
+
while (x != 0) {
|
|
305
|
+
if ((x & 1) == 1) {
|
|
306
|
+
out = "1" + out
|
|
307
|
+
} else {
|
|
308
|
+
out = "0" + out
|
|
309
|
+
}
|
|
310
|
+
x = (x >> 1) & 0x7FFFFFFFFFFFFFFF
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (width > strlen(out)) {
|
|
314
|
+
out = fmt_rep("0", width - strlen(out)) + out
|
|
315
|
+
}
|
|
316
|
+
return out
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// 布尔 -> "true" / "false"
|
|
320
|
+
// 本库选定与内建 bool_to_str 及打印输出一致的拼写 (不是 "yes"/"no");
|
|
321
|
+
// 零值为 false, 非零为 true
|
|
322
|
+
function fmt_bool(bool b) -> string {
|
|
323
|
+
if (b) { return "true" }
|
|
324
|
+
return "false"
|
|
325
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "frac"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: 有理数"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/frac.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "frac"]
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 有理数 (frac)
|
|
2
|
+
// 用法: import "frac.cin"
|
|
3
|
+
//
|
|
4
|
+
// 表示: num / den, 两个字段都是 64 位 int。构造与每次运算后都会
|
|
5
|
+
// **自动约分**并把分母规范化为正数: 3/-4 -> -3/4, 2/4 -> 1/2, 0/5 -> 0/1。
|
|
6
|
+
// 零恒为 0/1, 分母恒 >= 1。
|
|
7
|
+
//
|
|
8
|
+
// 边界:
|
|
9
|
+
// * 分母为 0 (fr_make(x, 0) 或 fr_div(_, 0)) -> 结果规范化为 0/1,
|
|
10
|
+
// 并把全局错误标志置 1 (fr_error() 查询 / fr_reset_error() 清除);
|
|
11
|
+
// * 加减乘除的中途乘法是原生 64 位整型运算, 分子分母的中间积可能回绕
|
|
12
|
+
// (例如 1/10^10 * 1/10^10 的分母 10^20 超出 int 范围), 本库不做大数处理;
|
|
13
|
+
// * 约分用 idiv 与 %, gcd 由 fr_gcd 自行实现, 不依赖其它库。
|
|
14
|
+
//
|
|
15
|
+
// 内存: Frac 只占 2 槽 = 16 字节, 每个"返回新值"的调用只分配 16 字节。
|
|
16
|
+
|
|
17
|
+
struct Frac {
|
|
18
|
+
int num
|
|
19
|
+
int den
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// 错误标志: 1 表示曾经发生过除以零等非法操作 (粘滞, 需要手动清除)
|
|
23
|
+
int fr_err = 0
|
|
24
|
+
|
|
25
|
+
// ---------------- 基础 ----------------
|
|
26
|
+
|
|
27
|
+
// 是否发生过非法操作 (除以零等): 1 / 0
|
|
28
|
+
function fr_error() -> int {
|
|
29
|
+
return fr_err
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// 清除错误标志
|
|
33
|
+
function fr_reset_error() -> void {
|
|
34
|
+
fr_err = 0
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 最大公约数 (对参数取绝对值; gcd(0, 0) = 0)
|
|
38
|
+
function fr_gcd(int a, int b) -> int {
|
|
39
|
+
int x = a
|
|
40
|
+
int y = b
|
|
41
|
+
if (x < 0) { x = 0 - x }
|
|
42
|
+
if (y < 0) { y = 0 - y }
|
|
43
|
+
while (y != 0) {
|
|
44
|
+
int t = x % y
|
|
45
|
+
x = y
|
|
46
|
+
y = t
|
|
47
|
+
}
|
|
48
|
+
return x
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 规范化: 分母为 0 时置错误标志并归零; 分母转正; 约分; 零写作 0/1
|
|
52
|
+
function fr_norm(Frac f) -> void {
|
|
53
|
+
if (f.den == 0) {
|
|
54
|
+
fr_err = 1
|
|
55
|
+
f.num = 0
|
|
56
|
+
f.den = 1
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
if (f.den < 0) {
|
|
60
|
+
f.num = 0 - f.num
|
|
61
|
+
f.den = 0 - f.den
|
|
62
|
+
}
|
|
63
|
+
if (f.num == 0) {
|
|
64
|
+
f.den = 1
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
int g = fr_gcd(f.num, f.den)
|
|
68
|
+
if (g > 1) {
|
|
69
|
+
f.num = idiv(f.num, g)
|
|
70
|
+
f.den = idiv(f.den, g)
|
|
71
|
+
}
|
|
72
|
+
// 防御: 分母为 -2^63 时 0-den 仍为负
|
|
73
|
+
if (f.den < 0) {
|
|
74
|
+
f.num = 0 - f.num
|
|
75
|
+
f.den = 0 - f.den
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ---------------- 构造与取字段 ----------------
|
|
80
|
+
|
|
81
|
+
// 由分子分母构造并约分; d == 0 时返回 0/1 并置错误标志
|
|
82
|
+
function fr_make(int n, int d) -> Frac {
|
|
83
|
+
Frac f
|
|
84
|
+
f.num = n
|
|
85
|
+
f.den = d
|
|
86
|
+
fr_norm(f)
|
|
87
|
+
return f
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 零 (0/1)
|
|
91
|
+
function fr_zero() -> Frac {
|
|
92
|
+
Frac f
|
|
93
|
+
f.num = 0
|
|
94
|
+
f.den = 1
|
|
95
|
+
return f
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 分子
|
|
99
|
+
function fr_num(Frac f) -> int {
|
|
100
|
+
return f.num
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 分母 (恒为正)
|
|
104
|
+
function fr_den(Frac f) -> int {
|
|
105
|
+
return f.den
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ---------------- 算术 ----------------
|
|
109
|
+
|
|
110
|
+
// a + b (先用 gcd 把分母降到 lcm, 减少中间积溢出的机会)
|
|
111
|
+
function fr_add(Frac a, Frac b) -> Frac {
|
|
112
|
+
Frac r
|
|
113
|
+
int g = fr_gcd(a.den, b.den)
|
|
114
|
+
int da = a.den
|
|
115
|
+
int db = b.den
|
|
116
|
+
if (g > 1) {
|
|
117
|
+
da = idiv(a.den, g)
|
|
118
|
+
db = idiv(b.den, g)
|
|
119
|
+
}
|
|
120
|
+
r.num = a.num * db + b.num * da
|
|
121
|
+
r.den = da * b.den
|
|
122
|
+
fr_norm(r)
|
|
123
|
+
return r
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// a - b
|
|
127
|
+
function fr_sub(Frac a, Frac b) -> Frac {
|
|
128
|
+
Frac r
|
|
129
|
+
int g = fr_gcd(a.den, b.den)
|
|
130
|
+
int da = a.den
|
|
131
|
+
int db = b.den
|
|
132
|
+
if (g > 1) {
|
|
133
|
+
da = idiv(a.den, g)
|
|
134
|
+
db = idiv(b.den, g)
|
|
135
|
+
}
|
|
136
|
+
r.num = a.num * db - b.num * da
|
|
137
|
+
r.den = da * b.den
|
|
138
|
+
fr_norm(r)
|
|
139
|
+
return r
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// a * b (先交叉约分)
|
|
143
|
+
function fr_mul(Frac a, Frac b) -> Frac {
|
|
144
|
+
Frac r
|
|
145
|
+
int n1 = a.num
|
|
146
|
+
int d1 = a.den
|
|
147
|
+
int n2 = b.num
|
|
148
|
+
int d2 = b.den
|
|
149
|
+
int g1 = fr_gcd(n1, d2)
|
|
150
|
+
int g2 = fr_gcd(n2, d1)
|
|
151
|
+
if (g1 > 1) {
|
|
152
|
+
n1 = idiv(n1, g1)
|
|
153
|
+
d2 = idiv(d2, g1)
|
|
154
|
+
}
|
|
155
|
+
if (g2 > 1) {
|
|
156
|
+
n2 = idiv(n2, g2)
|
|
157
|
+
d1 = idiv(d1, g2)
|
|
158
|
+
}
|
|
159
|
+
r.num = n1 * n2
|
|
160
|
+
r.den = d1 * d2
|
|
161
|
+
fr_norm(r)
|
|
162
|
+
return r
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// a / b; b 为 0 时返回 0/1 并置错误标志
|
|
166
|
+
function fr_div(Frac a, Frac b) -> Frac {
|
|
167
|
+
Frac r
|
|
168
|
+
if (b.num == 0) {
|
|
169
|
+
fr_err = 1
|
|
170
|
+
r.num = 0
|
|
171
|
+
r.den = 1
|
|
172
|
+
return r
|
|
173
|
+
}
|
|
174
|
+
int n1 = a.num
|
|
175
|
+
int d1 = a.den
|
|
176
|
+
int n2 = b.num
|
|
177
|
+
int d2 = b.den
|
|
178
|
+
int g1 = fr_gcd(n1, n2)
|
|
179
|
+
int g2 = fr_gcd(d2, d1)
|
|
180
|
+
if (g1 > 1) {
|
|
181
|
+
n1 = idiv(n1, g1)
|
|
182
|
+
n2 = idiv(n2, g1)
|
|
183
|
+
}
|
|
184
|
+
if (g2 > 1) {
|
|
185
|
+
d2 = idiv(d2, g2)
|
|
186
|
+
d1 = idiv(d1, g2)
|
|
187
|
+
}
|
|
188
|
+
r.num = n1 * d2
|
|
189
|
+
r.den = d1 * n2
|
|
190
|
+
fr_norm(r)
|
|
191
|
+
return r
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// -a (零的相反数仍为 0/1)
|
|
195
|
+
function fr_neg(Frac a) -> Frac {
|
|
196
|
+
Frac r
|
|
197
|
+
r.num = 0 - a.num
|
|
198
|
+
r.den = a.den
|
|
199
|
+
fr_norm(r)
|
|
200
|
+
return r
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// |a|
|
|
204
|
+
function fr_abs(Frac a) -> Frac {
|
|
205
|
+
Frac r
|
|
206
|
+
r.num = a.num
|
|
207
|
+
r.den = a.den
|
|
208
|
+
if (r.num < 0) { r.num = 0 - r.num }
|
|
209
|
+
fr_norm(r)
|
|
210
|
+
return r
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// ---------------- 比较与转换 ----------------
|
|
214
|
+
|
|
215
|
+
// 与数学顺序一致的比较: -1 (a < b) / 0 / 1 (a > b)
|
|
216
|
+
// 内部为交叉相乘, 极端数值下中间积可能回绕 (见文件头说明)
|
|
217
|
+
function fr_cmp(Frac a, Frac b) -> int {
|
|
218
|
+
int l = a.num * b.den
|
|
219
|
+
int r = b.num * a.den
|
|
220
|
+
if (l < r) { return -1 }
|
|
221
|
+
if (l > r) { return 1 }
|
|
222
|
+
return 0
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// 是否数值相等: 1 / 0
|
|
226
|
+
function fr_equals(Frac a, Frac b) -> int {
|
|
227
|
+
if (fr_cmp(a, b) == 0) { return 1 }
|
|
228
|
+
return 0
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// 是否为整数 (分母为 1): 1 / 0
|
|
232
|
+
function fr_is_int(Frac a) -> int {
|
|
233
|
+
if (a.den == 1) { return 1 }
|
|
234
|
+
if (a.den != 0 && a.num % a.den == 0) { return 1 }
|
|
235
|
+
return 0
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// 整数输出 "2", 否则输出 "3/4"
|
|
239
|
+
function fr_to_str(Frac a) -> string {
|
|
240
|
+
fr_norm(a)
|
|
241
|
+
if (a.den == 1) { return int_to_str(a.num) }
|
|
242
|
+
return int_to_str(a.num) + "/" + int_to_str(a.den)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// 浮点近似 (分子/分母为 64 位 int; 分母为 0 时置错误标志并返回 0.0)
|
|
246
|
+
function fr_to_float(Frac a) -> float {
|
|
247
|
+
if (a.den == 0) {
|
|
248
|
+
fr_err = 1
|
|
249
|
+
return 0.0
|
|
250
|
+
}
|
|
251
|
+
return a.num / a.den
|
|
252
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "gostd"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: Go 标准库兼容层"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/gostd.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "gostd"]
|