openscad/MCAD — pull request #25 by davidson16807 (2014-11-27 14:59:52+00:00)
Title: Implemented string functions and regular expression engine

Thingiverse project: http://www.thingiverse.com/thing:526023
Github project: https://github.com/davidson16807/relativity.scad
Also based in part off https://github.com/openscad/MCAD/pull/13

I have implemented string functions common to the libraries I see in most modern languages. Unit tests can be found in the github project, above - these test do not fit the structure of MCAD's python unit tests, but they should provide better code coverage than simply ensuring valid syntax. I have omitted these tests from the pull request.

The following functions are included:
* before(string, end)
* after(string, start)
* between(string, start, end)
* substring(string, start, length)
* upper(string)
* lower(string)
* is_empty(string)
* is_null_or_empty(string)
* is_null_or_whitespace(string)
* equals(this, that, ignore_case=false)
* starts_with(string, start, ignore_case=false)
* ends_with(string, end, ignore_case=false)
* reverse(string)
* trim(string)
* parse_int(string, base)
* token(string, index)
* join(strings, delimiter)
* split(string, seperator=" ", index=0, ignore_case = false)
* index_of(string, goal, index=0, ignore_case=false, regex=false)
* contains(string, substring, ignore_case=false, regex=false)
* replace(string, replaced, replacement, ignore_case=false, regex=false)
* grep(string, pattern, index=0)

The regular expression engine is built using a spinoff of a standard implementation that first parses the regular expression to a syntax tree using the shunting yard algorithm, then traverses a string using the syntax tree in a manner reminiscent to Thompson's algorithm. Due to limitations in OpenSCAD, no validation is performed against regular expressions. Branching is performed through ternary conditionals. Looping is performed through recursion. Stacks are implemented via nested, right associative lists, i.e. "["foo", ["bar", []]]", which is easiest to work with given the current lack of a working "concat" in prod.

Regular expressions support the following:
* implicit concatenation
* option ("?")
* kleene star ("*")
* kleene plus ("+")
* alternation ("|")
* escape characters ("\\d", "\\s", "\\w", "\\(")
* anchors ("$", "^")
* curly bracket repetition (".{2,3}", ".{2}")
* character sets ("[aeiou]")
* negative character sets ("[^aeiou]")
* character ranges ("[0-9abcdef]")