Serenity Operating System
1/*
2 * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibPDF/Reader.h>
8#include <ctype.h>
9
10namespace PDF {
11
12bool Reader::matches_eol() const
13{
14 return matches_any(0xa, 0xd);
15}
16
17bool Reader::matches_whitespace() const
18{
19 return matches_eol() || matches_any(0, 0x9, 0xc, ' ');
20}
21
22bool Reader::matches_number() const
23{
24 if (done())
25 return false;
26 auto ch = peek();
27 return isdigit(ch) || ch == '-' || ch == '+' || ch == '.';
28}
29
30bool Reader::matches_delimiter() const
31{
32 return matches_any('(', ')', '<', '>', '[', ']', '{', '}', '/', '%');
33}
34
35bool Reader::matches_regular_character() const
36{
37 return !done() && !matches_delimiter() && !matches_whitespace();
38}
39
40bool Reader::consume_eol()
41{
42 if (done()) {
43 return false;
44 }
45 if (matches("\r\n")) {
46 consume(2);
47 return true;
48 }
49 auto consumed = consume();
50 return consumed == 0xd || consumed == 0xa;
51}
52
53bool Reader::consume_whitespace()
54{
55 bool consumed = false;
56 while (matches_whitespace()) {
57 consumed = true;
58 consume();
59 }
60 return consumed;
61}
62
63char Reader::consume()
64{
65 return read();
66}
67
68void Reader::consume(int amount)
69{
70 for (size_t i = 0; i < static_cast<size_t>(amount); i++)
71 consume();
72}
73
74bool Reader::consume(char ch)
75{
76 return consume() == ch;
77}
78
79}