|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// |
| 4 | +/// Useful things for working with Emoji. |
| 5 | +/// |
| 6 | +public final class Emoji : EmojiData { |
| 7 | + |
| 8 | + /// |
| 9 | + /// Pattern that matches a single Emoji character, including combining marks and sequences. |
| 10 | + /// |
| 11 | + public static var SingleEmojiPattern:String = { |
| 12 | + // The "emoji" group needs to be followed by a special character to be rendered like emoji. |
| 13 | + let emojiVariants = "(?:(?:\(EmojiPatterns.joinWithSeparator("|")))\\uFE0F)" |
| 14 | + |
| 15 | + // Emoji can be followed by optional combining marks. The standard says only keycaps and |
| 16 | + // backslash are likely to be supported. |
| 17 | + let combiningMarks = "[\\u20E3\\u20E0]" |
| 18 | + |
| 19 | + // "Presentation" characters are rendered as emoji by default and need no variant. |
| 20 | + let emojiPresentation = "\(EmojiPresentationPatterns.joinWithSeparator("|"))" |
| 21 | + |
| 22 | + // Some other emoji are sequences of characters, joined with 'Zero Width Joiner' characters. |
| 23 | + // We want the longest match, so we sort these in reverse order. |
| 24 | + let zwjSequences = ZWJSequencePatterns.reverse().joinWithSeparator("|") |
| 25 | + let otherSequences = SequencePatterns.joinWithSeparator("|") |
| 26 | + |
| 27 | + return "(?:(?:\(zwjSequences)|\(otherSequences)|\(emojiVariants)|\(emojiPresentation))\(combiningMarks)?)" |
| 28 | + }() |
| 29 | + |
| 30 | + /// A regular expression that matches any emoji character. Useful for plucking individual emoji |
| 31 | + /// out of a string. |
| 32 | + public static var SingleEmojiRegex:NSRegularExpression = { |
| 33 | + return try! NSRegularExpression(pattern:SingleEmojiPattern, options:[]) |
| 34 | + }() |
| 35 | + |
| 36 | + /// Pattern that matches one or more emoji characters in a row. |
| 37 | + public static var MultiEmojiPattern:String = { |
| 38 | + return "(?:\(SingleEmojiPattern)+)" |
| 39 | + }() |
| 40 | + |
| 41 | + /// Matches one or more emoji characters in a row. |
| 42 | + public static var MultiEmojiRegex:NSRegularExpression = { |
| 43 | + return try! NSRegularExpression(pattern:MultiEmojiPattern, options:[]) |
| 44 | + }() |
| 45 | + |
| 46 | + /// |
| 47 | + /// Pattern that matches one or more Emoji or whitespace characters in a row. At least one emoji |
| 48 | + /// character is required; empty or blank strings will not be matched. Leading and trailing |
| 49 | + /// whitespace will be included in the match range. |
| 50 | + /// |
| 51 | + public static var MultiEmojiAndWhitespacePattern:String = { |
| 52 | + return "(?:(?:\\s*\(MultiEmojiPattern))+\\s*)" |
| 53 | + }() |
| 54 | + |
| 55 | + /// |
| 56 | + /// Matches one or more Emoji or whitespace characters in a row. At least one emoji character is |
| 57 | + /// required; empty or blank strings will not be matched. Leading and trailing whitespace will |
| 58 | + /// be included in the match range. |
| 59 | + /// |
| 60 | + public static var MultiEmojiAndWhitespaceRegex:NSRegularExpression = { |
| 61 | + return try! NSRegularExpression(pattern:MultiEmojiAndWhitespacePattern, options:[]) |
| 62 | + }() |
| 63 | + |
| 64 | + /// |
| 65 | + /// Pattern that matches any string composed solely of emoji and (optional) whitespace. Empty or |
| 66 | + /// blank strings will not match. |
| 67 | + /// |
| 68 | + public static var PureEmojiAndWhitespacePattern:String = { |
| 69 | + return "^\(MultiEmojiAndWhitespacePattern)$" |
| 70 | + }() |
| 71 | + |
| 72 | + /// |
| 73 | + /// Matches any string composed solely of emoji and (optional) whitespace. Empty or blank |
| 74 | + /// strings will not match. |
| 75 | + /// |
| 76 | + public static var PureEmojiAndWhitespaceRegex:NSRegularExpression = { |
| 77 | + return try! NSRegularExpression(pattern:PureEmojiAndWhitespacePattern, options:[]) |
| 78 | + }() |
| 79 | + |
| 80 | + /// |
| 81 | + /// Returns `true` if the given string is composed solely of emoji and (optional) whitespace. |
| 82 | + /// Empty or blank strings will not match. |
| 83 | + /// |
| 84 | + public static func isPureEmojiString(string:String) -> Bool { |
| 85 | + let range = NSRange(location: 0, length: string.utf16.count) |
| 86 | + let firstMatch = PureEmojiAndWhitespaceRegex.rangeOfFirstMatchInString(string, options:[], range:range) |
| 87 | + return firstMatch.location != NSNotFound |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments