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

Fixes #33

Merged
merged 10 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
15 changes: 6 additions & 9 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn get_table_data(unicode_data: &'static str) -> TableData {
Some((c, name))
}

let mut iter = unicode_data.split('\n');
let mut iter = unicode_data.lines();

let mut codepoint_names = vec![];
let mut cjk_ideograph_ranges = vec![];
Expand Down Expand Up @@ -98,17 +98,14 @@ pub struct Alias {

pub fn get_aliases(name_aliases: &'static str) -> Vec<Alias> {
let mut aliases = Vec::new();
for line in name_aliases.split(['\n', '\r']) {
if line.is_empty() {
continue;
}
if line.starts_with('#') {
for line in name_aliases.lines() {
if line.is_empty() | line.starts_with('#') {
continue;
}
let mut parts = line.splitn(3, ';');
let code = parts.next().unwrap();
let alias = parts.next().unwrap();
let category = parts.next().unwrap();
let code = parts.next().expect(line);
let alias = parts.next().expect(code);
let category = parts.next().expect(alias);
aliases.push(Alias {
code,
alias,
Expand Down
41 changes: 21 additions & 20 deletions generator/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,30 @@ pub struct Split<'a, 'b> {
impl<'a, 'b> Iterator for Split<'a, 'b> {
type Item = &'a str;
fn next(&mut self) -> Option<&'a str> {
if self.done {
return None;
} else if self.s.is_empty() {
self.done = true;
return Some("");
} else if !self.pending.is_empty() {
return Some(std::mem::take(&mut self.pending));
}

for (i, b) in self.s.bytes().enumerate() {
if b == b' ' || self.splitters.contains(&b) {
let ret = &self.s[..i];
// dont include the space, but include everything else on the next step
if b != b' ' {
self.pending = &self.s[i..i + 1]
match (self.done, self.s.is_empty(), self.pending.is_empty()) {
(true, _, _) => None,
(_, true, _) => {
self.done = true;
Some("")
}
(_, _, false) => Some(std::mem::take(&mut self.pending)),
_ => {
for (i, b) in self.s.bytes().enumerate() {
if b == b' ' || self.splitters.contains(&b) {
let ret = &self.s[..i];
// dont include the space, but include everything else on the next step
if b != b' ' {
self.pending = &self.s[i..i + 1]
}
self.s = &self.s[i + 1..];
return Some(ret);
}
}
self.s = &self.s[i + 1..];
return Some(ret);
// trailing data
self.done = true;
Some(self.s)
}
}
// trailing data
self.done = true;
Some(self.s)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced this is more readable than the existing code. Could you revert the change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you prefer to else as well?
If i take them out the first part would look like this

        if self.done {
            return None;
        } 
        if self.s.is_empty() {
            self.done = true;
            return Some("");
        } 
...

The reason I ask is that if-else with returns in them triggers some lints.

Of course if you'd rather keep them in, regardless, I'll do that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those sound like quite opinionated lints then. I'm usually fond of functional style, but this is a bit too much IMO.

}

Expand Down
24 changes: 12 additions & 12 deletions src/jamo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ pub fn is_hangul_syllable(c: char) -> bool {
}

pub fn syllable_decomposition(c: char) -> Option<(u8, u8, u8)> {
if !is_hangul_syllable(c) {
if is_hangul_syllable(c) {
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))
} else {
// outside the range
return None;
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))
}

fn slice_shift_byte(a: &[u8]) -> (Option<u8>, &[u8]) {
if !a.is_empty() {
(Some(a[0]), &a[1..])
} else {
if a.is_empty() {
(None, a)
} else {
(Some(a[0]), &a[1..])
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,10 @@ pub fn character(search_name: &str) -> Option<char> {
} // avoid overflow

let mut v = 0u32;
for &c in remaining.iter() {
match c {
b'0'..=b'9' => v = (v << 4) | (c - b'0') as u32,
b'A'..=b'F' => v = (v << 4) | (c - b'A' + 10) as u32,
for &c in remaining {
v = match c {
b'0'..=b'9' => (v << 4) | (c - b'0') as u32,
b'A'..=b'F' => (v << 4) | (c - b'A' + 10) as u32,
_ => return None,
}
}
Expand Down