1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//! Text processing utilities
//!
//! This module contains some utilities to enrich text with emojis.
//! The basis of this module is the [`parse_alias`] function which takes
//! emoji names (e.g. `:crab:`) and tries to look up the appropriate emoji
//! (e.g. `🦀`).
//!
//! Based on the [`parse_alias`] function, the [`parse_text`] function takes
//! an entire text and looks for such colon-fenced emoji names, which will then
//! be translated into the Unicode equivalent, and the entire text, with the
//! emojis replaced is returned.

use core::fmt;

use crate::emojis::Emoji;

#[cfg(feature = "alloc")]
use alloc::string::String;

/// Parses the given Emoji name into a unicode Emoji.
///
/// This function accepts strings of the form `:name:` and looks up an emojis for it.
/// The list of valid names is taken from: [github/gemoji](https://github.com/github/gemoji)
/// And additonally all the constant names (as listed in [`crate::flat`]) are also valid aliases
/// when spelled in lowercase.
///
/// # Examples
///
/// ```
/// use emojic::parse_alias;
///
/// // gemoji style
/// assert_eq!(
///     Some(&*emojic::flat::THUMBS_UP),
///     parse_alias(":+1:") //👍
/// );
///
/// // constant name style
/// assert_eq!(
///     Some(&emojic::flat::ALIEN_MONSTER),
///     parse_alias(":alien_monster:") //👾
/// );
/// ```
///
pub fn parse_alias(inp: &str) -> Option<&'static Emoji> {
    // make some basic checks
    if inp.starts_with(':') && inp.ends_with(':') && inp.is_ascii() && inp.len() > 2 {
        // go on with the middle part
        parse_pure_alias(&inp[1..(inp.len() - 1)])
    } else {
        None
    }
}

/// Parses a pice of string into an emoji (no colons)
fn parse_pure_alias(inp: &str) -> Option<&'static Emoji> {
    cfg_if::cfg_if! {
        if #[cfg(feature = "alloc")] {
            // If we have alloc, we use the faster hash map
            crate::alias::GEMOJI_MAP.get(inp).cloned()
        } else {
            // As a fallback, we can also use a huge match statement
            crate::matching::matching(inp)
        }
    }
}

/// Replaces all gemojis (`:[a-z0-9_+-]+:`) found in `text` with their Unicode equivalent.
///
/// This function is a convenience function for [`EmojiTextParser`]:
/// ```rust
/// # use emojic::text::{EmojiTextParser,parse_text};
/// # let text = ":some: :cat:";
/// # assert_eq!(
/// EmojiTextParser::new(text).to_string()
/// # , parse_text(text)
/// # );
/// ```
///
/// Notice, this convenience function requires `alloc` unlike the
/// [`EmojiTextParser`] iterator.
///
/// # Example
///
/// ```rust
/// use emojic::text::parse_text;
/// assert_eq!(
///     &parse_text("Hello :waving_hand:, I am a :technologist:."),
///     "Hello 👋, I am a 🧑‍💻.",
/// );
/// ```
///
/// ```rust
/// use emojic::text::parse_text;
/// assert_eq!(
///     &parse_text("Neither std::iter::Iterator nor :rustaceans: are emojis"),
///     "Neither std::iter::Iterator nor :rustaceans: are emojis",
/// );
/// ```
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "alloc")))]
pub fn parse_text(text: &str) -> String {
    EmojiTextParser::new(text).collect()
}

/// Finds and replaces gemojis (`:[a-z0-9_+-]+:`) in text.
///
/// This is the iterator behind [`parse_text`].
///
/// `EmojiTextParser` simply splits its input text into multiple fragments,
/// those of plain text, and those which are replacement emoji sequences.
///
/// Notice, that since this is simple iterator, it dose not depend on `alloc`,
/// unlike the convenience function [`parse_text`].
///
/// # Example
///
/// ```rust
/// use emojic::text::EmojiTextParser;
///
/// let input = "Hello :waving_hand:, I am a :technologist:.";
/// let mut parser = EmojiTextParser::new(input);
///
/// assert_eq!(Some("Hello "), parser.next());
/// assert_eq!(Some("👋"), parser.next());
/// assert_eq!(Some(", I am a "), parser.next());
/// assert_eq!(Some("🧑‍💻"), parser.next());
/// assert_eq!(Some("."), parser.next());
/// assert_eq!(None, parser.next());
/// ```
///
#[derive(Debug, Clone)]
pub struct EmojiTextParser<'a> {
    /// The original string that is parsed, used to fetch the output strings of
    /// the plain text sequences.
    original: &'a str,
    /// The next index into `original` to be processed, might be `original.len()`
    next_pos: usize,
    /// Indicates whether the next call to `next` has to process an emoji.
    emoji_fragment_start: bool,
}
impl<'a> EmojiTextParser<'a> {
    /// Creates a new parser for the given `original` text.
    pub fn new(original: &'a str) -> Self {
        Self {
            original,
            next_pos: 0,
            /// The very beginning is never a emoji
            emoji_fragment_start: false,
        }
    }

    fn is_valid_emoji_char(c: char) -> bool {
        c.is_ascii_alphanumeric() || c == '_' || c == '+' || c == '-'
    }

