-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Unicode normalization filters
Not sure how to test
- Loading branch information
1 parent
71f2a28
commit 3d5393e
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package norm | ||
|
||
import ( | ||
"github.com/clipperhouse/jargon" | ||
"github.com/clipperhouse/jargon/filters/mapper" | ||
"golang.org/x/text/unicode/norm" | ||
) | ||
|
||
// NFC normalizes tokens into Unicode Normalization Form C | ||
var NFC = newFilter(norm.NFC) | ||
|
||
// NFD normalizes tokens into Unicode Normalization Form D | ||
var NFD = newFilter(norm.NFD) | ||
|
||
// NFKC normalizes tokens into Unicode Normalization Form KC | ||
var NFKC = newFilter(norm.NFKC) | ||
|
||
// NFKD normalizes tokens into Unicode Normalization Form KD | ||
var NFKD = newFilter(norm.NFKD) | ||
|
||
func newFilter(form norm.Form) jargon.Filter { | ||
f := func(token *jargon.Token) *jargon.Token { | ||
if form.IsNormalString(token.String()) { | ||
return token | ||
} | ||
|
||
s := form.String(token.String()) | ||
return jargon.NewToken(s, true) | ||
} | ||
|
||
return mapper.NewFilter(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters