mirror of https://github.com/BOINC/boinc.git
New functions remove_duplicate_words(char * or string &). Removes duplicate
words from a space or comma delimited string. Returns a space delimited string. (i.e. "this is this a is test" -> "this is a test"). svn path=/trunk/boinc/; revision=11194
This commit is contained in:
parent
10f398b00f
commit
9e924706a0
27
lib/util.C
27
lib/util.C
|
@ -392,6 +392,33 @@ void strip_whitespace(string& str) {
|
|||
}
|
||||
}
|
||||
|
||||
void remove_duplicate_words(char *str) {
|
||||
char *buffer=(char *)malloc(strlen(str)+1);
|
||||
char *tok=(char *)malloc(strlen(str)+1);
|
||||
strcpy(buffer,str);
|
||||
str[0]=0;
|
||||
char *p=strtok(buffer," ,");
|
||||
while (p) {
|
||||
strlcpy(tok,p,1024);
|
||||
tok[strlen(p)]=' ';
|
||||
tok[strlen(p)+1]=0;
|
||||
if (!strstr(str,tok)) {
|
||||
strcat(str,tok);
|
||||
}
|
||||
p=strtok(NULL," ,");
|
||||
}
|
||||
free(tok);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
void remove_duplicate_words(string &str) {
|
||||
char *buffer=(char *)malloc(str.length()+1);
|
||||
strcpy(buffer,str.c_str());
|
||||
remove_duplicate_words(buffer);
|
||||
str=string(buffer);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
#if 0
|
||||
bool starts_with(const char* str, const char* prefix) {
|
||||
if (strstr(str, prefix) == str) return true;
|
||||
|
|
|
@ -76,6 +76,12 @@ extern std::string timediff_format(double);
|
|||
extern int read_file_string(const char* pathname, std::string& result);
|
||||
extern void escape_project_url(char *in, char* out);
|
||||
|
||||
// remove duplicated words in a comma or space delimited string.
|
||||
// result is a space delimited string.
|
||||
// "this is this a is test" -> "this is a test"
|
||||
extern void remove_duplicate_words(char *str);
|
||||
extern void remove_duplicate_words(std::string &str);
|
||||
|
||||
inline bool ends_with(std::string const& s, std::string const& suffix) {
|
||||
return
|
||||
s.size()>=suffix.size() &&
|
||||
|
|
Loading…
Reference in New Issue