├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── README.md └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "0.7.18" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "demoji" 16 | version = "0.0.3" 17 | dependencies = [ 18 | "once_cell", 19 | "regex", 20 | ] 21 | 22 | [[package]] 23 | name = "memchr" 24 | version = "2.4.1" 25 | source = "registry+https://github.com/rust-lang/crates.io-index" 26 | checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" 27 | 28 | [[package]] 29 | name = "once_cell" 30 | version = "1.10.0" 31 | source = "registry+https://github.com/rust-lang/crates.io-index" 32 | checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" 33 | 34 | [[package]] 35 | name = "regex" 36 | version = "1.5.4" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" 39 | dependencies = [ 40 | "aho-corasick", 41 | "memchr", 42 | "regex-syntax", 43 | ] 44 | 45 | [[package]] 46 | name = "regex-syntax" 47 | version = "0.6.25" 48 | source = "registry+https://github.com/rust-lang/crates.io-index" 49 | checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" 50 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "demoji" 3 | version = "0.0.3" 4 | edition = "2021" 5 | authors = ["XtremeDevX "] 6 | license = "MIT" 7 | description = "Remove all emojis from a string." 8 | keywords = ["emoji"] 9 | repository = "https://github.com/XtremeDevX/demoji" 10 | readme = "README.md" 11 | 12 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 13 | 14 | [dependencies] 15 | regex = "1.5.4" 16 | once_cell = "1.10.0" 17 | 18 | [profile.release] 19 | opt-level = 3 # Optimize for speed. 20 | lto = true # Enable Link Time Optimization 21 | codegen-units = 1 # Reduce number of codegen units to increase optimizations. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Demoji 2 | 3 | A lightweight and snappy crate to remove emojis from a string. 4 | 5 | ## Overview 6 | 7 | This crate allows for removal of emojis given a string, and that's it! 8 | 9 | This crate aims to be: 10 | 11 | - Fast 12 | - Lightweight 13 | - Easy To Use 14 | 15 | ## Examples 16 | 17 | ```rust 18 | // Remove all emojis from this string 19 | let demojified_string = demoji("⚡hel✅🙂lo🙂") 20 | assert_eq!(demojified_string, String::from("hello")); 21 | ``` 22 | 23 | ### What does the name mean? 24 | 25 | Well, you could interpret it as `demolish-emoji` or `de-emoji` 😁. 26 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! A lightweight and snappy crate to remove emojis from a string. 2 | //! 3 | //! ## Overview 4 | //! 5 | //! This crate allows for removal of emojis given a `String`, and that's it! 6 | //! 7 | //! This crate aims to be: 8 | //! 9 | //! - Fast 10 | //! - Lightweight 11 | //! - Easy To Use 12 | //! 13 | //! ## Examples 14 | //! 15 | //! ```rust 16 | //! # use demoji::demoji; 17 | //! // Remove all emojis from this string 18 | //! let demojified_string = demoji("⚡hel✅🙂lo🙂"); 19 | //! assert_eq!(demojified_string, "hello"); 20 | //! ``` 21 | use once_cell::sync::Lazy; 22 | use regex::Regex; 23 | use std::borrow::Cow; 24 | 25 | /// Removes all emojis from a string **(retains chinese characters)** 26 | /// 27 | /// # Arguments 28 | /// 29 | /// * `string` - String with emojis 30 | /// 31 | /// # Returns 32 | /// 33 | /// * `Cow` - De-emojified string 34 | /// 35 | /// If no emojis are found in the given string, then the original string is returned, avoiding the allocation of a new string. 36 | /// 37 | /// # Examples 38 | /// 39 | /// ``` 40 | /// # use demoji::demoji; 41 | /// // Remove all emojis from this string 42 | /// let demojified_string = demoji("⚡hel✅🙂lo🙂"); 43 | /// assert_eq!(demojified_string, String::from("hello")); 44 | /// // Output: `hello` 45 | /// ``` 46 | pub fn demoji(string: &str) -> Cow<'_, str> { 47 | // cache regex to avoid re-compiling it every call 48 | static REGEX: Lazy = Lazy::new(|| { 49 | Regex::new(concat!( 50 | "[", 51 | "\u{01F600}-\u{01F64F}", // emoticons 52 | "\u{01F300}-\u{01F5FF}", // symbols & pictographs 53 | "\u{01F680}-\u{01F6FF}", // transport & map symbols 54 | "\u{01F1E0}-\u{01F1FF}", // flags (iOS) 55 | "\u{002702}-\u{0027B0}", 56 | "\u{0024C2}-\u{01F251}", 57 | "]+", 58 | )) 59 | .unwrap() 60 | }); 61 | 62 | REGEX.replace_all(&string, "") 63 | } 64 | 65 | /// Demojify `String` and `&str` 66 | pub trait Demoji { 67 | fn demojify(&self) -> Cow<'_, str>; 68 | } 69 | 70 | impl Demoji for &str { 71 | fn demojify(&self) -> Cow<'_, str> { 72 | demoji(self) 73 | } 74 | } 75 | 76 | impl Demoji for String { 77 | fn demojify(&self) -> Cow<'_, str> { 78 | demoji(&self) 79 | } 80 | } 81 | --------------------------------------------------------------------------------