-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
liner, word separators: add configurable word separators
- Loading branch information
1 parent
162544a
commit b84b4fd
Showing
5 changed files
with
114 additions
and
32 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
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
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,30 @@ | ||
package liner | ||
|
||
import "unicode" | ||
|
||
// SpaceWordSeparatorChecker (default) returns true if r is a unicode-space | ||
func SpaceWordSeparatorChecker(r rune) bool { | ||
return unicode.IsSpace(r) | ||
} | ||
|
||
// PunctWordSeparatorChecker returns true if r is a unicode punctuation character | ||
func PunctWordSeparatorChecker(r rune) bool { | ||
return unicode.IsPunct(r) | ||
} | ||
|
||
// CombineWordSeparatorChecker combines checkers | ||
// eg. line.SetWordSeparatorChecker(liner.CombineWordSeparatorChecker( | ||
// liner.SpaceWordSeparatorChecker, | ||
// liner.PunctWordSeparatorChecker, | ||
// )) | ||
func CombineWordSeparatorChecker(checkers ...WordSeparatorChecker) WordSeparatorChecker { | ||
return func(r rune) bool { | ||
for _, isSeparator := range checkers { | ||
if isSeparator(r) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} |
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,42 @@ | ||
package liner | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSeparators(t *testing.T) { | ||
// test spaces | ||
spaces := []rune{' ', ' '} | ||
for _, r := range spaces { | ||
if !SpaceWordSeparatorChecker(r) { | ||
t.Errorf("'%s' was not recognized by the space separator", string(r)) | ||
} | ||
} | ||
|
||
// test punctuation character | ||
puncts := []rune{'(', ')', '{', '}'} // etc | ||
for _, r := range puncts { | ||
if !PunctWordSeparatorChecker(r) { | ||
t.Errorf(`'%s' was not recognized by the "punctuation" separator`, string(r)) | ||
} | ||
} | ||
|
||
// test combination of them | ||
combinedChecker := CombineWordSeparatorChecker( | ||
SpaceWordSeparatorChecker, | ||
PunctWordSeparatorChecker, | ||
) | ||
for _, r := range append(puncts, spaces...) { | ||
if !combinedChecker(r) { | ||
t.Errorf("'%s' was not recognized by the combined separator", string(r)) | ||
} | ||
} | ||
|
||
// test some letters | ||
justLetters := []rune{'a', 'b', 'c'} | ||
for _, r := range justLetters { | ||
if combinedChecker(r) { | ||
t.Errorf("'%s' was recognized as a space or a punctuation char", string(r)) | ||
} | ||
} | ||
} |