Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuji MATSUMOTO committed Aug 28, 2023
1 parent 0b00141 commit b71a2d9
Showing 1 changed file with 19 additions and 32 deletions.
51 changes: 19 additions & 32 deletions Sources/Emoji/String+Emoji.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,56 @@ extension String {

fileprivate static var emojiUnescapeRegExp = createEmojiUnescapeRegExp()
fileprivate static var emojiEscapeRegExp = createEmojiEscapeRegExp()
fileprivate static var indexedShortnames = indexShortnames()
fileprivate static var indexedCodepoints = indexCodepoints()

fileprivate static var indexedShortnames = createIndex(for: \.shortnames)
fileprivate static var indexedCodepoints = createIndex(for: \.codepoints)
fileprivate static func createEmojiUnescapeRegExp() -> NSRegularExpression? {
let v = Emoji.allCases.flatMap { $0.shortnames }
let pattern = Emoji.allCases
.flatMap { $0.shortnames }
.map { ":\(NSRegularExpression.escapedPattern(for: $0)):" }
.joined(separator: "|")
do {
let regex = try NSRegularExpression(pattern: v.joined(separator: "|"), options: [])
return regex
return try NSRegularExpression(pattern: pattern, options: [])
} catch {
print(error)
}
return nil
}

fileprivate static func createEmojiEscapeRegExp() -> NSRegularExpression? {
let v = Emoji.allCases.flatMap { $0.codepoints }
let pattern = Emoji.allCases
.flatMap { $0.codepoints }
.map { NSRegularExpression.escapedPattern(for: $0) }
.sorted()
.reversed()
.joined(separator: "|")
do {
let regex = try NSRegularExpression(pattern: v.joined(separator: "|"), options: [])
return regex
return try NSRegularExpression(pattern: pattern, options: [])
} catch {
print(error)
}
return nil
}

fileprivate static func indexShortnames() -> [String: Int] {
let emojis = Emoji.allCases
return emojis.reduce([String: Int](), { dict, emoji -> [String: Int] in
guard let index = emojis.firstIndex(of: emoji) else { return [:] }
return emoji.shortnames.reduce(dict, { eDict, shortname -> [String: Int] in

fileprivate static func createIndex(for propertyFunction: (Emoji) -> [String]) -> [String: Int] {
Emoji.allCases.reduce([String: Int](), { dict, emoji -> [String: Int] in
guard let index = Emoji.allCases.firstIndex(of: emoji) else { return [:] }
return propertyFunction(emoji).reduce(dict, { eDict, shortname -> [String: Int] in
var finalDict = eDict
finalDict[shortname] = index
return finalDict
})
})
}

fileprivate static func indexCodepoints() -> [String: Int] {
let emojis = Emoji.allCases
return emojis.reduce([String: Int](), { dict, emoji -> [String: Int] in
guard let index = emojis.firstIndex(of: emoji) else { return [:] }
return emoji.codepoints.reduce(dict, { eDict, codepoint -> [String: Int] in
var finalDict = eDict
finalDict[codepoint] = index
return finalDict
})
})
}


public var emojiUnescapedString: String {
var s = self as NSString
let ms = String.emojiUnescapeRegExp?.matches(in: self, options: [], range: NSMakeRange(0, s.length))
ms?.reversed().forEach { m in
let r = m.range
let p = s.substring(with: r)
let px = p[p.index(after: p.startIndex) ..< p.index(before: p.endIndex)]
let index = String.indexedShortnames[String(px)]
if let i = index {
if let i = String.indexedShortnames[String(px)] {
let e = Emoji.allCases[i]
s = s.replacingCharacters(in: r, with: e.codepoints.first!) as NSString
}
Expand All @@ -88,8 +76,7 @@ extension String {
ms?.reversed().forEach { m in
let r = m.range
let p = s.substring(with: r)
let index = String.indexedCodepoints[p]
if let i = index {
if let i = String.indexedCodepoints[p] {
let e = Emoji.allCases[i]
s = s.replacingCharacters(in: r, with: ":\(e.shortnames.first!):") as NSString
}
Expand Down

0 comments on commit b71a2d9

Please sign in to comment.