Skip to content

Commit dde0628

Browse files
committed
Initial Commit
0 parents  commit dde0628

File tree

19 files changed

+2909
-0
lines changed

19 files changed

+2909
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
xcuserdata
3+
*.xccheckout
4+
*.xcuserstate
5+
Carthage
6+
docs
7+
builds

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Christian Niles
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Swift Emoji
2+
3+
`SwiftEmoji` provides a set of regular expressions to find emoji in swift strings. All forms of emoji are matched, including:
4+
5+
* Single-character emoji (👍)
6+
* Emoji that are variants of other characters (e.g. ⌚️ instead of ⌚︎)
7+
* Fitzpatrick Modifiers (e.g. skintones 👍🏻👍🏼👍🏽👍🏾👍🏿)
8+
* ZWJ Sequences (e.g. 💑)
9+
* Combining sequences (e.g. 0️⃣)
10+
* Flag sequences (e.g 🇨🇦)
11+
12+
All emoji are derived directly from the standard unicode data files, using an automated script.
13+
14+
## Usage
15+
16+
The `Emoji` class exposes a number of useful regular expressions as static variables. They each come
17+
in compiled (`NSRegularExpression`) and uncompiled (`String`) forms.
18+
19+
The `String` values are useful when composing your own expressions. For example, the default expressions do not create any capture groups, to avoid the performance overhead.
20+
21+
Look at the source code in `Emoji.swift` for an example.
22+
23+
## License
24+
25+
Squeal is released under the MIT License. Details are in the `LICENSE.txt` file in the project.

Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Dir.glob('tasks/*.rake').each { |r| load r }

Sources/Emoji.swift

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)