summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibPDF/Reader.cpp
blob: 58421812f92f51477edf1794cd72781bcdbc4caa (plain)
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
/*
 * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibPDF/Reader.h>
#include <ctype.h>

namespace PDF {

bool Reader::matches_eol() const
{
    return matches_any(0xa, 0xd);
}

bool Reader::matches_whitespace() const
{
    return matches_eol() || matches_any(0, 0x9, 0xc, ' ');
}

bool Reader::matches_number() const
{
    if (done())
        return false;
    auto ch = peek();
    return isdigit(ch) || ch == '-' || ch == '+' || ch == '.';
}

bool Reader::matches_delimiter() const
{
    return matches_any('(', ')', '<', '>', '[', ']', '{', '}', '/', '%');
}

bool Reader::matches_regular_character() const
{
    return !matches_delimiter() && !matches_whitespace();
}

bool Reader::consume_eol()
{
    if (done()) {
        return false;
    }
    if (matches("\r\n")) {
        consume(2);
        return true;
    }
    auto consumed = consume();
    return consumed == 0xd || consumed == 0xa;
}

bool Reader::consume_whitespace()
{
    bool consumed = false;
    while (matches_whitespace()) {
        consumed = true;
        consume();
    }
    return consumed;
}

char Reader::consume()
{
    return read();
}

void Reader::consume(int amount)
{
    for (size_t i = 0; i < static_cast<size_t>(amount); i++)
        consume();
}

bool Reader::consume(char ch)
{
    return consume() == ch;
}

}