1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-01-17 14:18:13 +01:00

added split_str

This commit is contained in:
Justin Lin 2017-03-29 13:20:17 +08:00
parent 4d904d9eb0
commit 0b27893c59
3 changed files with 39 additions and 0 deletions

View File

@ -39,6 +39,7 @@ Some modules may depend on other modules. For example, the `polyline2d` module d
- Functon
- [rotate_p](https://openhome.cc/eGossip/OpenSCAD/lib-rotate_p.html)
- [sub_str](https://openhome.cc/eGossip/OpenSCAD/lib-sub_str.html)
- [split_str](https://openhome.cc/eGossip/OpenSCAD/lib-split_str.html)
- Path
- [circle_path](https://openhome.cc/eGossip/OpenSCAD/lib-circle_path.html)

16
docs/lib-split_str.md Normal file
View 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
View 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);