all repos — honk @ cf9b3f76bfaed7e0ccf32fa573526dda19f2db9f

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 bigboldshitz = "๐€๐๐‚๐ƒ๐„๐…๐†๐‡๐ˆ๐‰๐Š๐‹๐Œ๐๐Ž๐๐๐‘๐’๐“๐”๐•๐–๐—๐˜๐™"
 25var lilboldshitz = "๐š๐›๐œ๐๐ž๐Ÿ๐ ๐ก๐ข๐ฃ๐ค๐ฅ๐ฆ๐ง๐จ๐ฉ๐ช๐ซ๐ฌ๐ญ๐ฎ๐ฏ๐ฐ๐ฑ๐ฒ๐ณ"
 26var biggothshitz = "๐•ฌ๐•ญ๐•ฎ๐•ฏ๐•ฐ๐•ฑ๐•ฒ๐•ณ๐•ด๐•ต๐•ถ๐•ท๐•ธ๐•น๐•บ๐•ป๐•ผ๐•ฝ๐•พ๐•ฟ๐–€๐–๐–‚๐–ƒ๐–„๐–…"
 27var lilgothshitz = "๐–†๐–‡๐–ˆ๐–‰๐–Š๐–‹๐–Œ๐–๐–Ž๐–๐–๐–‘๐–’๐–“๐–”๐–•๐––๐–—๐–˜๐–™๐–š๐–›๐–œ๐–๐–ž๐–Ÿ"
 28var bigitalshitz = "๐‘จ๐‘ฉ๐‘ช๐‘ซ๐‘ฌ๐‘ญ๐‘ฎ๐‘ฏ๐‘ฐ๐‘ฑ๐‘ฒ๐‘ณ๐‘ด๐‘ต๐‘ถ๐‘ท๐‘ธ๐‘น๐‘บ๐‘ป๐‘ผ๐‘ฝ๐‘พ๐‘ฟ๐’€๐’"
 29var lilitalshitz = "๐’‚๐’ƒ๐’„๐’…๐’†๐’‡๐’ˆ๐’‰๐’Š๐’‹๐’Œ๐’๐’Ž๐’๐’๐’‘๐’’๐’“๐’”๐’•๐’–๐’—๐’˜๐’™๐’š๐’›"
 30var bigbangshitz = "๐”ธ๐”นโ„‚๐”ป๐”ผ๐”ฝ๐”พโ„๐•€๐•๐•‚๐•ƒ๐•„โ„•๐•†โ„™โ„šโ„๐•Š๐•‹๐•Œ๐•๐•Ž๐•๐•โ„ค"
 31var lilbangshitz = "๐•’๐•“๐•”๐••๐•–๐•—๐•˜๐•™๐•š๐•›๐•œ๐•๐•ž๐•Ÿ๐• ๐•ก๐•ข๐•ฃ๐•ค๐•ฅ๐•ฆ๐•ง๐•จ๐•ฉ๐•ช๐•ซ"
 32var bigblokshitz = "๐Ÿ…ฐ๐Ÿ…ฑ๐Ÿ…ฒ๐Ÿ…ณ๐Ÿ…ด๐Ÿ…ต๐Ÿ…ถ๐Ÿ…ท๐Ÿ…ธ๐Ÿ…น๐Ÿ…บ๐Ÿ…ป๐Ÿ…ผ๐Ÿ…ฝ๐Ÿ…พ๐Ÿ…ฟ๐Ÿ†€๐Ÿ†๐Ÿ†‚๐Ÿ†ƒ๐Ÿ†„๐Ÿ†…๐Ÿ††๐Ÿ†‡๐Ÿ†ˆ๐Ÿ†‰"
 33
 34var re_alltheshitz = regexp.MustCompile(`([` +
 35	bigboldshitz + lilboldshitz +
 36	biggothshitz + lilgothshitz +
 37	bigitalshitz + lilitalshitz +
 38	bigbangshitz + lilbangshitz +
 39	bigblokshitz +
 40	"][ \ufe0f]?){3,}")
 41
 42// this may not be especially fast
 43func unpucker(s string) string {
 44	fixer := func(r string) string {
 45		x := make([]byte, len(r))
 46		xi := 0
 47	loop1:
 48		for _, c := range r {
 49			xi++
 50			if c == ' ' {
 51				x[xi] = ' '
 52				continue
 53			}
 54			for _, set := range []string{bigboldshitz, biggothshitz, bigitalshitz, bigbangshitz, bigblokshitz} {
 55				i := 0
 56				for _, rr := range set {
 57					if rr == c {
 58						x[xi] = byte('A' + i)
 59						continue loop1
 60					}
 61					i++
 62				}
 63			}
 64			for _, set := range []string{lilboldshitz, lilgothshitz, lilitalshitz, lilbangshitz} {
 65				i := 0
 66				for _, rr := range set {
 67					if rr == c {
 68						x[xi] = byte('a' + i)
 69						continue loop1
 70					}
 71					i++
 72				}
 73			}
 74			x[xi] = '.'
 75		}
 76		return string(x)
 77	}
 78	s = re_alltheshitz.ReplaceAllStringFunc(s, fixer)
 79
 80	zw := false
 81	for _, c := range s {
 82		if runewidth.RuneWidth(c) == 0 {
 83			zw = true
 84			break
 85		}
 86	}
 87	if zw {
 88		x := make([]byte, 0, len(s))
 89		zw = false
 90		for _, c := range s {
 91			if runewidth.RuneWidth(c) == 0 {
 92				if zw {
 93					continue
 94				}
 95				zw = true
 96			} else {
 97				zw = false
 98			}
 99			q := string(c)
100			x = append(x, []byte(q)...)
101		}
102		return string(x)
103	}
104	return s
105}