Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented string functions and regular expression engine #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Implemented string functions and regular expression engine #25

wants to merge 1 commit into from

Conversation

davidson16807
Copy link

Thingiverse project: http://www.thingiverse.com/thing:526023
Github project: https://github.com/davidson16807/relativity.scad
Also based in part off #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]")

@hyperair hyperair mentioned this pull request Jul 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant