From 48257b7cc105be24118ae24b7860f2dc7a33217f Mon Sep 17 00:00:00 2001 From: Tony Olagbaiye Date: Fri, 11 May 2018 22:41:15 +0100 Subject: [PATCH] Implement emoji exact search functions --- .gitattributes | 1 + slack-emoji.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ slack-emoji.h | 6 ++++ slack-emoji.inc | 4 +++ slack-emoji.pl | 8 ++++++ slack-emoji.py | 8 ++++++ 6 files changed, 102 insertions(+) diff --git a/.gitattributes b/.gitattributes index 51f39db..11bf075 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ # Github language display *.h linguist-language=C +*.inc linguist-language=C diff --git a/slack-emoji.c b/slack-emoji.c index 21983cf..1c7e6ba 100644 --- a/slack-emoji.c +++ b/slack-emoji.c @@ -10,3 +10,78 @@ #include "slack-emoji.inc" +static int emoji_byname_cmp(const void *p1, const void *p2) +{ + return strcasecmp(((struct t_slack_emoji_by_name *)p1)->name, + ((struct t_slack_emoji_by_name *)p2)->name); +} + +static int emoji_bytext_cmp(const void *p1, const void *p2) +{ + return strcasecmp(((struct t_slack_emoji_by_text *)p1)->text, + ((struct t_slack_emoji_by_text *)p2)->text); +} + +const char *slack_emoji_get_unicode_by_name(const char *name) +{ + struct t_slack_emoji_by_name *result; + struct t_slack_emoji_by_name key; + key.name = name; + + size_t emoji_count = sizeof(slack_emoji_by_name) + / sizeof(struct t_slack_emoji_by_name); + result = (struct t_slack_emoji_by_name *)bsearch( + &key, slack_emoji_by_name, emoji_count, + sizeof(struct t_slack_emoji_by_name), + emoji_byname_cmp); + + return result->unicode; +} + +const char *slack_emoji_get_unicode_by_text(const char *text) +{ + struct t_slack_emoji_by_text *result; + struct t_slack_emoji_by_text key; + key.text = text; + + size_t emoji_count = sizeof(slack_emoji_by_text) + / sizeof(struct t_slack_emoji_by_text); + result = (struct t_slack_emoji_by_text *)bsearch( + &key, slack_emoji_by_text, emoji_count, + sizeof(struct t_slack_emoji_by_text), + emoji_bytext_cmp); + + return result->unicode; +} + +const char *slack_emoji_get_text_by_name(const char *name) +{ + struct t_slack_emoji_by_name *result; + struct t_slack_emoji_by_name key; + key.name = name; + + size_t emoji_count = sizeof(slack_emoji_by_name) + / sizeof(struct t_slack_emoji_by_name); + result = (struct t_slack_emoji_by_name *)bsearch( + &key, slack_emoji_by_name, emoji_count, + sizeof(struct t_slack_emoji_by_name), + emoji_byname_cmp); + + return result->text_to; +} + +const char *slack_emoji_get_text_by_text(const char *text) +{ + struct t_slack_emoji_by_text *result; + struct t_slack_emoji_by_text key; + key.text = text; + + size_t emoji_count = sizeof(slack_emoji_by_text) + / sizeof(struct t_slack_emoji_by_text); + result = (struct t_slack_emoji_by_text *)bsearch( + &key, slack_emoji_by_text, emoji_count, + sizeof(struct t_slack_emoji_by_text), + emoji_bytext_cmp); + + return result->text_to; +} diff --git a/slack-emoji.h b/slack-emoji.h index 26ae2c4..aab3445 100644 --- a/slack-emoji.h +++ b/slack-emoji.h @@ -5,6 +5,12 @@ #ifndef _SLACK_EMOJI_H_ #define _SLACK_EMOJI_H_ +const char *slack_emoji_get_unicode_by_name(const char *name); +const char *slack_emoji_get_unicode_by_text(const char *text); + +const char *slack_emoji_get_text_by_name(const char *name); + +const char *slack_emoji_get_text_by_text(const char *text); #endif /*SLACK_EMOJI_H*/ diff --git a/slack-emoji.inc b/slack-emoji.inc index 743c407..de277af 100644 --- a/slack-emoji.inc +++ b/slack-emoji.inc @@ -1,4 +1,8 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, version 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + #include #define MAX_TEXTS 7 diff --git a/slack-emoji.pl b/slack-emoji.pl index 4d500fb..357f528 100644 --- a/slack-emoji.pl +++ b/slack-emoji.pl @@ -1,5 +1,9 @@ #!/usr/bin/perl -l +# This Source Code Form is subject to the terms of the Mozilla Public +# License, version 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + use strict; use warnings; @@ -17,6 +21,10 @@ my $maxtexts = max (map { 1 + @{$_} } (grep defined, map { $_->{'texts'} } @arra my $maxnames = max (map { 1 + @{$_} } (grep defined, map { $_->{'short_names'} } @array)); print " +// This Source Code Form is subject to the terms of the Mozilla Public +// License, version 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + #include #define MAX_TEXTS $maxtexts diff --git a/slack-emoji.py b/slack-emoji.py index 36e3eef..558ce9b 100755 --- a/slack-emoji.py +++ b/slack-emoji.py @@ -1,6 +1,10 @@ #!/usr/bin/python # Compatible with python v2 and v3 +# This Source Code Form is subject to the terms of the Mozilla Public +# License, version 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + import requests import json import ast @@ -8,6 +12,10 @@ import ast emoji = requests.get("https://raw.githubusercontent.com/iamcal/emoji-data/master/emoji.json").json() print(""" +// This Source Code Form is subject to the terms of the Mozilla Public +// License, version 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + #include #define MAX_TEXTS %d