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.
Files changed (202) hide show
  1. codecin/__init__.py +27 -0
  2. codecin/aot.py +1057 -0
  3. codecin/assembler.py +619 -0
  4. codecin/cbackend.py +3024 -0
  5. codecin/cin.py +4265 -0
  6. codecin/cli.py +670 -0
  7. codecin/codecin_native.dll +0 -0
  8. codecin/config.py +50 -0
  9. codecin/console.py +183 -0
  10. codecin/cpu.py +312 -0
  11. codecin/crom.py +449 -0
  12. codecin/cvm.py +708 -0
  13. codecin/disasm.py +149 -0
  14. codecin/environment.py +176 -0
  15. codecin/errors.py +54 -0
  16. codecin/isa.py +560 -0
  17. codecin/lib/array/pic.toml +10 -0
  18. codecin/lib/array/src/array.cin +103 -0
  19. codecin/lib/bigint/pic.toml +10 -0
  20. codecin/lib/bigint/src/bigint.cin +589 -0
  21. codecin/lib/bits/pic.toml +10 -0
  22. codecin/lib/bits/src/bits.cin +143 -0
  23. codecin/lib/bitset/pic.toml +10 -0
  24. codecin/lib/bitset/src/bitset.cin +374 -0
  25. codecin/lib/codec/pic.toml +10 -0
  26. codecin/lib/codec/src/codec.cin +563 -0
  27. codecin/lib/combin/pic.toml +10 -0
  28. codecin/lib/combin/src/combin.cin +414 -0
  29. codecin/lib/conv/pic.toml +10 -0
  30. codecin/lib/conv/src/conv.cin +151 -0
  31. codecin/lib/cppstd/pic.toml +10 -0
  32. codecin/lib/cppstd/src/cppstd.cin +418 -0
  33. codecin/lib/cstd/pic.toml +10 -0
  34. codecin/lib/cstd/src/cstd.cin +424 -0
  35. codecin/lib/csv/pic.toml +10 -0
  36. codecin/lib/csv/src/csv.cin +405 -0
  37. codecin/lib/dp/pic.toml +10 -0
  38. codecin/lib/dp/src/dp.cin +325 -0
  39. codecin/lib/ffi/pic.toml +10 -0
  40. codecin/lib/ffi/src/ffi.cin +163 -0
  41. codecin/lib/fmt/pic.toml +10 -0
  42. codecin/lib/fmt/src/fmt.cin +325 -0
  43. codecin/lib/frac/pic.toml +10 -0
  44. codecin/lib/frac/src/frac.cin +252 -0
  45. codecin/lib/gostd/pic.toml +10 -0
  46. codecin/lib/gostd/src/gostd.cin +415 -0
  47. codecin/lib/graph/pic.toml +10 -0
  48. codecin/lib/graph/src/graph.cin +367 -0
  49. codecin/lib/gui/pic.toml +10 -0
  50. codecin/lib/gui/src/gui.cin +218 -0
  51. codecin/lib/hash/pic.toml +10 -0
  52. codecin/lib/hash/src/hash.cin +67 -0
  53. codecin/lib/heap/pic.toml +10 -0
  54. codecin/lib/heap/src/heap.cin +199 -0
  55. codecin/lib/io/pic.toml +10 -0
  56. codecin/lib/io/src/io.cin +141 -0
  57. codecin/lib/json/pic.toml +10 -0
  58. codecin/lib/json/src/json.cin +84 -0
  59. codecin/lib/key/pic.toml +10 -0
  60. codecin/lib/key/src/key.cin +78 -0
  61. codecin/lib/math/pic.toml +10 -0
  62. codecin/lib/math/src/math.cin +50 -0
  63. codecin/lib/matrix/pic.toml +10 -0
  64. codecin/lib/matrix/src/matrix.cin +155 -0
  65. codecin/lib/net/pic.toml +10 -0
  66. codecin/lib/net/src/net.cin +171 -0
  67. codecin/lib/path/pic.toml +10 -0
  68. codecin/lib/path/src/path.cin +372 -0
  69. codecin/lib/queue/pic.toml +10 -0
  70. codecin/lib/queue/src/queue.cin +112 -0
  71. codecin/lib/rand/pic.toml +10 -0
  72. codecin/lib/rand/src/rand.cin +46 -0
  73. codecin/lib/set/pic.toml +10 -0
  74. codecin/lib/set/src/set.cin +269 -0
  75. codecin/lib/sort/pic.toml +10 -0
  76. codecin/lib/sort/src/sort.cin +93 -0
  77. codecin/lib/stat/pic.toml +10 -0
  78. codecin/lib/stat/src/stat.cin +138 -0
  79. codecin/lib/str/pic.toml +10 -0
  80. codecin/lib/str/src/str.cin +54 -0
  81. codecin/lib/termux/pic.toml +10 -0
  82. codecin/lib/termux/src/termux.cin +118 -0
  83. codecin/lib/test/pic.toml +10 -0
  84. codecin/lib/test/src/test.cin +74 -0
  85. codecin/lib/text/pic.toml +10 -0
  86. codecin/lib/text/src/text.cin +323 -0
  87. codecin/lib/time/pic.toml +10 -0
  88. codecin/lib/time/src/time.cin +54 -0
  89. codecin/lib/token/pic.toml +10 -0
  90. codecin/lib/token/src/token.cin +183 -0
  91. codecin/lib/tree/pic.toml +10 -0
  92. codecin/lib/tree/src/tree.cin +256 -0
  93. codecin/lib/unionfind/pic.toml +10 -0
  94. codecin/lib/unionfind/src/unionfind.cin +115 -0
  95. codecin/lib/validate/pic.toml +10 -0
  96. codecin/lib/validate/src/validate.cin +148 -0
  97. codecin/lib/vec/pic.toml +10 -0
  98. codecin/lib/vec/src/vec.cin +88 -0
  99. codecin/logger.py +145 -0
  100. codecin/memory.py +306 -0
  101. codecin/mkn.py +916 -0
  102. codecin/native/aot/aot.go +114 -0
  103. codecin/native/aot/stub_main.go.txt +23 -0
  104. codecin/native/build.ps1 +33 -0
  105. codecin/native/build.sh +48 -0
  106. codecin/native/cmd/cincc/main.go +66 -0
  107. codecin/native/compiler/codegen.go +2238 -0
  108. codecin/native/compiler/compile_test.go +1069 -0
  109. codecin/native/compiler/parser.go +1331 -0
  110. codecin/native/compiler/syscalls.go +166 -0
  111. codecin/native/compiler/tokenizer.go +394 -0
  112. codecin/native/compiler/types.go +217 -0
  113. codecin/native/engine/args.go +37 -0
  114. codecin/native/engine/audio.go +402 -0
  115. codecin/native/engine/audio_other.go +105 -0
  116. codecin/native/engine/audio_test.go +279 -0
  117. codecin/native/engine/audio_windows.go +190 -0
  118. codecin/native/engine/canvas.go +271 -0
  119. codecin/native/engine/console_other.go +9 -0
  120. codecin/native/engine/console_windows.go +43 -0
  121. codecin/native/engine/crom.go +86 -0
  122. codecin/native/engine/encode.go +93 -0
  123. codecin/native/engine/engine_test.go +198 -0
  124. codecin/native/engine/ffi_nocgo.go +20 -0
  125. codecin/native/engine/ffi_registry.go +69 -0
  126. codecin/native/engine/ffi_unix.go +109 -0
  127. codecin/native/engine/ffi_windows.go +117 -0
  128. codecin/native/engine/gui.go +209 -0
  129. codecin/native/engine/gui_stub.go +13 -0
  130. codecin/native/engine/gui_test.go +148 -0
  131. codecin/native/engine/gui_windows.go +604 -0
  132. codecin/native/engine/gui_x11.go +723 -0
  133. codecin/native/engine/host_test.go +139 -0
  134. codecin/native/engine/hostcrypto.go +30 -0
  135. codecin/native/engine/hostdesktop.go +173 -0
  136. codecin/native/engine/hostnet.go +127 -0
  137. codecin/native/engine/isa_gen.go +689 -0
  138. codecin/native/engine/keyboard.go +432 -0
  139. codecin/native/engine/keyboard_other.go +94 -0
  140. codecin/native/engine/keyboard_stub.go +17 -0
  141. codecin/native/engine/keyboard_termios_darwin.go +9 -0
  142. codecin/native/engine/keyboard_termios_linux.go +9 -0
  143. codecin/native/engine/keyboard_test.go +246 -0
  144. codecin/native/engine/keyboard_windows.go +208 -0
  145. codecin/native/engine/keyboard_windows_test.go +123 -0
  146. codecin/native/engine/lineedit.go +262 -0
  147. codecin/native/engine/lineedit_test.go +105 -0
  148. codecin/native/engine/lineint_unix.go +17 -0
  149. codecin/native/engine/lineint_windows.go +19 -0
  150. codecin/native/engine/mem_other.go +37 -0
  151. codecin/native/engine/mem_windows.go +33 -0
  152. codecin/native/engine/native.go +640 -0
  153. codecin/native/engine/netsock.go +321 -0
  154. codecin/native/engine/ops_ext.go +515 -0
  155. codecin/native/engine/stdin.go +72 -0
  156. codecin/native/engine/stdin_other.go +9 -0
  157. codecin/native/engine/stdin_windows.go +60 -0
  158. codecin/native/engine/system.go +313 -0
  159. codecin/native/engine/termux.go +180 -0
  160. codecin/native/engine/version_gen.go +7 -0
  161. codecin/native/engine/vkb_darwin.go +62 -0
  162. codecin/native/engine/vkb_linux.go +95 -0
  163. codecin/native/engine/vkb_stub.go +11 -0
  164. codecin/native/engine/vm.go +1677 -0
  165. codecin/native/go.mod +3 -0
  166. codecin/native/ir/ir.go +48 -0
  167. codecin/native/main.go +448 -0
  168. codecin/native/mc/aarch64.go +165 -0
  169. codecin/native/mc/aarch64_test.go +30 -0
  170. codecin/native/mc/elf.go +88 -0
  171. codecin/native/mc/print_test.go +52 -0
  172. codecin/native/mc/x86_64.go +1262 -0
  173. codecin/native/mc/x86_64_test.go +102 -0
  174. codecin/native.py +432 -0
  175. codecin/native_codegen.py +987 -0
  176. codecin/nativegen.py +786 -0
  177. codecin/object.py +539 -0
  178. codecin/optimizer.py +236 -0
  179. codecin/packaging/__init__.py +46 -0
  180. codecin/packaging/archive.py +177 -0
  181. codecin/packaging/model.py +519 -0
  182. codecin/packaging/spec.py +193 -0
  183. codecin/packaging/store.py +372 -0
  184. codecin/packaging/toml.py +541 -0
  185. codecin/packaging/vcs.py +215 -0
  186. codecin/pic.py +1917 -0
  187. codecin/pic_api.py +374 -0
  188. codecin/pic_login.py +519 -0
  189. codecin/pic_registry.py +515 -0
  190. codecin/registers.py +130 -0
  191. codecin/sdgcc.py +493 -0
  192. codecin/stats.py +230 -0
  193. codecin/stdlib.py +138 -0
  194. codecin/toolchain.py +396 -0
  195. codecin/toolkit.py +26 -0
  196. codecin/version.py +260 -0
  197. codecin-7.22.10.dist-info/METADATA +1077 -0
  198. codecin-7.22.10.dist-info/RECORD +202 -0
  199. codecin-7.22.10.dist-info/WHEEL +5 -0
  200. codecin-7.22.10.dist-info/entry_points.txt +5 -0
  201. codecin-7.22.10.dist-info/licenses/LICENSE +7 -0
  202. codecin-7.22.10.dist-info/top_level.txt +1 -0
