Skip to content

Commit 9df6365

Browse files
committed
docs(readme): update slice and substring and other minor changes
1 parent 378fdc4 commit 9df6365

File tree

2 files changed

+80
-69
lines changed

2 files changed

+80
-69
lines changed

README.md

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# String Template Processor
1+
# String Pipeline
22

33
[![Crates.io](https://img.shields.io/crates/v/string_pipeline.svg)](https://crates.io/crates/string_pipeline)
44
[![Docs.rs](https://docs.rs/string_pipeline/badge.svg)](https://docs.rs/string_pipeline)
@@ -161,36 +161,36 @@ Arguments to operations are separated by `:`.
161161

162162
#### Supported Operations
163163

164-
| Operation | Syntax | Description |
165-
|-------------------|---------------------------------------------|---------------------------------------------|
166-
| Split | `split:<sep>:<range>` | Split by separator, select by index/range |
167-
| Join | `join:<sep>` | Join a list with separator |
168-
| Substring | `slice:<range>` | Extract substrings |
169-
| Replace | `replace:s/<pattern>/<replacement>/<flags>` | Regex replace (sed-like) |
170-
| Uppercase | `upper` | Convert to uppercase |
171-
| Lowercase | `lower` | Convert to lowercase |
172-
| Trim | `trim` | Trim whitespace |
173-
| Strip | `strip:<chars>` | Trim custom characters |
174-
| Append | `append:<suffix>` | Append text |
175-
| Prepend | `prepend:<prefix>` | Prepend text |
176-
| StripAnsi | `strip_ansi` | Removes ansi escape sequences |
177-
| Filter | `filter:<regex_pattern>` | Keep only items matching regex pattern |
178-
| FilterNot | `filter_not:<regex_pattern>` | Remove items matching regex pattern |
179-
| Slice | `filter_not:<regex_pattern>` | Select elements from a list |
164+
| Operation | Syntax | Description |
165+
| ---------- | ----------------------------------------- | ------------------------------------------- |
166+
| Split | `split:<sep>:<range>` | Split by separator, select by index/range |
167+
| Join | `join:<sep>` | Join a list with separator |
168+
| Substring | `substring:<range>` | Extract substrings |
169+
| Replace | `replace:s/<pattern>/<replacement>/<flags>` | Regex replace (sed-like) |
170+
| Uppercase | `upper` | Convert to uppercase |
171+
| Lowercase | `lower` | Convert to lowercase |
172+
| Trim | `trim` | Trim whitespace |
173+
| Strip | `strip:<chars>` | Trim custom characters |
174+
| Append | `append:<suffix>` | Append text |
175+
| Prepend | `prepend:<prefix>` | Prepend text |
176+
| StripAnsi | `strip_ansi` | Removes ansi escape sequences |
177+
| Filter | `filter:<regex_pattern>` | Keep only items matching regex pattern |
178+
| FilterNot | `filter_not:<regex_pattern>` | Remove items matching regex pattern |
179+
| Slice | `slice:<range>` | Select elements from a list |
180180

181181
#### Range Specifications
182182

183183
Ranges use Rust-like syntax and support negative indices like Python:
184184

185-
| Range | Description | Example |
186-
|-------|-------------|---------|
187-
| `N` | Single index | `{split:,:1}` → second element |
188-
| `N..M` | Exclusive range | `{split:,:1..3}` → elements 1,2 |
189-
| `N..=M` | Inclusive range | `{split:,:1..=3}` → elements 1,2,3 |
190-
| `N..` | From N to end | `{split:,:2..}` → from 2nd to end |
191-
| `..N` | From start to N | `{split:,:..3}` → first 3 elements |
192-
| `..=N` | From start to N inclusive | `{split:,:..=2}` → first 3 elements |
193-
| `..` | All elements | `{split:,:..)` → all elements |
185+
| Range | Description | Example |
186+
| ----- | ------------------------- | ---------------------------------- |
187+
| `N` | Single index | `{split:,:1}` → second element |
188+
| `N..M` | Exclusive range | `{split:,:1..3}` → elements 1,2 |
189+
| `N..=M` | Inclusive range | `{split:,:1..=3}` → elements 1,2,3 |
190+
| `N..` | From N to end | `{split:,:2..}` → from 2nd to end |
191+
| `..N` | From start to N | `{split:,:..3}` → first 3 elements |
192+
| `..=N` | From start to N inclusive | `{split:,:..=2}` → first 3 elements |
193+
| `..` | All elements | `{split:,:..)` → all elements |
194194

195195
Negative indices count from the end:
196196

@@ -230,10 +230,26 @@ The parser intelligently handles pipe characters (`|`) based on context:
230230
string-pipeline "{split:,:-1}" "a,b,c"
231231
# Output: c
232232

233+
# Get the second word using the short form (only works with space separator)
234+
string-pipeline "{1}" "foo bar baz"
235+
# Output: bar
236+
233237
# Get a range of items
234238
string-pipeline "{split:,:1..=3}" "a,b,c,d,e"
235239
# Output: b,c,d
236240

241+
# Get a range of words using the short form (only works with space separator)
242+
string-pipeline "{1..3}" "foo bar baz qux"
243+
# Output: bar baz
244+
245+
# Get a range of items, another way
246+
string-pipeline "{split:,:..|slice:1..=3}" "a,b,c,d,e"
247+
# Output: b,c,d
248+
249+
# Substring string characters
250+
string-pipeline "{substring:1..=3}" "hello"
251+
# Output: ell
252+
237253
# Replace 'foo' with 'bar' globally
238254
string-pipeline "{replace:s/foo/bar/g}" "foo foo"
239255
# Output: bar bar
@@ -245,6 +261,7 @@ string-pipeline "{upper|append:!}" "hello"
245261
# Prepend with a colon (escaped)
246262
string-pipeline "{prepend:\:foo}" "bar"
247263
# Output: :foobar
264+
248265
```
249266

250267
### Advanced
@@ -254,10 +271,6 @@ string-pipeline "{prepend:\:foo}" "bar"
254271
string-pipeline "{split:,:0..2|join:-|replace:s/a/X/|upper}" "a,b,c"
255272
# Output: X-B
256273

257-
# Slice string characters
258-
string-pipeline "{slice:1..=3}" "hello"
259-
# Output: ell
260-
261274
# Split, trim each item, then prepend
262275
echo " a , b , c " | string-pipeline "{split:,:..|trim|prepend:item_}"
263276
# Output: item_a,item_b,item_c
@@ -287,7 +300,7 @@ echo "2023-01-01 ERROR Failed to connect" | string-pipeline "{split: :1..|join:
287300
# Output: error failed to connect
288301

289302
# Clean colored git output
290-
git log --oneline --color=always | string-pipeline "{split:\\n:..|strip_ansi|join:\\n}"
303+
git log --oneline --color=always | string-pipeline "{split:\n:..|strip_ansi|join:\n}"
291304

292305
# Process ls colored output
293306
ls --color=always | string-pipeline "{strip_ansi}"
@@ -301,19 +314,7 @@ echo -e "\x1b[31mred\x1b[0m,\x1b[32mgreen\x1b[0m" | \
301314
# Output: RED | GREEN
302315

303316
# Process log files with ANSI codes
304-
cat colored.log | string-pipeline "{split:\n:-10..|strip_ansi|join:\\n}"
305-
```
306-
307-
### Shorthand
308-
309-
```sh
310-
# Get the second word (space-separated)
311-
string-pipeline "{1}" "foo bar baz"
312-
# Output: bar
313-
314-
# Get a range of words
315-
string-pipeline "{1..3}" "foo bar baz qux"
316-
# Output: bar baz
317+
cat colored.log | string-pipeline "{split:\n:-10..|strip_ansi|join:\n}"
317318
```
318319

319320
### Debug Mode
@@ -352,6 +353,16 @@ cargo test
352353

353354
---
354355

356+
## Benchmarks
357+
358+
Run the bench suite:
359+
360+
```sh
361+
cargo bench
362+
```
363+
364+
---
365+
355366
## Contributing
356367

357368
Contributions and suggestions are welcome!

src/pipeline/template.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -80,36 +80,36 @@ impl Template {
8080
///
8181
/// ## Supported Operations
8282
///
83-
/// | Operation | Syntax | Description |
84-
/// |-------------------|---------------------------------------------|---------------------------------------------|
85-
/// | Split | `split:<sep>:<range>` | Split by separator, select by index/range |
86-
/// | Join | `join:<sep>` | Join a list with separator |
87-
/// | Substring | `slice:<range>` | Extract substrings |
88-
/// | Replace | `replace:s/<pattern>/<replacement>/<flags>` | Regex replace (sed-like) |
89-
/// | Uppercase | `upper` | Convert to uppercase |
90-
/// | Lowercase | `lower` | Convert to lowercase |
91-
/// | Trim | `trim` | Trim whitespace |
92-
/// | Strip | `strip:<chars>` | Trim custom characters |
93-
/// | Append | `append:<suffix>` | Append text |
94-
/// | Prepend | `prepend:<prefix>` | Prepend text |
95-
/// | StripAnsi | `strip_ansi` | Removes ansi escape sequences |
96-
/// | Filter | `filter:<regex_pattern>` | Keep only items matching regex pattern |
97-
/// | FilterNot | `filter_not:<regex_pattern>` | Remove items matching regex pattern |
98-
/// | Slice | `filter_not:<regex_pattern>` | Select elements from a list |
83+
/// | Operation | Syntax | Description |
84+
/// | --------- | -------------------------------------------- | ----------------------------------------- |
85+
/// | Split | `split:<sep>:<range>` | Split by separator, select by index/range |
86+
/// | Join | `join:<sep>` | Join a list with separator |
87+
/// | Substring | `substring:<range>` | Extract substrings |
88+
/// | Replace | `replace:s/<pattern>/<replacement>/<flags>` | Regex replace (sed-like) |
89+
/// | Uppercase | `upper` | Convert to uppercase |
90+
/// | Lowercase | `lower` | Convert to lowercase |
91+
/// | Trim | `trim` | Trim whitespace |
92+
/// | Strip | `strip:<chars>` | Trim custom characters |
93+
/// | Append | `append:<suffix>` | Append text |
94+
/// | Prepend | `prepend:<prefix>` | Prepend text |
95+
/// | StripAnsi | `strip_ansi` | Removes ansi escape sequences |
96+
/// | Filter | `filter:<regex_pattern>` | Keep only items matching regex pattern |
97+
/// | FilterNot | `filter_not:<regex_pattern>` | Remove items matching regex pattern |
98+
/// | Slice | `slice:<range>` | Select elements from a list |
9999
///
100100
/// ## Range Specifications
101101
///
102102
/// Ranges use Rust-like syntax and support negative indices like Python:
103103
///
104-
/// | Range | Description | Example |
105-
/// |-------|-------------|---------|
106-
/// | `N` | Single index | `{split:,:1}` → second element |
107-
/// | `N..M` | Exclusive range | `{split:,:1..3}` → elements 1,2 |
108-
/// | `N..=M` | Inclusive range | `{split:,:1..=3}` → elements 1,2,3 |
109-
/// | `N..` | From N to end | `{split:,:2..}` → from 2nd to end |
110-
/// | `..N` | From start to N | `{split:,:..3}` → first 3 elements |
111-
/// | `..=N` | From start to N inclusive | `{split:,:..=2}` → first 3 elements |
112-
/// | `..` | All elements | `{split:,:..)` → all elements |
104+
/// | Range | Description | Example |
105+
/// | ------- | ------------------------- | ------------------------------------ |
106+
/// | `N` | Single index | `{split:,:1}` → second element |
107+
/// | `N..M` | Exclusive range | `{split:,:1..3}` → elements 1,2 |
108+
/// | `N..=M` | Inclusive range | `{split:,:1..=3}` → elements 1,2,3 |
109+
/// | `N..` | From N to end | `{split:,:2..}` → from 2nd to end |
110+
/// | `..N` | From start to N | `{split:,:..3}` → first 3 elements |
111+
/// | `..=N` | From start to N inclusive | `{split:,:..=2}` → first 3 elements |
112+
/// | `..` | All elements | `{split:,:..)` → all elements |
113113
///
114114
/// Negative indices count from the end:
115115
///

0 commit comments

Comments
 (0)