Use lazy_static! to avoid expensive regex compiles in loop
This commit is contained in:
parent
3ab9cedbcd
commit
bf84433d5b
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -224,6 +224,7 @@ name = "hexland-server"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"futures",
|
||||
"lazy_static",
|
||||
"rand",
|
||||
"regex",
|
||||
"sha2",
|
||||
@ -264,6 +265,12 @@ version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.133"
|
||||
|
@ -11,4 +11,5 @@ rand = "0.8.5"
|
||||
regex = "1.6.0"
|
||||
tokio = { version = "1.21.2", features = ["full"] }
|
||||
tokio-tungstenite = "0.17.2"
|
||||
futures = "0.3.24"
|
||||
futures = "0.3.24"
|
||||
lazy_static = "1.4.0"
|
@ -6,6 +6,7 @@ use tokio_tungstenite::{
|
||||
tungstenite::Message as WsMessage,
|
||||
};
|
||||
use futures::{StreamExt, SinkExt};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct Message {
|
||||
@ -25,8 +26,10 @@ pub enum Error {
|
||||
|
||||
impl Message {
|
||||
pub fn parse(text: String) -> Result<Message> {
|
||||
let re = regex::Regex::new(r"^([A-Z_]+):\s*(.*)").unwrap();
|
||||
match re.captures(text.as_str()) {
|
||||
lazy_static! {
|
||||
static ref RE: regex::Regex = regex::Regex::new(r"^([A-Z_]+):\s*(.*)").unwrap();
|
||||
}
|
||||
match RE.captures(text.as_str()) {
|
||||
Some(captures) => {
|
||||
if captures.len() < 3 {
|
||||
Err(Error::BadParse)
|
||||
|
Loading…
Reference in New Issue
Block a user