OpenFlexure Microscope OpenSCAD docs

libs/string_utils.scad

A collection of string utilities for OpenSCAD.

These functions provide basic string manipulation features that are not available natively in stable OpenSCAD.

function str_cat(input)
[Source]
function str_cat(input) =
    // Empty list becomes ""
    len(input) == 0 ? "" :
        // Single element is coerced into a string and output
        len(input) == 1 ? str(input[0]) :
            // 2 elements are concatedated as strings
            len(input) == 2 ? str(input[0], input[1]) :
                // Greater then than 2 elements, the first 2 strings in the list are
                // combined, then recursively recall this function.
                str_cat(
                    concat(
                        [str(input[0], input[1])],
                        [for (i = [2:len(input)-1]) input[i]]
                    )
                );

Concatenate a list of strings into a single string.

This function reduces the list recursively, concatenating pairs of elements until a single string remains.

input:A list of strings

Returns:A single string formed by concatenating all elements of input

function sub_str(string, start, end)
[Source]
function sub_str(string, start, end) =
    str_cat([ for (i = [start:end]) string[i] ]);

Return a substring using inclusive start and end indices.

No bounds checking is performed. Supplying indices outside the valid range may result in undefined behaviour.

string:The source string

start:Starting index (inclusive, zero-based)

end:Ending index (inclusive, zero-based)

Returns:The substring from start to end

function split_string_lines(string)
[Source]
function split_string_lines(string) = let(
    // get position of any \n
    pos = search("\n", string, 0)[0],
    s_pos = concat([0], [for (p=pos) p+1]),
    e_pos = concat( [for (p=pos) p-1], len(string)-1)
) [for (i = [0:len(pos)]) sub_str(string, s_pos[i], e_pos[i])];

Split a multi-line string into a list of one-line strings.

string:The input string

Returns:A list of strings, one per line