mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-08-01 12:30:33 +02:00
added split_str
This commit is contained in:
@@ -39,6 +39,7 @@ Some modules may depend on other modules. For example, the `polyline2d` module d
|
|||||||
- Functon
|
- Functon
|
||||||
- [rotate_p](https://openhome.cc/eGossip/OpenSCAD/lib-rotate_p.html)
|
- [rotate_p](https://openhome.cc/eGossip/OpenSCAD/lib-rotate_p.html)
|
||||||
- [sub_str](https://openhome.cc/eGossip/OpenSCAD/lib-sub_str.html)
|
- [sub_str](https://openhome.cc/eGossip/OpenSCAD/lib-sub_str.html)
|
||||||
|
- [split_str](https://openhome.cc/eGossip/OpenSCAD/lib-split_str.html)
|
||||||
|
|
||||||
- Path
|
- Path
|
||||||
- [circle_path](https://openhome.cc/eGossip/OpenSCAD/lib-circle_path.html)
|
- [circle_path](https://openhome.cc/eGossip/OpenSCAD/lib-circle_path.html)
|
||||||
|
16
docs/lib-split_str.md
Normal file
16
docs/lib-split_str.md
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# split_str
|
||||||
|
|
||||||
|
Splits the given string around matches of the given delimiting character. It depeneds on the `sub_str` function so remember to `include <sub_str.scad>`.
|
||||||
|
|
||||||
|
## Parameters
|
||||||
|
|
||||||
|
- `t` : The source string.
|
||||||
|
- `delimiter` : The delimiting character.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
include <sub_str.scad>;
|
||||||
|
include <split_str.scad>;
|
||||||
|
|
||||||
|
echo(split_str("hello,world", ",")); // ECHO: ["hello", "world"]
|
||||||
|
|
22
src/split_str.scad
Normal file
22
src/split_str.scad
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
* split_str.scad
|
||||||
|
*
|
||||||
|
* Splits the given string around matches of the given delimiting character.
|
||||||
|
* It depends on the sub_str function so remember to include sub_str.scad.
|
||||||
|
*
|
||||||
|
* @copyright Justin Lin, 2017
|
||||||
|
* @license https://opensource.org/licenses/lgpl-3.0.html
|
||||||
|
*
|
||||||
|
* @see https://openhome.cc/eGossip/OpenSCAD/lib-split_str.html
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
|
||||||
|
function _split_t_by(idxs, t, ts = [], i = -1) =
|
||||||
|
i == -1 ? _split_t_by(idxs, t, [sub_str(t, 0, idxs[0])], i + 1) : (
|
||||||
|
i == len(idxs) - 1 ? concat(ts, sub_str(t, idxs[i] + 1)) :
|
||||||
|
_split_t_by(idxs, t, concat(ts, sub_str(t, idxs[i] + 1, idxs[i + 1])), i + 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
function split_str(t, delimiter) =
|
||||||
|
len(search(delimiter, t)) == 0 ?
|
||||||
|
[t] : _split_t_by(search(delimiter, t, 0)[0], t);
|
Reference in New Issue
Block a user