all repos — honk @ 7ed3b24f1a2c7c1d9fed8486ebe3cdb7bf3edb41

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
24var skinTones = "\U0001F3FB\U0001F3FC\U0001F3FD\U0001F3FE\U0001F3FF"
25var re_moredumb = regexp.MustCompile("[\U0001f44f\U0001f6a8\U000026a0][" + skinTones + "\ufe0f]*")
26
27func demoji(s string) string {
28	s = re_moredumb.ReplaceAllString(s, ".")
29
30	zw := false
31	for _, c := range s {
32		if runewidth.RuneWidth(c) == 0 {
33			zw = true
34			break
35		}
36	}
37	if zw {
38		x := make([]byte, 0, len(s))
39		zw = false
40		for _, c := range s {
41			if runewidth.RuneWidth(c) == 0 {
42				if zw {
43					continue
44				}
45				zw = true
46			} else {
47				zw = false
48			}
49			q := string(c)
50			x = append(x, []byte(q)...)
51		}
52		return string(x)
53	}
54	return s
55}