tlx
union_words.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/string/union_words.cpp
3  *
4  * Part of tlx - http://panthema.net/tlx
5  *
6  * Copyright (C) 2016-2017 Timo Bingmann <tb@panthema.net>
7  *
8  * All rights reserved. Published under the Boost Software License, Version 1.0
9  ******************************************************************************/
10 
13 
14 namespace tlx {
15 
16 std::string union_words(const std::string& wordsA, const std::string& wordsB) {
17  std::string words = wordsA;
18 
19  std::string::const_iterator it = wordsB.begin();
20 
21  while (it != wordsB.end())
22  {
23  // skip over whitespace
24  while (*it == ' ' || *it == '\n' || *it == '\t' || *it == '\r') {
25  if (++it == wordsB.end()) break;
26  }
27 
28  std::string::const_iterator i1 = it;
29 
30  // find first non-whitespace
31  while (it != wordsB.end() &&
32  *it != ' ' && *it != '\n' && *it != '\t' && *it != '\r')
33  ++it;
34 
35  std::string w(i1, it);
36 
37  if (!contains_word(words, w)) {
38  if (!words.empty())
39  words += ' ';
40  words += w;
41  }
42  }
43 
44  return words;
45 }
46 
47 } // namespace tlx
48 
49 /******************************************************************************/
bool contains_word(const std::string &str, const char *word)
Search the given string for a whitespace-delimited word.
std::string union_words(const std::string &wordsA, const std::string &wordsB)
Return union of two keyword sets.
Definition: union_words.cpp:16