all repos — honk @ 186cd6ede048c09733991514eef34bf13b536465

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 c == '\n' {
33			continue
34		}
35		if runewidth.RuneWidth(c) == 0 {
36			zw = true
37			break
38		}
39	}
40	if zw {
41		x := make([]byte, 0, len(s))
42		zw = false
43		for _, c := range s {
44			if runewidth.RuneWidth(c) == 0 {
45				if zw {
46					continue
47				}
48				zw = true
49			} else {
50				zw = false
51			}
52			q := string(c)
53			x = append(x, []byte(q)...)
54		}
55		return string(x)
56	}
57	return s
58}