Skip to content

Commit 0af8e88

Browse files
veeshiwezm
authored andcommitted
add abbreviation method and fix clippy lints
1 parent cc14385 commit 0af8e88

3 files changed

Lines changed: 44 additions & 5 deletions

File tree

build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn compile_table() -> CompiledTable {
7373

7474
fn write_table(path: &Path, compiled_table: &CompiledTable) {
7575
let mut output =
76-
File::create(&path).expect(&format!("unable to open {}", path.to_string_lossy()));
76+
File::create(path).unwrap_or_else(|_| panic!("unable to open {}", path.to_string_lossy()));
7777

7878
writeln!(output, "use crate::GeneralCategory;").unwrap();
7979
writeln!(output, "use crate::GeneralCategory::*;").unwrap();
@@ -89,7 +89,7 @@ fn write_table(path: &Path, compiled_table: &CompiledTable) {
8989
// Write out the blocks in address order
9090
writeln!(
9191
output,
92-
"\nconst CATEGORY_BLOCKS: [GeneralCategory; {}] = [",
92+
"\n#[allow(clippy::large_const_arrays)]\nconst CATEGORY_BLOCKS: [GeneralCategory; {}] = [",
9393
compiled_table.blocks.len() * block::SIZE
9494
)
9595
.unwrap();

src/category.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ pub fn get_general_category(chr: char) -> GeneralCategory {
1616
let u = chr as u32;
1717

1818
if u <= LAST_CODEPOINT {
19-
return CATEGORY_BLOCKS
20-
[CATEGORY_BLOCK_OFFSETS[u as usize >> SHIFT] as usize + (u as usize & MASK)];
19+
CATEGORY_BLOCKS[CATEGORY_BLOCK_OFFSETS[u as usize >> SHIFT] as usize + (u as usize & MASK)]
2120
} else {
22-
return PrivateUse;
21+
PrivateUse
2322
}
2423
}

src/lib.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,46 @@ pub use tables::GeneralCategory;
1919
/// that this version of unicode-general-category was generated from.
2020
pub const UNICODE_VERSION: (u64, u64, u64) = (16, 0, 0);
2121

22+
impl GeneralCategory {
23+
/// Returns the general category abbreviation.
24+
/// See [Unicode General Category Values](https://www.unicode.org/reports/tr44/tr44-30.html#General_Category_Values)
25+
/// for the list of all general categories.
26+
pub fn abbreviation(&self) -> &'static str {
27+
match self {
28+
GeneralCategory::ClosePunctuation => "Pe",
29+
GeneralCategory::ConnectorPunctuation => "Pc",
30+
GeneralCategory::Control => "Cc",
31+
GeneralCategory::CurrencySymbol => "Sc",
32+
GeneralCategory::DashPunctuation => "Pd",
33+
GeneralCategory::DecimalNumber => "Nd",
34+
GeneralCategory::EnclosingMark => "Me",
35+
GeneralCategory::FinalPunctuation => "Pf",
36+
GeneralCategory::Format => "Cf",
37+
GeneralCategory::InitialPunctuation => "Pi",
38+
GeneralCategory::LetterNumber => "Nl",
39+
GeneralCategory::LineSeparator => "Zl",
40+
GeneralCategory::LowercaseLetter => "Ll",
41+
GeneralCategory::MathSymbol => "Sm",
42+
GeneralCategory::ModifierLetter => "Lm",
43+
GeneralCategory::ModifierSymbol => "Sk",
44+
GeneralCategory::NonspacingMark => "Mn",
45+
GeneralCategory::OpenPunctuation => "Ps",
46+
GeneralCategory::OtherLetter => "Lo",
47+
GeneralCategory::OtherNumber => "No",
48+
GeneralCategory::OtherPunctuation => "Po",
49+
GeneralCategory::OtherSymbol => "So",
50+
GeneralCategory::ParagraphSeparator => "Zp",
51+
GeneralCategory::PrivateUse => "Co",
52+
GeneralCategory::SpaceSeparator => "Zs",
53+
GeneralCategory::SpacingMark => "Mc",
54+
GeneralCategory::Surrogate => "Cs",
55+
GeneralCategory::TitlecaseLetter => "Lt",
56+
GeneralCategory::Unassigned => "Cn",
57+
GeneralCategory::UppercaseLetter => "Lu",
58+
}
59+
}
60+
}
61+
2262
#[cfg(test)]
2363
mod test {
2464
use super::{get_general_category, GeneralCategory};

0 commit comments

Comments
 (0)