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,563 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 编解码与简单密码 (codec)
|
|
2
|
+
// 用法: import "codec.cin"
|
|
3
|
+
//
|
|
4
|
+
// 纯 CIN 实现, 只使用非宿主内建 (strlen / strcmp / substr / int_to_str),
|
|
5
|
+
// 不调用 sha256 / base64_encode / base64_decode 等宿主能力, 原生与 AOT
|
|
6
|
+
// 路径下行为完全一致。
|
|
7
|
+
//
|
|
8
|
+
// 字节语义: strlen / substr / s[i] 全是**字节**语义, 本库沿用同一约定 ——
|
|
9
|
+
// 所有编解码都按 UTF-8 字节序列逐个字节处理, 不做码点解码; 非 ASCII 输入
|
|
10
|
+
// (含中文) 会被当成普通字节序列参与编码, 解码后原样还原。
|
|
11
|
+
//
|
|
12
|
+
// 堆与容量 (CIN 没有动态内存分配, 字符串拼接的堆块**只增不减**):
|
|
13
|
+
// * 一次 `a + b` 会新建 len(a)+len(b) 字节的堆块, 因此"结果 = 结果 + 小块"
|
|
14
|
+
// 的逐段拼接总拷贝量是 O(n^2) 字节, 同时每个堆块还有约 16 字节固定开销。
|
|
15
|
+
// * 实测 (默认 64 KiB 内存, 堆自 0x8000 起向上增长, 栈自顶向下):
|
|
16
|
+
// - 十六进制编码约 134 字节堆/输入字节 (输出 2 字符, 约 67 字节/输出字符)
|
|
17
|
+
// - base64 编码约 61 字节堆/输入字节
|
|
18
|
+
// - 反转 / 异或约 16 字节堆/输入字节 (走内部定长缓冲, 近线性)
|
|
19
|
+
// 一次调用不要指望处理上千字节: 推荐单次输入 ≤ 200 字节;
|
|
20
|
+
// 更长的串请拆成多次调用, 或用 `--mem-size` 扩容后再用。
|
|
21
|
+
// * 十六进制 / URL / base64 的输出长度分别约为输入的 2 / 3 / 4/3 倍,
|
|
22
|
+
// 仓库内的官方标准库不保证超出上述规模的输入。
|
|
23
|
+
// * 本库没有全局可变状态, 所有函数都是纯函数 (可重入, 多次调用互不影响)。
|
|
24
|
+
|
|
25
|
+
// 局部重建缓冲容量 (字节); 供内部辅助函数使用, 不对外暴露语义
|
|
26
|
+
const int codec_buf_cap = 256
|
|
27
|
+
|
|
28
|
+
// 256 字节双向字母表: codec_bytes[i] == i (0..255),
|
|
29
|
+
// 用于把任意字节值写回字符串 (CIN 字符串不可原地修改, 只能取单字节再拼接)。
|
|
30
|
+
// 只含 ASCII 与 \xHH 转义, 因此源码本身是纯 ASCII, 不依赖文件编码。
|
|
31
|
+
// 255 字节字母表: codec_bytes[i] == 字节值 (i + 1), 即覆盖 1..255。
|
|
32
|
+
// 字节 0 不在表内 —— CIN 的 string 是 NUL 结尾的, 字符串里**无法表示字节 0**
|
|
33
|
+
// (字面量 "\x00...\xFF" 会在第一个字节就截断成空串), 因此 codec_chr_of(0) 返回 ""。
|
|
34
|
+
// 只含 ASCII 与 \xHH 转义, 源码本身是纯 ASCII, 不依赖文件编码。
|
|
35
|
+
string codec_bytes = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF"
|
|
36
|
+
|
|
37
|
+
// ---------------- 内部辅助函数 (非公开 API, 统一 codec_ 前缀) ----------------
|
|
38
|
+
|
|
39
|
+
// ASCII 数字 '0'-'9'
|
|
40
|
+
function codec_is_digit(int c) -> int {
|
|
41
|
+
if (c >= '0' && c <= '9') { return 1 }
|
|
42
|
+
return 0
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// URL 无需转义的字节: A-Za-z0-9-_.~
|
|
46
|
+
function codec_is_url_safe(int c) -> int {
|
|
47
|
+
if (c >= 'A' && c <= 'Z') { return 1 }
|
|
48
|
+
if (c >= 'a' && c <= 'z') { return 1 }
|
|
49
|
+
if (c >= '0' && c <= '9') { return 1 }
|
|
50
|
+
if (c == '-' || c == '_' || c == '.' || c == '~') { return 1 }
|
|
51
|
+
return 0
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 逐字节重建 s 的 [lo, hi) 区间, 结果与 substr(s, lo, hi - lo) 相同;
|
|
55
|
+
// 用一个定长局部缓冲每轮取 codec_buf_cap 个字节再前置, 把逐字节前缀拼接的
|
|
56
|
+
// 拷贝量从 O(n^2) 降到 O(n^2 / codec_buf_cap), 堆开销接近线性。
|
|
57
|
+
function codec_rebuild_range(string s, int lo, int hi) -> string {
|
|
58
|
+
if (lo < 0) { lo = 0 }
|
|
59
|
+
if (hi > strlen(s)) { hi = strlen(s) }
|
|
60
|
+
if (hi <= lo) { return "" }
|
|
61
|
+
string out = ""
|
|
62
|
+
int buf[codec_buf_cap]
|
|
63
|
+
int pos = lo
|
|
64
|
+
while (pos < hi) {
|
|
65
|
+
int k = 0
|
|
66
|
+
int stop = pos + codec_buf_cap
|
|
67
|
+
if (stop > hi) { stop = hi }
|
|
68
|
+
while (pos < stop) {
|
|
69
|
+
buf[k] = s[pos]
|
|
70
|
+
k = k + 1
|
|
71
|
+
pos = pos + 1
|
|
72
|
+
}
|
|
73
|
+
int j = k - 1
|
|
74
|
+
while (j >= 0) {
|
|
75
|
+
out = out + codec_chr_of(buf[j])
|
|
76
|
+
j = j - 1
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return out
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 字节值 -> 单字符字符串 (1..255); 越界按 [0, 255] 截断。
|
|
83
|
+
// **字节 0 返回 ""**: CIN 的 string 以 NUL 结尾, 无法在字符串中表示字节 0
|
|
84
|
+
// (表里也没有 0), 所以"输出任意字节"的解码函数遇到 0 一律判为不可表示。
|
|
85
|
+
function codec_chr_of(int b) -> string {
|
|
86
|
+
int v = b & 0xFF
|
|
87
|
+
if (v == 0) { return "" }
|
|
88
|
+
return substr(codec_bytes, v - 1, 1)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// base64 正向字母表下标是否合法 (0..63)
|
|
92
|
+
function codec_b64_ok(int c) -> int {
|
|
93
|
+
if (c >= 'A' && c <= 'Z') { return 1 }
|
|
94
|
+
if (c >= 'a' && c <= 'z') { return 1 }
|
|
95
|
+
if (c >= '0' && c <= '9') { return 1 }
|
|
96
|
+
if (c == '+' || c == '/') { return 1 }
|
|
97
|
+
return 0
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// base64 字符 -> 6 位值 (调用前必须先用 codec_b64_ok 校验; 非法返回 -1)
|
|
101
|
+
function codec_b64_val(int c) -> int {
|
|
102
|
+
if (c >= 'A' && c <= 'Z') { return c - 'A' }
|
|
103
|
+
if (c >= 'a' && c <= 'z') { return c - 'a' + 26 }
|
|
104
|
+
if (c >= '0' && c <= '9') { return c - '0' + 52 }
|
|
105
|
+
if (c == '+') { return 62 }
|
|
106
|
+
if (c == '/') { return 63 }
|
|
107
|
+
return -1
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 摩尔斯码查表: 大写字母 / 数字 -> 点划串, 未知返回 ""
|
|
111
|
+
function codec_morse_of(int c) -> string {
|
|
112
|
+
if (c == 'A') { return ".-" }
|
|
113
|
+
if (c == 'B') { return "-..." }
|
|
114
|
+
if (c == 'C') { return "-.-." }
|
|
115
|
+
if (c == 'D') { return "-.." }
|
|
116
|
+
if (c == 'E') { return "." }
|
|
117
|
+
if (c == 'F') { return "..-." }
|
|
118
|
+
if (c == 'G') { return "--." }
|
|
119
|
+
if (c == 'H') { return "...." }
|
|
120
|
+
if (c == 'I') { return ".." }
|
|
121
|
+
if (c == 'J') { return ".---" }
|
|
122
|
+
if (c == 'K') { return "-.-" }
|
|
123
|
+
if (c == 'L') { return ".-.." }
|
|
124
|
+
if (c == 'M') { return "--" }
|
|
125
|
+
if (c == 'N') { return "-." }
|
|
126
|
+
if (c == 'O') { return "---" }
|
|
127
|
+
if (c == 'P') { return ".--." }
|
|
128
|
+
if (c == 'Q') { return "--.-" }
|
|
129
|
+
if (c == 'R') { return ".-." }
|
|
130
|
+
if (c == 'S') { return "..." }
|
|
131
|
+
if (c == 'T') { return "-" }
|
|
132
|
+
if (c == 'U') { return "..-" }
|
|
133
|
+
if (c == 'V') { return "...-" }
|
|
134
|
+
if (c == 'W') { return ".--" }
|
|
135
|
+
if (c == 'X') { return "-..-" }
|
|
136
|
+
if (c == 'Y') { return "-.--" }
|
|
137
|
+
if (c == 'Z') { return "--.." }
|
|
138
|
+
if (c == '0') { return "-----" }
|
|
139
|
+
if (c == '1') { return ".----" }
|
|
140
|
+
if (c == '2') { return "..---" }
|
|
141
|
+
if (c == '3') { return "...--" }
|
|
142
|
+
if (c == '4') { return "....-" }
|
|
143
|
+
if (c == '5') { return "....." }
|
|
144
|
+
if (c == '6') { return "-...." }
|
|
145
|
+
if (c == '7') { return "--..." }
|
|
146
|
+
if (c == '8') { return "---.." }
|
|
147
|
+
if (c == '9') { return "----." }
|
|
148
|
+
return ""
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 摩尔斯码 -> 字符 (未知返回 0)
|
|
152
|
+
function codec_morse_char(string code) -> int {
|
|
153
|
+
if (strcmp(code, ".-") == 0) { return 'A' }
|
|
154
|
+
if (strcmp(code, "-...") == 0) { return 'B' }
|
|
155
|
+
if (strcmp(code, "-.-.") == 0) { return 'C' }
|
|
156
|
+
if (strcmp(code, "-..") == 0) { return 'D' }
|
|
157
|
+
if (strcmp(code, ".") == 0) { return 'E' }
|
|
158
|
+
if (strcmp(code, "..-.") == 0) { return 'F' }
|
|
159
|
+
if (strcmp(code, "--.") == 0) { return 'G' }
|
|
160
|
+
if (strcmp(code, "....") == 0) { return 'H' }
|
|
161
|
+
if (strcmp(code, "..") == 0) { return 'I' }
|
|
162
|
+
if (strcmp(code, ".---") == 0) { return 'J' }
|
|
163
|
+
if (strcmp(code, "-.-") == 0) { return 'K' }
|
|
164
|
+
if (strcmp(code, ".-..") == 0) { return 'L' }
|
|
165
|
+
if (strcmp(code, "--") == 0) { return 'M' }
|
|
166
|
+
if (strcmp(code, "-.") == 0) { return 'N' }
|
|
167
|
+
if (strcmp(code, "---") == 0) { return 'O' }
|
|
168
|
+
if (strcmp(code, ".--.") == 0) { return 'P' }
|
|
169
|
+
if (strcmp(code, "--.-") == 0) { return 'Q' }
|
|
170
|
+
if (strcmp(code, ".-.") == 0) { return 'R' }
|
|
171
|
+
if (strcmp(code, "...") == 0) { return 'S' }
|
|
172
|
+
if (strcmp(code, "-") == 0) { return 'T' }
|
|
173
|
+
if (strcmp(code, "..-") == 0) { return 'U' }
|
|
174
|
+
if (strcmp(code, "...-") == 0) { return 'V' }
|
|
175
|
+
if (strcmp(code, ".--") == 0) { return 'W' }
|
|
176
|
+
if (strcmp(code, "-..-") == 0) { return 'X' }
|
|
177
|
+
if (strcmp(code, "-.--") == 0) { return 'Y' }
|
|
178
|
+
if (strcmp(code, "--..") == 0) { return 'Z' }
|
|
179
|
+
if (strcmp(code, "-----") == 0) { return '0' }
|
|
180
|
+
if (strcmp(code, ".----") == 0) { return '1' }
|
|
181
|
+
if (strcmp(code, "..---") == 0) { return '2' }
|
|
182
|
+
if (strcmp(code, "...--") == 0) { return '3' }
|
|
183
|
+
if (strcmp(code, "....-") == 0) { return '4' }
|
|
184
|
+
if (strcmp(code, ".....") == 0) { return '5' }
|
|
185
|
+
if (strcmp(code, "-....") == 0) { return '6' }
|
|
186
|
+
if (strcmp(code, "--...") == 0) { return '7' }
|
|
187
|
+
if (strcmp(code, "---..") == 0) { return '8' }
|
|
188
|
+
if (strcmp(code, "----.") == 0) { return '9' }
|
|
189
|
+
return 0
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ---------------- 公开 API ----------------
|
|
193
|
+
|
|
194
|
+
// 字节下标 i 处的字符 (单字符字符串); 越界返回 "" (与内置下标不同, 本函数做边界检查)
|
|
195
|
+
function codec_char_at(string s, int i) -> string {
|
|
196
|
+
if (i < 0 || i >= strlen(s)) { return "" }
|
|
197
|
+
return substr(s, i, 1)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// 单字节的十六进制 (小写), 高半字节在前
|
|
201
|
+
function codec_hex_byte(int b) -> string {
|
|
202
|
+
string d = "0123456789abcdef"
|
|
203
|
+
int v = b & 0xFF
|
|
204
|
+
return substr(d, v >> 4, 1) + substr(d, v & 15, 1)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// 字节串 -> 小写十六进制 (无分隔); 空串返回空串
|
|
208
|
+
function codec_hex_encode(string s) -> string {
|
|
209
|
+
string out = ""
|
|
210
|
+
int n = strlen(s)
|
|
211
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
212
|
+
out = out + codec_hex_byte(s[i])
|
|
213
|
+
}
|
|
214
|
+
return out
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// 十六进制字符 -> 数值; 非法返回 -1
|
|
218
|
+
function codec_hex_digit(int c) -> int {
|
|
219
|
+
if (c >= '0' && c <= '9') { return c - '0' }
|
|
220
|
+
if (c >= 'a' && c <= 'f') { return c - 'a' + 10 }
|
|
221
|
+
if (c >= 'A' && c <= 'F') { return c - 'A' + 10 }
|
|
222
|
+
return -1
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// 小写/大写十六进制 -> 字节串; 长度为奇数或含非法字符时返回 ""
|
|
226
|
+
function codec_hex_decode(string h) -> string {
|
|
227
|
+
int n = strlen(h)
|
|
228
|
+
if (n == 0) { return "" }
|
|
229
|
+
if ((n % 2) != 0) { return "" }
|
|
230
|
+
int i = 0
|
|
231
|
+
while (i < n) {
|
|
232
|
+
if (codec_hex_digit(h[i]) < 0) { return "" }
|
|
233
|
+
if (codec_hex_digit(h[i + 1]) < 0) { return "" }
|
|
234
|
+
i = i + 2
|
|
235
|
+
}
|
|
236
|
+
string out = ""
|
|
237
|
+
i = 0
|
|
238
|
+
while (i < n) {
|
|
239
|
+
int hi = codec_hex_digit(h[i])
|
|
240
|
+
int lo = codec_hex_digit(h[i + 1])
|
|
241
|
+
int byte = hi * 16 + lo
|
|
242
|
+
if (byte == 0) { return "" }
|
|
243
|
+
out = out + codec_chr_of(byte)
|
|
244
|
+
i = i + 2
|
|
245
|
+
}
|
|
246
|
+
return out
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// 字节串 -> 百分号编码 (URL): 只保留 A-Za-z0-9-_.~, 其余为 %XX (大写十六进制)
|
|
250
|
+
function codec_url_encode(string s) -> string {
|
|
251
|
+
string out = ""
|
|
252
|
+
int n = strlen(s)
|
|
253
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
254
|
+
int c = s[i]
|
|
255
|
+
if (codec_is_url_safe(c) == 1) {
|
|
256
|
+
out = out + substr(s, i, 1)
|
|
257
|
+
} else {
|
|
258
|
+
out = out + "%" + lower(codec_hex_byte(c))
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return out
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// 百分号编码 -> 字节串; '+' 视为空格; 非法 % 序列 (截断或非十六进制) 返回 ""
|
|
265
|
+
function codec_url_decode(string s) -> string {
|
|
266
|
+
int n = strlen(s)
|
|
267
|
+
if (n == 0) { return "" }
|
|
268
|
+
int i = 0
|
|
269
|
+
while (i < n) {
|
|
270
|
+
int c = s[i]
|
|
271
|
+
if (c == '%') {
|
|
272
|
+
if (i + 2 >= n) { return "" }
|
|
273
|
+
if (codec_hex_digit(s[i + 1]) < 0) { return "" }
|
|
274
|
+
if (codec_hex_digit(s[i + 2]) < 0) { return "" }
|
|
275
|
+
i = i + 3
|
|
276
|
+
} else {
|
|
277
|
+
i = i + 1
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
string out = ""
|
|
281
|
+
i = 0
|
|
282
|
+
while (i < n) {
|
|
283
|
+
int c = s[i]
|
|
284
|
+
if (c == '+') {
|
|
285
|
+
out = out + " "
|
|
286
|
+
i = i + 1
|
|
287
|
+
} else if (c == '%') {
|
|
288
|
+
int d1 = codec_hex_digit(s[i + 1])
|
|
289
|
+
int d2 = codec_hex_digit(s[i + 2])
|
|
290
|
+
if (d1 * 16 + d2 == 0) { return "" }
|
|
291
|
+
out = out + codec_chr_of(d1 * 16 + d2)
|
|
292
|
+
i = i + 3
|
|
293
|
+
} else {
|
|
294
|
+
out = out + substr(s, i, 1)
|
|
295
|
+
i = i + 1
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return out
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// 凯撒密码加密: 只移动 ASCII 字母, 大小写各自保持; shift 可为负, 按 26 取模
|
|
302
|
+
function codec_caesar(string s, int shift) -> string {
|
|
303
|
+
int m = shift % 26
|
|
304
|
+
if (m < 0) { m = m + 26 }
|
|
305
|
+
string out = ""
|
|
306
|
+
int n = strlen(s)
|
|
307
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
308
|
+
int c = s[i]
|
|
309
|
+
if (c >= 'A' && c <= 'Z') {
|
|
310
|
+
out = out + codec_chr_of((c - 'A' + m) % 26 + 'A')
|
|
311
|
+
} else if (c >= 'a' && c <= 'z') {
|
|
312
|
+
out = out + codec_chr_of((c - 'a' + m) % 26 + 'a')
|
|
313
|
+
} else {
|
|
314
|
+
out = out + substr(s, i, 1)
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return out
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// 凯撒密码解密: 等价 codec_caesar(s, -shift)
|
|
321
|
+
function codec_decaesar(string s, int shift) -> string {
|
|
322
|
+
return codec_caesar(s, 0 - shift)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// ROT13: 凯撒的特例 (shift = 13), 只转字母
|
|
326
|
+
function codec_rot13(string s) -> string {
|
|
327
|
+
return codec_caesar(s, 13)
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// 循环密钥 XOR: 密钥为空时原样返回 s; 自逆 (加密 = 解密)
|
|
331
|
+
function codec_xor_cipher(string s, string key) -> string {
|
|
332
|
+
int kn = strlen(key)
|
|
333
|
+
if (kn == 0) { return s }
|
|
334
|
+
string out = ""
|
|
335
|
+
int n = strlen(s)
|
|
336
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
337
|
+
out = out + codec_chr_of((s[i] ^ key[i % kn]) & 0xFF)
|
|
338
|
+
}
|
|
339
|
+
return out
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// 字节串 -> 标准 base64 (字母表 A-Za-z0-9+/, '=' 填充); 空串返回空串
|
|
343
|
+
function codec_base64_encode(string s) -> string {
|
|
344
|
+
string a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
345
|
+
int n = strlen(s)
|
|
346
|
+
if (n == 0) { return "" }
|
|
347
|
+
string out = ""
|
|
348
|
+
int i = 0
|
|
349
|
+
while (i + 3 <= n) {
|
|
350
|
+
int b0 = s[i]
|
|
351
|
+
int b1 = s[i + 1]
|
|
352
|
+
int b2 = s[i + 2]
|
|
353
|
+
out = out + substr(a, b0 >> 2, 1) + substr(a, ((b0 & 3) << 4) | (b1 >> 4), 1) + substr(a, ((b1 & 15) << 2) | (b2 >> 6), 1) + substr(a, b2 & 63, 1)
|
|
354
|
+
i = i + 3
|
|
355
|
+
}
|
|
356
|
+
int rem = n - i
|
|
357
|
+
if (rem == 1) {
|
|
358
|
+
int b0 = s[i]
|
|
359
|
+
out = out + substr(a, b0 >> 2, 1) + substr(a, (b0 & 3) << 4, 1) + "=="
|
|
360
|
+
} else if (rem == 2) {
|
|
361
|
+
int b0 = s[i]
|
|
362
|
+
int b1 = s[i + 1]
|
|
363
|
+
out = out + substr(a, b0 >> 2, 1) + substr(a, ((b0 & 3) << 4) | (b1 >> 4), 1) + substr(a, (b1 & 15) << 2, 1) + "="
|
|
364
|
+
}
|
|
365
|
+
return out
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// base64 -> 字节串; 长度非 4 的倍数 / 非法字符 / 填充位置错误时返回 ""
|
|
369
|
+
function codec_base64_decode(string b) -> string {
|
|
370
|
+
int n = strlen(b)
|
|
371
|
+
if (n == 0) { return "" }
|
|
372
|
+
if ((n % 4) != 0) { return "" }
|
|
373
|
+
int pad = 0
|
|
374
|
+
if (b[n - 1] == '=') { pad = pad + 1 }
|
|
375
|
+
if (b[n - 2] == '=') { pad = pad + 1 }
|
|
376
|
+
if (pad > 2) { return "" }
|
|
377
|
+
if (pad == 1 && n < 4) { return "" }
|
|
378
|
+
if (pad == 2 && n < 4) { return "" }
|
|
379
|
+
int i = 0
|
|
380
|
+
while (i < n) {
|
|
381
|
+
int c = b[i]
|
|
382
|
+
if (i >= n - pad) {
|
|
383
|
+
if (c != '=') { return "" }
|
|
384
|
+
} else {
|
|
385
|
+
if (codec_b64_ok(c) == 0) { return "" }
|
|
386
|
+
}
|
|
387
|
+
i = i + 1
|
|
388
|
+
}
|
|
389
|
+
string out = ""
|
|
390
|
+
i = 0
|
|
391
|
+
while (i < n) {
|
|
392
|
+
if (i + 3 >= n) { break }
|
|
393
|
+
int c0 = codec_b64_val(b[i])
|
|
394
|
+
int c1 = codec_b64_val(b[i + 1])
|
|
395
|
+
int o0 = (c0 << 2) | (c1 >> 4)
|
|
396
|
+
if (o0 == 0) { return "" }
|
|
397
|
+
out = out + codec_chr_of(o0)
|
|
398
|
+
int ch2 = b[i + 2]
|
|
399
|
+
if (ch2 != '=') {
|
|
400
|
+
int c2 = codec_b64_val(ch2)
|
|
401
|
+
int o1 = ((c1 & 15) << 4) | (c2 >> 2)
|
|
402
|
+
if (o1 == 0) { return "" }
|
|
403
|
+
out = out + codec_chr_of(o1)
|
|
404
|
+
int ch3 = b[i + 3]
|
|
405
|
+
if (ch3 != '=') {
|
|
406
|
+
int c3 = codec_b64_val(ch3)
|
|
407
|
+
int o2 = ((c2 & 3) << 6) | c3
|
|
408
|
+
if (o2 == 0) { return "" }
|
|
409
|
+
out = out + codec_chr_of(o2)
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
i = i + 4
|
|
413
|
+
}
|
|
414
|
+
return out
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// 1..999 -> 3 位十进制 (前导 0 补齐)
|
|
418
|
+
function codec_rle_count(int k) -> string {
|
|
419
|
+
int v = k
|
|
420
|
+
if (v < 1) { v = 1 }
|
|
421
|
+
if (v > 999) { v = 999 }
|
|
422
|
+
int hi = idiv(v, 100)
|
|
423
|
+
int mid = idiv(v, 10) % 10
|
|
424
|
+
int lo = v % 10
|
|
425
|
+
return codec_chr_of('0' + hi) + codec_chr_of('0' + mid) + codec_chr_of('0' + lo)
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// 字节串 -> RLE。**格式以"无歧义"为第一优先级**:
|
|
429
|
+
// * 连续 k>=2 次重复的字节 b -> 3 位十进制计数 (001..999) + b; k>999 的段拆开;
|
|
430
|
+
// * 单次出现且**本身是数字字符**的字节 -> 也用 "001" + b (否则解码时会与计数混淆);
|
|
431
|
+
// * 其它单次出现的字节 -> 原样输出 1 个字节。
|
|
432
|
+
// 于是解码器只需看当前字节: 是数字就固定读 3 位计数再读 1 个字节, 否则就是 1 个原样字节。
|
|
433
|
+
// 旧实现写 `<count><byte>` 且计数位数不定, 于是 encode("555") 得到 "35",
|
|
434
|
+
// 而解码器会把 "35" 读成"计数 35、却缺字节" -> 返回空串, 编解码互不自洽。
|
|
435
|
+
// 输入不可能含字节 0 (CIN 字符串以 NUL 结尾)。
|
|
436
|
+
function codec_rle_encode(string s) -> string {
|
|
437
|
+
int n = strlen(s)
|
|
438
|
+
if (n == 0) { return "" }
|
|
439
|
+
string out = ""
|
|
440
|
+
int i = 0
|
|
441
|
+
while (i < n) {
|
|
442
|
+
int c = s[i]
|
|
443
|
+
int j = i
|
|
444
|
+
while (j < n) {
|
|
445
|
+
if (s[j] != c) { break }
|
|
446
|
+
j = j + 1
|
|
447
|
+
}
|
|
448
|
+
int run = j - i
|
|
449
|
+
while (run > 0) {
|
|
450
|
+
int piece = run
|
|
451
|
+
if (piece > 999) { piece = 999 }
|
|
452
|
+
run = run - piece
|
|
453
|
+
if (piece >= 2 || codec_is_digit(c) == 1) {
|
|
454
|
+
out = out + codec_rle_count(piece) + substr(s, i, 1)
|
|
455
|
+
} else {
|
|
456
|
+
out = out + substr(s, i, 1)
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
i = j
|
|
460
|
+
}
|
|
461
|
+
return out
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// RLE -> 字节串; 非法 (计数不足 3 位 / 含非数字 / 计数为 000 / 缺字节) 返回 ""
|
|
465
|
+
function codec_rle_decode(string s) -> string {
|
|
466
|
+
int n = strlen(s)
|
|
467
|
+
if (n == 0) { return "" }
|
|
468
|
+
string out = ""
|
|
469
|
+
int i = 0
|
|
470
|
+
while (i < n) {
|
|
471
|
+
int c = s[i]
|
|
472
|
+
if (codec_is_digit(c) == 0) {
|
|
473
|
+
out = out + substr(s, i, 1)
|
|
474
|
+
i = i + 1
|
|
475
|
+
} else {
|
|
476
|
+
if (i + 4 > n) { return "" }
|
|
477
|
+
if (codec_is_digit(s[i + 1]) == 0) { return "" }
|
|
478
|
+
if (codec_is_digit(s[i + 2]) == 0) { return "" }
|
|
479
|
+
int count = (c - '0') * 100 + (s[i + 1] - '0') * 10 + (s[i + 2] - '0')
|
|
480
|
+
if (count < 1) { return "" }
|
|
481
|
+
string unit = substr(s, i + 3, 1)
|
|
482
|
+
int k = 0
|
|
483
|
+
while (k < count) {
|
|
484
|
+
out = out + unit
|
|
485
|
+
k = k + 1
|
|
486
|
+
}
|
|
487
|
+
i = i + 4
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return out
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// 字母/数字 -> 摩尔斯码: 字符间单空格, 单词间 "/"; 不支持的字节跳过; 空串返回空串
|
|
494
|
+
function codec_morse_encode(string s) -> string {
|
|
495
|
+
string out = ""
|
|
496
|
+
int n = strlen(s)
|
|
497
|
+
int last_word = 0
|
|
498
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
499
|
+
int c = s[i]
|
|
500
|
+
if (c >= 'a' && c <= 'z') { c = c - 32 }
|
|
501
|
+
string code = codec_morse_of(c)
|
|
502
|
+
if (strlen(code) > 0) {
|
|
503
|
+
if (strlen(out) > 0) { out = out + " " }
|
|
504
|
+
out = out + code
|
|
505
|
+
last_word = 1
|
|
506
|
+
} else if (c == ' ') {
|
|
507
|
+
if (last_word == 1) {
|
|
508
|
+
out = out + " /"
|
|
509
|
+
last_word = 0
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
return out
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// 摩尔斯码 -> 大写字母/数字: 未知码字跳过, 无有效码字时返回 ""
|
|
517
|
+
function codec_morse_decode(string m) -> string {
|
|
518
|
+
int n = strlen(m)
|
|
519
|
+
if (n == 0) { return "" }
|
|
520
|
+
string out = ""
|
|
521
|
+
int i = 0
|
|
522
|
+
int last_word = 0
|
|
523
|
+
while (i < n) {
|
|
524
|
+
if (m[i] == ' ') {
|
|
525
|
+
i = i + 1
|
|
526
|
+
continue
|
|
527
|
+
}
|
|
528
|
+
if (m[i] == '/') {
|
|
529
|
+
if (strlen(out) > 0 && last_word == 1) {
|
|
530
|
+
out = out + " "
|
|
531
|
+
last_word = 0
|
|
532
|
+
}
|
|
533
|
+
i = i + 1
|
|
534
|
+
continue
|
|
535
|
+
}
|
|
536
|
+
int start = i
|
|
537
|
+
while (i < n) {
|
|
538
|
+
if (m[i] == ' ' || m[i] == '/') { break }
|
|
539
|
+
i = i + 1
|
|
540
|
+
}
|
|
541
|
+
int ch = codec_morse_char(substr(m, start, i - start))
|
|
542
|
+
if (ch != 0) {
|
|
543
|
+
out = out + codec_chr_of(ch)
|
|
544
|
+
last_word = 1
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
return out
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// 按字节反转字符串; 空串返回空串 (多字节 UTF-8 字符会被拆散)
|
|
551
|
+
function codec_reverse_bytes(string s) -> string {
|
|
552
|
+
int n = strlen(s)
|
|
553
|
+
string out = ""
|
|
554
|
+
int i = n
|
|
555
|
+
while (i >= codec_buf_cap) {
|
|
556
|
+
out = out + codec_rebuild_range(s, i - codec_buf_cap, i)
|
|
557
|
+
i = i - codec_buf_cap
|
|
558
|
+
}
|
|
559
|
+
if (i > 0) {
|
|
560
|
+
out = out + codec_rebuild_range(s, 0, i)
|
|
561
|
+
}
|
|
562
|
+
return out
|
|
563
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "combin"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: 数论与组合数学"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/combin.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "combin"]
|