Function split_string (o2scl)

O2scl : Function List

void o2scl::split_string(std::string str, std::vector<std::string> &sv)

Split a string into words using whitespace for delimiters and (partially) respecting quotes.

This function separates a string into words, and handles words that begin with a

"</tt> by adding more words until finding

one which ends with another <tt>"

. Strings like
this is a test
get parsed as “this”, “is”, “a”, “test” and strings like
"this is" a test
get parsed as “this is”, “a”, “test”.

This is used to reformat command descriptions and help text for the screen width in cli::comm_option_help(), to process lines read from a file in cli::comm_option_run(), and to process input in cli::run_interactive().

Todo

In function split_string(), the rules surrounding spaces and quotes are not well documented.

  • Future: Replace with a better algorithm. Should quotes be escaped?

Note

The string vector is not emptied before processing, and entries from str are added to the end of sv.

Note

The parsing algorithm here is simple-minded and can produce unexpected results in several ways. For example, it may not properly handle nested quotes, like

"""test" test2"

test3"

.