Blame view

morfeusz/charset/TextReader.cpp 3.56 KB
Michał Lenart authored
1
/* 
Michał Lenart authored
2
 * File:   TextReader.cpp
Michał Lenart authored
3
4
5
6
7
8
9
10
 * Author: lennyn
 * 
 * Created on May 28, 2014, 11:43 AM
 */

#include "TextReader.hpp"
#include "charset_utils.hpp"
Michał Lenart authored
11
12
using namespace std;
Michał Lenart authored
13
14
namespace morfeusz {
Michał Lenart authored
15
16
17
18
19
    TextReader::TextReader(
            const char* inputStart,
            const char* inputEnd,
            const Environment& env)
    : codepointsNum(0),
Michał Lenart authored
20
    chunkStartPtr(inputStart),
Michał Lenart authored
21
22
    wordStartPtr(inputStart),
    currPtr(inputStart),
Michał Lenart authored
23
24
25
26
27
28
29
30
    inputEnd(inputEnd),
    env(env),
    knowsAboutWhitespace(false),
    atWhitespace(false),
    peekIsRead(false),
    thePeek(0x00),
    theNormalizedPeek(0x00),
    ptrAfterThePeek(NULL) {
Michał Lenart authored
31
32
    }
Michał Lenart authored
33
34
    TextReader::TextReader(const std::string& text, const Environment& env)
    : codepointsNum(0),
Michał Lenart authored
35
    chunkStartPtr(text.c_str()),
Michał Lenart authored
36
37
38
39
40
41
42
43
44
45
    wordStartPtr(text.c_str()),
    currPtr(text.c_str()),
    inputEnd(text.c_str() + text.length()),
    env(env),
    knowsAboutWhitespace(false),
    atWhitespace(false),
    peekIsRead(false),
    thePeek(0x00),
    theNormalizedPeek(0x00),
    ptrAfterThePeek(NULL) {
Michał Lenart authored
46
Michał Lenart authored
47
    }
Michał Lenart authored
48
Michał Lenart authored
49
50
51
52
    void TextReader::markWordStartsHere() {
        codepointsNum = 0;
        wordStartPtr = currPtr;
    }
Michał Lenart authored
53
54
55
56

    void TextReader::markChunkStartsHere() {
        chunkStartPtr = currPtr;
    }
Michał Lenart authored
57
Michał Lenart authored
58
59
60
    const char* TextReader::getWordStartPtr() const {
        return wordStartPtr;
    }
Michał Lenart authored
61
62
63
64

    const char* TextReader::getChunkStartPtr() const {
        return chunkStartPtr;
    }
Michał Lenart authored
65
Michał Lenart authored
66
67
    const char* TextReader::getCurrPtr() const {
        return currPtr;
Michał Lenart authored
68
    }
Michał Lenart authored
69
70

    const char* TextReader::getNextPtr() {
Michał Lenart authored
71
72
73
        if (!peekIsRead) {
            peek();
        }
Michał Lenart authored
74
        return ptrAfterThePeek;
Michał Lenart authored
75
76
    }
Michał Lenart authored
77
78
79
    const char* TextReader::getEndPtr() const {
        return inputEnd;
    }
Michał Lenart authored
80
Michał Lenart authored
81
82
    int TextReader::getCodepointsRead() const {
        return codepointsNum;
Michał Lenart authored
83
    }
Michał Lenart authored
84
85
86

    bool TextReader::isAtEnd() const {
        return currPtr == inputEnd;
Michał Lenart authored
87
88
    }
Michał Lenart authored
89
90
91
92
93
94
95
96
97
98
    bool TextReader::isAtWhitespace() {
        if (isAtEnd()) {
            return true;
        }
        else {
            if (!peekIsRead) {
                peek();
            }
            return atWhitespace;
        }
Michał Lenart authored
99
100
    }
Michał Lenart authored
101
102
    bool TextReader::isInsideAWord() {
        return !isAtEnd() && !isAtWhitespace();
Michał Lenart authored
103
104
    }
Michał Lenart authored
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    uint32_t TextReader::peek() {
        if (peekIsRead) {
            return thePeek;
        }
        else {
            ptrAfterThePeek = currPtr;
            thePeek = env.getCharsetConverter().next(ptrAfterThePeek, inputEnd);
            theNormalizedPeek = env.getProcessorType() == ANALYZER
                    ? env.getCaseConverter().toLower(thePeek)
                    : thePeek;
            atWhitespace = isWhitespace(thePeek);
            peekIsRead = true;
            return thePeek;
        }
Michał Lenart authored
119
120
    }
Michał Lenart authored
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    uint32_t TextReader::normalizedPeek() {
        if (!peekIsRead) {
            peek();
        }
        return theNormalizedPeek;
    }

    uint32_t TextReader::next() {
        if (!peekIsRead) {
            peek();
        }
        currPtr = ptrAfterThePeek;
        peekIsRead = false;
        knowsAboutWhitespace = false;
        codepointsNum++;
        return thePeek;
Michał Lenart authored
137
138
    }
Michał Lenart authored
139
140
141
142
    void TextReader::skipWhitespaces() {
        while (!isAtEnd() && isAtWhitespace()) {
            next();
        }
Michał Lenart authored
143
144
    }
Michał Lenart authored
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    string TextReader::readWhitespacesChunk() {
        const char* startPtr = currPtr;
        while (!isAtEnd() && isAtWhitespace()) {
            next();
        }
        string res(startPtr, currPtr);
        return res;
    }

    void TextReader::proceedToEnd() {
        while (!isAtEnd()) {
            next();
        }
    }

    TextReader::~TextReader() {
Michał Lenart authored
161
Michał Lenart authored
162
    }
Michał Lenart authored
163
164

}