string_pipeline 0.13.4

A flexible, template-driven string transformation pipeline for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# Template System

This document describes the template syntax used by `string_pipeline`.

## Contents

- [Quick Start]#quick-start
- [Template Syntax]#template-syntax
- [Evaluation Rules]#evaluation-rules
- [Multi-template Strings]#multi-template-strings
- [Operation Reference]#operation-reference
- [Range Specifications]#range-specifications
- [Escaping Rules]#escaping-rules
- [Map Semantics]#map-semantics
- [Debug Mode]#debug-mode
- [Troubleshooting]#troubleshooting

## Quick Start

Templates are enclosed in braces and operations are separated by `|`.

```text
{operation1|operation2|operation3}
```

Example:

```bash
string-pipeline "{split:,:..|map:{upper}|join:-}" "hello,world,test"
# HELLO-WORLD-TEST
```

## Template Syntax

```text
{[!][operation[|operation...]]}
```

| Component      | Required | Description                                |
|----------------|----------|--------------------------------------------|
| `{` `}`        | yes      | Template delimiters                        |
| `!`            | no       | Debug flag, immediately after `{`          |
| operation list | no       | One or more operations separated by a pipe |

Notes:

- `{}` is valid and returns the input unchanged.
- Operations are evaluated from left to right.
- A template can contain either only a template block (`{...}`) or literal text with one or more blocks (multi-template
mode).

### Shorthand syntax

Shorthand index/range syntax is equivalent to splitting on a space separator.

```text
{1}      == {split: :1}
{1..3}   == {split: :1..3}
{..}     == {split: :..}
```

## Evaluation Rules

The pipeline works with two runtime value types:

- `String`
- `List<String>`

Operations are type-checked during execution.

### Type categories

| Category         | Operations                                                                                                                       |
|------------------|----------------------------------------------------------------------------------------------------------------------------------|
| string -> string | `replace`, `upper`, `lower`, `trim`, `substring`, `append`, `prepend`, `surround`, `quote`, `strip_ansi`, `pad`, `regex_extract` |
| list -> list     | `slice`, `sort`, `unique`, `map`                                                                                                 |
| type-preserving  | `filter`, `filter_not`, `reverse`                                                                                                |
| type-converting  | `split`, `join`                                                                                                                  |

### Final list rendering

If a pipeline ends with a list and no explicit `join`, the list is rendered as a string using the separator from the
most recent `split` or `join` operation in that pipeline.

```text
{split:,:..}                    # "a,b,c" -> "a,b,c"
{split:,:..|sort}               # "c,a,b" -> "a,b,c"
{split:,:..|join:-}             # "a,b,c" -> "a-b-c"
{split:\|:..|split:a:..}        # "apple|banana|cherry" -> "appleabananaacherry"
```

Use an explicit `join` as the final step when output format must be fixed.

## Multi-template Strings

A multi-template combines literal text and one or more template sections.

```text
User: {split:,:0} Score: {split:,:1}
```

Examples:

```bash
string-pipeline "Hello {upper}, welcome" "world"
# Hello WORLD, welcome

string-pipeline "Name: {split: :0} Age: {split: :1}" "John 25"
# Name: John Age: 25

string-pipeline "Host: {split: :0|split:=:1} Port: {split: :1|split:=:1} SSL: {split: :-1|split:=:1|upper}" "host=localhost port=8080 ssl=true"
# Host: localhost Port: 8080 SSL: TRUE
```

### Caching behavior

Within one `format()` call, repeated template sections with the same operation sequence and input are cached.

```bash
string-pipeline "First: {split:,:0} Again: {split:,:0}" "apple,banana,cherry"
# First: apple Again: apple
```

## Operation Reference

### split

- Syntax: `split:SEPARATOR:RANGE`
- Input: string or list
- Output: string (index range) or list (range)

Notes:

- `RANGE` is required; use `..` for all parts.
- For list input, each item is split and the results are flattened.

```text
{split:,:..}            # split all items by comma
{split: :0..2}          # keep first two parts
{split:\n:-1}          # keep last line

{split: :..|map:{append:,x}|split:,:..|join:-}
# "a b" -> "a-x-b-x"
```

### slice

