Text Processing

- Implemented splitq, which can handle quoted and oddly-quoted string
  splits.
This commit is contained in:
Paul Beckingham 2011-05-16 00:22:14 -04:00
parent 0308ee953a
commit 05b3fa0bb6
3 changed files with 59 additions and 5 deletions

View file

@ -33,10 +33,10 @@
#include <string>
#include <strings.h>
#include <ctype.h>
#include "Context.h"
#include "util.h"
#include "text.h"
#include "utf8.h"
#include <Context.h>
#include <util.h>
#include <text.h>
#include <utf8.h>
extern Context context;
@ -59,6 +59,48 @@ void wrapText (
}
}
////////////////////////////////////////////////////////////////////////////////
void splitq (
std::vector<std::string>& results,
const std::string& input,
const char delimiter)
{
results.clear ();
std::string::size_type start = 0;
std::string::size_type i = 0;
std::string word;
bool in_quote = false;
char quote;
while (utf8_next_char (input, i))
{
if (in_quote)
{
if (input[i] == quote)
{
in_quote = false;
}
}
else
{
if (input[i] == delimiter)
{
results.push_back (unquoteText (input.substr (start, i - start)));
start = i + 1;
}
else if (input[i] == '\'' ||
input[i] == '"')
{
quote = input[i];
in_quote = true;
}
}
}
results.push_back (unquoteText (input.substr (start)));
}
////////////////////////////////////////////////////////////////////////////////
void split (
std::vector<std::string>& results,