-
Notifications
You must be signed in to change notification settings - Fork 35
[0039] 实现 SRFI 233: INI 文件解析与生成 #771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kabukimono-Sakura
wants to merge
6
commits into
MoganLab:main
Choose a base branch
from
Kabukimono-Sakura:feat/srfi-233-ini-files
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
14123b7
docs: add (liii ini-file) to standard library list
ced8fa0
feat: implement SRFI 233 (INI files)
2338ea1
add development record file
8500f82
[0039] 重构为 (srfi srfi-233) + (liii ini) 两层结构
b8f6293
修改测试文件tests/liii/ini-file-test.scm注释细节
0f6492d
[0039] 修改版权年份为2026,函数重命名 ini-trim 为 ini-string-trim
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # [0039] 实现 SRFI 233: INI Files | ||
|
|
||
| ## 任务相关的代码文件 | ||
| - `goldfish/liii/ini-file.scm` | ||
| - `tests/liii/ini-file-test.scm` | ||
| - `README.md` | ||
|
|
||
| ## 如何测试 | ||
| ```bash | ||
| xmake b goldfish | ||
| bin/gf fmt goldfish/liii/ini-file.scm | ||
| bin/gf fmt tests/liii/ini-file-test.scm | ||
| bin/gf tests/liii/ini-file-test.scm | ||
| ``` | ||
|
|
||
| ## 2026/05/05 实现 (liii ini-file) 库 | ||
|
|
||
| ### What | ||
| 1. 创建 `goldfish/liii/ini-file.scm`,实现 SRFI 233 规范中的两个核心函数: | ||
| - `make-ini-file-generator`:从文本输入端口创建生成器,逐行解析 INI 格式,返回 `(section key value)` 三元素列表 | ||
| - `make-ini-file-accumulator`:向文本输出端口写入 INI 格式数据 | ||
| 2. 创建 `tests/liii/ini-file-test.scm`,包含 22 个测试场景、46 个断言 | ||
| 3. 在 `README.md` 的 "Python-like standard library" 表格中加入 `(liii ini-file)` 条目 | ||
|
|
||
| ### Why | ||
| SRFI 233 是 Scheme 社区标准化的 INI 文件访问接口。INI 文件虽然简单,但在配置管理场景中广泛使用。作为 Goldfish Scheme 标准库的补充,提供开箱即用的 INI 文件解析和生成能力。 | ||
|
|
||
| ### How | ||
| **解析逻辑(Generator):** | ||
| - 逐行 `read-line` 读取输入端口 | ||
| - 用 `char-index` 找到首个注释分隔符,截断注释部分 | ||
| - 用 `ini-trim` 去除首尾空白(仅空格和 Tab,符合 SRFI 233 定义) | ||
| - `[...]` 模式匹配节名,转为 symbol | ||
| - 含分隔符的行用 `split-at-first` 按第一个分隔符拆分为键值对 | ||
| - 不含分隔符的行视为键,值为 `#f` | ||
| - 通过闭包维护 `current-section` 状态 | ||
|
|
||
| **写入逻辑(Accumulator):** | ||
| - 通过闭包维护 `current-section` 状态,仅在 section 变化时写入 `[section]` 行 | ||
| - `#f` section 不写入 section 头 | ||
| - 字符串参数以注释格式写入(`comment-delim` + 空格 + 内容) | ||
| - EOF 对象触发关闭状态,后续调用报错 | ||
|
|
||
| **关键设计决策:** | ||
| - 使用纯整数索引的辅助函数(`char-index`、`ini-trim`),避免 `string-cursor` 库的 cursor/index 转换复杂性 | ||
| - 用 `define*` 实现可选参数,支持自定义 `key-value-sep` 和 `comment-delim` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| ;; | ||
| ;; Copyright (C) 2024 The Goldfish Scheme Authors | ||
| ;; | ||
| ;; Licensed under the Apache License, Version 2.0 (the "License"); | ||
| ;; you may not use this file except in compliance with the License. | ||
| ;; You may obtain a copy of the License at | ||
| ;; | ||
| ;; http://www.apache.org/licenses/LICENSE-2.0 | ||
| ;; | ||
| ;; Unless required by applicable law or agreed to in writing, software | ||
| ;; distributed under the License is distributed on an "AS IS" BASIS, | ||
| ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| ;; See the License for the specific language governing permissions and | ||
| ;; limitations under the License. | ||
| ;; | ||
| ;; SRFI 233: INI files | ||
| ;; Authors: John Cowan (spec), Arvydas Silanskas (reference implementation) | ||
|
|
||
| (define-library (liii ini-file) | ||
| (import (scheme base) (liii base)) | ||
| (export make-ini-file-generator make-ini-file-accumulator) | ||
| (begin | ||
|
|
||
| ;; Whitespace for SRFI 233: space and tab only | ||
| (define (ini-whitespace? ch) | ||
| (or (char=? ch #\space) (char=? ch #\tab)) | ||
| ) ;define | ||
|
|
||
| ;; Find first occurrence of char in string, returns integer index or #f | ||
| (define (char-index str ch) | ||
| (let ((len (string-length str))) | ||
| (let loop | ||
| ((i 0)) | ||
| (cond ((= i len) #f) | ||
| ((char=? (string-ref str i) ch) i) | ||
| (else (loop (+ i 1))) | ||
| ) ;cond | ||
| ) ;let | ||
| ) ;let | ||
| ) ;define | ||
|
|
||
| ;; Trim leading and trailing whitespace (space/tab) | ||
| (define (ini-trim str) | ||
| (let ((len (string-length str))) | ||
| (let loop-start | ||
| ((i 0)) | ||
| (if (and (< i len) (ini-whitespace? (string-ref str i))) | ||
| (loop-start (+ i 1)) | ||
| (let loop-end | ||
| ((j (- len 1))) | ||
| (if (and (>= j i) (ini-whitespace? (string-ref str j))) | ||
| (loop-end (- j 1)) | ||
| (substring str i (+ j 1)) | ||
| ) ;if | ||
| ) ;let | ||
| ) ;if | ||
| ) ;let | ||
| ) ;let | ||
| ) ;define | ||
|
|
||
| ;; Remove comment from a line: find first comment-delim and truncate | ||
| (define (remove-comment line comment-delim) | ||
| (let ((idx (char-index line comment-delim))) | ||
| (if idx (substring line 0 idx) line) | ||
| ) ;let | ||
| ) ;define | ||
|
|
||
| ;; Split string at first occurrence of a character | ||
| ;; Returns (before . after) or #f if not found | ||
| (define (split-at-first str sep-char) | ||
| (let ((idx (char-index str sep-char))) | ||
| (if idx | ||
| (cons (substring str 0 idx) (substring str (+ idx 1) (string-length str))) | ||
| #f | ||
| ) ;if | ||
| ) ;let | ||
| ) ;define | ||
|
|
||
| ;; Parse a section header line like "[name]" | ||
| ;; Returns section name string, or #f if not a section header | ||
| (define (parse-section-name line) | ||
| (let* ((trimmed (ini-trim line)) (len (string-length trimmed))) | ||
| (if (and (> len 1) | ||
| (char=? (string-ref trimmed 0) #\[) | ||
| (char=? (string-ref trimmed (- len 1)) #\]) | ||
| ) ;and | ||
| (ini-trim (substring trimmed 1 (- len 1))) | ||
| #f | ||
| ) ;if | ||
| ) ;let* | ||
| ) ;define | ||
|
|
||
| ;; make-ini-file-generator | ||
| ;; Returns a thunk that yields (section key value) lists from the INI port | ||
| (define* (make-ini-file-generator inport (key-value-sep #\=) (comment-delim #\;)) | ||
| (let ((current-section #f)) | ||
| (lambda () | ||
| (let loop | ||
| () | ||
| (let ((line (read-line inport))) | ||
| (cond ((eof-object? line) (eof-object)) | ||
| (else (let* ((no-comment (remove-comment line comment-delim)) | ||
| (trimmed (ini-trim no-comment)) | ||
| ) ; | ||
| (if (string=? trimmed "") | ||
| (loop) | ||
| (let ((section-name (parse-section-name trimmed))) | ||
| (cond (section-name (set! current-section (string->symbol section-name)) (loop)) | ||
| (else (let ((kv (split-at-first trimmed key-value-sep))) | ||
| (if kv | ||
| (let ((key-str (ini-trim (car kv))) (val-str (ini-trim (cdr kv)))) | ||
| (list current-section (string->symbol key-str) val-str) | ||
| ) ;let | ||
| (list current-section (string->symbol trimmed) #f) | ||
| ) ;if | ||
| ) ;let | ||
| ) ;else | ||
| ) ;cond | ||
| ) ;let | ||
| ) ;if | ||
| ) ;let* | ||
| ) ;else | ||
| ) ;cond | ||
| ) ;let | ||
| ) ;let | ||
| ) ;lambda | ||
| ) ;let | ||
| ) ;define* | ||
|
|
||
| ;; make-ini-file-accumulator | ||
| ;; Returns a procedure that writes INI data to outport | ||
| (define* (make-ini-file-accumulator outport (key-value-sep #\=) (comment-delim #\;)) | ||
| (let ((current-section '*ini-none*) (closed #f)) | ||
| (lambda (item) | ||
| (cond (closed (error 'ini-file "accumulator called after EOF")) | ||
| ((eof-object? item) (set! closed #t) (eof-object)) | ||
| ((string? item) | ||
| (when (char-index item #\newline) | ||
| (error 'ini-file "comment string contains newline") | ||
| ) ;when | ||
| (display comment-delim outport) | ||
| (display #\space outport) | ||
| (display item outport) | ||
| (newline outport) | ||
| ) ; | ||
| ((and (list? item) (= (length item) 3)) | ||
| (let ((section (car item)) (key (cadr item)) (value (caddr item))) | ||
| ;; Write section header if changed | ||
| (when (not (eq? section current-section)) | ||
| (when section | ||
| (display #\[ outport) | ||
| (display (symbol->string section) outport) | ||
| (display #\] outport) | ||
| (newline outport) | ||
| ) ;when | ||
| (set! current-section section) | ||
| ) ;when | ||
| ;; Write key-value line | ||
| (display (symbol->string key) outport) | ||
| (when value | ||
| (display key-value-sep outport) | ||
| (display value outport) | ||
| ) ;when | ||
| (newline outport) | ||
| ) ;let | ||
| ) ; | ||
| (else (error 'ini-file "invalid argument to accumulator")) | ||
| ) ;cond | ||
| ) ;lambda | ||
| ) ;let | ||
| ) ;define* | ||
|
|
||
| ) ;begin | ||
| ) ;define-library | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.