- Syntax: `slice:RANGE`
- Input: list
- Output: list

```text
{split:,:..|slice:1..3}   # "a,b,c,d" -> "b,c"
```

### join

- Syntax: `join:SEPARATOR`
- Input: list or string
- Output: string

Behavior:

- On lists, joins items using `SEPARATOR`.
- On strings, returns the input unchanged.

```text
{split:,:..|join:-}       # "a,b,c" -> "a-b-c"
{split:,:..|join:}        # "a,b,c" -> "abc"
{join:-}                  # "hello" -> "hello"
```

### substring

- Syntax: `substring:RANGE`
- Input: string
- Output: string

```text
{substring:1..4}          # "hello" -> "ell"
{substring:-3..}          # "hello" -> "llo"
```

### trim

- Syntax: `trim[:CHARS][:DIRECTION]`
- Input: string
- Output: string
- `DIRECTION`: `both` (default), `left`, `right`

```text
{trim}                    # trim whitespace
{trim:left}               # trim from start only
{trim:xy}                 # trim x/y from both ends
{trim:*-+:right}          # trim from right only
```

### pad

- Syntax: `pad:WIDTH[:CHAR[:DIRECTION]]`
- Input: string
- Output: string
- `DIRECTION`: `left`, `right` (default), `both`

```text
{pad:5}                   # "hi" -> "hi   "
{pad:5:0:left}            # "42" -> "00042"
```

### upper

- Syntax: `upper`
- Input: string
- Output: string

```text
{upper}                   # "hello" -> "HELLO"
```

### lower

- Syntax: `lower`
- Input: string
- Output: string

```text
{lower}                   # "HELLO" -> "hello"
```

### append

- Syntax: `append:TEXT`
- Input: string
- Output: string

```text
{append:.txt}             # "file" -> "file.txt"
```

### prepend

- Syntax: `prepend:TEXT`
- Input: string
- Output: string

```text
{prepend:/tmp/}           # "file.txt" -> "/tmp/file.txt"
```

### surround

- Syntax: `surround:TEXT`
- Input: string
- Output: string

```text
{surround:"}             # "hello" -> "\"hello\""
{surround:**}             # "text" -> "**text**"
```

### quote

- Syntax: `quote:TEXT`
- Input: string
- Output: string

`quote` is an alias of `surround`.

```text
{quote:'}                 # "hello" -> "'hello'"
```

### replace

- Syntax: `replace:s/PATTERN/REPLACEMENT/FLAGS`
- Input: string
- Output: string
- Supported flags: `g`, `i`, `m`, `s`

```text
{replace:s/hello/hi/}     # first match
{replace:s/\d+/NUM/g}     # global replacement
{replace:s/(.+)/[$1]/}    # capture groups
```

### regex_extract

- Syntax: `regex_extract:PATTERN[:GROUP]`
- Input: string
- Output: string

If there is no match, returns an empty string.

```text
{regex_extract:\d+}        # first number
{regex_extract:@(.+):1}    # group extraction
```

### sort

- Syntax: `sort[:DIRECTION]`
- Input: list
- Output: list
- `DIRECTION`: `asc` (default), `desc`

```text
{split:,:..|sort}          # "c,a,b" -> "a,b,c"
{split:,:..|sort:desc}     # "a,b,c" -> "c,b,a"
```

### reverse

- Syntax: `reverse`
- Input: string or list
- Output: same type as input

```text
{reverse}                  # "hello" -> "olleh"
{split:,:..|reverse}       # "a,b,c" -> "c,b,a"
```

### unique

- Syntax: `unique`
- Input: list
- Output: list

Keeps first occurrence order.

```text
{split:,:..|unique}        # "a,b,a,c,b" -> "a,b,c"
```

### filter

- Syntax: `filter:PATTERN`
- Input: string or list
- Output: same type as input

```text
{split:,:..|filter:^test}  # keep list items starting with "test"
```

### filter_not

- Syntax: `filter_not:PATTERN`
- Input: string or list
- Output: same type as input

```text
{split:,:..|filter_not:^#} # remove items starting with "#"
```

### strip_ansi

- Syntax: `strip_ansi`
- Input: string
- Output: string

```text
{strip_ansi}               # remove ANSI escape sequences
```

