7 #ifndef __SHEPP_STR_UTIL_H__
8 #define __SHEPP_STR_UTIL_H__
14 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
15 #define LIMITED_READLINE
30 static char *
trim(
char *line)
36 for (head = line; whitespace(*head); head++)
46 tail = head + strlen(head) - 1;
47 while (tail > head && whitespace(*tail)) {
68 char word[MAX_WORD_LENGTH + 1];
76 while (line[from] !=
'\0' && whitespace(line[from])) {
81 while (line[to] !=
'\0' && !whitespace(line[to])) {
86 if (to - from > MAX_WORD_LENGTH) {
87 printf(
"Error: MAX_WORD_LENGTH is %d.\n", MAX_WORD_LENGTH);
93 strncpy(word, line, to - from);
94 word[to - from] =
'\0';
95 if (strlen(word) > 0) {
96 words.push_back((
string) word);
99 if (line[to] ==
'\0') {
120 static int split(
string input,
string &first,
string &second,
121 string splitter,
bool relaxed =
false)
123 int split_pos = input.find(splitter, 0);
126 if (split_pos == 0) {
129 first = input.substr(0, split_pos);
132 if (split_pos == -1 ||
133 split_pos == (
int) (input.length() - 1)) {
140 second = input.substr(split_pos + 1);
155 if (gather.empty()) {
159 string tmp_str = gather.substr(0, 1);
164 if (words.empty() && tmp_str ==
"\"" &&
165 (gather.length() == 1 || gather.substr(gather.length()-1) !=
"\"")) {
169 if (tmp_str ==
"\"") {
170 gather = gather.substr(1, gather.length() - 1);
172 tmp_str = gather.substr(gather.length() - 1);
173 while (tmp_str !=
"\"") {
178 gather +=
" " + words[0];
180 words.erase(words.begin());
181 tmp_str = gather.substr(gather.length() - 1);
185 gather = gather.substr(0, gather.length() - 1);
198 string numbers = doc;
208 #endif //__SHEPP_STR_UTIL_H__
static int split(string input, string &first, string &second, string splitter, bool relaxed=false)
Definition: SheppStrUtil.H:120
static vector< string > parse_line(char *line)
Given a line, split its words by white space into a string vector (shepp)
Definition: SheppStrUtil.H:63
Useful string manipulation routines used by shepp.
Definition: SheppStrUtil.H:22
static char * trim(char *line)
Removes leading and ending white spaces from line (shepp)
Definition: SheppStrUtil.H:30
static int gsub(string &buffer, const char *pat, const char *drp)
Used for substitution of pat for drp within buffer.
static int quote_gathering(vector< string > &words, string &gather)
Gathers command-line arguments bounded by quotes in a string.
Definition: SheppStrUtil.H:152
static string doc2id(const string &doc)
Removes chars [./-] from document strings.
Definition: SheppStrUtil.H:196