Reduces lazy_static usages

pull/22/head
Tpt 5 years ago
parent 10b1fa68f3
commit c49da613ea
  1. 8
      lib/src/sparql/algebra.rs
  2. 46
      lib/src/sparql/parser.rs

@ -2,7 +2,6 @@
use crate::model::*; use crate::model::*;
use crate::sparql::model::*; use crate::sparql::model::*;
use lazy_static::lazy_static;
use rio_api::iri::Iri; use rio_api::iri::Iri;
use rio_api::model as rio; use rio_api::model as rio;
use std::collections::BTreeMap; use std::collections::BTreeMap;
@ -1201,9 +1200,10 @@ impl fmt::Display for DatasetSpec {
} }
} }
lazy_static! { const EMPTY_DATASET: DatasetSpec = DatasetSpec {
static ref EMPTY_DATASET: DatasetSpec = DatasetSpec::default(); default: Vec::new(),
} named: Vec::new(),
};
#[derive(Eq, PartialEq, Debug, Clone, Hash)] #[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum QueryVariants { pub enum QueryVariants {

@ -14,7 +14,6 @@ mod grammar {
use crate::model::*; use crate::model::*;
use crate::sparql::algebra::*; use crate::sparql::algebra::*;
use crate::sparql::model::*; use crate::sparql::model::*;
use lazy_static::lazy_static;
use rio_api::iri::{Iri, IriParseError}; use rio_api::iri::{Iri, IriParseError};
use std::borrow::Cow; use std::borrow::Cow;
use std::char; use std::char;
@ -416,7 +415,7 @@ mod grammar {
pub fn unescape_characters<'a>( pub fn unescape_characters<'a>(
input: &'a str, input: &'a str,
characters: &'static [u8], characters: &'static [u8],
replacement: &'static StaticSliceMap<char, char>, replacement: &'static StaticCharSliceMap,
) -> Cow<'a, str> { ) -> Cow<'a, str> {
if needs_unescape_characters(input, characters) { if needs_unescape_characters(input, characters) {
UnescapeCharsIterator::new(input, replacement).collect() UnescapeCharsIterator::new(input, replacement).collect()
@ -438,11 +437,11 @@ mod grammar {
struct UnescapeCharsIterator<'a> { struct UnescapeCharsIterator<'a> {
iter: Chars<'a>, iter: Chars<'a>,
buffer: Option<char>, buffer: Option<char>,
replacement: &'static StaticSliceMap<char, char>, replacement: &'static StaticCharSliceMap,
} }
impl<'a> UnescapeCharsIterator<'a> { impl<'a> UnescapeCharsIterator<'a> {
fn new(string: &'a str, replacement: &'static StaticSliceMap<char, char>) -> Self { fn new(string: &'a str, replacement: &'static StaticCharSliceMap) -> Self {
Self { Self {
iter: string.chars(), iter: string.chars(),
buffer: None, buffer: None,
@ -475,22 +474,17 @@ mod grammar {
} }
} }
pub struct StaticSliceMap<K: 'static + Copy + Eq, V: 'static + Copy> { pub struct StaticCharSliceMap {
keys: &'static [K], keys: &'static [char],
values: &'static [V], values: &'static [char],
} }
impl<K: 'static + Copy + Eq, V: 'static + Copy> StaticSliceMap<K, V> { impl StaticCharSliceMap {
pub fn new(keys: &'static [K], values: &'static [V]) -> Self { pub const fn new(keys: &'static [char], values: &'static [char]) -> Self {
assert_eq!(
keys.len(),
values.len(),
"keys and values slices of StaticSliceMap should have the same size"
);
Self { keys, values } Self { keys, values }
} }
pub fn get(&self, key: K) -> Option<V> { pub fn get(&self, key: char) -> Option<char> {
for i in 0..self.keys.len() { for i in 0..self.keys.len() {
if self.keys[i] == key { if self.keys[i] == key {
return Some(self.values[i]); return Some(self.values[i]);
@ -501,15 +495,13 @@ mod grammar {
} }
const UNESCAPE_CHARACTERS: [u8; 8] = [b't', b'b', b'n', b'r', b'f', b'"', b'\'', b'\\']; const UNESCAPE_CHARACTERS: [u8; 8] = [b't', b'b', b'n', b'r', b'f', b'"', b'\'', b'\\'];
lazy_static! { const UNESCAPE_REPLACEMENT: StaticCharSliceMap = StaticCharSliceMap::new(
static ref UNESCAPE_REPLACEMENT: StaticSliceMap<char, char> = StaticSliceMap::new(
&['t', 'b', 'n', 'r', 'f', '"', '\'', '\\'], &['t', 'b', 'n', 'r', 'f', '"', '\'', '\\'],
&[ &[
'\u{0009}', '\u{0008}', '\u{000A}', '\u{000D}', '\u{000C}', '\u{0022}', '\u{0027}', '\u{0009}', '\u{0008}', '\u{000A}', '\u{000D}', '\u{000C}', '\u{0022}', '\u{0027}',
'\u{005C}' '\u{005C}',
] ],
); );
}
fn unescape_echars(input: &str) -> Cow<'_, str> { fn unescape_echars(input: &str) -> Cow<'_, str> {
unescape_characters(input, &UNESCAPE_CHARACTERS, &UNESCAPE_REPLACEMENT) unescape_characters(input, &UNESCAPE_CHARACTERS, &UNESCAPE_REPLACEMENT)
@ -519,18 +511,16 @@ mod grammar {
b'_', b'~', b'.', b'-', b'!', b'$', b'&', b'\'', b'(', b')', b'*', b'+', b',', b';', b'=', b'_', b'~', b'.', b'-', b'!', b'$', b'&', b'\'', b'(', b')', b'*', b'+', b',', b';', b'=',
b'/', b'?', b'#', b'@', b'%', b'/', b'?', b'#', b'@', b'%',
]; ];
lazy_static! { const UNESCAPE_PN_REPLACEMENT: StaticCharSliceMap = StaticCharSliceMap::new(
static ref UNESCAPE_PN_REPLACEMENT: StaticSliceMap<char, char> = StaticSliceMap::new(
&[ &[
'_', '~', '.', '-', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', '/', '_', '~', '.', '-', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', '/', '?',
'?', '#', '@', '%' '#', '@', '%',
], ],
&[ &[
'_', '~', '.', '-', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', '/', '_', '~', '.', '-', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', '/', '?',
'?', '#', '@', '%' '#', '@', '%',
] ],
); );
}
pub fn unescape_pn_local(input: &str) -> Cow<'_, str> { pub fn unescape_pn_local(input: &str) -> Cow<'_, str> {
unescape_characters(input, &UNESCAPE_PN_CHARACTERS, &UNESCAPE_PN_REPLACEMENT) unescape_characters(input, &UNESCAPE_PN_CHARACTERS, &UNESCAPE_PN_REPLACEMENT)

Loading…
Cancel
Save