-
Notifications
You must be signed in to change notification settings - Fork 0
/
Word.h
64 lines (54 loc) · 1.61 KB
/
Word.h
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
#ifndef WORD_H
#define WORD_H
#include <string>
#include <algorithm>
#include "Wikidiff2.h"
// a small class to accomodate word-level diffs; basically, a body and an
// optional suffix (the latter consisting of a single whitespace), where
// only the bodies are compared on operator==.
//
// This class stores iterators pointing to the line string, this is to avoid
// excessive allocation calls. To avoid invalidation, the source string should
// not be changed or destroyed.
class Word {
public:
typedef std::basic_string<char, std::char_traits<char>, WD2_ALLOCATOR<char> > String;
typedef String::const_iterator Iterator;
Iterator bodyStart;
Iterator bodyEnd;
Iterator suffixEnd;
/**
* The body is the character sequence [bs, be)
* The whitespace suffix is the character sequence [be, se)
*/
Word(Iterator bs, Iterator be, Iterator se)
: bodyStart(bs), bodyEnd(be), suffixEnd(se)
{}
bool operator== (const Word &w) const {
return (bodyEnd - bodyStart == w.bodyEnd - w.bodyStart)
&& std::equal(bodyStart, bodyEnd, w.bodyStart);
}
bool operator!=(const Word &w) const {
return !operator==(w);
}
bool operator<(const Word &w) const {
return std::lexicographical_compare(bodyStart, bodyEnd, w.bodyStart, w.bodyEnd);
}
// Get the whole word as a string
String whole() const {
String w;
get_whole(w);
return w;
}
// Assign the whole word to a string
void get_whole(String & w) const {
// Do it with swap() to avoid a second copy
String temp(bodyStart, suffixEnd);
temp.swap(w);
}
// Get the body as a string
operator String() const {
return String(bodyStart, bodyEnd);
}
};
#endif