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
183183Ranges 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
195195Negative indices count from the end:
196196
@@ -230,10 +230,26 @@ The parser intelligently handles pipe characters (`|`) based on context:
230230string-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
234238string-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
238254string-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)
246262string-pipeline " {prepend:\:foo}" " bar"
247263# Output: :foobar
264+
248265```
249266
250267### Advanced
@@ -254,10 +271,6 @@ string-pipeline "{prepend:\:foo}" "bar"
254271string-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
262275echo " 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
293306ls --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
357368Contributions and suggestions are welcome!
0 commit comments