Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Identifier hashing into Self's Impl #59

Merged
merged 2 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/glyph/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::collections::hash_map::DefaultHasher;
use std::collections::HashSet;
use std::hash::{Hash, Hasher};

use crate::error::ErrorKind;
use crate::glyph::{
Expand Down Expand Up @@ -144,7 +142,7 @@ impl GlyphBuilder {
return Err(ErrorKind::UnexpectedTag);
}
if let Some(identifier) = &guideline.identifier {
let identifier_hash = Self::hash_identifier(&identifier);
let identifier_hash = identifier.hash();
if !self.identifiers.insert(identifier_hash) {
return Err(ErrorKind::DuplicateIdentifier);
}
Expand All @@ -161,7 +159,7 @@ impl GlyphBuilder {
return Err(ErrorKind::UnexpectedTag);
}
if let Some(identifier) = &anchor.identifier {
let identifier_hash = Self::hash_identifier(&identifier);
let identifier_hash = identifier.hash();
if !self.identifiers.insert(identifier_hash) {
return Err(ErrorKind::DuplicateIdentifier);
}
Expand Down Expand Up @@ -242,7 +240,7 @@ impl GlyphBuilder {
return Err(ErrorKind::UnexpectedAttribute);
}
if let Some(identifier) = &identifier {
let identifier_hash = Self::hash_identifier(&identifier);
let identifier_hash = identifier.hash();
if !self.identifiers.insert(identifier_hash) {
return Err(ErrorKind::DuplicateIdentifier);
}
Expand Down Expand Up @@ -304,7 +302,7 @@ impl GlyphBuilder {
}
if let Some(identifier) = &point.identifier {
// TODO: test membership at fn start and insert() before push()?
let identifier_hash = Self::hash_identifier(&identifier);
let identifier_hash = identifier.hash();
if !self.identifiers.insert(identifier_hash) {
return Err(ErrorKind::DuplicateIdentifier);
}
Expand Down Expand Up @@ -400,7 +398,7 @@ impl GlyphBuilder {
return Err(ErrorKind::UnexpectedAttribute);
}
if let Some(identifier) = &identifier {
let identifier_hash = Self::hash_identifier(&identifier);
let identifier_hash = identifier.hash();
if !self.identifiers.insert(identifier_hash) {
return Err(ErrorKind::DuplicateIdentifier);
}
Expand All @@ -410,13 +408,6 @@ impl GlyphBuilder {
Ok(self)
}

/// Return the u64 hash value of an Identifier.
fn hash_identifier(identifier: &Identifier) -> u64 {
let mut hasher = DefaultHasher::new();
identifier.hash(&mut hasher);
hasher.finish()
}

/// Check if a contour is really an informal anchor according to the Glif v2 specification.
fn contour_is_v1_anchor(format: &GlifVersion, points: &[ContourPoint]) -> bool {
*format == GlifVersion::V1
Expand All @@ -426,6 +417,17 @@ impl GlyphBuilder {
}
}

// #[derive(Debug)]
// pub struct OutlineBuilder {
// identifiers: HashSet<u64>, // All identifiers within a glyph must be unique.
// scratch_contour: Option<Contour>,
// number_of_offcurves: u32,
// }

// impl OutlineBuilder {

// }

#[cfg(test)]
mod tests {
use super::*;
Expand Down
9 changes: 9 additions & 0 deletions src/shared_types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::collections::hash_map::DefaultHasher;
use std::convert::TryFrom;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::str::FromStr;

Expand Down Expand Up @@ -76,6 +78,13 @@ impl Identifier {
pub fn as_str(&self) -> &str {
&self.0
}

/// Return the u64 hash value of self.
pub fn hash(&self) -> u64 {
let mut hasher = DefaultHasher::new();
Hash::hash(&self, &mut hasher);
hasher.finish()
}
}

impl FromStr for Identifier {
Expand Down