-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_set.cc
113 lines (102 loc) · 2.33 KB
/
word_set.cc
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* -*- mode: C++; c-basic-offset: 4; tab-width: 8; -*-
* vi: set shiftwidth=4 tabstop=8:
* :indentSize=4:tabSize=8:
*/
#include "word_set.h"
#include <cctype>
#include <cassert>
namespace {
std::set<std::string> word_set;
bool is_word_character(int c)
{
return isalnum(c) || c=='_' || c=='-';
}
}
void
add_to_word_set(const std::string& line)
{
std::string::const_iterator it = line.begin();
std::string::const_iterator beg = it, end = it;
while(it != line.end()) {
// search for start of word
while(it != line.end() && !is_word_character(*it)) {
++it;
}
beg = it;
// search for end of word
while(it != line.end() && is_word_character(*it)) {
++it;
}
end = it;
if (beg != end) {
word_set.insert(std::string(beg,end));
}
}
}
void
clear_word_set()
{
word_set.clear();
}
std::set<std::string>
complete_word_set(std::string& word, std::string& err)
{
std::set<std::string> s;
if (word.empty()) {
s = word_set;
} else {
for(const auto& w : word_set) {
if (w.find(word) == 0) {
s.insert(w);
}
}
}
complete_longest_prefix(word, s);
return s;
}
void
complete_longest_prefix(std::string& str, const std::set<std::string>& s)
{
// if the set is empty, there's nothing to do
if (s.empty()) {
return;
}
// if the set has only one element?
if (s.size() == 1) {
auto it = s.begin();
// if str is empty, set str to the element
if (str.empty()) {
str = *it;
// if str is prefixing the element, set str to the element
} else if (it->find(str) == 0) {
str = *it;
}
// else: str does not prefix the element, don't modify str.
return;
}
assert(s.size() > 1);
std::string prefix;
// iterator the set and find the longest matching prefix
for(const auto& element : s) {
if (element.find(str) != 0) {
continue;
}
// set prefix to the first element (that is not empty string)
if (prefix.empty()) {
prefix = element;
continue;
}
// check how many characters match from the beginning of element
unsigned i;
for(i = 0; i < element.size() && i < prefix.size() && element[i] == prefix[i]; ++i) {}
// remove characters from prefix that did not match
prefix.erase(i);
if (i == 0) {
// nothing matched, we won't find anything better, abort
break;
}
}
if (! prefix.empty()) {
str = prefix;
}
}