all repos — honk @ 10fa56adf4d7462777ce55e16451a6b3bf954c75

my fork of honk

skulduggery.go (view raw)

 1//
 2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
 3//
 4// Permission to use, copy, modify, and distribute this software for any
 5// purpose with or without fee is hereby granted, provided that the above
 6// copyright notice and this permission notice appear in all copies.
 7//
 8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16package main
17
18import (
19	"regexp"
20
21	"github.com/mattn/go-runewidth"
22)
23
24
25var skinTones = "\U0001F3FB\U0001F3FC\U0001F3FD\U0001F3FE\U0001F3FF"
26var re_moredumb = regexp.MustCompile("[\U0001f44f\U0001f6a8\U000026a0][" + skinTones + "\ufe0f]*")
27
28func demoji(s string) string {
29	s = re_moredumb.ReplaceAllString(s, ".")
30
31	zw := false
32	for _, c := range s {
33		if runewidth.RuneWidth(c) == 0 {
34			zw = true
35			break
36		}
37	}
38	if zw {
39		x := make([]byte, 0, len(s))
40		zw = false
41		for _, c := range s {
42			if runewidth.RuneWidth(c) == 0 {
43				if zw {
44					continue
45				}
46				zw = true
47			} else {
48				zw = false
49			}
50			q := string(c)
51			x = append(x, []byte(q)...)
52		}
53		return string(x)
54	}
55	return s
56}