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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/*
* Hurl (https://hurl.dev)
* Copyright (C) 2025 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
//! Represents a text reader.
/// The `Reader` implements methods to read a stream of text. A reader manages
/// an internal `cursor` : it's the current read index position within the reader's internal buffer.
///
/// Methods like [`Reader::read`], [`Reader::read_while`] do advance the internal reader's `cursor`.
/// Other methods, like [`Reader::peek`], [`Reader::peek_n`] allows to get the next chars in the
/// buffer without modifying the current reader cursor.
///
/// The cursor is composed of an offset, which is always related to the reader internal buffer.
/// Along the buffer offset, a position [`Pos`] is updated each time a char is read. This position
/// corresponds to the column and row index in the buffer document. In most of the case, the
/// position is initialized to the first char, but a reader instance can be created using
/// [`Reader::with_pos`] to set a given started position. This can be useful when a reader
/// is instantiated as a "sub reader" of a given reader, and we want to report position relatively
/// to the main reader (for errors but also for constructed structures).
///
/// # Example
/// ```
/// use hurl_core::reader::Reader;
///
/// let mut reader = Reader::new("hi");
/// assert_eq!(reader.cursor().index, 0);
/// assert!(!reader.is_eof());
/// assert_eq!(reader.peek_n(2), "hi".to_string());
/// assert_eq!(reader.read(), Some('h'));
/// assert_eq!(reader.cursor().index, 1);
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Reader {
buf: Vec<char>,
cursor: Cursor,
}
/// Represents a line and column position in a reader.
///
/// Indices are 1-based.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Pos {
pub line: usize,
pub column: usize,
}
impl Pos {
/// Creates a new position.
pub fn new(line: usize, column: usize) -> Pos {
Pos { line, column }
}
}
/// A position in a text buffer.
///
/// The position has two components: a char `offset` in the internal buffer of the reader, and
/// a column-row oriented position `pos`, used for human display. `pos` is usually initialized to
/// the first char of the buffer but it can also be set with a position inside another reader. This
/// allows the report of error of a sub-reader, relative to a parent reader.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Cursor {
pub index: usize,
pub pos: Pos,
}
impl Reader {
/// Creates a new reader, position of the index is at the first char.
pub fn new(s: &str) -> Self {
Reader {
buf: s.chars().collect(),
cursor: Cursor {
index: 0,
pos: Pos { line: 1, column: 1 },
},
}
}
/// Creates a new reader, `pos` is position of the index: this allow to report created
/// structures and error to be referenced from this position.
///
/// Note: the `buffer` offset is still initialized to 0.
pub fn with_pos(s: &str, pos: Pos) -> Self {
Reader {
buf: s.chars().collect(),
cursor: Cursor { index: 0, pos },
}
}
/// Returns the current position of the read index.
pub fn cursor(&self) -> Cursor {
self.cursor
}
/// Position the read index to a new position.
pub fn seek(&mut self, to: Cursor) {
self.cursor = to;
}
/// Returns true if the reader has read all the buffer, false otherwise.
pub fn is_eof(&self) -> bool {
self.cursor.index == self.buf.len()
}
/// Returns the next char from the buffer advancing the internal state.
pub fn read(&mut self) -> Option<char> {
match self.buf.get(self.cursor.index) {
None => None,
Some(c) => {
self.cursor.index += 1;
if !is_combining_character(*c) {
self.cursor.pos.column += 1;
}
if *c == '\n' {
self.cursor.pos.column = 1;
self.cursor.pos.line += 1;
}
Some(*c)
}
}
}
/// Returns `count` chars from the buffer advancing the internal state.
/// This methods can returns less than `count` chars if there is not enough chars in the buffer.
pub fn read_n(&mut self, count: usize) -> String {
let mut s = String::new();
for _ in 0..count {
match self.read() {
None => {}
Some(c) => s.push(c),
}
}
s
}
/// Returns chars from the buffer while `predicate` is true, advancing the internal state.
pub fn read_while(&mut self, predicate: fn(char) -> bool) -> String {
let mut s = String::new();
loop {
match self.peek() {
None => return s,
Some(c) => {
if predicate(c) {
_ = self.read();
s.push(c);
} else {
return s;
}
}
}
}
}
/// Reads a string from a `start` position to the current position (excluded).
///
/// This method doesn't modify the read index since we're reading "backwards" to the current
/// read index.
pub fn read_from(&self, start: usize) -> String {
let end = self.cursor.index;
self.buf[start..end].iter().collect()
}
/// Peeks the next char from the buffer without advancing the internal state.
pub fn peek(&self) -> Option<char> {
self.buf.get(self.cursor.index).copied()
}
/// Peeks the next char that meet a `predicate`.
pub fn peek_if(&self, predicate: fn(char) -> bool) -> Option<char> {
let mut i = self.cursor.index;
loop {
let &c = self.buf.get(i)?;
if predicate(c) {
return Some(c);
}
i += 1;
}
}
/// Peeks a string of `count` char without advancing the internal state.
/// This methods can return less than `count` chars if there is not enough chars in the buffer.
pub fn peek_n(&self, count: usize) -> String {
let start = self.cursor.index;
let end = (start + count).min(self.buf.len());
self.buf[start..end].iter().collect()
}
}
fn is_combining_character(c: char) -> bool {
c > '\u{0300}' && c < '\u{036F}' // Combining Diacritical Marks (0300–036F)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basic_reader() {
let mut reader = Reader::new("hi");
assert_eq!(reader.cursor().index, 0);
assert!(!reader.is_eof());
assert_eq!(reader.peek_n(2), "hi".to_string());
assert_eq!(reader.cursor().index, 0);
assert_eq!(reader.read().unwrap(), 'h');
assert_eq!(reader.cursor().index, 1);
assert_eq!(reader.peek().unwrap(), 'i');
assert_eq!(reader.cursor().index, 1);
assert_eq!(reader.read().unwrap(), 'i');
assert!(reader.is_eof());
assert_eq!(reader.read(), None);
}
#[test]
fn peek_back() {
let mut reader = Reader::new("abcdefgh");
assert_eq!(reader.read(), Some('a'));
assert_eq!(reader.read(), Some('b'));
assert_eq!(reader.read(), Some('c'));
assert_eq!(reader.read(), Some('d'));
assert_eq!(reader.read(), Some('e'));
assert_eq!(reader.peek(), Some('f'));
assert_eq!(reader.read_from(3), "de");
}
#[test]
fn read_while() {
let mut reader = Reader::new("123456789");
assert_eq!(reader.read_while(|c| c.is_numeric()), "123456789");
assert_eq!(reader.cursor().index, 9);
assert!(reader.is_eof());
let mut reader = Reader::new("123456789abcde");
assert_eq!(reader.read_while(|c| c.is_numeric()), "123456789");
assert_eq!(reader.cursor().index, 9);
assert!(!reader.is_eof());
let mut reader = Reader::new("abcde123456789");
assert_eq!(reader.read_while(|c| c.is_numeric()), "");
assert_eq!(reader.cursor().index, 0);
}
#[test]
fn reader_create_with_from_pos() {
let mut main_reader = Reader::new("aaabb");
_ = main_reader.read();
_ = main_reader.read();
_ = main_reader.read();
let pos = main_reader.cursor().pos;
let s = main_reader.read_while(|_| true);
let mut sub_reader = Reader::with_pos(&s, pos);
assert_eq!(
sub_reader.cursor,
Cursor {
index: 0,
pos: Pos::new(1, 4)
}
);
_ = sub_reader.read();
assert_eq!(
sub_reader.cursor,
Cursor {
index: 1,
pos: Pos::new(1, 5)
}
);
}
#[test]
fn peek_ignoring_whitespace() {
fn is_whitespace(c: char) -> bool {
c == ' ' || c == '\t'
}
let reader = Reader::new("\t\t\tabc");
assert_eq!(reader.peek_if(|c| !is_whitespace(c)), Some('a'));
let reader = Reader::new("foo");
assert_eq!(reader.peek_if(|c| !is_whitespace(c)), Some('f'));
}
}
|