Skip to content

Commit 6746fc2

Browse files
committed
all implemented tests passing
1 parent d0e8a47 commit 6746fc2

File tree

7 files changed

+713
-61
lines changed

7 files changed

+713
-61
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ authors = ["Quentin M. Kniep <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8+
lazy_static = "1.4"

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ the [`deno_lint` project](https://github.com/denoland/deno_lint).
1111

1212
## Using the Library
1313

14-
TBA
14+
```
15+
let mut validator = EcmaRegexValidator::new(EcmaVersion::ES2018);
16+
assert_eq!(validator.validate_pattern("foo|abc(d)?", false), Ok(()));
17+
assert_eq!(validator.validate_flags("gim", false), Ok(()));
18+
```
1519

1620
## Performance
1721

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// Copyright (C) 2020 Quentin M. Kniep <[email protected]>
22
// Distributed under terms of the MIT license.
33

4+
#[macro_use]
5+
extern crate lazy_static;
6+
47
mod reader;
8+
mod unicode;
59
mod validator;
610

711
pub use validator::{EcmaRegexValidator, EcmaVersion};

src/reader.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Reader {
1717
pub fn new() -> Self {
1818
Self {
1919
unicode: false,
20-
src: "".to_string(),
20+
src: "".to_owned(),
2121
index: 0,
2222
end: 0,
2323
cps: VecDeque::with_capacity(4),
@@ -39,7 +39,7 @@ impl Reader {
3939

4040
pub fn reset(&mut self, source: &str, start: usize, end: usize, u_flag: bool) {
4141
self.unicode = u_flag;
42-
self.src = source.into();
42+
self.src = source.to_owned();
4343
self.end = end;
4444
self.rewind(start);
4545
}
@@ -109,22 +109,27 @@ impl Reader {
109109
}
110110

111111
fn at(&self, i: usize) -> Option<char> {
112+
//println!("{:?}", self.src.as_bytes());
113+
//println!("{:?}", self.src.encode_utf16().collect::<Vec<u16>>());
112114
if i >= self.end {
113115
None
114116
} else if self.unicode {
115117
// TODO: read non ASCII as UTF-8
116-
let c: char = self.src.as_bytes()[i].into();
118+
//let c: char = self.src.as_bytes()[i].into();
119+
let c = self.src.chars().skip(i).next().unwrap();
117120
Some(c)
118121
} else {
119-
// TODO: read non ASCII as UTF-16
120-
let c: char = self.src.as_bytes()[i].into();
121-
Some(c)
122+
// TODO: move the conversion out of this method and make it safe
123+
unsafe {
124+
let c: char = std::char::from_u32_unchecked(self.src.encode_utf16().skip(i).next().unwrap() as u32);
125+
Some(c)
126+
}
122127
}
123128
}
124129

125130
fn width(&self, c: char) -> usize {
126131
if self.unicode && c > '\u{FFFF}' {
127-
2
132+
1
128133
} else {
129134
1
130135
}
@@ -162,12 +167,26 @@ mod tests {
162167
assert_eq!(reader.eat3('b', 'c', 'd'), true);
163168
}
164169

165-
/*#[test]
170+
#[test]
166171
fn at_test_es_compliance() {
167172
let mut reader = Reader::new();
168-
reader.reset("􀃃a🩢☃★♲", 0, 20, false);
173+
// without unicode flag
174+
reader.reset("Hello", 0, 5, false);
175+
assert_eq!(reader.at(1).unwrap() as u32, 101);
176+
reader.reset("􀃃a🩢☃★♲", 0, 6, false);
169177
assert_eq!(reader.at(0).unwrap() as u32, 56256);
170-
reader.reset("􀃃a🩢☃★♲", 0, 20, true);
178+
reader.reset("􀃃ello", 0, 6, false);
179+
assert_eq!(reader.at(0).unwrap() as u32, 56256);
180+
reader.reset("􀃃ello", 0, 6, false);
181+
assert_eq!(reader.at(1).unwrap() as u32, 56515);
182+
// with unicode flag
183+
reader.reset("Hello", 0, 5, true);
184+
assert_eq!(reader.at(1).unwrap() as u32, 101);
185+
reader.reset("􀃃a🩢☃★♲", 0, 6, true);
186+
assert_eq!(reader.at(0).unwrap() as u32, 1048771);
187+
reader.reset("􀃃ello", 0, 6, true);
171188
assert_eq!(reader.at(0).unwrap() as u32, 1048771);
172-
}*/
189+
//reader.reset("􀃃ello", 0, 6, true);
190+
//assert_eq!(reader.at(1).unwrap() as u32, 56515);
191+
}
173192
}

0 commit comments

Comments
 (0)