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 
 32 class BufferCursor;
 33 
 34 class Range;
 35 
 36 class Mark : public PureRange {
 37 private:
 38     BufferCursor *cursor;
 39     char *ptr;
 40     char *end;
 41 
 42 private:
 43     friend BufferCursor;
 44     Mark(BufferCursor *);
 45 
 46 public:
 47     char *getStart() override;
 48     char *getEnd() override;
 49     size_t getSize() override ;
 50     char *setEnd();
 51     std::string str(char *end);
 52     std::string str();
 53     std::string str(int delta);
 54 };
 55 
 56 class BufferCursor : public PureRange {
 57 protected:
 58     char *startPtr, *ptr, *endPtr;
 59     std::vector<Mark *> marks;
 60 public:
 61 
 62     char *get();
 63 
 64     char *getStart() override;
 65     char *getEnd() override;
 66     size_t getSize() override ;
 67 
 68     BufferCursor *moveToOffset(int offset);
 69 
 70     virtual bool isValid(char *p);
 71 
 72     bool end();
 73 
 74     BufferCursor *advance(int i);
 75 
 76     BufferCursor *advance();
 77 
 78     BufferCursor *backup(int i);
 79 
 80     BufferCursor *backup();
 81 
 82     char ch();
 83 
 84     int chAsDigit();
 85 
 86     int chAsHexDigit();
 87 
 88     char chAndAdvance();
 89 
 90     bool isLookingAt(const char c);
 91 
 92     BufferCursor *skipWhiteSpace();
 93 
 94     BufferCursor *skipWhiteSpaceOrNewLine();
 95 
 96     bool isLookingAt(const char *str);
 97 
 98     bool isLookingAtAndStepOver(const char *str);
 99 
100     BufferCursor *skipUntilLookingAt(const char *str);
101 
102     BufferCursor *backupUntilLookingAt(const char *str);
103 
104     BufferCursor *skipUntilLookingAtOneOf(const char *str);
105 
106     BufferCursor *skipWhileLookingAt(const char *str);
107 
108     BufferCursor *skipWhileLookingAtOneOf(const char *str);
109 
110     bool isLookingAtOneOf(const char *str);
111 
112     bool isLookingAtCRNL();
113 
114     BufferCursor *stepOverCRNL();
115 
116     bool isLookingAtNL();
117 
118     BufferCursor *stepOverNL();
119 
120     Mark *mark();
121 
122     Mark *markUntil(const char *s);
123 
124     bool isLookingAtAlpha();
125 
126     bool isLookingAtDigit();
127 
128     bool isLookingAtHexDigit();
129 
130     bool isLookingAtOctalDigit();
131 
132     bool isLookingAtAlphaNum();
133 
134     bool isLookingAtAlphaNumOr(const char *s);
135 
136     BufferCursor *moveTo(Mark *mark);
137 
138     BufferCursor *stepOver(const char c);
139 
140     BufferCursor *stepOver(const char *s);
141 
142     BufferCursor *skipTill(const char *str);
143 
144     BufferCursor *reset();
145 
146     BufferCursor(PureRange *pureRange);
147     BufferCursor(char *ptr, size_t len);
148 
149 
150     BufferCursor(char *ptr);
151 
152     virtual ~BufferCursor();
153 
154     void show(std::ostream &o);
155 };
156 
157 
158 std::ostream &operator<<(std::ostream &o, BufferCursor &c);
159 
160 std::ostream &operator<<(std::ostream &o, BufferCursor *c);