~emersion/soju-dev

Make casemapping work over bytes instead of runes v1 APPLIED

Hubert Hirtz: 1
 Make casemapping work over bytes instead of runes

 1 files changed, 25 insertions(+), 33 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/soju-dev/patches/22006/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] Make casemapping work over bytes instead of runes Export this patch

Fixes a panic in partialCasemap when the input string was invalid UTF-8.
---
 irc.go | 58 +++++++++++++++++++++++++---------------------------------
 1 file changed, 25 insertions(+), 33 deletions(-)

diff --git a/irc.go b/irc.go
index 5aeeb95..bfc9c99 100644
--- a/irc.go
+++ b/irc.go
@@ -429,57 +429,51 @@ func casemapNone(name string) string {
// CasemapASCII of name is the canonical representation of name according to the
// ascii casemapping.
func casemapASCII(name string) string {
	var sb strings.Builder
	sb.Grow(len(name))
	for _, r := range name {
	nameBytes := []byte(name)
	for i, r := range nameBytes {
		if 'A' <= r && r <= 'Z' {
			r += 'a' - 'A'
			nameBytes[i] = r + 'a' - 'A'
		}
		sb.WriteRune(r)
	}
	return sb.String()
	return string(nameBytes)
}

// casemapRFC1459 of name is the canonical representation of name according to the
// rfc1459 casemapping.
func casemapRFC1459(name string) string {
	var sb strings.Builder
	sb.Grow(len(name))
	for _, r := range name {
	nameBytes := []byte(name)
	for i, r := range nameBytes {
		if 'A' <= r && r <= 'Z' {
			r += 'a' - 'A'
			nameBytes[i] = r + 'a' - 'A'
		} else if r == '{' {
			r = '['
			nameBytes[i] = '['
		} else if r == '}' {
			r = ']'
			nameBytes[i] = ']'
		} else if r == '\\' {
			r = '|'
			nameBytes[i] = '|'
		} else if r == '~' {
			r = '^'
			nameBytes[i] = '^'
		}
		sb.WriteRune(r)
	}
	return sb.String()
	return string(nameBytes)
}

// casemapRFC1459Strict of name is the canonical representation of name
// according to the rfc1459-strict casemapping.
func casemapRFC1459Strict(name string) string {
	var sb strings.Builder
	sb.Grow(len(name))
	for _, r := range name {
	nameBytes := []byte(name)
	for i, r := range nameBytes {
		if 'A' <= r && r <= 'Z' {
			r += 'a' - 'A'
			nameBytes[i] = r + 'a' - 'A'
		} else if r == '{' {
			r = '['
			nameBytes[i] = '['
		} else if r == '}' {
			r = ']'
			nameBytes[i] = ']'
		} else if r == '\\' {
			r = '|'
			nameBytes[i] = '|'
		}
		sb.WriteRune(r)
	}
	return sb.String()
	return string(nameBytes)
}

func parseCasemappingToken(tokenValue string) (casemap casemapping, ok bool) {
@@ -497,16 +491,14 @@ func parseCasemappingToken(tokenValue string) (casemap casemapping, ok bool) {
}

func partialCasemap(higher casemapping, name string) string {
	nameFullyCM := higher(name)
	var sb strings.Builder
	sb.Grow(len(name))
	for i, r := range nameFullyCM {
		if 'a' <= r && r <= 'z' {
			r = rune(name[i])
	nameFullyCM := []byte(higher(name))
	nameBytes := []byte(name)
	for i, r := range nameBytes {
		if !('A' <= r && r <= 'Z') && !('a' <= r && r <= 'z') {
			nameBytes[i] = nameFullyCM[i]
		}
		sb.WriteRune(r)
	}
	return sb.String()
	return string(nameBytes)
}

type casemapMap struct {
-- 
2.31.1
Pushed, thanks for the fix!