### map

- Syntax: `map:{operation1|operation2|...}`
- Input: list
- Output: list

Notes:

- Nested `map` is not allowed.
- String operations and list operations are both available inside `map`.

```text
{split:,:..|map:{trim|upper}}                    # " a , b " -> "A,B"
{split:,:..|map:{split: :..|join:-}}             # "hello world,foo bar" -> "hello-world,foo-bar"
{split:,:..|map:{split: :..|filter:o}}           # "hello world,foo bar,test orange" -> "hello world,foo,orange"
```

### shorthand index and ranges

Shorthand forms operate as `split` with a space separator.

```text
{1}                      # "a b c d" -> "b"
{-1}                     # "a b c d" -> "d"
{1..3}                   # "a b c d" -> "b c"
{1..=3}                  # "a b c d" -> "b c d"
{..3}                    # "a b c d" -> "a b c"
{..}                     # "a b c d" -> "a b c d"
```

## Range Specifications

Ranges are used by `split`, `slice`, `substring`, and shorthand syntax.

| Syntax  | Description         |
|---------|---------------------|
| `N`     | single index        |
| `N..M`  | exclusive range     |
| `N..=M` | inclusive range     |
| `N..`   | from index to end   |
| `..M`   | from start to `M-1` |
| `..=M`  | from start to `M`   |
| `..`    | full range          |

Negative indexes count from the end (`-1` is last item).

Edge behavior:

- Single indexes are clamped to valid bounds (out-of-range resolves to nearest valid index).
- Ranges are clamped to valid bounds.
- If computed start is greater than or equal to end, the result is empty.
- Empty input always returns empty output.

## Escaping Rules

### Simple arguments

For operations such as `append`, `prepend`, `join`, `surround`, `quote`, and `trim` arguments, escape these characters when needed:

| Character | Escape |
|-----------|--------|
| `:`       | `\:`   |
| pipe      | `\|`   |
| `{`       | `\{`   |
| `}`       | `\}`   |
| `\`       | `\\`   |

### Special escape sequences

`process_arg` supports these sequences in simple/split arguments:

- `\n` newline
- `\t` tab
- `\r` carriage return
- `\:` literal colon
- `\|` literal pipe
- `\\` literal backslash
- `\/` literal slash
- `\{` literal `{`
- `\}` literal `}`

Any other `\X` sequence is treated as literal `X`.

### Regex arguments

For `filter`, `filter_not`, and `regex_extract`, the pattern is read as raw template content and passed to the regex engine.

Examples:

```text
{append:\:value}         # append ":value"
{split:\:\::..|join:-}   # split on "::"
{filter:\.txt$}          # regex pattern for .txt suffix
```

When calling from a shell, prefer single quotes around templates to reduce extra shell escaping.

## Map Semantics

`map` runs a sub-pipeline for each list item.

Important details:

- Each item is processed independently.
- If a map item ends as a list, it is auto-rendered to a string using that sub-pipeline's current separator.
- After mapping, the outer pipeline continues with a list of mapped strings.

```text
Input: "hello world,foo bar,test orange"
Template: {split:,:..|map:{split: :..|filter:o}}
Output: "hello world,foo,orange"
```

## Debug Mode

Enable debug mode with either:

- Inline template flag: `{!...}`
- CLI flag: `--debug` or `-d`

Debug output is written to `stderr`; final result is written to `stdout`.

`--quiet` suppresses debug output even when debug is enabled inline or via CLI.

```bash
string-pipeline "{!split:,:..|map:{upper}|join:-}" "hello,world"
```

For full debug output structure and examples, see `docs/debug-system.md`.

## Troubleshooting

Common issues:

- Parse errors: check missing braces, missing separators, or invalid operation names.
- Type errors: apply string-only operations through `map` when working with lists.
- Empty output: verify regex and range expressions; filter/range steps may remove all items.

Quick checks:

```bash
string-pipeline --validate '{split:,:..|map:{upper}|join:-}'
string-pipeline -d '{split:,:..|map:{upper}|join:-}' 'a,b,c'
```

Related documentation:

- `docs/command-line-options.md`
- `docs/debug-system.md`
- `docs/benchmarking.md`