@@ -0,0 +1,372 @@
1
+ // Code CIN 官方标准库: 纯字符串路径工具 (path)
2
+ // 用法: import "path.cin"
3
+ // 前缀: path_
4
+ //
5
+ // 与 io.cin 的 io_join / io_basename / io_dirname 不同, 本库是**纯字符串**实现:
6
+ // 不访问文件系统, 也不调用任何宿主能力内建 (不使用内建 path_join / path_basename /
7
+ // path_dirname / path_abs / cwd / is_dir / file_exists ...)。
8
+ // 只使用非宿主内建: strlen / substr / strcmp / int_to_str。
9
+ //
10
+ // 命名冲突 (重要):
11
+ // path_join / path_basename / path_dirname 是**宿主能力内建名**, 编译器优先分派内建,
12
+ // 因此**同名用户函数永远不会被调用** (无原生库时直接报 "host builtins ... require
13
+ // the native Go runtime")。本库把这三个纯字符串版本命名为
14
+ // path_str_join / path_str_basename / path_str_dirname
15
+ // 其余函数名与任务规格完全一致 (path_sep_posix / path_is_sep / path_ext / path_stem /
16
+ // path_normalize / path_is_absolute / path_trim_sep / path_split_count /
17
+ // path_split_get / path_common_prefix / path_within / path_change_ext)。
18
+ //
19
+ // 分隔符: '/' 与 '\' 等价 (两种都识别), 路径总是按 Unix 风格 ('/') 输出。
20
+ // 容量常量:
21
+ // PATH_MAX_PARTS = 128 规范化 / 分段时可处理的路径分段上限
22
+ // (请用 path_max_parts() 读取)
23
+ // 全局数据段 = path_parts_buf[128] = 1024 字节 (分段解析的暂存区)。
24
+ // 局部固长数组出现在 path_common_prefix / path_within 里 (各 128 个 string 槽),
25
+ // 全部在栈上, 无动态内存分配。
26
+ //
27
+ // 明确不支持 (与真实文件系统语义不同):
28
+ // - UNC 路径 (\\server\share): 前导双反斜杠按普通分隔符处理, 会折叠成单个 '/',
29
+ // 不保留 UNC 语义;
30
+ // - 盘符只做**字符串**识别 (path_is_absolute 认 "C:\.." 形式), 不做大小写折叠,
31
+ // 不校验盘符是否真实存在;
32
+ // - path_normalize 的 ".." 只做字符串层面的折叠 (不访问文件系统, 不解析符号链接,
33
+ // 也不受真实目录结构约束): "a/../b" -> "b", 而超出根部的 ".." 被保留
34
+ // ("/../a" -> "/a" 是因为 "/" 之上无处可退, 相对路径的 "../a" 保持 "../a");
35
+ // - 尾随分隔符在规范化后被丢弃 ("./a/" -> "a");
36
+ // - 空串被规范化为 "."。
37
+
38
+ const int PATH_MAX_PARTS = 128
39
+
40
+ string path_parts_buf[128]
41
+
42
+ // ---------------- 常量与小工具 ----------------
43
+
44
+ // POSIX 分隔符 "/"
45
+ function path_sep_posix() -> string {
46
+ return "/"
47
+ }
48
+
49
+ // Windows 分隔符 "\" (单字节 92)
50
+ function path_sep_win() -> string {
51
+ return "\\"
52
+ }
53
+
54
+ // 该字节编码是否为路径分隔符 ('/' = 47 或 '\' = 92); 是返回 1, 否返回 0
55
+ function path_is_sep(int ch) -> int {
56
+ if (ch == 47 || ch == 92) { return 1 }
57
+ return 0
58
+ }
59
+
60
+ // 规范化时可处理的分段上限 (常量 PATH_MAX_PARTS = 128)
61
+ function path_max_parts() -> int {
62
+ return PATH_MAX_PARTS
63
+ }
64
+
65
+ // 把字符串切成路径分段, 结果写入全局数组 path_parts_buf (不返回值)。
66
+ // 连续分隔符视为一个, 尾随分隔符不产生空段, 返回分段个数 (<= PATH_MAX_PARTS;
67
+ // 超过上限的部分被丢弃)。全局数组尾部总是补 NULL, 便于调用方判断结束。
68
+ function path_parts_into(string p) -> int {
69
+ int n = strlen(p)
70
+ int c = 0
71
+ int i = 0
72
+ while (i < n) {
73
+ if (path_is_sep(p[i]) == 1) {
74
+ i = i + 1
75
+ continue
76
+ }
77
+ int start = i
78
+ while (i < n && path_is_sep(p[i]) == 0) {
79
+ i = i + 1
80
+ }
81
+ if (c < PATH_MAX_PARTS) {
82
+ path_parts_buf[c] = substr(p, start, i - start)
83
+ c = c + 1
84
+ }
85
+ }
86
+ if (c < PATH_MAX_PARTS) { path_parts_buf[c] = "" }
87
+ return c
88
+ }
89
+
90
+ // 路径分段个数: "a/b/c" -> 3, "" / "/" / "///" -> 0, "a" -> 1.
91
+ // 连续分隔符与尾随分隔符都不产生空段 (与 path_split_get 使用同一套分段规则)
92
+ function path_split_count(string p) -> int {
93
+ return path_parts_into(p)
94
+ }
95
+
96
+ // 第 idx 段 (0 起); 越界 (含负数) 返回 ""
97
+ function path_split_get(string p, int idx) -> string {
98
+ if (idx < 0) { return "" }
99
+ int c = path_parts_into(p)
100
+ if (idx >= c) { return "" }
101
+ return path_parts_buf[idx]
102
+ }
103
+
104
+ // ---------------- 取最后一段 / 前缀 / 扩展名 ----------------
105
+
106
+ // 最后一段 (文件名); "" 与 "/" 返回 "", "a/b/" 返回 "b" (折叠尾随分隔符)。
107
+ // 纯字符串版本: 内建 path_basename 是宿主能力, 故本函数名为 path_str_basename
108
+ function path_str_basename(string p) -> string {
109
+ int c = path_parts_into(p)
110
+ if (c == 0) { return "" }
111
+ return path_parts_buf[c - 1]
112
+ }
113
+
114
+ // 去掉最后一段, 结果**不带**尾随分隔符 (root 除外):
115
+ // "/a/b/c.txt" -> "/a/b", "a" -> "", "/a" -> "/", "" -> "".
116
+ // 不做 "." / ".." 折叠 (用 path_normalize 做)。纯字符串版本 (内建 path_dirname 是宿主能力)
117
+ function path_str_dirname(string p) -> string {
118
+ int c = path_parts_into(p)
119
+ if (c == 0) { return "" }
120
+ if (c == 1) {
121
+ if (path_is_absolute(p) == 1) { return "/" }
122
+ return ""
123
+ }
124
+ string out = path_parts_buf[0]
125
+ for (int i = 1; i < c - 1; i = i + 1) {
126
+ out = out + "/" + path_parts_buf[i]
127
+ }
128
+ string head = substr(p, 0, 1)
129
+ if (path_is_sep(head[0]) == 1) { return "/" + out }
130
+ return out
131
+ }
132
+
133
+ // 扩展名 (**含点**): "a/b/c.txt" -> ".txt"。
134
+ // 无扩展名返回 "": 没有点、点在最前 (隐藏文件 ".gitignore")、以及 "." / ".." 都返回 ""。
135
+ // 点在最后 ("a.") 视为"扩展名就是点本身", 返回 "."; 只看最后一段 (目录里的点不算)
136
+ function path_ext(string p) -> string {
137
+ string b = path_str_basename(p)
138
+ int lb = strlen(b)
139
+ if (lb < 1) { return "" }
140
+ if (strcmp(b, ".") == 0 || strcmp(b, "..") == 0) { return "" }
141
+ int dot = -1
142
+ for (int i = 0; i < lb; i = i + 1) {
143
+ if (b[i] == 46) { dot = i }
144
+ }
145
+ if (dot < 1) { return "" }
146
+ return substr(b, dot, lb - dot)
147
+ }
148
+
149
+ // 去掉扩展名的最后一段 (不含目录): "a/b/c.txt" -> "c", ".gitignore" -> ".gitignore",
150
+ // "a." -> "a", "." -> ".", "a/b/" -> "b"
151
+ function path_stem(string p) -> string {
152
+ string b = path_str_basename(p)
153
+ int lb = strlen(b)
154
+ if (lb < 1) { return "" }
155
+ if (strcmp(b, ".") == 0 || strcmp(b, "..") == 0) { return b }
156
+ int dot = -1
157
+ for (int i = 0; i < lb; i = i + 1) {
158
+ if (b[i] == 46) { dot = i }
159
+ }
160
+ if (dot < 1) { return b }
161
+ return substr(b, 0, dot)
162
+ }
163
+
164
+ // ---------------- 拼接 / 规范化 ----------------
165
+
166
+ // 拼接两个路径 (智能分隔符):
167
+ // - a 为空返回 b; b 为空返回 a (都不折叠尾部/前导分隔符);
168
+ // - 两侧都不为空: a 末尾已有分隔符则不重复添加, 否则补一个 '/';
169
+ // - b 是绝对路径 ("/x" 或 "C:\x") 时忽略 a, 直接返回 b;
170
+ // - 两侧都为空返回 ""
171
+ // 纯字符串版本: 内建 path_join 是宿主能力 (用系统分隔符), 故本函数名为 path_str_join
172
+ function path_str_join(string a, string b) -> string {
173
+ if (strlen(a) == 0) { return b }
174
+ if (strlen(b) == 0) { return a }
175
+ if (path_is_absolute(b) == 1) { return b }
176
+ int last = a[strlen(a) - 1]
177
+ if (path_is_sep(last) == 1) { return a + b }
178
+ return a + "/" + b
179
+ }
180
+
181
+ // 字符串层面的路径规范化 (不访问文件系统):
182
+ // - 折叠重复分隔符: "a//b" -> "a/b"
183
+ // - 去掉 "." 段: "a/./b" -> "a/b"
184
+ // - ".." 回退上一个段: "a/b/../c" -> "a/c"; 无处可退时保留 ("../a" -> "../a",
185
+ // 但 "/.." -> "/" 因为根之上没有目录)
186
+ // - 保留前导分隔符 (绝对路径): "//a/b" -> "/a/b"
187
+ // - 丢掉尾随分隔符 (root 除外): "a/b/" -> "a/b"
188
+ // - 空串、"."、"///" 规范化为 "."; 超出 PATH_MAX_PARTS 段的部分被丢弃
189
+ // - 分隔符统一输出为 '/'
190
+ function path_normalize(string p) -> string {
191
+ int absolute = 0
192
+ if (strlen(p) > 0 && path_is_sep(p[0]) == 1) { absolute = 1 }
193
+ int n = strlen(p)
194
+ int i = 0
195
+ if (absolute == 1) { i = 1 }
196
+ int count = 0
197
+ while (i < n) {
198
+ if (path_is_sep(p[i]) == 1) {
199
+ i = i + 1
200
+ continue
201
+ }
202
+ int start = i
203
+ while (i < n && path_is_sep(p[i]) == 0) {
204
+ i = i + 1
205
+ }
206
+ string part = substr(p, start, i - start)
207
+ if (strcmp(part, ".") == 0) {
208
+ continue
209
+ }
210
+ if (strcmp(part, "..") == 0) {
211
+ if (count > 0 && strcmp(path_parts_buf[count - 1], "..") != 0) {
212
+ count = count - 1
213
+ } else if (absolute == 0 && count < PATH_MAX_PARTS) {
214
+ path_parts_buf[count] = part
215
+ count = count + 1
216
+ }
217
+ continue
218
+ }
219
+ if (count < PATH_MAX_PARTS) {
220
+ path_parts_buf[count] = part
221
+ count = count + 1
222
+ }
223
+ }
224
+ if (count == 0) {
225
+ if (absolute == 1) { return "/" }
226
+ return "."
227
+ }
228
+ string out = path_parts_buf[0]
229
+ for (int k = 1; k < count; k = k + 1) {
230
+ out = out + "/" + path_parts_buf[k]
231
+ }
232
+ if (absolute == 1) { return "/" + out }
233
+ return out
234
+ }
235
+
236
+ // 是否绝对路径: 以 '/' 或 '\' 开头 ("/a"、"\\a") 返回 1;
237
+ // 盘符形式 "C:\a"、"C:/a"、"c:" 也返回 1 (第二个字符是 ':' 即算, 不校验字母范围)
238
+ function path_is_absolute(string p) -> int {
239
+ int n = strlen(p)
240
+ if (n == 0) { return 0 }
241
+ if (path_is_sep(p[0]) == 1) { return 1 }
242
+ if (n >= 2 && p[1] == 58) { return 1 }
243
+ return 0
244
+ }
245
+
246
+ // 去掉首尾分隔符: "/a/b/" -> "a/b", "a" -> "a", "/" -> "", "///" -> ""。
247
+ // 中间的重复分隔符不折叠 (用 path_normalize 折叠)
248
+ function path_trim_sep(string p) -> string {
249
+ int n = strlen(p)
250
+ int s = 0
251
+ while (s < n && path_is_sep(p[s]) == 1) {
252
+ s = s + 1
253
+ }
254
+ int e = n
255
+ while (e > s && path_is_sep(p[e - 1]) == 1) {
256
+ e = e - 1
257
+ }
258
+ if (e <= s) { return "" }
259
+ return substr(p, s, e - s)
260
+ }
261
+
262
+ // ---------------- 前缀 / 包含判断 ----------------
263
+
264
+ // 最长公共**分段**前缀 (不会把 "foo" 与 "foobar" 当成公共前缀):
265
+ // path_common_prefix("/a/b/c", "/a/b/d") = "/a/b"
266
+ // path_common_prefix("a/b", "a") = "a"
267
+ // path_common_prefix("x/y", "z/y") = ""
268
+ // path_common_prefix("/a", "/a") = "/a"
269
+ // 绝对路径与相对路径不可能指向同一目录, 因此两者一侧绝对、另一侧相对时返回 ""
270
+ // (即使分段相同: path_common_prefix("/a", "a") = "")。
271
+ // 结果带前导 '/' 仅当前缀是 POSIX 根形式; 盘符路径保留盘符段本身
272
+ // (path_common_prefix("C:\a\b", "C:\a\c") = "C:/a")。
273
+ // 结果不带尾随分隔符; 无公共分段返回 ""
274
+ function path_common_prefix(string a, string b) -> string {
275
+ int absa = path_is_absolute(a)
276
+ if (absa != path_is_absolute(b)) { return "" }
277
+ int posix = 0
278
+ if (absa == 1 && path_is_sep(a[0]) == 1) { posix = 1 }
279
+ int na = path_parts_into(a)
280
+ string sa[PATH_MAX_PARTS]
281
+ for (int i = 0; i < na; i = i + 1) {
282
+ sa[i] = path_parts_buf[i]
283
+ }
284
+ // 注意: 第二次 path_parts_into 会覆盖全局数组 path_parts_buf,
285
+ // 因此第一侧的分段必须先拷进本地数组 sa (本地固长数组不清零, 只读已写入的 0..na-1)。
286
+ int nb = path_parts_into(b)
287
+ string sb[PATH_MAX_PARTS]
288
+ for (int j = 0; j < nb; j = j + 1) {
289
+ sb[j] = path_parts_buf[j]
290
+ }
291
+ int c = 0
292
+ while (c < na && c < nb) {
293
+ if (strcmp(sa[c], sb[c]) != 0) { break }
294
+ c = c + 1
295
+ }
296
+ if (c == 0) { return "" }
297
+ string out = sa[0]
298
+ for (int k = 1; k < c; k = k + 1) {
299
+ out = out + "/" + sa[k]
300
+ }
301
+ if (posix == 1) { return "/" + out }
302
+ return out
303
+ }
304
+
305
+ // child 是否位于 parent 之内 (1/0)。纯字符串判断, 按**分段边界**比较 (不做字符串前缀,
306
+ // 因此 "/a/b" 不包含 "/a/bc"), 两种分隔符与尾随分隔符都按同一段处理
307
+ // ("a/" 与 "a\\b" 视为 a 之下):
308
+ // path_within("/a/b", "/a/b/c.txt") = 1
309
+ // path_within("/a/b", "/a/bc") = 0 (不是分段前缀)
310
+ // path_within("/a/b", "/a/b") = 1 (相等也算在内)
311
+ // path_within("", "a/b") = 0
312
+ // path_within("/", "/a/b") = 1 (root 包含一切绝对路径)
313
+ // path_within("/", "a/b") = 0 (绝对不包含相对)
314
+ // parent 的分段必须与 child 的**开头若干段逐一相同** (大小写敏感)
315
+ function path_within(string parent, string child) -> int {
316
+ if (strlen(parent) == 0) { return 0 }
317
+ int pabs = path_is_absolute(parent)
318
+ if (path_is_absolute(child) != pabs) { return 0 }
319
+ if (strcmp(path_trim_sep(parent), "") == 0) { return pabs }
320
+ int np = path_parts_into(parent)
321
+ string pc[PATH_MAX_PARTS]
322
+ for (int i = 0; i < np; i = i + 1) {
323
+ pc[i] = path_parts_buf[i]
324
+ }
325
+ // 第二次 path_parts_into 覆盖全局数组, 所以 parent 的分段必须已拷进 pc
326
+ int nc = path_parts_into(child)
327
+ if (np > nc) { return 0 }
328
+ for (int k = 0; k < np; k = k + 1) {
329
+ if (strcmp(pc[k], path_parts_buf[k]) != 0) { return 0 }
330
+ }
331
+ return 1
332
+ }
333
+
334
+ // ---------------- 替换扩展名 ----------------
335
+
336
+ // 替换 (或补上) 最后一段的扩展名。
337
+ // - newext 允许带点或不带点: path_change_ext("a.txt", "md") 与
338
+ // path_change_ext("a.txt", ".md") 都得到 "a.md";
339
+ // - newext 为空串 -> 删除扩展名 ("a.txt" -> "a");
340
+ // - 目标没有扩展名时补上 ("a/b" + ".txt" -> "a/b.txt");
341
+ // - "." / ".." / "" 这类基名不做扩展名替换, 直接返回原路径 (空串也原样返回);
342
+ // - 以分隔符结尾的路径 (目录形式, 如 "/a/b/") 原样返回, 不当作文件名;
343
+ // - 目录部分原样保留: 只丢掉**尾随**分隔符再拼回去 ("a/b/c.txt" -> "a/b/c.md"),
344
+ // 中间的重复分隔符不折叠 (用 path_normalize 折叠)
345
+ function path_change_ext(string p, string newext) -> string {
346
+ int n = strlen(p)
347
+ if (n == 0) { return p }
348
+ if (path_is_sep(p[n - 1]) == 1) { return p }
349
+ string b = path_str_basename(p)
350
+ int lb = strlen(b)
351
+ if (strcmp(b, ".") == 0 || strcmp(b, "..") == 0) { return p }
352
+ int dot = -1
353
+ for (int i = 0; i < lb; i = i + 1) {
354
+ if (b[i] == 46) { dot = i }
355
+ }
356
+ if (dot < 1) { dot = lb }
357
+ string stem = substr(b, 0, dot)
358
+ string ext = newext
359
+ if (strlen(ext) > 0 && ext[0] != 46) { ext = "." + ext }
360
+ // 最后一段的起点 = 最后一个分隔符之后; 目录部分原样保留, 只是丢掉尾随分隔符
361
+ int cut = 0
362
+ for (int k = 0; k < n; k = k + 1) {
363
+ if (path_is_sep(p[k]) == 1) { cut = k + 1 }
364
+ }
365
+ string dir = substr(p, 0, cut)
366
+ string keep = ""
367
+ if (path_is_sep(dir[0]) == 1) { keep = "/" }
368
+ string body = path_trim_sep(dir)
369
+ if (strlen(body) > 0) { return keep + body + "/" + stem + ext }
370
+ if (path_is_absolute(p) == 1) { return "/" + stem + ext }
371
+ return stem + ext
372
+ }
@@ -0,0 +1,10 @@
1
+ # 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
2
+ [package]
3
+ name = "queue"
4
+ version = "7.22.10"
5
+ description = "Code CIN 官方标准库: 队列与栈"
6
+ license = "MIT"
7
+ authors = ["ByUsi Studio"]
8
+ entry = "src/queue.cin"
9
+ repo = "https://gitee.com/codecin/internal-libraries"
10
+ keywords = ["codecin", "stdlib", "queue"]
@@ -0,0 +1,112 @@
1
+ // Code CIN 官方标准库: 队列与栈 (queue)
2
+ // 用法: import "queue.cin"
3
+ //
4
+ // 定长环形队列 (FIFO) 与定长栈 (LIFO), 使用库内全局状态,
5
+ // 因此同一程序内只保留一份实例; 需要多实例时请用 struct 自行封装。
6
+ // 容量上限 QUEUE_CAP / STACK_CAP = 64。
7
+
8
+ int queue_ring[64]
9
+ int queue_head = 0
10
+ int queue_tail = 0
11
+ int queue_len = 0
12
+
13
+ int stack_data[64]
14
+ int stack_len = 0
15
+
16
+ // ---------------- 环形队列 ----------------
17
+
18
+ function queue_capacity() -> int {
19
+ return 64
20
+ }
21
+
22
+ function queue_clear() -> void {
23
+ queue_head = 0
24
+ queue_tail = 0
25
+ queue_len = 0
26
+ }
27
+
28
+ function queue_size() -> int {
29
+ return queue_len
30
+ }
31
+
32
+ function queue_is_empty() -> int {
33
+ if (queue_len == 0) { return 1 }
34
+ return 0
35
+ }
36
+
37
+ function queue_is_full() -> int {
38
+ if (queue_len == 64) { return 1 }
39
+ return 0
40
+ }
41
+
42
+ // 入队; 满时返回 0, 成功返回 1
43
+ function queue_push(int v) -> int {
44
+ if (queue_len == 64) { return 0 }
45
+ queue_ring[queue_tail] = v
46
+ queue_tail = (queue_tail + 1) % 64
47
+ queue_len = queue_len + 1
48
+ return 1
49
+ }
50
+
51
+ // 出队; 空时返回 0
52
+ function queue_pop() -> int {
53
+ if (queue_len == 0) { return 0 }
54
+ int v = queue_ring[queue_head]
55
+ queue_head = (queue_head + 1) % 64
56
+ queue_len = queue_len - 1
57
+ return v
58
+ }
59
+
60
+ // 查看队首 (不出队)
61
+ function queue_front() -> int {
62
+ if (queue_len == 0) { return 0 }
63
+ return queue_ring[queue_head]
64
+ }
65
+
66
+ // 查看队尾
67
+ function queue_back() -> int {
68
+ if (queue_len == 0) { return 0 }
69
+ int idx = queue_tail - 1
70
+ if (idx < 0) { idx = 63 }
71
+ return queue_ring[idx]
72
+ }
73
+
74
+ // ---------------- 栈 ----------------
75
+
76
+ function stack_capacity() -> int {
77
+ return 64
78
+ }
79
+
80
+ function stack_clear() -> void {
81
+ stack_len = 0
82
+ }
83
+
84
+ function stack_size() -> int {
85
+ return stack_len
86
+ }
87
+
88
+ function stack_is_empty() -> int {
89
+ if (stack_len == 0) { return 1 }
90
+ return 0
91
+ }
92
+
93
+ // 入栈; 满时返回 0, 成功返回 1
94
+ function stack_push(int v) -> int {
95
+ if (stack_len == 64) { return 0 }
96
+ stack_data[stack_len] = v
97
+ stack_len = stack_len + 1
98
+ return 1
99
+ }
100
+
101
+ // 出栈; 空时返回 0
102
+ function stack_pop() -> int {
103
+ if (stack_len == 0) { return 0 }
104
+ stack_len = stack_len - 1
105
+ return stack_data[stack_len]
106
+ }
107
+
108
+ // 查看栈顶 (不出栈)
109
+ function stack_peek() -> int {
110
+ if (stack_len == 0) { return 0 }
111
+ return stack_data[stack_len - 1]
112
+ }
@@ -0,0 +1,10 @@
1
+ # 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
2
+ [package]
3
+ name = "rand"
4
+ version = "7.22.10"
5
+ description = "Code CIN 官方标准库: 随机工具"
6
+ license = "MIT"
7
+ authors = ["ByUsi Studio"]
8
+ entry = "src/rand.cin"
9
+ repo = "https://gitee.com/codecin/internal-libraries"
10
+ keywords = ["codecin", "stdlib", "rand"]
@@ -0,0 +1,46 @@
1
+ // Code CIN 官方标准库: 随机工具 (rand)
2
+ // 用法: import "rand.cin"
3
+ // 说明: 基于内建 rand(); 需要可复现时先调用内建 srand(种子)。
4
+
5
+ // [lo, hi] 闭区间随机整数
6
+ function r_range(int lo, int hi) -> int {
7
+ if (hi <= lo) { return lo }
8
+ return lo + (rand() % (hi - lo + 1))
9
+ }
10
+
11
+ // 随机布尔
12
+ function r_bool() -> bool {
13
+ return r_range(0, 1) == 1
14
+ }
15
+
16
+ // [0.0, 1.0) 随机浮点
17
+ function r_float() -> float {
18
+ return rand() / 2147483648.0
19
+ }
20
+
21
+ // [lo, hi) 随机浮点
22
+ function r_float_range(float lo, float hi) -> float {
23
+ return lo + r_float() * (hi - lo)
24
+ }
25
+
26
+ // 原地洗牌 (Fisher-Yates)
27
+ function r_shuffle(int[] a, int n) -> void {
28
+ for (int i = n - 1; i > 0; i = i - 1) {
29
+ int j = r_range(0, i)
30
+ int t = a[i]
31
+ a[i] = a[j]
32
+ a[j] = t
33
+ }
34
+ }
35
+
36
+ // 随机取一个元素
37
+ function r_choice(int[] a, int n) -> int {
38
+ if (n <= 0) { return 0 }
39
+ return a[r_range(0, n - 1)]
40
+ }
41
+
42
+ // 按权重概率返回 1 (p 为 0..100 的百分比)
43
+ function r_chance(int p) -> int {
44
+ if (rand() % 100 < p) { return 1 }
45
+ return 0
46
+ }
@@ -0,0 +1,10 @@
1
+ # 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
2
+ [package]
3
+ name = "set"
4
+ version = "7.22.10"
5
+ description = "Code CIN 官方标准库: 整数集合"
6
+ license = "MIT"
7
+ authors = ["ByUsi Studio"]
8
+ entry = "src/set.cin"
9
+ repo = "https://gitee.com/codecin/internal-libraries"
10
+ keywords = ["codecin", "stdlib", "set"]