This repository has been archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathzerowidth.go
62 lines (48 loc) · 1.81 KB
/
zerowidth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2018 Nikola Trubitsyn. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Package zerowidth implements functions to remove zero-width characters from strings.
package zerowidth
import "strings"
const (
// ZWSP represents zero-width space.
ZWSP = '\u200B'
// ZWNBSP represents zero-width no-break space.
ZWNBSP = '\uFEFF'
// ZWJ represents zero-width joiner.
ZWJ = '\u200D'
// ZWNJ represents zero-width non-joiner.
ZWNJ = '\u200C'
empty = ""
)
var replacer = strings.NewReplacer(string(ZWSP), empty,
string(ZWNBSP), empty,
string(ZWJ), empty,
string(ZWNJ), empty)
// HasZeroWidthCharacters reports whether string s contains zero-width characters.
func HasZeroWidthCharacters(s string) bool {
return strings.ContainsRune(s, ZWSP) ||
strings.ContainsRune(s, ZWNBSP) ||
strings.ContainsRune(s, ZWJ) ||
strings.ContainsRune(s, ZWNJ)
}
// RemoveZeroWidthCharacters removes all zero-width characters from string s.
func RemoveZeroWidthCharacters(s string) string {
return replacer.Replace(s)
}
// RemoveZeroWidthSpace removes zero-width space characters from string s.
func RemoveZeroWidthSpace(s string) string {
return strings.Replace(s, string(ZWSP), empty, -1)
}
// RemoveZeroWidthNoBreakSpace removes zero-width no-break space characters from string s.
func RemoveZeroWidthNoBreakSpace(s string) string {
return strings.Replace(s, string(ZWNBSP), empty, -1)
}
// RemoveZeroWidthJoiner removes zero-width joiner characters from string s.
func RemoveZeroWidthJoiner(s string) string {
return strings.Replace(s, string(ZWJ), empty, -1)
}
// RemoveZeroWidthNonJoiner removes zero-width non-joiner characters from string s.
func RemoveZeroWidthNonJoiner(s string) string {
return strings.Replace(s, string(ZWNJ), empty, -1)
}