Skip to content
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
4 changes: 2 additions & 2 deletions statement/src/_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub enum Literal<'a> {

impl Literal<'_> {
/// Borrow this [`Literal`] as another [`Literal`].
pub fn borrowed(&self) -> Literal {
pub fn borrowed(&self) -> Literal<'_> {
match self {
Literal::Typed(lex, iri) => Literal::Typed(Cow::from(lex.as_ref()), iri.borrowed()),
Literal::LanguageString(lex, lang_tag, base_dir) => {
Expand All @@ -46,7 +46,7 @@ impl Literal<'_> {
}

/// [lexical form](https://www.w3.org/TR/rdf12-concepts/#dfn-lexical-form) of this literal
pub fn lexical_form(&self) -> Cow<str> {
pub fn lexical_form(&self) -> Cow<'_, str> {
let ref_cow = match self {
Literal::Typed(lex, ..) => lex,
Literal::LanguageString(lex, ..) => lex,
Expand Down
42 changes: 33 additions & 9 deletions statement/src/_literal/_language_tag.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::borrow::Cow;
use std::{borrow::Cow, cmp::Ordering};

/// Wrapper around a [`Cow<str>`] guaranteeing that the underlying text satisfies [BCP47].
///
Expand All @@ -7,7 +7,7 @@ use std::borrow::Cow;
/// (i.e. ISO 639 for 2-3 characters language tag, or ISO 15924 for the script)
///
/// [BCP47]: https://datatracker.ietf.org/doc/bcp47/
#[derive(Clone, Debug, Eq, Ord)]
#[derive(Clone, Debug, Eq)]
pub struct LangTag<'a>(Cow<'a, str>);

impl<'a> LangTag<'a> {
Expand Down Expand Up @@ -76,25 +76,27 @@ impl std::cmp::PartialEq<LangTag<'_>> for &str {
}
}

impl std::cmp::Ord for LangTag<'_> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
cmp_ignore_case_ascii(&self.0, &other.0)
}
}

impl std::cmp::PartialOrd for LangTag<'_> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(
self.0
.to_ascii_lowercase()
.cmp(&other.0.to_ascii_lowercase()),
)
Some(self.cmp(other))
}
}

impl std::cmp::PartialOrd<&str> for LangTag<'_> {
fn partial_cmp(&self, other: &&'_ str) -> Option<std::cmp::Ordering> {
Some(self.0.to_ascii_lowercase().cmp(&other.to_ascii_lowercase()))
Some(cmp_ignore_case_ascii(&self.0, *other))
}
}

impl std::cmp::PartialOrd<LangTag<'_>> for &str {
fn partial_cmp(&self, other: &LangTag<'_>) -> Option<std::cmp::Ordering> {
Some(self.to_ascii_lowercase().cmp(&other.0.to_ascii_lowercase()))
Some(cmp_ignore_case_ascii(self, &other.0))
}
}

Expand All @@ -104,8 +106,26 @@ impl std::fmt::Display for LangTag<'_> {
}
}

fn cmp_ignore_case_ascii(a: &str, b: &str) -> Ordering {
cmp_ignore_case_ascii_bytes(a.as_bytes(), b.as_bytes())
}

fn cmp_ignore_case_ascii_bytes(a: &[u8], b: &[u8]) -> Ordering {
match (a, b) {
([], []) => Ordering::Equal,
([], [_, ..]) => Ordering::Less,
([_, ..], []) => Ordering::Greater,
([ah, ar @ ..], [bh, br @ ..]) => ah
.to_ascii_lowercase()
.cmp(&bh.to_ascii_lowercase())
.then_with(|| cmp_ignore_case_ascii_bytes(ar, br)),
}
}

#[cfg(test)]
mod test {
use std::cmp::Ordering;

use super::*;

#[test]
Expand Down Expand Up @@ -140,6 +160,10 @@ mod test {
assert_eq!(tag1, tag2);
assert_eq!(tag1, "en-gb");
assert!(tag1 <= tag2 && tag2 <= tag1);
assert_eq!(tag1.partial_cmp(&tag2), Some(Ordering::Equal));
assert_eq!(tag2.partial_cmp(&tag1), Some(Ordering::Equal));
assert_eq!(tag1.cmp(&tag2), Ordering::Equal);
assert_eq!(tag2.cmp(&tag1), Ordering::Equal);
assert!("EN" < tag1 && tag1 < "EN-ZZ");
}
}
2 changes: 1 addition & 1 deletion statement/src/impl_oxrdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! This module is developed as if [`oxrdf`] implemented RDF 1.2 completely and strictly,
//! which is not entirely true:
//! - [`oxrdf`] does not support base direction in literals, so it is not complete;
//! - [`oxrdf`] with the [`rdf-star`] feature allows triple terms in the subject position, so it is not strict.
//! - [`oxrdf`] with the `rdf-star` feature allows triple terms in the subject position, so it is not strict.
//!
//! This is handled by panic'ing when those situations are encountered.
//!
Expand Down