Skip to content

Commit 54f90d4

Browse files
committed
Removed regex entirely
This removes `regex` as suggested in the PR.
1 parent 657f61f commit 54f90d4

File tree

3 files changed

+28
-81
lines changed

3 files changed

+28
-81
lines changed

Cargo.toml

-6
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ lazy_static = "^1.4"
3131
libc = { version = "^0.2", optional = true }
3232
parking_lot = "^0.11"
3333
protobuf = { version = "^2.0", optional = true }
34-
# DO NOT RELY ON THIS FEATURE TO STAY AVAILABLE!
35-
# It doesn't change the API.
36-
# Intended for testing/debugging only.
37-
# It can affect the performance.
38-
# Report any interesting findings, especially if the performance IMPROVES with `regex` turned ON.
39-
regex = { version = "^1.3", optional = true }
4034
memchr = "^2.3"
4135
reqwest = { version = "^0.11", features = ["blocking"], optional = true }
4236
thiserror = "^1.0"

src/desc.rs

+28-54
Original file line numberDiff line numberDiff line change
@@ -10,66 +10,40 @@ use crate::errors::{Error, Result};
1010
use crate::metrics::SEPARATOR_BYTE;
1111
use crate::proto::LabelPair;
1212

13-
#[cfg(not(feature = "regex"))]
14-
mod validation {
15-
fn matches_charset_without_colon(c: char) -> bool {
16-
c.is_ascii_alphabetic() || c == '_'
17-
}
18-
19-
fn matches_charset_with_colon(c: char) -> bool {
20-
matches_charset_without_colon(c) || c == ':'
21-
}
22-
23-
/// Equivalent to regex `^[?][?0-9]*$` where `?` denotes char set as validated by `charset_validator`.
24-
fn is_valid_ident<F: FnMut(char) -> bool>(input: &str, mut charset_validator: F) -> bool {
25-
let mut chars = input.chars();
26-
let zeroth = chars.next();
27-
zeroth
28-
.and_then(|zeroth| {
29-
if charset_validator(zeroth) {
30-
Some(chars.all(|c| charset_validator(c) || c.is_digit(10)))
31-
} else {
32-
None
33-
}
34-
})
35-
.unwrap_or(false)
36-
}
37-
38-
// Details of required format are at
39-
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
40-
pub(super) fn is_valid_metric_name(name: &str) -> bool {
41-
is_valid_ident(name, matches_charset_with_colon)
42-
}
43-
44-
pub(super) fn is_valid_label_name(name: &str) -> bool {
45-
is_valid_ident(name, matches_charset_without_colon)
46-
}
13+
// [a-zA-Z_]
14+
fn matches_charset_without_colon(c: char) -> bool {
15+
c.is_ascii_alphabetic() || c == '_'
4716
}
4817

49-
#[cfg(feature = "regex")]
50-
mod validation {
51-
use regex::Regex;
52-
53-
pub(super) fn is_valid_metric_name(name: &str) -> bool {
54-
lazy_static! {
55-
static ref VALIDATOR: Regex =
56-
Regex::new("^[a-zA-Z_:][a-zA-Z0-9_:]*$").expect("Regex to be valid.");
57-
}
58-
59-
VALIDATOR.is_match(name)
60-
}
18+
// [a-zA-Z_:]
19+
fn matches_charset_with_colon(c: char) -> bool {
20+
matches_charset_without_colon(c) || c == ':'
21+
}
6122

62-
pub(super) fn is_valid_label_name(name: &str) -> bool {
63-
lazy_static! {
64-
static ref VALIDATOR: Regex =
65-
Regex::new("^[a-zA-Z_][a-zA-Z0-9_]*$").expect("Regex to be valid.");
66-
}
23+
// Equivalent to regex ^[?][?0-9]*$ where ? denotes char set as validated by charset_validator
24+
fn is_valid_ident<F: FnMut(char) -> bool>(input: &str, mut charset_validator: F) -> bool {
25+
let mut chars = input.chars();
26+
let zeroth = chars.next();
27+
zeroth
28+
.and_then(|zeroth| {
29+
if charset_validator(zeroth) {
30+
Some(chars.all(|c| charset_validator(c) || c.is_digit(10)))
31+
} else {
32+
None
33+
}
34+
})
35+
.unwrap_or(false)
36+
}
6737

68-
VALIDATOR.is_match(name)
69-
}
38+
// Details of required format are at
39+
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
40+
pub(super) fn is_valid_metric_name(name: &str) -> bool {
41+
is_valid_ident(name, matches_charset_with_colon)
7042
}
7143

72-
use validation::*;
44+
pub(super) fn is_valid_label_name(name: &str) -> bool {
45+
is_valid_ident(name, matches_charset_without_colon)
46+
}
7347

7448
/// The descriptor used by every Prometheus [`Metric`](crate::core::Metric). It is essentially
7549
/// the immutable meta-data of a metric. The normal metric implementations

src/encoder/text.rs

-21
Original file line numberDiff line numberDiff line change
@@ -215,27 +215,6 @@ fn label_pairs_to_text(
215215
Ok(())
216216
}
217217

218-
#[cfg(feature = "regex")]
219-
fn find_first_occurence(v: &str, include_double_quote: bool) -> Option<usize> {
220-
use regex::{Match, Regex};
221-
222-
// Regex compilation is expensive. Use `lazy_static` to compile the regexes
223-
// once per process lifetime and not once per function invocation.
224-
lazy_static! {
225-
static ref ESCAPER: Regex = Regex::new("(\\\\|\n)").expect("Regex to be valid.");
226-
static ref QUOTED_ESCAPER: Regex = Regex::new("(\\\\|\n|\")").expect("Regex to be valid.");
227-
}
228-
229-
if include_double_quote {
230-
QUOTED_ESCAPER.find(v)
231-
} else {
232-
ESCAPER.find(v)
233-
}
234-
.as_ref()
235-
.map(Match::start)
236-
}
237-
238-
#[cfg(not(feature = "regex"))]
239218
fn find_first_occurence(v: &str, include_double_quote: bool) -> Option<usize> {
240219
if include_double_quote {
241220
memchr::memchr3(b'\\', b'\n', b'\"', v.as_bytes())

0 commit comments

Comments
 (0)