Skip to content

Instantly share code, notes, and snippets.

@takkkun
Created January 7, 2010 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takkkun/271074 to your computer and use it in GitHub Desktop.
Save takkkun/271074 to your computer and use it in GitHub Desktop.
-module(twitter_status).
-author("KONDO Takahiro <heartery@gmail.com>").
-export([hashtags/1]).
-define(ALPHANUMERIC(C), ($a =< C andalso C =< $z orelse
$A =< C andalso C =< $Z orelse
$0 =< C andalso C =< $9)).
-define(HASHTAG_PREFIX(C), (C =:= $# orelse C =:= 65283)).
-define(HASHTAG_CHARS(C), (?ALPHANUMERIC(C) orelse C =:= $_)).
hashtags(Text) when is_binary(Text) ->
hashtags(unicode:characters_to_list(Text));
hashtags(Text) ->
lists:reverse(hashtags(Text, -1, "", [])).
hashtags([C|Text], -1, [], Hashtags) when ?HASHTAG_PREFIX(C) ->
hashtags(Text, 0, [C], Hashtags);
hashtags([C|Text], -1, [LC|_] = Read, Hashtags) when ?HASHTAG_PREFIX(C), not ?ALPHANUMERIC(LC) ->
hashtags(Text, 0, [C|Read], Hashtags);
hashtags([C|Text], Size, Read, Hashtags) when ?HASHTAG_CHARS(C), Size > -1 ->
hashtags(Text, Size + 1, [C|Read], Hashtags);
hashtags([C|Text], Size, Read, Hashtags) when Size > 0 ->
hashtags(Text, -1, [C|Read], [hashtag(Read, Size)|Hashtags]);
hashtags([C|Text], Size, Read, Hashtags) ->
hashtags(Text, Size, [C|Read], Hashtags);
hashtags([], Size, Read, Hashtags) when Size > 0 ->
[hashtag(Read, Size)|Hashtags];
hashtags([], _, _, Hashtags) ->
Hashtags.
hashtag(Read, Size) -> hashtag(Read, Size, []).
hashtag(_, 0, Acc) -> Acc;
hashtag([C|Read], Size, Acc) -> hashtag(Read, Size - 1, [C|Acc]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment