summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-06-13 23:16:40 +0200
committerLaurenz <laurmaedje@gmail.com>2022-06-14 13:53:02 +0200
commitc81e2a5f56eb262663f292578c683fba7f18251f (patch)
tree6c045a8dcbec5e75e01a15f970ef8cee6ff042d0 /src/util
parent891af17260a6750a74a102388a05e59cf1ffc3c1 (diff)
Many fixes
Diffstat (limited to 'src/util')
-rw-r--r--src/util/eco.rs (renamed from src/util/eco_string.rs)4
-rw-r--r--src/util/hash.rs (renamed from src/util/prehashed.rs)0
-rw-r--r--src/util/mac_roman.rs25
-rw-r--r--src/util/mod.rs135
4 files changed, 36 insertions, 128 deletions
diff --git a/src/util/eco_string.rs b/src/util/eco.rs
index e3c8fc8a..63abe9e7 100644
--- a/src/util/eco_string.rs
+++ b/src/util/eco.rs
@@ -22,8 +22,8 @@ macro_rules! format_eco {
pub struct EcoString(Repr);
/// The internal representation. Either:
-/// - inline when below a certain number of bytes,
-/// - or reference-counted on the heap with COW semantics.
+/// - inline when below a certain number of bytes, or
+/// - reference-counted on the heap with clone-on-write semantics.
#[derive(Clone)]
enum Repr {
Small { buf: [u8; LIMIT], len: u8 },
diff --git a/src/util/prehashed.rs b/src/util/hash.rs
index 79455918..79455918 100644
--- a/src/util/prehashed.rs
+++ b/src/util/hash.rs
diff --git a/src/util/mac_roman.rs b/src/util/mac_roman.rs
deleted file mode 100644
index 95e8fcd6..00000000
--- a/src/util/mac_roman.rs
+++ /dev/null
@@ -1,25 +0,0 @@
-/// Decode mac roman encoded bytes into a string.
-pub fn decode_mac_roman(coded: &[u8]) -> String {
- coded.iter().copied().map(char_from_mac_roman).collect()
-}
-
-/// Convert a mac roman coded character to a unicode char.
-fn char_from_mac_roman(code: u8) -> char {
- #[rustfmt::skip]
- const TABLE: [char; 128] = [
- 'Ä', 'Å', 'Ç', 'É', 'Ñ', 'Ö', 'Ü', 'á', 'à', 'â', 'ä', 'ã', 'å', 'ç', 'é', 'è',
- 'ê', 'ë', 'í', 'ì', 'î', 'ï', 'ñ', 'ó', 'ò', 'ô', 'ö', 'õ', 'ú', 'ù', 'û', 'ü',
- '†', '°', '¢', '£', '§', '•', '¶', 'ß', '®', '©', '™', '´', '¨', '≠', 'Æ', 'Ø',
- '∞', '±', '≤', '≥', '¥', 'µ', '∂', '∑', '∏', 'π', '∫', 'ª', 'º', 'Ω', 'æ', 'ø',
- '¿', '¡', '¬', '√', 'ƒ', '≈', '∆', '«', '»', '…', '\u{a0}', 'À', 'Ã', 'Õ', 'Œ', 'œ',
- '–', '—', '“', '”', '‘', '’', '÷', '◊', 'ÿ', 'Ÿ', '⁄', '€', '‹', '›', 'fi', 'fl',
- '‡', '·', '‚', '„', '‰', 'Â', 'Ê', 'Á', 'Ë', 'È', 'Í', 'Î', 'Ï', 'Ì', 'Ó', 'Ô',
- '\u{f8ff}', 'Ò', 'Ú', 'Û', 'Ù', 'ı', 'ˆ', '˜', '¯', '˘', '˙', '˚', '¸', '˝', '˛', 'ˇ',
- ];
-
- if code < 128 {
- code as char
- } else {
- TABLE[(code - 128) as usize]
- }
-}
diff --git a/src/util/mod.rs b/src/util/mod.rs
index df3858d6..e0ba312f 100644
--- a/src/util/mod.rs
+++ b/src/util/mod.rs
@@ -1,18 +1,14 @@
//! Utilities.
#[macro_use]
-mod eco_string;
-mod mac_roman;
-mod prehashed;
+mod eco;
+mod hash;
-pub use eco_string::EcoString;
-pub use mac_roman::decode_mac_roman;
-pub use prehashed::Prehashed;
+pub use eco::EcoString;
+pub use hash::Prehashed;
use std::any::TypeId;
-use std::cmp::Ordering;
use std::fmt::{self, Debug, Formatter};
-use std::ops::Range;
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;
@@ -35,35 +31,6 @@ where
Wrapper(f)
}
-/// An alternative type id that prints as something readable in debug mode.
-#[derive(Copy, Clone, Eq, PartialEq, Hash)]
-pub struct ReadableTypeId {
- id: TypeId,
- #[cfg(debug_assertions)]
- name: &'static str,
-}
-
-impl ReadableTypeId {
- /// The type id of the given type.
- pub fn of<T: 'static>() -> Self {
- Self {
- id: TypeId::of::<T>(),
- #[cfg(debug_assertions)]
- name: std::any::type_name::<T>(),
- }
- }
-}
-
-impl Debug for ReadableTypeId {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- #[cfg(debug_assertions)]
- f.pad(self.name)?;
- #[cfg(not(debug_assertions))]
- f.pad("ReadableTypeId")?;
- Ok(())
- }
-}
-
/// Extra methods for [`str`].
pub trait StrExt {
/// The number of code units this string would use if it was encoded in
@@ -77,41 +44,6 @@ impl StrExt for str {
}
}
-/// Extra methods for [`Option<T>`].
-pub trait OptionExt<T> {
- /// Sets `other` as the value if `self` is `None` or if it contains a value
- /// larger than `other`.
- fn set_min(&mut self, other: T)
- where
- T: Ord;
-
- /// Sets `other` as the value if `self` is `None` or if it contains a value
- /// smaller than `other`.
- fn set_max(&mut self, other: T)
- where
- T: Ord;
-}
-
-impl<T> OptionExt<T> for Option<T> {
- fn set_min(&mut self, other: T)
- where
- T: Ord,
- {
- if self.as_ref().map_or(true, |x| other < *x) {
- *self = Some(other);
- }
- }
-
- fn set_max(&mut self, other: T)
- where
- T: Ord,
- {
- if self.as_ref().map_or(true, |x| other > *x) {
- *self = Some(other);
- }
- }
-}
-
/// Extra methods for [`Arc`].
pub trait ArcExt<T> {
/// Takes the inner value if there is exactly one strong reference and
@@ -131,7 +63,7 @@ where
}
}
-/// Extra methods for `[T]`.
+/// Extra methods for [`[T]`](slice).
pub trait SliceExt<T> {
/// Split a slice into consecutive runs with the same key and yield for
/// each such run the key and the slice of elements with that key.
@@ -170,34 +102,6 @@ where
}
}
-/// Extra methods for [`Range<usize>`].
-pub trait RangeExt {
- /// Locate a position relative to a range.
- ///
- /// This can be used for binary searching the range that contains the
- /// position as follows:
- /// ```
- /// # use typst::util::RangeExt;
- /// assert_eq!(
- /// [1..2, 2..7, 7..10].binary_search_by(|r| r.locate(5)),
- /// Ok(1),
- /// );
- /// ```
- fn locate(&self, pos: usize) -> Ordering;
-}
-
-impl RangeExt for Range<usize> {
- fn locate(&self, pos: usize) -> Ordering {
- if pos < self.start {
- Ordering::Greater
- } else if pos < self.end {
- Ordering::Equal
- } else {
- Ordering::Less
- }
- }
-}
-
/// Extra methods for [`Path`].
pub trait PathExt {
/// Lexically normalize a path.
@@ -222,3 +126,32 @@ impl PathExt for Path {
out
}
}
+
+/// An alternative type id that prints as something readable in debug mode.
+#[derive(Copy, Clone, Eq, PartialEq, Hash)]
+pub struct ReadableTypeId {
+ id: TypeId,
+ #[cfg(debug_assertions)]
+ name: &'static str,
+}
+
+impl ReadableTypeId {
+ /// The type id of the given type.
+ pub fn of<T: 'static>() -> Self {
+ Self {
+ id: TypeId::of::<T>(),
+ #[cfg(debug_assertions)]
+ name: std::any::type_name::<T>(),
+ }
+ }
+}
+
+impl Debug for ReadableTypeId {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ #[cfg(debug_assertions)]
+ f.pad(self.name)?;
+ #[cfg(not(debug_assertions))]
+ f.pad("ReadableTypeId")?;
+ Ok(())
+ }
+}