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,405 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 单行 CSV 解析与生成 (csv)
|
|
2
|
+
// 用法: import "csv.cin"
|
|
3
|
+
//
|
|
4
|
+
// 纯 CIN 实现, 只使用非宿主能力内建 (strlen / substr / strcmp / indexof /
|
|
5
|
+
// int_to_str / trim / atoi), 不依赖任何宿主能力, 解释器与 Go 原生 VM 行为一致。
|
|
6
|
+
// 风格与 json.cin 相同: 面向"一行一条记录"的轻量场景, 不做完整语法解析。
|
|
7
|
+
//
|
|
8
|
+
// ===== 限制 (重要) =====
|
|
9
|
+
// * 本库是**单行解析器**: 以 ',' 为唯一分隔符, 不支持多行字段;
|
|
10
|
+
// 引号内的换行 (RFC 4180 的多行记录) 不会被识别, 换行只会被当作普通字节;
|
|
11
|
+
// * 分隔符固定为 ',' (44), 需要别的分隔符请用 csv_raw_count / csv_raw_get
|
|
12
|
+
// (按单字节分隔符切分, 不做引号处理);
|
|
13
|
+
// * 字段转义规则与 RFC 4180 一致: 字段两端加双引号, 字段内部的 " 写成 "";
|
|
14
|
+
// * 所有长度都是**字节**语义 (strlen / substr / s[i]), UTF-8 等非 ASCII 按字节处理,
|
|
15
|
+
// 但字节级处理不影响引号/逗号判定 (',' 与 '"' 都是单字节 ASCII, 不会出现在
|
|
16
|
+
// UTF-8 多字节序列内部);
|
|
17
|
+
// * 字符串拼接产生**不回收**的堆块, 不要在无界循环里反复拼接。
|
|
18
|
+
//
|
|
19
|
+
// ===== 全局状态 (全局数据段) =====
|
|
20
|
+
// csv_buf : 行缓冲 (string 指针 1 槽), 由 csv_line_start / csv_line_add 维护
|
|
21
|
+
// csv_buf_n : 已累积的字段个数 (1 槽)
|
|
22
|
+
// 合计 2 槽 = 16 字节; 缓冲内容在堆上, 上限 CSV_BUF_MAX = 1024 字节
|
|
23
|
+
// (csv_line_add 返回值 0 表示已满, 此时缓冲**不被修改**)。
|
|
24
|
+
//
|
|
25
|
+
// ===== 空行与尾随空字段约定 =====
|
|
26
|
+
// * csv_count("") == 0 (空行 = 没有字段, 便于跳过空记录)
|
|
27
|
+
// * csv_count(",") == 2 (两个空字段)
|
|
28
|
+
// * csv_count("a,") == 2 (尾随空字段保留)
|
|
29
|
+
// * csv_count("a,,b") == 3 (中间空字段保留)
|
|
30
|
+
// * 因此 csv_get 返回 "" 不能区分"越界"与"空字段", 需要区分时先查 csv_has;
|
|
31
|
+
// * 推论: 只累积了一个空字段的行缓冲也是 "" (csv_line_get() 得到 ""),
|
|
32
|
+
// 读回来会被当成 0 个字段; 需要区分时查 csv_line_count()。
|
|
33
|
+
//
|
|
34
|
+
// ===== 局部变量 =====
|
|
35
|
+
// 本库所有局部变量都显式赋初值 (CIN 局部标量不自动清零)。
|
|
36
|
+
|
|
37
|
+
// 全局行缓冲 (由 csv_line_start / csv_line_add 维护)
|
|
38
|
+
string csv_buf = ""
|
|
39
|
+
// 已累积的字段个数
|
|
40
|
+
int csv_buf_n = 0
|
|
41
|
+
|
|
42
|
+
// 全局行缓冲最多能累积的字节数
|
|
43
|
+
function csv_buf_capacity() -> int {
|
|
44
|
+
return 1024
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ---------------- 内部辅助 ----------------
|
|
48
|
+
|
|
49
|
+
// 内部: 跳过 ASCII 空白 (空格 / \t / \r / \n), 返回第一个非空白字节下标
|
|
50
|
+
function csv_skip_ws(string s, int i) -> int {
|
|
51
|
+
int n = strlen(s)
|
|
52
|
+
while (i < n) {
|
|
53
|
+
int c = s[i]
|
|
54
|
+
if (c != 32 && c != 9 && c != 10 && c != 13) { return i }
|
|
55
|
+
i = i + 1
|
|
56
|
+
}
|
|
57
|
+
return i
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 内部: 若字段 (允许前导空白) 是引号字段, 返回开引号下标; 否则返回 -1
|
|
61
|
+
function csv_quote_pos(string s, int start) -> int {
|
|
62
|
+
int n = strlen(s)
|
|
63
|
+
int i = csv_skip_ws(s, start)
|
|
64
|
+
if (i < n && s[i] == 34) { return i }
|
|
65
|
+
return -1
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 内部: 下一个字段的起始下标 (跳过当前字段与其后的 ','); 没有下一个返回 -1
|
|
69
|
+
// 引号字段按 RFC 4180 扫描: "" 视为转义引号, 单个 " 结束字段
|
|
70
|
+
function csv_next_start(string line, int start) -> int {
|
|
71
|
+
int n = strlen(line)
|
|
72
|
+
int i = start
|
|
73
|
+
int qp = csv_quote_pos(line, start)
|
|
74
|
+
if (qp >= 0) {
|
|
75
|
+
i = qp + 1
|
|
76
|
+
int closed = 0
|
|
77
|
+
while (i < n && closed == 0) {
|
|
78
|
+
if (line[i] == 34) {
|
|
79
|
+
if (i + 1 < n && line[i + 1] == 34) {
|
|
80
|
+
i = i + 2
|
|
81
|
+
} else {
|
|
82
|
+
closed = 1
|
|
83
|
+
i = i + 1
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
i = i + 1
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
while (i < n && line[i] != 44) {
|
|
91
|
+
i = i + 1
|
|
92
|
+
}
|
|
93
|
+
if (i >= n) { return -1 }
|
|
94
|
+
return i + 1
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 内部: 解析 line 中从 start 开始的字段值 (去引号并还原 "" -> ")
|
|
98
|
+
// 引号字段未闭合时取到行尾; 引号字段结束引号之后的字符被忽略
|
|
99
|
+
function csv_val_at(string line, int start) -> string {
|
|
100
|
+
int n = strlen(line)
|
|
101
|
+
if (n == 0) { return "" }
|
|
102
|
+
int qp = csv_quote_pos(line, start)
|
|
103
|
+
if (qp < 0) {
|
|
104
|
+
int e = start
|
|
105
|
+
while (e < n && line[e] != 44) {
|
|
106
|
+
e = e + 1
|
|
107
|
+
}
|
|
108
|
+
return substr(line, start, e - start)
|
|
109
|
+
}
|
|
110
|
+
string out = ""
|
|
111
|
+
int i = qp + 1
|
|
112
|
+
int seg = i
|
|
113
|
+
while (i < n) {
|
|
114
|
+
if (line[i] == 34) {
|
|
115
|
+
if (i + 1 < n && line[i + 1] == 34) {
|
|
116
|
+
out = out + substr(line, seg, i - seg)
|
|
117
|
+
out = out + "\""
|
|
118
|
+
i = i + 2
|
|
119
|
+
seg = i
|
|
120
|
+
} else {
|
|
121
|
+
out = out + substr(line, seg, i - seg)
|
|
122
|
+
return out
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
i = i + 1
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
out = out + substr(line, seg, n - seg)
|
|
129
|
+
return out
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ---------------- 字段访问 ----------------
|
|
133
|
+
|
|
134
|
+
// 字段数; 空行 ("") 返回 0; 尾随 ',' 产生的空字段计入
|
|
135
|
+
function csv_count(string line) -> int {
|
|
136
|
+
if (strlen(line) == 0) { return 0 }
|
|
137
|
+
int c = 1
|
|
138
|
+
int i = csv_next_start(line, 0)
|
|
139
|
+
while (i >= 0) {
|
|
140
|
+
c = c + 1
|
|
141
|
+
i = csv_next_start(line, i)
|
|
142
|
+
}
|
|
143
|
+
return c
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 取第 idx 个字段 (0 起, 已去引号并还原转义); 越界或 idx < 0 返回 ""
|
|
147
|
+
function csv_get(string line, int idx) -> string {
|
|
148
|
+
if (idx < 0) { return "" }
|
|
149
|
+
int n = strlen(line)
|
|
150
|
+
if (n == 0) { return "" }
|
|
151
|
+
int i = 0
|
|
152
|
+
int cur = 0
|
|
153
|
+
while (true) {
|
|
154
|
+
if (cur == idx) { return csv_val_at(line, i) }
|
|
155
|
+
i = csv_next_start(line, i)
|
|
156
|
+
if (i < 0) { return "" }
|
|
157
|
+
cur = cur + 1
|
|
158
|
+
}
|
|
159
|
+
return ""
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// idx 是否是一个存在的字段 (存在时即使字段内容为空也返回 1)
|
|
163
|
+
function csv_has(string line, int idx) -> int {
|
|
164
|
+
if (idx < 0) { return 0 }
|
|
165
|
+
if (idx < csv_count(line)) { return 1 }
|
|
166
|
+
return 0
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// ---------------- 数值字段 ----------------
|
|
170
|
+
|
|
171
|
+
// 内部: 去空白后是否为合法十进制整数 (可带 +/- 号, 至少一位数字)
|
|
172
|
+
function csv_is_int(string s) -> int {
|
|
173
|
+
string t = trim(s)
|
|
174
|
+
int n = strlen(t)
|
|
175
|
+
if (n == 0) { return 0 }
|
|
176
|
+
int i = 0
|
|
177
|
+
if (t[0] == 43 || t[0] == 45) { i = 1 }
|
|
178
|
+
if (i >= n) { return 0 }
|
|
179
|
+
while (i < n) {
|
|
180
|
+
if (t[i] < 48 || t[i] > 57) { return 0 }
|
|
181
|
+
i = i + 1
|
|
182
|
+
}
|
|
183
|
+
return 1
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// 整数字段; 越界 / 空 / 非法 (如 "12a") 返回 fallback
|
|
187
|
+
function csv_get_int(string line, int idx, int fallback) -> int {
|
|
188
|
+
string s = csv_get(line, idx)
|
|
189
|
+
if (csv_is_int(s) == 0) { return fallback }
|
|
190
|
+
return atoi(trim(s))
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// 内部: 去空白后是否为合法十进制小数 (可带 +/- 号, 至多一个小数点, 至少一位数字)
|
|
194
|
+
// 不接受指数形式 (1e3 / 1.5E2); 小数点两侧允许缺数字, 因此 ".5" 与 "5." 都算合法,
|
|
195
|
+
// 单独的 "." / "+" / "" 不合法
|
|
196
|
+
function csv_is_float(string s) -> int {
|
|
197
|
+
string t = trim(s)
|
|
198
|
+
int n = strlen(t)
|
|
199
|
+
if (n == 0) { return 0 }
|
|
200
|
+
int i = 0
|
|
201
|
+
if (t[0] == 43 || t[0] == 45) { i = 1 }
|
|
202
|
+
int digits = 0
|
|
203
|
+
int dots = 0
|
|
204
|
+
while (i < n) {
|
|
205
|
+
int c = t[i]
|
|
206
|
+
if (c >= 48 && c <= 57) {
|
|
207
|
+
digits = digits + 1
|
|
208
|
+
} else if (c == 46) {
|
|
209
|
+
if (dots == 1) { return 0 }
|
|
210
|
+
dots = dots + 1
|
|
211
|
+
} else {
|
|
212
|
+
return 0
|
|
213
|
+
}
|
|
214
|
+
i = i + 1
|
|
215
|
+
}
|
|
216
|
+
if (digits == 0) { return 0 }
|
|
217
|
+
return 1
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// 内部: 解析可选正负号 + 整数部分 + 可选小数部分 (不处理指数)
|
|
221
|
+
function csv_parse_float(string s) -> float {
|
|
222
|
+
string t = trim(s)
|
|
223
|
+
int n = strlen(t)
|
|
224
|
+
int i = 0
|
|
225
|
+
int neg = 0
|
|
226
|
+
if (i < n && (t[i] == 43 || t[i] == 45)) {
|
|
227
|
+
if (t[i] == 45) { neg = 1 }
|
|
228
|
+
i = i + 1
|
|
229
|
+
}
|
|
230
|
+
float v = 0.0
|
|
231
|
+
while (i < n && t[i] >= 48 && t[i] <= 57) {
|
|
232
|
+
v = v * 10.0 + (t[i] - 48)
|
|
233
|
+
i = i + 1
|
|
234
|
+
}
|
|
235
|
+
if (i < n && t[i] == 46) {
|
|
236
|
+
i = i + 1
|
|
237
|
+
float scale = 0.1
|
|
238
|
+
while (i < n && t[i] >= 48 && t[i] <= 57) {
|
|
239
|
+
v = v + (t[i] - 48) * scale
|
|
240
|
+
scale = scale * 0.1
|
|
241
|
+
i = i + 1
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (neg == 1) { return -v }
|
|
245
|
+
return v
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// 浮点字段; 越界 / 空 / 非法返回 fallback
|
|
249
|
+
function csv_get_float(string line, int idx, float fallback) -> float {
|
|
250
|
+
string s = csv_get(line, idx)
|
|
251
|
+
if (csv_is_float(s) == 0) { return fallback }
|
|
252
|
+
return csv_parse_float(s)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// ---------------- 清洗 ----------------
|
|
256
|
+
|
|
257
|
+
// 内部: 去掉一个字段原始文本两侧的空白; 引号字段保留引号内的原始内容
|
|
258
|
+
function csv_cell_trim(string raw) -> string {
|
|
259
|
+
int n = strlen(raw)
|
|
260
|
+
int qp = csv_quote_pos(raw, 0)
|
|
261
|
+
if (qp < 0) { return trim(raw) }
|
|
262
|
+
int i = qp + 1
|
|
263
|
+
int closed = -1
|
|
264
|
+
while (i < n) {
|
|
265
|
+
if (raw[i] == 34) {
|
|
266
|
+
if (i + 1 < n && raw[i + 1] == 34) {
|
|
267
|
+
i = i + 2
|
|
268
|
+
} else {
|
|
269
|
+
closed = i
|
|
270
|
+
i = n
|
|
271
|
+
}
|
|
272
|
+
} else {
|
|
273
|
+
i = i + 1
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (closed >= 0) { return substr(raw, qp, closed - qp + 1) }
|
|
277
|
+
return substr(raw, qp, n - qp)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// 去掉每个字段两侧的空白并原样拼接 (不重新加引号, 引号字段内部不动)
|
|
281
|
+
// csv_trim_cells(" a , b ") -> "a,b"
|
|
282
|
+
// csv_trim_cells(" \"a, b\" ") -> "\"a, b\"" (引号保留, 逗号仍在字段内)
|
|
283
|
+
// 空行返回 ""; 结果长度不会超过输入长度
|
|
284
|
+
function csv_trim_cells(string line) -> string {
|
|
285
|
+
int n = strlen(line)
|
|
286
|
+
if (n == 0) { return "" }
|
|
287
|
+
string out = ""
|
|
288
|
+
int i = 0
|
|
289
|
+
int first = 1
|
|
290
|
+
while (true) {
|
|
291
|
+
int nxt = csv_next_start(line, i)
|
|
292
|
+
int end = n
|
|
293
|
+
if (nxt >= 0) { end = nxt - 1 }
|
|
294
|
+
string cell = csv_cell_trim(substr(line, i, end - i))
|
|
295
|
+
if (first == 0) { out = out + "," }
|
|
296
|
+
out = out + cell
|
|
297
|
+
first = 0
|
|
298
|
+
if (nxt < 0) { break }
|
|
299
|
+
i = nxt
|
|
300
|
+
}
|
|
301
|
+
return out
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// ---------------- 生成 ----------------
|
|
305
|
+
|
|
306
|
+
// 只做转义: 字段内部的每个 " 写成 "" (不加外层引号)
|
|
307
|
+
// csv_escape("a\"b") -> "a\"\"b"
|
|
308
|
+
function csv_escape(string field) -> string {
|
|
309
|
+
int n = strlen(field)
|
|
310
|
+
string out = ""
|
|
311
|
+
int seg = 0
|
|
312
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
313
|
+
if (field[i] == 34) {
|
|
314
|
+
out = out + substr(field, seg, i - seg)
|
|
315
|
+
out = out + "\"\""
|
|
316
|
+
seg = i + 1
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
out = out + substr(field, seg, n - seg)
|
|
320
|
+
return out
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// 需要时加双引号并转义内部 " (否则原样返回)
|
|
324
|
+
// 需要加引号的条件: 含 ',' 或 '"' 或换行, 或首尾是空白 (空格 / \t / \r / \n)
|
|
325
|
+
// 空串返回 ""
|
|
326
|
+
// csv_quote("a,b") -> "\"a,b\""; csv_quote("ab") -> "ab"
|
|
327
|
+
function csv_quote(string field) -> string {
|
|
328
|
+
int n = strlen(field)
|
|
329
|
+
if (n == 0) { return "" }
|
|
330
|
+
int need = 0
|
|
331
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
332
|
+
int c = field[i]
|
|
333
|
+
if (c == 44 || c == 34 || c == 10 || c == 13) { need = 1 }
|
|
334
|
+
}
|
|
335
|
+
if (field[0] == 32 || field[0] == 9) { need = 1 }
|
|
336
|
+
if (field[n - 1] == 32 || field[n - 1] == 9) { need = 1 }
|
|
337
|
+
if (need == 0) { return field }
|
|
338
|
+
return "\"" + csv_escape(field) + "\""
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// 开始新的一行 (清空全局缓冲, 字段计数归零)
|
|
342
|
+
function csv_line_start() -> void {
|
|
343
|
+
csv_buf = ""
|
|
344
|
+
csv_buf_n = 0
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// 追加一个字段 (自动加前导分隔符并按需加引号)
|
|
348
|
+
// 成功返回 1; 若追加后缓冲会超过 CSV_BUF_MAX = 1024 字节, 返回 0 且**不改动缓冲**
|
|
349
|
+
function csv_line_add(string field) -> int {
|
|
350
|
+
string enc = csv_quote(field)
|
|
351
|
+
string piece = enc
|
|
352
|
+
if (csv_buf_n > 0) {
|
|
353
|
+
piece = "," + enc
|
|
354
|
+
}
|
|
355
|
+
if (strlen(csv_buf) + strlen(piece) > 1024) { return 0 }
|
|
356
|
+
csv_buf = csv_buf + piece
|
|
357
|
+
csv_buf_n = csv_buf_n + 1
|
|
358
|
+
return 1
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// 取当前累积的行 (未调用 csv_line_start 前为空串)
|
|
362
|
+
function csv_line_get() -> string {
|
|
363
|
+
return csv_buf
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// 当前累积的字段个数 (区分"0 个字段"与"一个空字段"时使用)
|
|
367
|
+
function csv_line_count() -> int {
|
|
368
|
+
return csv_buf_n
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// ---------------- 自定义分隔符 (不做引号处理) ----------------
|
|
372
|
+
|
|
373
|
+
// 按单字节分隔符 delim 切分的字段数; line 为空返回 0; delim <= 0 时整行算 1 个字段
|
|
374
|
+
function csv_raw_count(string line, int delim) -> int {
|
|
375
|
+
int n = strlen(line)
|
|
376
|
+
if (n == 0) { return 0 }
|
|
377
|
+
int c = 1
|
|
378
|
+
if (delim <= 0) { return c }
|
|
379
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
380
|
+
if (line[i] == delim) { c = c + 1 }
|
|
381
|
+
}
|
|
382
|
+
return c
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// 按单字节分隔符 delim 取第 idx 个字段 (0 起, 原样返回, 不做引号处理)
|
|
386
|
+
// 越界或 idx < 0 返回 ""; delim <= 0 时只有 idx == 0 命中整行
|
|
387
|
+
function csv_raw_get(string line, int delim, int idx) -> string {
|
|
388
|
+
if (idx < 0) { return "" }
|
|
389
|
+
int n = strlen(line)
|
|
390
|
+
if (n == 0) { return "" }
|
|
391
|
+
if (delim <= 0) {
|
|
392
|
+
if (idx == 0) { return line }
|
|
393
|
+
return ""
|
|
394
|
+
}
|
|
395
|
+
int cur = 0
|
|
396
|
+
int start = 0
|
|
397
|
+
for (int i = 0; i <= n; i = i + 1) {
|
|
398
|
+
if (i == n || line[i] == delim) {
|
|
399
|
+
if (cur == idx) { return substr(line, start, i - start) }
|
|
400
|
+
cur = cur + 1
|
|
401
|
+
start = i + 1
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return ""
|
|
405
|
+
}
|
codecin/lib/dp/pic.toml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "dp"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: 经典动态规划"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/dp.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "dp"]
|