Files
marqdo/lib/text.mq.md
T
cflmy 9331af78e2 release(2.8.0): 合入 CHANGELOG/README/发版说明/skills prove 任务;CI 链接指向 doc/releases
- CHANGELOG.md:v2.8.0 条目(中英双语);
- README.md:版本行 2.7.1→2.8.0 + 愿景第 6 条(知识可证明);
- doc/releases/v2.8.0.md:双语发版说明(亮点/用法/已知限制);
- skills/marqdo-dev/index.mq.md:新增 prove 任务(路径/目录模式/
  require_lean/json;参数用 + 行声明升参,check 与 narrative-ratio 通过);
- 设计文档 §12.5 标记已合入;发版链接统一指向 doc/releases/v2.8.0.md。
2026-11-18 10:29:26 +08:00

236 lines
6.3 KiB
Markdown
Executable File

---
markup: occlusion
title: lib/text
description: Text helpers (Mid M4). Literal replace — use lib/re for regex.
---
Small text helpers. Prefer these over hand-rolled splits; regex belongs in lib/re.
Clean and combine Unicode text.
Behavior and limits: Use re for patterns and bytes for arbitrary binary. split and join use exact separators; no implicit document parsing.
This single source file is the usage and development manual. Import it directly, or use marqdo call with a function heading below. Parameter tables give types and defaults; required parameters must be supplied. Return tables are progressive contracts; prose describes failure and limits.
Implementation: [src/host/text_ops.rs](../src/host/text_ops.rs)
For development, keep EN/ZH behavior aligned and compose public libraries first. Add host primitives only at OS or algorithm boundaries and declare their effects. Run marqdo check --narrative-ratio on this file and the real workflows in tests/stdlib_tasks.rs after edits.
## str_trim
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| s | text | required | Input text |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | text | Trim ends of string s |
Use `s` as the inputs for this operation.
Effects: pure.
Call this function directly:
```sh
marqdo call lib/text.mq.md str_trim 's=" readable text "' --effects none --json
```
Trim ends of string s.
Return *[Trim](trim(`s`))*.
## str_split
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| s | text | required | Input text |
| sep | text | required | Text separator used for splitting or joining |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | list | Split string s on separator sep |
Use `s`, `sep` as the inputs for this operation.
Effects: pure.
Split string s on separator sep.
Return *[Split](split(value=`s`, sep=`sep`))*.
## str_join
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| xs | any | required | Input list or collection, as required by this operation |
| sep | text | required | Text separator used for splitting or joining |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | text | Join list xs with separator sep |
Use `xs`, `sep` as the inputs for this operation.
Effects: pure.
Join list xs with separator sep.
Return *[Join](join(value=`xs`, sep=`sep`))*.
## contains
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| text | text | required | Text supplied by the caller |
| sub | any | required | Substring to search for |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | bool | Whether text contains substring sub |
Use `text`, `sub` as the inputs for this operation.
Effects: pure.
Whether text contains substring sub.
Caller: [text.contains] text="hello" sub="ell"
Return *[Whether text contains substring sub](> host_text_contains text=`text` sub=`sub`)*.
## starts_with
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| text | text | required | Text supplied by the caller |
| prefix | text | required | Prefix added to the generated name or tested against text |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | bool | Whether text starts with prefix |
Use `text`, `prefix` as the inputs for this operation.
Effects: pure.
Whether text starts with prefix.
Return *[Whether text starts with prefix](> host_text_starts_with text=`text` prefix=`prefix`)*.
## ends_with
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| text | text | required | Text supplied by the caller |
| suffix | any | required | Text suffix to test |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | bool | Whether text ends with suffix |
Use `text`, `suffix` as the inputs for this operation.
Effects: pure.
Whether text ends with suffix.
Return *[Whether text ends with suffix](> host_text_ends_with text=`text` suffix=`suffix`)*.
## replace
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| text | text | required | Text supplied by the caller |
| old | any | required | Exact substring to replace |
| new | any | required | Replacement text |
| count | any | None | Maximum replacement count; None uses the documented default |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | text | Literal replace in text: replace old with new, optional count |
Use `text`, `old`, `new`, `count`=None as the inputs for this operation.
Effects: pure.
Literal replace in text: replace old with new, optional count.
Return *[Literal replace in text: replace old with new, optional count](> host_text_replace text=`text` old=`old` new=`new` count=`count`)*.
## to_upper
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| text | text | required | Text supplied by the caller |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | text | Uppercase text |
Use `text` as the inputs for this operation.
Effects: pure.
Uppercase text.
Return *[Uppercase text](> host_text_to_upper text=`text`)*.
## to_lower
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| text | text | required | Text supplied by the caller |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | text | Lowercase text |
Use `text` as the inputs for this operation.
Effects: pure.
Lowercase text.
Return *[Lowercase text](> host_text_to_lower text=`text`)*.
## repeat
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| text | text | required | Text supplied by the caller |
| n | number | required | Number of repetitions |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | text | Repeat text n times |
Use `text`, `n` as the inputs for this operation.
Effects: pure.
Repeat text n times.
Return *[Repeat text n times](> host_text_repeat text=`text` n=`n`)*.
## pad
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| text | text | required | Text supplied by the caller |
| width | number | required | Target padded character width |
| fill | any | None | Single padding character |
| align | any | None | Padding alignment: left, right or center |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | text | Pad text to width with fill and align (left / right / center) |
Use `text`, `width`, `fill`=None, `align`=None as the inputs for this operation.
Effects: pure.
Pad text to width with fill and align (left / right / center).
Return *[Pad text to width with fill and align (left / right / center)](> host_text_pad text=`text` width=`width` fill=`fill` align=`align`)*.