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,118 @@
1
+ // Code CIN 官方标准库: Termux API 便捷封装 (termux)
2
+ // 用法: import "termux.cin"
3
+ // 说明: 需 termux-api 命令 + Termux:API 应用; 非 Termux 环境下调用返回 -1/空串。
4
+ import "json.cin"
5
+
6
+ // Termux API 是否可用 (1/0)
7
+ function tx_ok() -> int {
8
+ return termux_available()
9
+ }
10
+
11
+ // 发送系统通知
12
+ function tx_notify(string title, string content) -> int {
13
+ return termux_notify(title, content)
14
+ }
15
+
16
+ // 弹出 Toast
17
+ function tx_toast(string msg) -> int {
18
+ return termux_toast(msg)
19
+ }
20
+
21
+ // 复制到剪贴板
22
+ function tx_copy(string s) -> int {
23
+ return termux_clipboard_set(s)
24
+ }
25
+
26
+ // 读取剪贴板
27
+ function tx_paste() -> string {
28
+ return termux_clipboard_get()
29
+ }
30
+
31
+ // 振动 ms 毫秒
32
+ function tx_vibrate(int ms) -> int {
33
+ return termux_vibrate(ms)
34
+ }
35
+
36
+ // 文字转语音
37
+ function tx_say(string text) -> int {
38
+ return termux_tts(text)
39
+ }
40
+
41
+ // 发送短信
42
+ function tx_sms(string number, string text) -> int {
43
+ return termux_sms_send(number, text)
44
+ }
45
+
46
+ // 电池状态原始 JSON
47
+ function tx_battery_json() -> string {
48
+ return termux_battery()
49
+ }
50
+
51
+ // 电池百分比 (-1 表示不可用)
52
+ function tx_battery_level() -> int {
53
+ string j = termux_battery()
54
+ if (strlen(j) == 0) { return -1 }
55
+ return j_int(j, "percentage")
56
+ }
57
+
58
+ // 电池温度 (-1.0 表示不可用)
59
+ function tx_battery_temp() -> float {
60
+ string j = termux_battery()
61
+ if (strlen(j) == 0) { return -1.0 }
62
+ return j_float(j, "temperature")
63
+ }
64
+
65
+ // 电池是否充电中 (1/0)
66
+ function tx_battery_plugged() -> int {
67
+ string j = termux_battery()
68
+ if (strlen(j) == 0) { return 0 }
69
+ string p = j_str(j, "plugged")
70
+ if (strcmp(p, "UNPLUGGED") == 0) { return 0 }
71
+ if (strlen(p) == 0) { return 0 }
72
+ return 1
73
+ }
74
+
75
+ // 定位原始 JSON
76
+ function tx_location_json() -> string {
77
+ return termux_location()
78
+ }
79
+
80
+ // 定位纬度 (不可用为 0.0)
81
+ function tx_latitude() -> float {
82
+ string j = termux_location()
83
+ if (strlen(j) == 0) { return 0.0 }
84
+ return j_float(j, "latitude")
85
+ }
86
+
87
+ // 定位经度
88
+ function tx_longitude() -> float {
89
+ string j = termux_location()
90
+ if (strlen(j) == 0) { return 0.0 }
91
+ return j_float(j, "longitude")
92
+ }
93
+
94
+ // WiFi 连接信息原始 JSON
95
+ function tx_wifi_json() -> string {
96
+ return termux_wifi_info()
97
+ }
98
+
99
+ // 当前 WiFi SSID (不可用为空串)
100
+ function tx_wifi_ssid() -> string {
101
+ string j = termux_wifi_info()
102
+ if (strlen(j) == 0) { return "" }
103
+ return j_str(j, "ssid")
104
+ }
105
+
106
+ // 弹出输入对话框, 返回用户输入的文本
107
+ function tx_prompt(string title) -> string {
108
+ string j = termux_dialog(title)
109
+ if (strlen(j) == 0) { return "" }
110
+ return j_str(j, "text")
111
+ }
112
+
113
+ // 通知 + 振动 组合
114
+ function tx_alert(string title, string content) -> int {
115
+ int r = termux_notify(title, content)
116
+ termux_vibrate(200)
117
+ return r
118
+ }
@@ -0,0 +1,10 @@
1
+ # 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
2
+ [package]
3
+ name = "test"
4
+ version = "7.22.10"
5
+ description = "Code CIN 官方标准库: 轻量测试断言"
6
+ license = "MIT"
7
+ authors = ["ByUsi Studio"]
8
+ entry = "src/test.cin"
9
+ repo = "https://gitee.com/codecin/internal-libraries"
10
+ keywords = ["codecin", "stdlib", "test"]
@@ -0,0 +1,74 @@
1
+ // Code CIN 官方标准库: 轻量测试断言 (test)
2
+ // 用法: import "test.cin"
3
+ // 说明: 提供计数器与断言; 结束时调用 t_report() 输出汇总 (返回失败数)。
4
+
5
+ // 通过 / 失败计数 (全局)
6
+ int T_PASS = 0
7
+ int T_FAIL = 0
8
+
9
+ // 断言两个整数相等
10
+ function t_eq_int(int got, int want, string label) -> int {
11
+ if (got == want) {
12
+ T_PASS = T_PASS + 1
13
+ return 1
14
+ }
15
+ T_FAIL = T_FAIL + 1
16
+ println("FAIL " + label + ": got=" + int_to_str(got) + " want=" + int_to_str(want))
17
+ return 0
18
+ }
19
+
20
+ // 断言两个字符串相等
21
+ function t_eq_str(string got, string want, string label) -> int {
22
+ if (strcmp(got, want) == 0) {
23
+ T_PASS = T_PASS + 1
24
+ return 1
25
+ }
26
+ T_FAIL = T_FAIL + 1
27
+ println("FAIL " + label + ": got=[" + got + "] want=[" + want + "]")
28
+ return 0
29
+ }
30
+
31
+ // 断言浮点近似相等 (容差 eps)
32
+ function t_near(float got, float want, float eps, string label) -> int {
33
+ float d = got - want
34
+ if (d < 0.0) { d = -d }
35
+ if (d <= eps) {
36
+ T_PASS = T_PASS + 1
37
+ return 1
38
+ }
39
+ T_FAIL = T_FAIL + 1
40
+ println("FAIL " + label + ": got=" + float_to_str(got) + " want=" + float_to_str(want))
41
+ return 0
42
+ }
43
+
44
+ // 断言条件为真
45
+ function t_true(int cond, string label) -> int {
46
+ if (cond != 0) {
47
+ T_PASS = T_PASS + 1
48
+ return 1
49
+ }
50
+ T_FAIL = T_FAIL + 1
51
+ println("FAIL " + label + ": condition false")
52
+ return 0
53
+ }
54
+
55
+ // 断言条件为假
56
+ function t_false(int cond, string label) -> int {
57
+ return t_true(cond == 0, label)
58
+ }
59
+
60
+ // 重置计数
61
+ function t_reset() -> void {
62
+ T_PASS = 0
63
+ T_FAIL = 0
64
+ }
65
+
66
+ // 输出汇总, 返回失败数
67
+ function t_report() -> int {
68
+ if (T_FAIL == 0) {
69
+ println("OK: " + int_to_str(T_PASS) + " assertions passed")
70
+ } else {
71
+ println("FAILED: " + int_to_str(T_FAIL) + " of " + int_to_str(T_PASS + T_FAIL))
72
+ }
73
+ return T_FAIL
74
+ }
@@ -0,0 +1,10 @@
1
+ # 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
2
+ [package]
3
+ name = "text"
4
+ version = "7.22.10"
5
+ description = "Code CIN 官方标准库: 文本处理"
6
+ license = "MIT"
7
+ authors = ["ByUsi Studio"]
8
+ entry = "src/text.cin"
9
+ repo = "https://gitee.com/codecin/internal-libraries"
10
+ keywords = ["codecin", "stdlib", "text"]
@@ -0,0 +1,323 @@
1
+ // Code CIN 官方标准库: 文本处理 (text)
2
+ // 用法: import "text.cin"
3
+ //
4
+ // 比 str.cin 更进一步: 大小写无关比较、反转、替换、区间编辑、标题化、词数统计。
5
+ // 纯 CIN 实现, 只使用非宿主内建 (strlen / substr / indexof / upper / lower /
6
+ // strcmp), 不依赖 Go 原生运行时, 原生与 AOT 路径下同样可用。
7
+ //
8
+ // 字节语义: strlen / substr / s[i] 全是**字节**语义, 本库沿用同一约定 ——
9
+ // 非 ASCII 一律按 UTF-8 字节序列逐个字节处理, 不做码点解码; 下标、长度、切片
10
+ // 边界都是字节偏移。大小写转换只折叠 ASCII 字母 (a-z / A-Z), 其余字节原样保留。
11
+ //
12
+ // 内存注意: CIN 没有堆回收, 每次拼接/子串都会新建堆块。本库不做无界循环拼接,
13
+ // 但 txt_reverse / txt_swap_case 需要逐段重建, 分配量随长度平方增长 (n 字节约
14
+ // O(n^2) 字节堆), 请只对短到中等长度的串使用 (经验值: 单次调用 ≤ 几百字节)。
15
+ // 另外逐字节重建会把多字节字符切成单字节片段, 纯 Python 路径的字符串拼接按
16
+ // UTF-8 解码, 因此**非 ASCII 输入请用 txt_slice / txt_delete_range /
17
+ // txt_insert / txt_replace (模式为完整字符) 等按整段搬运的接口**。
18
+
19
+ // ---------------- 内部辅助函数 (非公开 API, 统一 txt_ 前缀) ----------------
20
+
21
+ // ASCII 小写折叠: 'A'-'Z' -> 'a'-'z'; 其他字节原样返回
22
+ function txt_lower_byte(int c) -> int {
23
+ if (c >= 'A' && c <= 'Z') { return c + 32 }
24
+ return c
25
+ }
26
+
27
+ // 是否 ASCII 字母
28
+ function txt_is_alpha_byte(int c) -> int {
29
+ if (c >= 'a' && c <= 'z') { return 1 }
30
+ if (c >= 'A' && c <= 'Z') { return 1 }
31
+ return 0
32
+ }
33
+
34
+ // 是否 ASCII 字母或数字 (txt_title 的单词字符)
35
+ function txt_is_alnum_byte(int c) -> int {
36
+ if (txt_is_alpha_byte(c) == 1) { return 1 }
37
+ if (c >= '0' && c <= '9') { return 1 }
38
+ return 0
39
+ }
40
+
41
+ // 是否空白: 空格 / \t / \n / \r
42
+ function txt_is_space_byte(int c) -> int {
43
+ if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { return 1 }
44
+ return 0
45
+ }
46
+
47
+ // needle 是否正好从 hay 的 at 处开始 (逐字节比较, 不分配堆块)
48
+ function txt_match_at(string hay, string needle, int at) -> int {
49
+ if (at < 0) { return 0 }
50
+ int hn = strlen(hay)
51
+ int nn = strlen(needle)
52
+ if (at + nn > hn) { return 0 }
53
+ int i = 0
54
+ while (i < nn) {
55
+ if (hay[at + i] != needle[i]) { return 0 }
56
+ i = i + 1
57
+ }
58
+ return 1
59
+ }
60
+
61
+ // 内部: 把 s 的 [start, end) 按 mode 重建 (0 = 原样, 1 = ASCII 大写, 2 = ASCII 小写)。
62
+ // 只有连续的 ASCII 字母才交给 upper / lower, 其余字节整段拷贝,
63
+ // 因此多字节字符不会被从中间切开。
64
+ function txt_case_range(string s, int start, int end, int mode) -> string {
65
+ int n = strlen(s)
66
+ if (start < 0) { start = 0 }
67
+ if (start > n) { start = n }
68
+ if (end > n) { end = n }
69
+ if (end < start) { end = start }
70
+ string out = ""
71
+ int i = start
72
+ while (i < end) {
73
+ int cls = txt_is_alpha_byte(s[i])
74
+ int j = i
75
+ while (j < end) {
76
+ if (txt_is_alpha_byte(s[j]) != cls) { break }
77
+ j = j + 1
78
+ }
79
+ string chunk = substr(s, i, j - i)
80
+ if (cls == 1 && mode == 1) {
81
+ out = out + upper(chunk)
82
+ } else if (cls == 1 && mode == 2) {
83
+ out = out + lower(chunk)
84
+ } else {
85
+ out = out + chunk
86
+ }
87
+ i = j
88
+ }
89
+ return out
90
+ }
91
+
92
+ // ---------------- 公开 API ----------------
93
+
94
+ // 字节长度是否为 0; 空白串不算空 (需要"全空白"判定时用 txt_word_count == 0)
95
+ function txt_is_empty(string s) -> int {
96
+ if (strlen(s) == 0) { return 1 }
97
+ return 0
98
+ }
99
+
100
+ // ASCII 大小写无关的整串相等 (1/0); 长度必须相同, 非 ASCII 字节需逐一相同
101
+ function txt_equals_ignore_case(string a, string b) -> int {
102
+ int n = strlen(a)
103
+ if (n != strlen(b)) { return 0 }
104
+ int i = 0
105
+ while (i < n) {
106
+ if (txt_lower_byte(a[i]) != txt_lower_byte(b[i])) { return 0 }
107
+ i = i + 1
108
+ }
109
+ return 1
110
+ }
111
+
112
+ // s 是否以 p 开头 (ASCII 大小写无关, 1/0); 空 p 恒为 1, 空 s 只有空 p 为 1
113
+ function txt_startswith_ignore_case(string s, string p) -> int {
114
+ int pn = strlen(p)
115
+ if (pn > strlen(s)) { return 0 }
116
+ int i = 0
117
+ while (i < pn) {
118
+ if (txt_lower_byte(s[i]) != txt_lower_byte(p[i])) { return 0 }
119
+ i = i + 1
120
+ }
121
+ return 1
122
+ }
123
+
124
+ // 按字节反转; 空串返回空串 (非 ASCII 会被切成单字节片段, 见文件头说明)
125
+ function txt_reverse(string s) -> string {
126
+ string out = ""
127
+ int i = strlen(s) - 1
128
+ while (i >= 0) {
129
+ out = out + substr(s, i, 1)
130
+ i = i - 1
131
+ }
132
+ return out
133
+ }
134
+
135
+ // 从字节下标 from (含) 开始查找 needle 的首次出现, 返回绝对字节下标; 未找到 -1。
136
+ // from 越界裁剪到 [0, strlen(hay)]; needle 为空串时返回裁剪后的 from。
137
+ function txt_index_from(string hay, string needle, int from) -> int {
138
+ int hn = strlen(hay)
139
+ if (from < 0) { from = 0 }
140
+ if (from > hn) { from = hn }
141
+ if (strlen(needle) == 0) { return from }
142
+ int i = from
143
+ while (i < hn) {
144
+ if (txt_match_at(hay, needle, i) == 1) { return i }
145
+ i = i + 1
146
+ }
147
+ return -1
148
+ }
149
+
150
+ // needle 最后一次出现的字节下标 (允许重叠匹配); 未找到 -1; 空 needle 返回 strlen(hay)
151
+ function txt_last_index_of(string hay, string needle) -> int {
152
+ if (strlen(needle) == 0) { return strlen(hay) }
153
+ int last = -1
154
+ int pos = 0
155
+ int hn = strlen(hay)
156
+ while (pos <= hn) {
157
+ int at = txt_index_from(hay, needle, pos)
158
+ if (at < 0) { break }
159
+ last = at
160
+ pos = at + 1
161
+ }
162
+ return last
163
+ }
164
+
165
+ // 全部替换 (从左到右扫描, 不重叠); from 为空串时原样返回 s (不做插入)
166
+ function txt_replace(string s, string from, string to) -> string {
167
+ if (strlen(from) == 0) { return s }
168
+ int n = strlen(s)
169
+ int fl = strlen(from)
170
+ string out = ""
171
+ int pos = 0
172
+ while (pos <= n) {
173
+ int at = txt_index_from(s, from, pos)
174
+ if (at < 0) { break }
175
+ out = out + substr(s, pos, at - pos) + to
176
+ pos = at + fl
177
+ }
178
+ return out + substr(s, pos, n - pos)
179
+ }
180
+
181
+ // 只替换首次出现; from 为空串或无匹配时原样返回 s (无匹配返回同一指针)
182
+ function txt_replace_first(string s, string from, string to) -> string {
183
+ if (strlen(from) == 0) { return s }
184
+ int n = strlen(s)
185
+ int fl = strlen(from)
186
+ int at = txt_index_from(s, from, 0)
187
+ if (at < 0) { return s }
188
+ return substr(s, 0, at) + to + substr(s, at + fl, n - at - fl)
189
+ }
190
+
191
+ // 删除全部 sub; sub 为空串时原样返回 s
192
+ function txt_remove(string s, string sub) -> string {
193
+ if (strlen(sub) == 0) { return s }
194
+ return txt_replace(s, sub, "")
195
+ }
196
+
197
+ // 左闭右开切片 [start, end), 越界自动裁剪; end <= start 时返回空串
198
+ function txt_slice(string s, int start, int end) -> string {
199
+ int n = strlen(s)
200
+ if (start < 0) { start = 0 }
201
+ if (end < 0) { end = 0 }
202
+ if (start > n) { start = n }
203
+ if (end > n) { end = n }
204
+ if (end <= start) { return "" }
205
+ return substr(s, start, end - start)
206
+ }
207
+
208
+ // 在字节下标 pos 处插入 sub; pos 裁剪到 [0, strlen(s)] (超过末尾就追加到末尾)
209
+ function txt_insert(string s, int pos, string sub) -> string {
210
+ int n = strlen(s)
211
+ if (pos < 0) { pos = 0 }
212
+ if (pos > n) { pos = n }
213
+ return substr(s, 0, pos) + sub + substr(s, pos, n - pos)
214
+ }
215
+
216
+ // 删除 [pos, pos+len) 区间; pos 裁剪到 [0, strlen(s)], len < 0 视为 0, 越界自动缩短
217
+ function txt_delete_range(string s, int pos, int len) -> string {
218
+ int n = strlen(s)
219
+ if (pos < 0) { pos = 0 }
220
+ if (pos > n) { pos = n }
221
+ if (len < 0) { len = 0 }
222
+ if (pos + len > n) { len = n - pos }
223
+ return substr(s, 0, pos) + substr(s, pos + len, n - pos - len)
224
+ }
225
+
226
+ // 首个 ASCII 字母大写、其余 ASCII 字母小写 (Python str.capitalize 风格);
227
+ // 前导非字母字节原样保留; 完全没有 ASCII 字母时原样返回; 空串返回空串
228
+ function txt_capitalize(string s) -> string {
229
+ int n = strlen(s)
230
+ if (n == 0) { return "" }
231
+ int i = 0
232
+ while (i < n) {
233
+ if (txt_is_alpha_byte(s[i]) == 1) { break }
234
+ i = i + 1
235
+ }
236
+ if (i == n) { return s }
237
+ string head = txt_case_range(s, 0, i, 0)
238
+ string first = txt_case_range(s, i, i + 1, 1)
239
+ string rest = txt_case_range(s, i + 1, n, 2)
240
+ return head + first + rest
241
+ }
242
+
243
+ // 每个"单词"的首个字节大写、其余小写; 单词 = 连续的 ASCII 字母/数字。
244
+ // 注意: 只把单词首字节大写, 因此 "abc3def" -> "Abc3def" (与 Python str.title
245
+ // 的 "Abc3Def" 不同); 撇号等非字母数字都是分隔符 ("it's" -> "It'S")。
246
+ function txt_title(string s) -> string {
247
+ int n = strlen(s)
248
+ if (n == 0) { return "" }
249
+ string out = ""
250
+ int i = 0
251
+ while (i < n) {
252
+ if (txt_is_alnum_byte(s[i]) == 1) {
253
+ int ws = i
254
+ while (i < n) {
255
+ if (txt_is_alnum_byte(s[i]) == 0) { break }
256
+ i = i + 1
257
+ }
258
+ out = out + txt_case_range(s, ws, ws + 1, 1)
259
+ out = out + txt_case_range(s, ws + 1, i, 2)
260
+ } else {
261
+ int ss = i
262
+ while (i < n) {
263
+ if (txt_is_alnum_byte(s[i]) == 1) { break }
264
+ i = i + 1
265
+ }
266
+ out = out + txt_case_range(s, ss, i, 0)
267
+ }
268
+ }
269
+ return out
270
+ }
271
+
272
+ // 空白分隔的单词个数; 连续空白算一个, 忽略首尾空白, 全空白为 0。
273
+ // 空白 = 空格 / \t / \n / \r; 非 ASCII 字节属于单词内部字节。
274
+ function txt_word_count(string s) -> int {
275
+ int n = strlen(s)
276
+ int count = 0
277
+ int in_word = 0
278
+ int i = 0
279
+ while (i < n) {
280
+ if (txt_is_space_byte(s[i]) == 1) {
281
+ in_word = 0
282
+ } else {
283
+ if (in_word == 0) {
284
+ count = count + 1
285
+ in_word = 1
286
+ }
287
+ }
288
+ i = i + 1
289
+ }
290
+ return count
291
+ }
292
+
293
+ // ASCII 大小写互换 ('a'<->'A'), 非 ASCII 字节原样保留; 空串返回空串
294
+ function txt_swap_case(string s) -> string {
295
+ int n = strlen(s)
296
+ string out = ""
297
+ int i = 0
298
+ while (i < n) {
299
+ int c = s[i]
300
+ int cls = 0
301
+ if (c >= 'a' && c <= 'z') { cls = 1 }
302
+ if (c >= 'A' && c <= 'Z') { cls = 2 }
303
+ int j = i
304
+ while (j < n) {
305
+ int d = s[j]
306
+ int dc = 0
307
+ if (d >= 'a' && d <= 'z') { dc = 1 }
308
+ if (d >= 'A' && d <= 'Z') { dc = 2 }
309
+ if (dc != cls) { break }
310
+ j = j + 1
311
+ }
312
+ string chunk = substr(s, i, j - i)
313
+ if (cls == 1) {
314
+ out = out + upper(chunk)
315
+ } else if (cls == 2) {
316
+ out = out + lower(chunk)
317
+ } else {
318
+ out = out + chunk
319
+ }
320
+ i = j
321
+ }
322
+ return out
323
+ }
@@ -0,0 +1,10 @@
1
+ # 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
2
+ [package]
3
+ name = "time"
4
+ version = "7.22.10"
5
+ description = "Code CIN 官方标准库: 时间工具"
6
+ license = "MIT"
7
+ authors = ["ByUsi Studio"]
8
+ entry = "src/time.cin"
9
+ repo = "https://gitee.com/codecin/internal-libraries"
10
+ keywords = ["codecin", "stdlib", "time"]
@@ -0,0 +1,54 @@
1
+ // Code CIN 官方标准库: 时间工具 (time)
2
+ // 用法: import "time.cin"
3
+ // 依赖内建: time()
4
+
5
+ // 当前 Unix 时间戳 (秒)
6
+ function t_now() -> int {
7
+ return time()
8
+ }
9
+
10
+ // 两位左补零
11
+ function t_two(int v) -> string {
12
+ if (v < 0) { v = -v }
13
+ if (v < 10) { return "0" + int_to_str(v) }
14
+ return int_to_str(v)
15
+ }
16
+
17
+ // 秒 -> "HH:MM:SS"
18
+ function t_hms(int secs) -> string {
19
+ if (secs < 0) { secs = -secs }
20
+ int h = idiv(secs, 3600)
21
+ int m = idiv(secs % 3600, 60)
22
+ int s = secs % 60
23
+ return t_two(h) + ":" + t_two(m) + ":" + t_two(s)
24
+ }
25
+
26
+ // 秒 -> "MM:SS"
27
+ function t_ms(int secs) -> string {
28
+ if (secs < 0) { secs = -secs }
29
+ return t_two(idiv(secs, 60)) + ":" + t_two(secs % 60)
30
+ }
31
+
32
+ // 天/时/分/秒 拆解, 存入 4 元素数组
33
+ function t_breakdown(int secs, int[] out) -> void {
34
+ if (secs < 0) { secs = -secs }
35
+ out[0] = idiv(secs, 86400)
36
+ out[1] = idiv(secs % 86400, 3600)
37
+ out[2] = idiv(secs % 3600, 60)
38
+ out[3] = secs % 60
39
+ }
40
+
41
+ // 人性化时长 "1d 2h 3m 4s" (省略为 0 的高位单位)
42
+ function t_human(int secs) -> string {
43
+ if (secs < 0) { secs = -secs }
44
+ int d = idiv(secs, 86400)
45
+ int h = idiv(secs % 86400, 3600)
46
+ int m = idiv(secs % 3600, 60)
47
+ int s = secs % 60
48
+ string out = ""
49
+ if (d > 0) { out = out + int_to_str(d) + "d " }
50
+ if (d > 0 || h > 0) { out = out + int_to_str(h) + "h " }
51
+ if (d > 0 || h > 0 || m > 0) { out = out + int_to_str(m) + "m " }
52
+ out = out + int_to_str(s) + "s"
53
+ return out
54
+ }
@@ -0,0 +1,10 @@
1
+ # 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
2
+ [package]
3
+ name = "token"
4
+ version = "7.22.10"
5
+ description = "Code CIN 官方标准库: 切分与分词"
6
+ license = "MIT"
7
+ authors = ["ByUsi Studio"]
8
+ entry = "src/token.cin"
9
+ repo = "https://gitee.com/codecin/internal-libraries"
10
+ keywords = ["codecin", "stdlib", "token"]