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,155 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 方阵运算 (matrix)
|
|
2
|
+
// 用法: import "matrix.cin"
|
|
3
|
+
//
|
|
4
|
+
// 矩阵以**一维数组**行主序存放 (m[i*n + j] 表示第 i 行第 j 列),
|
|
5
|
+
// 因此可直接用 CIN 的 int[] 与固定数组, 无需动态内存。
|
|
6
|
+
// 所有 "_to" 形式把结果写入调用方提供的输出数组。
|
|
7
|
+
|
|
8
|
+
// 置零 / 单位阵
|
|
9
|
+
function mat_zero(int[] m, int n) -> void {
|
|
10
|
+
for (int i = 0; i < n * n; i = i + 1) {
|
|
11
|
+
m[i] = 0
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function mat_identity(int[] m, int n) -> void {
|
|
16
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
17
|
+
for (int j = 0; j < n; j = j + 1) {
|
|
18
|
+
if (i == j) {
|
|
19
|
+
m[i * n + j] = 1
|
|
20
|
+
} else {
|
|
21
|
+
m[i * n + j] = 0
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 逐元素取值
|
|
28
|
+
function mat_get(int[] m, int n, int i, int j) -> int {
|
|
29
|
+
if (i < 0 || j < 0 || i >= n || j >= n) { return 0 }
|
|
30
|
+
return m[i * n + j]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function mat_set(int[] m, int n, int i, int j, int v) -> void {
|
|
34
|
+
if (i < 0 || j < 0 || i >= n || j >= n) { return }
|
|
35
|
+
m[i * n + j] = v
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// out = a + b
|
|
39
|
+
function mat_add(int[] a, int[] b, int[] out, int n) -> void {
|
|
40
|
+
for (int i = 0; i < n * n; i = i + 1) {
|
|
41
|
+
out[i] = a[i] + b[i]
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// out = a - b
|
|
46
|
+
function mat_sub(int[] a, int[] b, int[] out, int n) -> void {
|
|
47
|
+
for (int i = 0; i < n * n; i = i + 1) {
|
|
48
|
+
out[i] = a[i] - b[i]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// out = a * k
|
|
53
|
+
function mat_scale(int[] a, int k, int[] out, int n) -> void {
|
|
54
|
+
for (int i = 0; i < n * n; i = i + 1) {
|
|
55
|
+
out[i] = a[i] * k
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// out = a × b (经典三重循环; out 不得与 a/b 为同一数组)
|
|
60
|
+
function mat_mul(int[] a, int[] b, int[] out, int n) -> void {
|
|
61
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
62
|
+
for (int j = 0; j < n; j = j + 1) {
|
|
63
|
+
int s = 0
|
|
64
|
+
for (int k = 0; k < n; k = k + 1) {
|
|
65
|
+
s = s + a[i * n + k] * b[k * n + j]
|
|
66
|
+
}
|
|
67
|
+
out[i * n + j] = s
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// out = a^T
|
|
73
|
+
function mat_transpose(int[] a, int[] out, int n) -> void {
|
|
74
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
75
|
+
for (int j = 0; j < n; j = j + 1) {
|
|
76
|
+
out[j * n + i] = a[i * n + j]
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// 迹 (对角线之和)
|
|
82
|
+
function mat_trace(int[] a, int n) -> int {
|
|
83
|
+
int s = 0
|
|
84
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
85
|
+
s = s + a[i * n + i]
|
|
86
|
+
}
|
|
87
|
+
return s
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 所有元素之和
|
|
91
|
+
function mat_sum(int[] a, int n) -> int {
|
|
92
|
+
int s = 0
|
|
93
|
+
for (int i = 0; i < n * n; i = i + 1) {
|
|
94
|
+
s = s + a[i]
|
|
95
|
+
}
|
|
96
|
+
return s
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 是否相等
|
|
100
|
+
function mat_equals(int[] a, int[] b, int n) -> int {
|
|
101
|
+
for (int i = 0; i < n * n; i = i + 1) {
|
|
102
|
+
if (a[i] != b[i]) { return 0 }
|
|
103
|
+
}
|
|
104
|
+
return 1
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 是否为对称矩阵
|
|
108
|
+
function mat_is_symmetric(int[] a, int n) -> int {
|
|
109
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
110
|
+
for (int j = i + 1; j < n; j = j + 1) {
|
|
111
|
+
if (a[i * n + j] != a[j * n + i]) { return 0 }
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return 1
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 行列式 (拉普拉斯展开, 递归; 适合 n <= 6 的小矩阵)
|
|
118
|
+
function mat_det(int[] a, int n) -> int {
|
|
119
|
+
if (n <= 0) { return 0 }
|
|
120
|
+
if (n == 1) { return a[0] }
|
|
121
|
+
if (n == 2) { return a[0] * a[3] - a[1] * a[2] }
|
|
122
|
+
int minor[36] // (n-1)^2 <= 25 (n <= 6)
|
|
123
|
+
int det = 0
|
|
124
|
+
int sign = 1
|
|
125
|
+
for (int j = 0; j < n; j = j + 1) {
|
|
126
|
+
int k = 0
|
|
127
|
+
for (int r = 1; r < n; r = r + 1) {
|
|
128
|
+
for (int c = 0; c < n; c = c + 1) {
|
|
129
|
+
if (c == j) { continue }
|
|
130
|
+
minor[k] = a[r * n + c]
|
|
131
|
+
k = k + 1
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
int sub = mat_det(minor, n - 1)
|
|
135
|
+
if (sign == 1) {
|
|
136
|
+
det = det + a[j] * sub
|
|
137
|
+
} else {
|
|
138
|
+
det = det - a[j] * sub
|
|
139
|
+
}
|
|
140
|
+
sign = 0 - sign
|
|
141
|
+
}
|
|
142
|
+
return det
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// 打印 (调试用, 每行一个方括号)
|
|
146
|
+
function mat_print(int[] a, int n) -> void {
|
|
147
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
148
|
+
string line = "["
|
|
149
|
+
for (int j = 0; j < n; j = j + 1) {
|
|
150
|
+
line = line + int_to_str(a[i * n + j])
|
|
151
|
+
if (j < n - 1) { line = line + " " }
|
|
152
|
+
}
|
|
153
|
+
println(line + "]")
|
|
154
|
+
}
|
|
155
|
+
}
|
codecin/lib/net/pic.toml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "net"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: 网络便捷封装"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/net.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "net"]
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 网络便捷封装 (net)
|
|
2
|
+
// 用法: import "net.cin"
|
|
3
|
+
// 依赖: 原生运行时 (http_req / http_code / tcp_* / udp_* / dns_lookup,
|
|
4
|
+
// SYS 145-157, Go 引擎实现); `--sandbox` 下宿主调用会被拦截。
|
|
5
|
+
//
|
|
6
|
+
// 底层内建 (int fd 是套接字句柄, 0/-1 表示失败; 失败不抛异常只返回 -1):
|
|
7
|
+
// http_req(method, url, headers, body) -> 响应体字符串
|
|
8
|
+
// http_code() -> 最近一次 HTTP 状态码 (从未请求过为 -1)
|
|
9
|
+
// tcp_dial(host, port) / tcp_send(fd, buf, n) / tcp_recv(fd, buf, max)
|
|
10
|
+
// tcp_close(fd) / tcp_listen(port) / tcp_accept(lfd)
|
|
11
|
+
// udp_open(port) / udp_sendto(fd, host, port, buf, n)
|
|
12
|
+
// udp_recvfrom(fd, buf, max, srcbuf) / udp_close(fd)
|
|
13
|
+
// dns_lookup(host) -> IP 字符串 (失败空串)
|
|
14
|
+
//
|
|
15
|
+
// 本库在其上提供:
|
|
16
|
+
// * HTTP: 带自定义头的 GET/POST、状态码便捷判断;
|
|
17
|
+
// * TCP: 发送字符串、按行接收 (文本协议友好)、echo 客户端一步到位;
|
|
18
|
+
// * UDP: 字符串收发封装 (主机名自动解析);
|
|
19
|
+
// * DNS: 解析 + 反查。
|
|
20
|
+
//
|
|
21
|
+
// 缓冲区约定: CIN 数组每个元素占 8 字节, tcp_recv / udp_recvfrom 把收到的
|
|
22
|
+
// 字节流原样写入缓冲区内存。接收辅助函数接收 `int[] buf` 并按小端字节序
|
|
23
|
+
// 逐字节取值; 调用前无需清零 (辅助函数内部按收到的字节数重组字符串)。
|
|
24
|
+
//
|
|
25
|
+
// 性能提示: 收包 -> 字符串的重组是逐字节拼接 (O(n^2) 堆拷贝),
|
|
26
|
+
// 建议单次接收 <= 4096 字节; 二进制/大流量场景请直接用底层内建处理原始字节。
|
|
27
|
+
//
|
|
28
|
+
// 示例 (TCP echo):
|
|
29
|
+
// import "net.cin"
|
|
30
|
+
// int fd = tcp_dial("127.0.0.1", 7000)
|
|
31
|
+
// tcp_send_str(fd, "hello")
|
|
32
|
+
// int buf[512]
|
|
33
|
+
// print(tcp_recv_line(fd, buf, 4096))
|
|
34
|
+
// tcp_close(fd)
|
|
35
|
+
|
|
36
|
+
// 逐字节重组字符串用的 255 字节字母表: net_bytes[i-1] == 字节值 i (1..255)。
|
|
37
|
+
// 字节 0 不是合法字符串内容 (CIN string 是 NUL 结尾的), 重组时遇 0 即截断。
|
|
38
|
+
string net_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"
|
|
39
|
+
|
|
40
|
+
// ---------------- HTTP ----------------
|
|
41
|
+
|
|
42
|
+
// GET (可带自定义头)。headers 为 "Key: Value\nKey2: Value2" 形式, 空串表示无。
|
|
43
|
+
function http_get_headers(string url, string headers) -> string {
|
|
44
|
+
return http_req("GET", url, headers, "")
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// POST (可带自定义头)。调用方自行提供 Content-Type 等头。
|
|
48
|
+
function http_post_headers(string url, string body, string headers) -> string {
|
|
49
|
+
return http_req("POST", url, headers, body)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 最近一次 HTTP 请求是否成功 (2xx)。从未请求过返回 0。
|
|
53
|
+
function http_ok() -> int {
|
|
54
|
+
int code = http_code()
|
|
55
|
+
return code >= 200 && code < 300
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ---------------- TCP: 客户端 ----------------
|
|
59
|
+
|
|
60
|
+
// 发送整个字符串 (不含结尾 NUL), 返回发送字节数 / -1。
|
|
61
|
+
function tcp_send_str(int fd, string s) -> int {
|
|
62
|
+
return tcp_send(fd, s, strlen(s))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 发送一行 (自动追加 "\n"), 文本协议友好。
|
|
66
|
+
function tcp_send_line(int fd, string s) -> int {
|
|
67
|
+
return tcp_send_str(fd, s + "\n")
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 原始接收: 收到的字节写入 buf (调用方自行解码), 返回字节数 / 0 对端关闭 / -1。
|
|
71
|
+
function tcp_recv_bytes(int fd, int[] buf, int max) -> int {
|
|
72
|
+
return tcp_recv(fd, buf, max)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 接收并以字符串返回 (遇 NUL 截断; 建议单次 <= 4096 字节, 见头部性能提示)。
|
|
76
|
+
function tcp_recv_str(int fd, int[] buf, int max) -> string {
|
|
77
|
+
int n = tcp_recv(fd, buf, max)
|
|
78
|
+
if (n <= 0) { return "" }
|
|
79
|
+
string s = ""
|
|
80
|
+
int i = 0
|
|
81
|
+
while (i < n) {
|
|
82
|
+
int b = (buf[i / 8] >> ((i % 8) * 8)) & 0xFF
|
|
83
|
+
if (b == 0) { break }
|
|
84
|
+
if (b > 0) { s = s + substr(net_bytes, b - 1, 1) }
|
|
85
|
+
i = i + 1
|
|
86
|
+
}
|
|
87
|
+
return s
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 接收一行 (到 "\n" 为止, 不含 "\n"; 行尾 "\r" 一并去掉)。
|
|
91
|
+
// 缓冲区里 "\n" 之后的内容会被丢弃 —— 需要保留请用 tcp_recv_bytes。
|
|
92
|
+
function tcp_recv_line(int fd, int[] buf, int max) -> string {
|
|
93
|
+
string s = tcp_recv_str(fd, buf, max)
|
|
94
|
+
int pos = indexof(s, "\n")
|
|
95
|
+
if (pos >= 0) {
|
|
96
|
+
s = substr(s, 0, pos)
|
|
97
|
+
}
|
|
98
|
+
int n = strlen(s)
|
|
99
|
+
if (n > 0 && strcmp(substr(s, n - 1, 1), "\r") == 0) {
|
|
100
|
+
s = substr(s, 0, n - 1)
|
|
101
|
+
}
|
|
102
|
+
return s
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 一步到位: 连接 -> 发送 -> 收一行 -> 关闭。失败返回空串。
|
|
106
|
+
function tcp_roundtrip(string host, int port, string message) -> string {
|
|
107
|
+
int fd = tcp_dial(host, port)
|
|
108
|
+
if (fd <= 0) { return "" }
|
|
109
|
+
int buf[512]
|
|
110
|
+
tcp_send_line(fd, message)
|
|
111
|
+
string reply = tcp_recv_line(fd, buf, 4096)
|
|
112
|
+
tcp_close(fd)
|
|
113
|
+
return reply
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ---------------- TCP: 服务端 ----------------
|
|
117
|
+
|
|
118
|
+
// 在端口上监听, 返回监听句柄 (失败 -1); 用 tcp_accept 接受连接。
|
|
119
|
+
function tcp_server(int port) -> int {
|
|
120
|
+
return tcp_listen(port)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 接受一个连接 (阻塞), 返回连接句柄 (失败 -1); 用完后 tcp_close。
|
|
124
|
+
function tcp_server_accept(int lfd) -> int {
|
|
125
|
+
return tcp_accept(lfd)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ---------------- UDP ----------------
|
|
129
|
+
|
|
130
|
+
// 打开 UDP 套接字并绑定端口 (0 = 系统分配), 返回句柄 / -1。
|
|
131
|
+
function udp_bind(int port) -> int {
|
|
132
|
+
return udp_open(port)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// 发送字符串数据报 (host 支持主机名或 IP 字面量), 返回发送字节数 / -1。
|
|
136
|
+
function udp_send_str(int fd, string host, int port, string s) -> int {
|
|
137
|
+
return udp_sendto(fd, host, port, s, strlen(s))
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// 原始接收: 字节写入 buf, 源地址 "ip:port" 写入 src (可为 0 忽略), 返回字节数 / -1。
|
|
141
|
+
// src 需是调用方提供的缓冲 (建议 >= 64 字节), 内部写入 NUL 结尾文本。
|
|
142
|
+
function udp_recv_bytes(int fd, int[] buf, int max, int[] src) -> int {
|
|
143
|
+
return udp_recvfrom(fd, buf, max, src)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 接收数据报并以字符串返回 (遇 NUL 截断; 建议单次 <= 4096 字节)。
|
|
147
|
+
function udp_recv_str(int fd, int[] buf, int max) -> string {
|
|
148
|
+
int n = udp_recvfrom(fd, buf, max, 0)
|
|
149
|
+
if (n <= 0) { return "" }
|
|
150
|
+
string s = ""
|
|
151
|
+
int i = 0
|
|
152
|
+
while (i < n) {
|
|
153
|
+
int b = (buf[i / 8] >> ((i % 8) * 8)) & 0xFF
|
|
154
|
+
if (b == 0) { break }
|
|
155
|
+
if (b > 0) { s = s + substr(net_bytes, b - 1, 1) }
|
|
156
|
+
i = i + 1
|
|
157
|
+
}
|
|
158
|
+
return s
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ---------------- DNS ----------------
|
|
162
|
+
|
|
163
|
+
// 解析主机名 -> IP 字符串 (失败空串)。dns_lookup 的语义别名。
|
|
164
|
+
function dns_resolve(string host) -> string {
|
|
165
|
+
return dns_lookup(host)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 主机名是否可解析。
|
|
169
|
+
function dns_ok(string host) -> int {
|
|
170
|
+
return dns_lookup(host) != ""
|
|
171
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "path"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: 纯字符串路径工具"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/path.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "path"]
|