Skip to content

Commit

Permalink
rustfmt except for generated modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rustfmt authored and progval committed Mar 2, 2023
1 parent d121f8a commit 7b34023
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 185 deletions.
4 changes: 1 addition & 3 deletions examples/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use std::char;

fn main() {
let number = (0u32..0x10FFFF)
.filter(|x| {
char::from_u32(*x).map_or(false, |c| unicode_names2::name(c).is_some())
})
.filter(|x| char::from_u32(*x).map_or(false, |c| unicode_names2::name(c).is_some()))
.count();

println!("there are {} named code points", number)
Expand Down
29 changes: 12 additions & 17 deletions src/iter_str.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
#[cfg(feature = "no_std")]
use core::prelude::*;
use core::{slice, fmt};
use core::{fmt, slice};


use generated::{PHRASEBOOK_SHORT, PHRASEBOOK, LEXICON_SHORT_LENGTHS, LEXICON_ORDERED_LENGTHS,
LEXICON_OFFSETS, LEXICON};
use generated::{
LEXICON, LEXICON_OFFSETS, LEXICON_ORDERED_LENGTHS, LEXICON_SHORT_LENGTHS, PHRASEBOOK,
PHRASEBOOK_SHORT,
};

#[derive(Clone)]
pub struct IterStr {
phrasebook: slice::Iter<'static, u8>,
last_was_word: bool
last_was_word: bool,
}

impl IterStr {
pub fn new(start_index: usize) -> IterStr {
IterStr {
phrasebook: PHRASEBOOK[start_index..].iter(),
last_was_word: false
last_was_word: false,
}
}
}
Expand All @@ -30,8 +31,7 @@ impl Iterator for IterStr {
tmp.next().map(|&raw_b| {
// the first byte includes if it is the last in this name
// in the high bit.
let (is_end, b) = (raw_b & 0b1000_0000 != 0,
raw_b & 0b0111_1111);
let (is_end, b) = (raw_b & 0b1000_0000 != 0, raw_b & 0b0111_1111);

let ret = if b == HYPHEN {
// have to handle this before the case below, because a -
Expand All @@ -43,7 +43,7 @@ impl Iterator for IterStr {
// early return, we don't want to update the
// phrasebook (i.e. we're pretending we didn't touch
// this byte).
return " "
return " ";
} else {
self.last_was_word = true;

Expand All @@ -53,24 +53,19 @@ impl Iterator for IterStr {
// these lengths are hard-coded
LEXICON_SHORT_LENGTHS[idx] as usize
} else {
idx = (b - PHRASEBOOK_SHORT) as usize * 256 +
(*tmp.next().unwrap()) as usize;
idx = (b - PHRASEBOOK_SHORT) as usize * 256 + (*tmp.next().unwrap()) as usize;

// search for the right place: the first one where
// the end-point is after our current index.
match LEXICON_ORDERED_LENGTHS.iter().find(|&&(end, _)| idx < end) {
Some(&(_, len)) => len as usize,
None => unreachable!()
None => unreachable!(),
}
};
let offset = LEXICON_OFFSETS[idx] as usize;
&LEXICON[offset..offset + length]
};
self.phrasebook = if is_end {
(&[]).iter()
} else {
tmp
};
self.phrasebook = if is_end { (&[]).iter() } else { tmp };
ret
})
}
Expand Down
46 changes: 26 additions & 20 deletions src/jamo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
use core::prelude::*;

// derived from Jamo.txt
pub static CHOSEONG: &'static [&'static str] =
&["G", "GG", "N", "D", "DD", "R", "M", "B", "BB", "S",
"SS", "", "J", "JJ", "C", "K", "T", "P", "H"];
pub static JUNGSEONG: &'static [&'static str] =
&["A", "AE", "YA", "YAE", "EO", "E", "YEO", "YE", "O", "WA",
"WAE", "OE", "YO", "U", "WEO", "WE", "WI", "YU", "EU", "YI", "I"];
pub static JONGSEONG: &'static [&'static str] =
&["", "G", "GG", "GS", "N", "NJ", "NH", "D", "L", "LG",
"LM", "LB", "LS", "LT", "LP", "LH", "M", "B", "BS", "S",
"SS", "NG", "J", "C", "K", "T", "P", "H"];
pub static CHOSEONG: &'static [&'static str] = &[
"G", "GG", "N", "D", "DD", "R", "M", "B", "BB", "S", "SS", "", "J", "JJ", "C", "K", "T", "P",
"H",
];
pub static JUNGSEONG: &'static [&'static str] = &[
"A", "AE", "YA", "YAE", "EO", "E", "YEO", "YE", "O", "WA", "WAE", "OE", "YO", "U", "WEO", "WE",
"WI", "YU", "EU", "YI", "I",
];
pub static JONGSEONG: &'static [&'static str] = &[
"", "G", "GG", "GS", "N", "NJ", "NH", "D", "L", "LG", "LM", "LB", "LS", "LT", "LP", "LH", "M",
"B", "BS", "S", "SS", "NG", "J", "C", "K", "T", "P", "H",
];

pub fn is_hangul_syllable(c: char) -> bool {
'\u{AC00}' <= c && c <= '\u{D7A3}'
Expand All @@ -22,17 +24,15 @@ pub fn is_hangul_syllable(c: char) -> bool {
pub fn syllable_decomposition(c: char) -> Option<(u8, u8, u8)> {
if !is_hangul_syllable(c) {
// outside the range
return None
return None;
}
let n = c as u32 - 0xAC00;
// break this into the various parts.
let jongseong = n % 28;
let jungseong = (n / 28) % 21;
let choseong = n / (28 * 21);

Some((choseong as u8,
jungseong as u8,
jongseong as u8))
Some((choseong as u8, jungseong as u8, jongseong as u8))
}

fn slice_shift_byte<'a>(a: &'a [u8]) -> (Option<u8>, &'a [u8]) {
Expand Down Expand Up @@ -174,16 +174,20 @@ mod tests {
#[test]
fn correct_slice_shift_choseong() {
for (i, &choseong) in super::CHOSEONG.iter().enumerate() {
assert_eq!(super::slice_shift_choseong(choseong.as_bytes()),
(Some(i as u32), b"" as &[u8]));
assert_eq!(
super::slice_shift_choseong(choseong.as_bytes()),
(Some(i as u32), b"" as &[u8])
);
}
}

#[test]
fn correct_slice_shift_jungseong() {
for (i, &jungseong) in super::JUNGSEONG.iter().enumerate() {
assert_eq!(super::slice_shift_jungseong(jungseong.as_bytes()),
(Some(i as u32), b"" as &[u8]));
assert_eq!(
super::slice_shift_jungseong(jungseong.as_bytes()),
(Some(i as u32), b"" as &[u8])
);
}

// there is no empty jungseong
Expand All @@ -193,8 +197,10 @@ mod tests {
#[test]
fn correct_slice_shift_jongseong() {
for (i, &jongseong) in super::JONGSEONG.iter().enumerate() {
assert_eq!(super::slice_shift_jongseong(jongseong.as_bytes()),
(Some(i as u32), b"" as &[u8]));
assert_eq!(
super::slice_shift_jongseong(jongseong.as_bytes()),
(Some(i as u32), b"" as &[u8])
);
}
}
}
Loading

0 comments on commit 7b34023

Please sign in to comment.