Skip to content
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

Closed
wants to merge 2 commits into from
Closed

Supports streaming with reader #60

wants to merge 2 commits into from

Conversation

rnotorni
Copy link

#59

Supports Streaming with Transfer.

Signed-off-by: rnotorni <kojiro.honkawa@geniee.co.jp>
emoji.go Outdated Show resolved Hide resolved
@kyokomi
Copy link
Owner

kyokomi commented Feb 18, 2023

@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)

@rnotorni
Copy link
Author

rnotorni commented Feb 18, 2023

@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.)

@kyokomi
Copy link
Owner

kyokomi commented Feb 19, 2023

@rnotorni Exported here.

For example, if you import github.com/kyokomi/emoji as the emojitransfer package and create emojiTransfer, this is what you will get.

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
}

@rnotorni
Copy link
Author

rnotorni commented Feb 19, 2023

This code is your idea, so if you want to publish it on github, I would prefer that you publish it on your github.

That's right.

Thank you for your response!

@rnotorni rnotorni closed this Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants