Skip to content

Commit

Permalink
Merge pull request #238 from Turbo87/lazy
Browse files Browse the repository at this point in the history
Replace `lazy_static` dependency with `once_cell`
  • Loading branch information
kivikakk authored Aug 4, 2022
2 parents f1222b3 + b0b29f3 commit bdd3f20
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 73 deletions.
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ doc = false
[dependencies]
typed-arena = "1.4.1"
regex = "1.5.5"
lazy_static = "1.0.1"
once_cell = "1.13.0"
entities = "1.0.1"
unicode_categories = "0.1.1"
clap = { version = "2.32.0", optional = true }
Expand Down
52 changes: 24 additions & 28 deletions src/html.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ctype::isspace;
use nodes::{AstNode, ListType, NodeCode, NodeValue, TableAlignment};
use once_cell::sync::Lazy;
use parser::{ComrakOptions, ComrakPlugins};
use regex::Regex;
use scanners;
Expand Down Expand Up @@ -99,9 +100,8 @@ impl Anchorizer {
/// assert_eq!("ticks-arent-in".to_string(), anchorizer.anchorize(source.to_string()));
/// ```
pub fn anchorize(&mut self, header: String) -> String {
lazy_static! {
static ref REJECTED_CHARS: Regex = Regex::new(r"[^\p{L}\p{M}\p{N}\p{Pc} -]").unwrap();
}
static REJECTED_CHARS: Lazy<Regex> =
Lazy::new(|| Regex::new(r"[^\p{L}\p{M}\p{N}\p{Pc} -]").unwrap());

let mut id = header;
id = id.to_lowercase();
Expand Down Expand Up @@ -173,19 +173,17 @@ const NEEDS_ESCAPED : [bool; 256] = [
];

fn tagfilter(literal: &[u8]) -> bool {
lazy_static! {
static ref TAGFILTER_BLACKLIST: [&'static str; 9] = [
"title",
"textarea",
"style",
"xmp",
"iframe",
"noembed",
"noframes",
"script",
"plaintext"
];
}
static TAGFILTER_BLACKLIST: [&'static str; 9] = [
"title",
"textarea",
"style",
"xmp",
"iframe",
"noembed",
"noframes",
"script",
"plaintext",
];

if literal.len() < 3 || literal[0] != b'<' {
return false;
Expand Down Expand Up @@ -289,18 +287,16 @@ impl<'o> HtmlFormatter<'o> {
}

fn escape_href(&mut self, buffer: &[u8]) -> io::Result<()> {
lazy_static! {
static ref HREF_SAFE: [bool; 256] = {
let mut a = [false; 256];
for &c in b"-_.+!*(),%#@?=;:/,+$~abcdefghijklmnopqrstuvwxyz".iter() {
a[c as usize] = true;
}
for &c in b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".iter() {
a[c as usize] = true;
}
a
};
}
static HREF_SAFE: Lazy<[bool; 256]> = Lazy::new(|| {
let mut a = [false; 256];
for &c in b"-_.+!*(),%#@?=;:/,+$~abcdefghijklmnopqrstuvwxyz".iter() {
a[c as usize] = true;
}
for &c in b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".iter() {
a[c as usize] = true;
}
a
});

let size = buffer.len();
let mut i = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,11 @@
#![allow(unknown_lints, clippy::doc_markdown, cyclomatic_complexity)]

extern crate entities;
#[macro_use]
extern crate lazy_static;
extern crate pest;
#[macro_use]
extern crate pest_derive;
extern crate memchr;
extern crate once_cell;
#[cfg(all(test, not(target_arch = "wasm32")))]
extern crate propfuzz;
extern crate regex;
Expand Down
53 changes: 23 additions & 30 deletions src/parser/autolink.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ctype::{isalnum, isalpha, isspace};
use nodes::{AstNode, NodeLink, NodeValue};
use once_cell::sync::Lazy;
use parser::inlines::make_inline;
use std::str;
use typed_arena::Arena;
Expand Down Expand Up @@ -60,15 +61,13 @@ fn www_match<'a>(
contents: &[u8],
i: usize,
) -> Option<(&'a AstNode<'a>, usize, usize)> {
lazy_static! {
static ref WWW_DELIMS: [bool; 256] = {
let mut sc = [false; 256];
for c in &[b'*', b'_', b'~', b'(', b'['] {
sc[*c as usize] = true;
}
sc
};
}
static WWW_DELIMS: Lazy<[bool; 256]> = Lazy::new(|| {
let mut sc = [false; 256];
for c in &[b'*', b'_', b'~', b'(', b'['] {
sc[*c as usize] = true;
}
sc
});

if i > 0 && !isspace(contents[i - 1]) && !WWW_DELIMS[contents[i - 1] as usize] {
return None;
Expand Down Expand Up @@ -135,15 +134,13 @@ fn is_valid_hostchar(ch: char) -> bool {
}

fn autolink_delim(data: &[u8], mut link_end: usize) -> usize {
lazy_static! {
static ref LINK_END_ASSORTMENT: [bool; 256] = {
let mut sc = [false; 256];
for c in &[b'?', b'!', b'.', b',', b':', b'*', b'_', b'~', b'\'', b'"'] {
sc[*c as usize] = true;
}
sc
};
}
static LINK_END_ASSORTMENT: Lazy<[bool; 256]> = Lazy::new(|| {
let mut sc = [false; 256];
for c in &[b'?', b'!', b'.', b',', b':', b'*', b'_', b'~', b'\'', b'"'] {
sc[*c as usize] = true;
}
sc
});

for (i, &b) in data.iter().enumerate().take(link_end) {
if b == b'<' {
Expand Down Expand Up @@ -200,9 +197,7 @@ fn url_match<'a>(
contents: &[u8],
i: usize,
) -> Option<(&'a AstNode<'a>, usize, usize)> {
lazy_static! {
static ref SCHEMES: Vec<&'static [u8]> = vec![b"http", b"https", b"ftp"];
}
const SCHEMES: [&'static [u8]; 3] = [b"http", b"https", b"ftp"];

let size = contents.len();

Expand Down Expand Up @@ -249,15 +244,13 @@ fn email_match<'a>(
contents: &[u8],
i: usize,
) -> Option<(&'a AstNode<'a>, usize, usize)> {
lazy_static! {
static ref EMAIL_OK_SET: [bool; 256] = {
let mut sc = [false; 256];
for c in &[b'.', b'+', b'-', b'_'] {
sc[*c as usize] = true;
}
sc
};
}
static EMAIL_OK_SET: Lazy<[bool; 256]> = Lazy::new(|| {
let mut sc = [false; 256];
for c in &[b'.', b'+', b'-', b'_'] {
sc[*c as usize] = true;
}
sc
});

let size = contents.len();

Expand Down
6 changes: 3 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use nodes::{
Ast, AstNode, ListDelimType, ListType, NodeCodeBlock, NodeDescriptionItem, NodeHeading,
NodeHtmlBlock, NodeList, NodeValue,
};
use once_cell::sync::Lazy;
use regex::bytes::{Regex, RegexBuilder};
use scanners;
use std::cell::RefCell;
Expand Down Expand Up @@ -1648,9 +1649,8 @@ impl<'a, 'o, 'c> Parser<'a, 'o, 'c> {
}

fn process_tasklist(&mut self, node: &'a AstNode<'a>, text: &mut Vec<u8>) {
lazy_static! {
static ref TASKLIST: Regex = Regex::new(r"\A(\s*\[([xX ])\])(?:\z|\s)").unwrap();
}
static TASKLIST: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\A(\s*\[([xX ])\])(?:\z|\s)").unwrap());

let (active, end) = match TASKLIST.captures(text) {
None => return,
Expand Down
14 changes: 6 additions & 8 deletions src/scanners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,23 @@ pub fn close_code_fence(line: &[u8]) -> Option<usize> {

#[inline(always)]
pub fn html_block_start(line: &[u8]) -> Option<usize> {
lazy_static! {
static ref STR2: &'static [u8] = b"<!--";
static ref STR3: &'static [u8] = b"<?";
static ref STR5: &'static [u8] = b"<![CDATA[";
}
const STR2: &'static [u8] = b"<!--";
const STR3: &'static [u8] = b"<?";
const STR5: &'static [u8] = b"<![CDATA[";

if !line.starts_with(b"<") {
return None;
}

if is_match(Rule::html_block_start_1, line) {
Some(1)
} else if line.starts_with(*STR2) {
} else if line.starts_with(STR2) {
Some(2)
} else if line.starts_with(*STR3) {
} else if line.starts_with(STR3) {
Some(3)
} else if is_match(Rule::html_block_start_4, line) {
Some(4)
} else if line.starts_with(*STR5) {
} else if line.starts_with(STR5) {
Some(5)
} else if is_match(Rule::html_block_start_6, line) {
Some(6)
Expand Down

0 comments on commit bdd3f20

Please sign in to comment.