1 /*
  2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 #pragma once
 26 
 27 #include <string>
 28 #include <vector>
 29 #include "buffer.h"
 30 
 31 class BufferCursor;
 32 class Range;
 33 
 34 class Mark final : public PureRange {
 35     BufferCursor *cursor;
 36     char *ptr;
 37     char *end;
 38     friend BufferCursor;
 39 
 40     explicit Mark(BufferCursor *);
 41 
 42 public:
 43     char *getStart() override;
 44 
 45     char *getEnd() override;
 46 
 47     size_t getSize() override;
 48 
 49     char *setEnd();
 50 
 51     std::string str(char *end) const;
 52 
 53     std::string str();
 54 
 55     std::string str(int delta);
 56 };
 57 
 58 class BufferCursor final : public PureRange {
 59 protected:
 60     char *startPtr, *ptr, *endPtr;
 61     std::vector<Mark *> marks;
 62 
 63 public:
 64     char *get();
 65 
 66     char *getStart() override;
 67 
 68     char *getEnd() override;
 69 
 70     size_t getSize() override;
 71 
 72     BufferCursor *moveToOffset(int offset);
 73 
 74     virtual bool isValid(char *p);
 75 
 76     bool end() const;
 77 
 78     BufferCursor *advance(int i);
 79 
 80     BufferCursor *advance();
 81 
 82     BufferCursor *backup(int i);
 83 
 84     BufferCursor *backup();
 85 
 86     char ch();
 87 
 88     int chAsDigit();
 89 
 90     int chAsHexDigit();
 91 
 92     char chAndAdvance();
 93 
 94     bool isLookingAt(char c);
 95 
 96     BufferCursor *skipWhiteSpace();
 97 
 98     BufferCursor *skipWhiteSpaceOrNewLine();
 99 
100     bool isLookingAt(const char *str);
101 
102     bool isLookingAtAndStepOver(const char *str);
103 
104     BufferCursor *skipUntilLookingAt(const char *str);
105 
106     BufferCursor *backupUntilLookingAt(const char *str);
107 
108     BufferCursor *skipUntilLookingAtOneOf(const char *str);
109 
110     BufferCursor *skipWhileLookingAt(const char *str);
111 
112     BufferCursor *skipWhileLookingAtOneOf(const char *str);
113 
114     bool isLookingAtOneOf(const char *str);
115 
116     bool isLookingAtCRNL();
117 
118     BufferCursor *stepOverCRNL();
119 
120     bool isLookingAtNL();
121 
122     BufferCursor *stepOverNL();
123 
124     Mark *mark();
125 
126     Mark *markUntil(const char *s);
127 
128     bool isLookingAtAlpha();
129 
130     bool isLookingAtDigit();
131 
132     bool isLookingAtHexDigit();
133 
134     bool isLookingAtOctalDigit();
135 
136     bool isLookingAtAlphaNum();
137 
138     bool isLookingAtAlphaNumOr(const char *s);
139 
140     BufferCursor *moveTo(Mark *mark);
141 
142     BufferCursor *stepOver(char c);
143 
144     BufferCursor *stepOver(const char *s);
145 
146     BufferCursor *skipTill(const char *str);
147 
148     BufferCursor *reset();
149 
150     explicit BufferCursor(PureRange *pureRange);
151 
152     BufferCursor(char *ptr, size_t len);
153 
154     explicit BufferCursor(char *ptr);
155 
156     ~BufferCursor() override;
157 
158     void show(std::ostream &o);
159 };
160 
161 
162 std::ostream &operator<<(std::ostream &o, BufferCursor &c);
163 
164 std::ostream &operator<<(std::ostream &o, BufferCursor *c);