Skip to content

Commit babf1fc

Browse files
authored
Add hyphenation_character method to Lang (#31)
1 parent 8715c4c commit babf1fc

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ hypher = "0.1"
1818
disable the `alloc` feature, but then overly long words lead to a panic.
1919
- Support for many languages.
2020
- No unsafe code, no dependencies, no std.
21+
- Hyphenation character awareness: `Lang::hyphenation_character()` returns
22+
`None` for Indic scripts where visual hyphenation is not conventional.
2123

2224
## Example
2325
```rust

src/lang.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,40 @@ impl Lang {
368368
}
369369
}
370370

371+
/// The default character used to join syllables.
372+
///
373+
/// Returns `Some('\u{ad}')` (SOFT HYPHEN) for most languages, but `None`
374+
/// for Indic scripts where visual hyphenation is not conventional.
375+
pub fn hyphenation_character(self) -> Option<char> {
376+
match self {
377+
#[cfg(feature = "assamese")]
378+
Self::Assamese => None,
379+
#[cfg(feature = "bengali")]
380+
Self::Bengali => None,
381+
#[cfg(feature = "gujarati")]
382+
Self::Gujarati => None,
383+
#[cfg(feature = "hindi")]
384+
Self::Hindi => None,
385+
#[cfg(feature = "kannada")]
386+
Self::Kannada => None,
387+
#[cfg(feature = "malayalam")]
388+
Self::Malayalam => None,
389+
#[cfg(feature = "marathi")]
390+
Self::Marathi => None,
391+
#[cfg(feature = "oriya")]
392+
Self::Oriya => None,
393+
#[cfg(feature = "panjabi")]
394+
Self::Panjabi => None,
395+
#[cfg(feature = "sanskrit")]
396+
Self::Sanskrit => None,
397+
#[cfg(feature = "tamil")]
398+
Self::Tamil => None,
399+
#[cfg(feature = "telugu")]
400+
Self::Telugu => None,
401+
_ => Some('\u{ad}'),
402+
}
403+
}
404+
371405
fn root(self) -> State<'static> {
372406
match self {
373407
#[cfg(feature = "afrikaans")]

tests/generate.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ fn write_lang(
160160
writeln!(w, " }}")?;
161161
writeln!(w)?;
162162

163+
// Implementation of `hyphenation_character`.
164+
writeln!(w, " /// The default character used to join syllables.")?;
165+
writeln!(w, " ///")?;
166+
writeln!(w, " /// Returns `Some('\\u{{ad}}')` (SOFT HYPHEN) for most languages, but `None`")?;
167+
writeln!(
168+
w,
169+
" /// for Indic scripts where visual hyphenation is not conventional."
170+
)?;
171+
writeln!(w, " pub fn hyphenation_character(self) -> Option<char> {{")?;
172+
writeln!(w, " match self {{")?;
173+
for &(name, _, _, script, ..) in languages {
174+
if !is_indic_script(script) {
175+
continue;
176+
}
177+
let feature = name.to_lowercase();
178+
write!(w, " ")?;
179+
write_cfg(w, &feature)?;
180+
writeln!(w, " Self::{name} => None,")?;
181+
}
182+
writeln!(w, " _ => Some('\\u{{ad}}'),")?;
183+
writeln!(w, " }}")?;
184+
writeln!(w, " }}")?;
185+
writeln!(w)?;
186+
163187
// Implementation of `root`.
164188
writeln!(w, " fn root(self) -> State<'static> {{")?;
165189
writeln!(w, " match self {{")?;
@@ -175,6 +199,14 @@ fn write_lang(
175199
writeln!(w, "}}")
176200
}
177201

202+
/// Returns true for Indic scripts where visual hyphenation is not conventional.
203+
fn is_indic_script(script: &str) -> bool {
204+
matches!(
205+
script,
206+
"Beng" | "Deva" | "Gujr" | "Guru" | "Knda" | "Mlym" | "Orya" | "Taml" | "Telu"
207+
)
208+
}
209+
178210
fn write_cfg(w: &mut String, feature: &str) -> fmt::Result {
179211
writeln!(w, r#"#[cfg(feature = "{feature}")]"#)
180212
}

0 commit comments

Comments
 (0)