-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Supports streaming with reader #60
Conversation
Signed-off-by: rnotorni <kojiro.honkawa@geniee.co.jp>
@rnotorni Thanks for the PR. I'm sorry, but I can't merge this PR because I have a policy of not using non-standard packages. Instead, I am thinking of exporting emojize so that emojiTransfer can be created as a separate library from kyokomi/emoji, but what do you think? +func Emojize(x string) string {
-func emojize(x string) string {
str, ok := emojiCode()[x]
if ok {
return str + ReplacePadding
}
if match := flagRegexp.FindStringSubmatch(x); len(match) == 2 {
return regionalIndicator(match[1][0]) + regionalIndicator(match[1][1])
}
return x
} (I'm not very good at English, so sorry if I didn't communicate well) |
@kyokomi That's a good policy! I think it would be nice to exporting the emojize, although it is different from the purpose of this PR. Wrapping a Reader and Writer for emoji is possible with standard packages only. However, if the feature deviates from the role of this repository, it should not be implemented. After all, I don't know if it's okay to support streaming without defining the role of this repository. (Don't worry I'm not good at English.) |
For example, if you import This code is your idea, so if you want to publish it on github, I would prefer that you publish it on your github. package emojitransfer
import (
"bytes"
"unicode"
"github.com/kyokomi/emoji/v2"
"golang.org/x/text/transform"
)
type emojiTransfer struct {
atEOF bool
emojiBuf *bytes.Buffer
buf []byte
transform.NopResetter
}
// NewEmojiTransfer return transform.Transfer implementd.
func NewEmojiTransfer() transform.Transformer {
return &emojiTransfer{}
}
func (e *emojiTransfer) Transform(dst, src []byte, atEOF bool) (int, int, error) {
e.atEOF = atEOF
if e.buf == nil {
e.buf = e.encode(bytes.NewBuffer(src)).Bytes()
}
n := copy(dst, e.buf)
if len(e.buf) <= n {
e.buf = nil
return n, len(src), nil
}
e.buf = e.buf[n:]
return n, 0, transform.ErrShortDst
}
func (e *emojiTransfer) replaceEmoji(input *bytes.Buffer) string {
emojiStr := bytes.NewBufferString(":")
for {
i, _, err := input.ReadRune()
if err != nil {
return e.replaceNotFindEnd(emojiStr)
}
if i == ':' && emojiStr.Len() == 1 {
return emojiStr.String() + e.replaceEmoji(input)
}
emojiStr.WriteRune(i)
switch {
case unicode.IsSpace(i):
return emojiStr.String()
case i == ':':
return emoji.Emojize(emojiStr.String())
}
}
}
func (e *emojiTransfer) replaceNotFindEnd(emojiBuf *bytes.Buffer) string {
if e.atEOF {
return emojiBuf.String()
}
e.emojiBuf = emojiBuf
return ""
}
func (e *emojiTransfer) mergeBuf(input *bytes.Buffer) *bytes.Buffer {
if e.emojiBuf == nil {
return input
}
return bytes.NewBuffer(append(e.emojiBuf.Bytes(), input.Bytes()...))
}
func (e *emojiTransfer) encode(input *bytes.Buffer) *bytes.Buffer {
target := e.mergeBuf(input)
e.emojiBuf = nil
output := &bytes.Buffer{}
output.Grow(input.Len())
for {
i, _, err := target.ReadRune()
if err != nil {
break
}
switch i {
default:
output.WriteRune(i)
case ':':
output.WriteString(e.replaceEmoji(target))
}
}
return output
} |
That's right. Thank you for your response! |
#59
Supports Streaming with Transfer.