1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include "string.ih"
/*
Each character of the String is inspected in turn. The following
situations are recognized:
The character is in the set of separators:
If currently constructing an element, the element is finished, and the
first non-separator is searched. The thus recognized separator is
handled next.
The character is a double quote:
If not currently constructing an element, read all subsequent
characters until a matching dquote is found. If, while doing so, an
escape character is encountered, then add the escape char and the char
following the escape char. Handle the recognized dquoted string next.
If currently constructing an element, add the dquote to the current
element.
If the end-of-string is encountered, flag an error.
The character is a single quote:
If not currently constructing an element, read all subsequent
characters until a matching squote is found. If, while doing so, an
escape character is encountered, then add the escape char and the char
following the escape char. Handle the recognized squoted string next.
If currently constructing an element, add the squote to the current
element.
If the end-of-string is encountered, flag an error.
The character is another character:
If not currently constructing an element, read all subsequent
characters until a separator or the end of the string is found. If,
while doing so, escape character is encountered, then add the escape
char and the char following the escape char. Handle the recognized
squoted string next.
*/
size_t String::split(vector<SplitPair> *words,
string const &str, char const *sep, bool addEmpty)
{
words->clear();
const_iterator from = str.begin();
const_iterator beyond = str.end();
string separators(sep);
const_iterator until;
while (from != beyond)
{
Type type = nextField(str, &until, // get the from field, `until'
from, separators); // points beyond the field's
// last character
// see if it is a quoted string
bool quoted = (type == DQUOTE || type == SQUOTE);
from += quoted; // skip the quote of quoted strings
// add the field to `words'
if (type != SEPARATOR || addEmpty)
words->push_back(SplitPair(string(from, until), type));
// update from: skip the quote of quoted
from = until + quoted; // strings
}
return words->size();
}
|