    fn text_until_next_colon(&mut self, start_idx: usize, skip: usize) -> &'a str {
        if let Some(colon_idx) = self.original[(start_idx + skip)..].find(':') {
            let true_colon_idx = start_idx + skip + colon_idx;
            // Found a colon, so let's continue next time behind it
            self.emoji_fragment_start = true;
            self.next_pos = true_colon_idx + 1;

            &self.original[start_idx..true_colon_idx]
        } else {
            // There are no further fragment
            self.emoji_fragment_start = false;
            self.next_pos = self.original.len(); // the end

            &self.original[start_idx..]
        }
    }
}
impl<'a> Iterator for EmojiTextParser<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        if self.emoji_fragment_start {
            // We must be preceded by a colon, so `next_pos` must not be zero
            debug_assert!(self.next_pos > 0);

            let start_idx = self.next_pos - 1;

            let chars = self.original[self.next_pos..].char_indices();

            // Validate all chars for an emoji alias
            for (i, c) in chars {
                if c == ':' {
                    let current_pos = self.next_pos + i;
                    // This is the second colon

                    let emoji_name = &self.original[start_idx..=current_pos];

                    if let Some(e) = crate::parse_alias(emoji_name) {
                        self.emoji_fragment_start = false;
                        self.next_pos = current_pos + 1;
                        return Some(e.grapheme);
                    } else {
                        // Here a user might have misspelled a emoji name.
                        // However, the conservative thing to do is to ignore it
                        // => meaning we output it as normal text
                        self.emoji_fragment_start = true;
                        self.next_pos = current_pos + 1;
                        return Some(&self.original[start_idx..current_pos]);
                    }
                } else if Self::is_valid_emoji_char(c) {
                    // A valid emoji char, lets continue
                } else {
                    // An invalid char, this makes this part just normal text,
                    // so lets output everything until the next colon
                    return Some(self.text_until_next_colon(start_idx, 1));
                }
            }

            // Here we hit the end of the text, but we have not found our ending
            // colon, so this is just text

            // There are no further fragment
            self.emoji_fragment_start = false;
            self.next_pos = self.original.len(); // the end

            Some(&self.original[start_idx..])
        } else if self.next_pos < self.original.len() {
            // we basically look for the next colon
            Some(self.text_until_next_colon(self.next_pos, 0))
        } else {
            // No more text left
            None
        }
    }
}
impl<'a> fmt::Display for EmojiTextParser<'a> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        let copy = self.clone();
        for frag in copy {
            write!(fmt, "{}", frag)?
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    // Tests are going to be on development systems => there will be std.
    extern crate std;
    use std::prelude::v1::*;

    use super::*;

    #[test]
    fn parser_test() {
        let input = "Hello :waving_hand:, I am a :technologist:.";
        let mut parser = EmojiTextParser::new(input);

        assert_eq!(Some("Hello "), parser.next());
        assert_eq!(Some("👋"), parser.next());
        assert_eq!(Some(", I am a "), parser.next());
        assert_eq!(Some("🧑‍💻"), parser.next());
        assert_eq!(Some("."), parser.next());
        assert_eq!(None, parser.next());
    }

    #[test]
    fn parser_misspelled() {
        let input = "Hello :wavinghand:, I am a :tchnologist:."; // sic
        let mut parser = EmojiTextParser::new(input);

        assert_eq!(Some("Hello "), parser.next());
        assert_eq!(Some(":wavinghand"), parser.next());
        assert_eq!(Some(":, I am a "), parser.next());
        assert_eq!(Some(":tchnologist"), parser.next());
        assert_eq!(Some(":."), parser.next());
        assert_eq!(None, parser.next());
    }

    #[test]
    fn parser_thumbs() {
        let input = ":thumbs_up::+1::-1::thumbs_down:";
        let output = "👍👍👎👎";

        let parser = EmojiTextParser::new(input);

        assert_eq!(output, &parser.collect::<String>());
    }

    #[test]
    fn parser_nothing() {
        let input = "";
        let mut parser = EmojiTextParser::new(input);

        assert_eq!(None, parser.next());
    }

    #[test]
    fn parser_corner_cases() {
        let input = "100: :100:100:100: :100";
        let output = "100: 💯100💯 :100";

        let parser = EmojiTextParser::new(input);

        assert_eq!(output, &parser.collect::<String>());
    }

    #[test]
    fn parser_no_emoji() {
        let input = "Hello :: I am: a technologist, :=: :).";
        let parser = EmojiTextParser::new(input);

        assert_eq!(input, &parser.collect::<String>());
    }

    #[test]
    fn parser_no_colons() {
        let input = "Hello, I am a technologist.";
        let parser = EmojiTextParser::new(input);

        assert_eq!(input, &parser.collect::<String>());
    }

    #[test]
    fn parser_single_colon() {
        let input = ":";
        let parser = EmojiTextParser::new(input);

        assert_eq!(input, &parser.collect::<String>());
    }

    #[test]
    fn parser_only_colons() {
        let input = ":::";
        let parser = EmojiTextParser::new(input);

        assert_eq!(input, &parser.collect::<String>());
    }

    #[test]
    fn parser_double_colons() {
        let input = "abc::technologist::def";
        let output = "abc:🧑‍💻:def";

        let parser = EmojiTextParser::new(input);

        assert_eq!(output, &parser.collect::<String>());
    }

    #[test]
    fn parser_many_colons() {
        let input = "abc:::technologist:::def";
        let output = "abc::🧑‍💻::def";

        let parser = EmojiTextParser::new(input);

        assert_eq!(output, &parser.collect::<String>());
    }

    #[test]
    fn parse_alias_test() {
        assert_eq!(
            Some(&crate::flat::FLAG_ECUADOR),
            parse_alias(":flag_ecuador:")
        );
    }

    #[test]
    fn parse_alias_none() {
        assert_eq!(None, parse_alias(":hebele:"));
    }
}