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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ hypher = "0.1"
disable the `alloc` feature, but then overly long words lead to a panic.
- Support for many languages.
- No unsafe code, no dependencies, no std.
- Hyphenation character awareness: `Lang::hyphenation_character()` returns
`None` for Indic scripts where visual hyphenation is not conventional.

## Example
```rust
Expand Down
34 changes: 34 additions & 0 deletions src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,40 @@ impl Lang {
}
}

/// The default character used to join syllables.
///
/// Returns `Some('\u{ad}')` (SOFT HYPHEN) for most languages, but `None`
/// for Indic scripts where visual hyphenation is not conventional.
pub fn hyphenation_character(self) -> Option<char> {
match self {
#[cfg(feature = "assamese")]
Self::Assamese => None,
#[cfg(feature = "bengali")]
Self::Bengali => None,
#[cfg(feature = "gujarati")]
Self::Gujarati => None,
#[cfg(feature = "hindi")]
Self::Hindi => None,
#[cfg(feature = "kannada")]
Self::Kannada => None,
#[cfg(feature = "malayalam")]
Self::Malayalam => None,
#[cfg(feature = "marathi")]
Self::Marathi => None,
#[cfg(feature = "oriya")]
Self::Oriya => None,
#[cfg(feature = "panjabi")]
Self::Panjabi => None,
#[cfg(feature = "sanskrit")]
Self::Sanskrit => None,
#[cfg(feature = "tamil")]
Self::Tamil => None,
#[cfg(feature = "telugu")]
Self::Telugu => None,
_ => Some('\u{ad}'),
}
}

fn root(self) -> State<'static> {
match self {
#[cfg(feature = "afrikaans")]
Expand Down
32 changes: 32 additions & 0 deletions tests/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,30 @@ fn write_lang(
writeln!(w, " }}")?;
writeln!(w)?;

// Implementation of `hyphenation_character`.
writeln!(w, " /// The default character used to join syllables.")?;
writeln!(w, " ///")?;
writeln!(w, " /// Returns `Some('\\u{{ad}}')` (SOFT HYPHEN) for most languages, but `None`")?;
writeln!(
w,
" /// for Indic scripts where visual hyphenation is not conventional."
)?;
writeln!(w, " pub fn hyphenation_character(self) -> Option<char> {{")?;
writeln!(w, " match self {{")?;
for &(name, _, _, script, ..) in languages {
if !is_indic_script(script) {
continue;
}
let feature = name.to_lowercase();
write!(w, " ")?;
write_cfg(w, &feature)?;
writeln!(w, " Self::{name} => None,")?;
}
writeln!(w, " _ => Some('\\u{{ad}}'),")?;
writeln!(w, " }}")?;
writeln!(w, " }}")?;
writeln!(w)?;

// Implementation of `root`.
writeln!(w, " fn root(self) -> State<'static> {{")?;
writeln!(w, " match self {{")?;
Expand All @@ -175,6 +199,14 @@ fn write_lang(
writeln!(w, "}}")
}

/// Returns true for Indic scripts where visual hyphenation is not conventional.
fn is_indic_script(script: &str) -> bool {
matches!(
script,
"Beng" | "Deva" | "Gujr" | "Guru" | "Knda" | "Mlym" | "Orya" | "Taml" | "Telu"
)
}

fn write_cfg(w: &mut String, feature: &str) -> fmt::Result {
writeln!(w, r#"#[cfg(feature = "{feature}")]"#)
}
Expand Down
Loading