1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 14:50:03 -04:00

use map_or and map_or_else

This commit is contained in:
Emil Ernerfeldt
2021-10-20 16:33:59 +02:00
parent a0cd41755e
commit 40445c450c
8 changed files with 26 additions and 41 deletions

View File

@@ -28,10 +28,7 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
while !text.is_empty() {
if start_of_line && text.starts_with("```") {
let end = text
.find("\n```")
.map(|i| i + 4)
.unwrap_or_else(|| text.len());
let end = text.find("\n```").map_or_else(|| text.len(), |i| i + 4);
job.append(
&text[..end],
0.0,
@@ -52,8 +49,7 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
style.code = true;
let end = text[1..]
.find(&['`', '\n'][..])
.map(|i| i + 2)
.unwrap_or_else(|| text.len());
.map_or_else(|| text.len(), |i| i + 2);
job.append(&text[..end], 0.0, format_from_style(visuals, &style));
text = &text[end..];
style.code = false;
@@ -112,12 +108,10 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
// Swallow everything up to the next special character:
let line_end = text[skip..]
.find('\n')
.map(|i| (skip + i + 1))
.unwrap_or_else(|| text.len());
.map_or_else(|| text.len(), |i| (skip + i + 1));
let end = text[skip..]
.find(&['*', '`', '~', '_', '/', '$', '^', '\\', '<', '['][..])
.map(|i| (skip + i).max(1)) // make sure we swallow at least one character
.unwrap_or_else(|| text.len());
.map_or_else(|| text.len(), |i| (skip + i).max(1));
if line_end <= end {
job.append(&text[..line_end], 0.0, format_from_style(visuals, &style));

View File

@@ -319,8 +319,7 @@ impl<'a> Iterator for Parser<'a> {
let end = self
.s
.find(&['*', '`', '~', '_', '/', '$', '^', '\\', '<', '[', '\n'][..])
.map(|special| special.max(1)) // make sure we swallow at least one character
.unwrap_or_else(|| self.s.len());
.map_or_else(|| self.s.len(), |special| special.max(1));
let item = Item::Text(self.style, &self.s[..end]);
self.s = &self.s[end..];