Line data Source code
1 : #ifndef WORD_H
2 : #define WORD_H
3 :
4 : #include <string>
5 : #include <algorithm>
6 : #include <iostream>
7 : #include "wd2_allocator.h"
8 :
9 : namespace wikidiff2 {
10 :
11 : // A small class to accomodate word-level diffs; basically, a body and an
12 : // optional suffix (the latter consisting of a single whitespace), where
13 : // only the bodies are compared on operator==.
14 : //
15 : // This class stores iterators pointing to the line string, this is to avoid
16 : // excessive allocation calls. To avoid invalidation, the source string should
17 : // not be changed or destroyed.
18 : class Word {
19 : public:
20 : typedef std::basic_string<char, std::char_traits<char>, WD2_ALLOCATOR<char> > String;
21 : typedef String::const_iterator Iterator;
22 :
23 : Iterator start;
24 : Iterator bodyEnd;
25 : Iterator end;
26 :
27 : /**
28 : * The body is the character sequence [bs, be)
29 : * The whitespace suffix is the character sequence [be, se)
30 : */
31 76571 : Word(Iterator bs, Iterator be, Iterator se)
32 76571 : : start(bs), bodyEnd(be), end(se)
33 76571 : {}
34 :
35 24747 : bool operator== (const Word &w) const {
36 24747 : return (bodyEnd - start == w.bodyEnd - w.start)
37 24747 : && std::equal(start, bodyEnd, w.start);
38 : }
39 15934 : bool operator!=(const Word &w) const {
40 15934 : return !operator==(w);
41 : }
42 193469 : bool operator<(const Word &w) const {
43 193469 : return std::lexicographical_compare(start, bodyEnd, w.start, w.bodyEnd);
44 : }
45 :
46 86500 : size_t size() const {
47 86500 : return end - start;
48 : }
49 :
50 17 : bool isNewline() const {
51 17 : return size() == 1 && *start == '\n';
52 : }
53 : };
54 :
55 : } // namespace wikidiff2
56 :
57 : template<class CharT, class Traits>
58 : std::basic_ostream<CharT, Traits>&
59 0 : operator<<(std::basic_ostream<CharT, Traits> & os, const wikidiff2::Word & word)
60 : {
61 0 : os.write(&*word.start, word.size());
62 0 : return os;
63 : }
64 :
65 : #endif
|