Skip to content

Commit

Permalink
cleaner regex gen
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Jul 17, 2023
1 parent 54f746c commit 3489db7
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions martin/src/fonts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ use std::collections::HashMap;
use std::ffi::OsStr;
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;

use bit_set::BitSet;
use log::{debug, info, warn};
use pbf_font_tools::freetype::{Face, Library};
use pbf_font_tools::protobuf::Message;
use pbf_font_tools::{render_sdf_glyph, Fontstack, Glyphs, PbfFontError};
use regex::Regex;
use serde::{Deserialize, Serialize};

use crate::fonts::FontError::IoError;
Expand Down Expand Up @@ -79,6 +81,8 @@ fn recurse_dirs(
fonts: &mut HashMap<String, FontSource>,
catalog: &mut HashMap<String, FontEntry>,
) -> Result<(), FontError> {
static RE_SPACES: OnceLock<Regex> = OnceLock::new();

for dir_entry in path
.read_dir()
.map_err(|e| IoError(e, path.to_path_buf()))?
Expand Down Expand Up @@ -115,10 +119,11 @@ fn recurse_dirs(
name.push_str(style);
}
// Make sure font name has no slashes or commas, replacing them with spaces and de-duplicating spaces
name = name
.replace(['/', ','], " ")
.replace(" ", " ")
.replace(" ", " ");
name = name.replace(['/', ','], " ");
name = RE_SPACES
.get_or_init(|| Regex::new(r"\s+").unwrap())
.replace_all(name.as_str(), " ")
.to_string();

match fonts.entry(name) {
Entry::Occupied(v) => {
Expand Down

0 comments on commit 3489db7

Please sign in to comment.