Skip to content

Commit 75cdfa1

Browse files
committed
fixup! feat(base): Introduce a DisplayName struct
1 parent fd33e75 commit 75cdfa1

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

crates/matrix-sdk-base/src/deserialized_responses.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ pub struct AmbiguityChanges {
7070
pub changes: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, AmbiguityChange>>,
7171
}
7272

73+
static MXID_REGEX: Lazy<Regex> = Lazy::new(|| {
74+
Regex::new(DisplayName::MXID_PATTERN)
75+
.expect("We should be able to create a regex from our static MXID pattern")
76+
});
77+
static LEFT_TO_RIGHT_REGEX: Lazy<Regex> = Lazy::new(|| {
78+
Regex::new(DisplayName::LEFT_TO_RIGHT_PATTERN)
79+
.expect("We should be able to create a regex from our static left-to-right pattern")
80+
});
81+
static HIDDEN_CHARACTERS_REGEX: Lazy<Regex> = Lazy::new(|| {
82+
Regex::new(DisplayName::HIDDEN_CHARACTERS_PATTERN)
83+
.expect("We should be able to create a regex from our static hidden characters pattern")
84+
});
85+
7386
/// A high-level wrapper for strings representing display names.
7487
///
7588
/// This wrapper provides attempts to determine whether a display name
@@ -145,19 +158,6 @@ impl DisplayName {
145158
const HIDDEN_CHARACTERS_PATTERN: &str =
146159
"[\u{2000}-\u{200D}\u{300}-\u{036f}\u{2062}-\u{2063}\u{2800}\u{061c}\u{feff}]";
147160

148-
const MXID_REGEX: Lazy<Regex> = Lazy::new(|| {
149-
Regex::new(DisplayName::MXID_PATTERN)
150-
.expect("We should be able to create a regex from our static MXID pattern")
151-
});
152-
const LEFT_TO_RIGHT_REGEX: Lazy<Regex> = Lazy::new(|| {
153-
Regex::new(DisplayName::LEFT_TO_RIGHT_PATTERN)
154-
.expect("We should be able to create a regex from our static left-to-right pattern")
155-
});
156-
const HIDDEN_CHARACTERS_REGEX: Lazy<Regex> = Lazy::new(|| {
157-
Regex::new(DisplayName::HIDDEN_CHARACTERS_PATTERN)
158-
.expect("We should be able to create a regex from our static hidden characters pattern")
159-
});
160-
161161
/// Creates a new [`DisplayName`] from the given raw string.
162162
///
163163
/// The raw display name is transformed into a Unicode-normalized form, with
@@ -169,11 +169,11 @@ impl DisplayName {
169169
/// during construction.
170170
pub fn new(raw: &str) -> Self {
171171
let normalized = raw.nfd().collect::<String>();
172-
let replaced = Self::HIDDEN_CHARACTERS_REGEX.replace_all(&normalized, "");
172+
let replaced = HIDDEN_CHARACTERS_REGEX.replace_all(&normalized, "");
173173

174174
let decancered = decancer::cure!(&replaced)
175175
.ok()
176-
.map(|cured| Self::LEFT_TO_RIGHT_REGEX.replace_all(cured.as_ref(), "").to_string());
176+
.map(|cured| LEFT_TO_RIGHT_REGEX.replace_all(cured.as_ref(), "").to_string());
177177

178178
Self { raw: raw.to_owned(), decancered }
179179
}
@@ -226,11 +226,11 @@ impl DisplayName {
226226
}
227227

228228
fn has_left_to_right_characters(&self) -> bool {
229-
Self::LEFT_TO_RIGHT_REGEX.is_match(&self.raw)
229+
LEFT_TO_RIGHT_REGEX.is_match(&self.raw)
230230
}
231231

232232
fn might_look_like_an_mxid(&self) -> bool {
233-
Self::MXID_REGEX.is_match(&self.raw)
233+
MXID_REGEX.is_match(&self.raw)
234234
}
235235
}
236236

0 commit comments

Comments
 (0)