Skip to content

Commit

Permalink
multibyte character support
Browse files Browse the repository at this point in the history
  • Loading branch information
thevilledev committed May 17, 2023
1 parent 33566a9 commit 873c944
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
28 changes: 18 additions & 10 deletions thespine.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package thespine

import "strings"

const THE_SIZE = 3

func Decode(s string) string {
l := len(s)
g := make([]string, 0)
sr := []rune(s)
l := len(sr)
g := make([][]rune, 0)
gc := l / THE_SIZE
if l%THE_SIZE != 0 {
gc++
Expand All @@ -17,15 +16,16 @@ func Decode(s string) string {
if si < 0 {
si = 0
}
gs := s[si:ei]
gs := sr[si:ei]
g = append(g, gs)
}
return strings.Join(g, "")
return concat(g)
}

func Encode(s string) string {
l := len(s)
g := make([]string, 0)
sr := []rune(s)
l := len(sr)
g := make([][]rune, 0)
gc := l / THE_SIZE
if l%THE_SIZE != 0 {
gc++
Expand All @@ -36,11 +36,19 @@ func Encode(s string) string {
if ei > l {
ei = l
}
gs := s[si:ei]
gs := sr[si:ei]
g = append(g, gs)
}
for i, j := 0, len(g)-1; i < j; i, j = i+1, j-1 {
g[i], g[j] = g[j], g[i]
}
return strings.Join(g, "")
return concat(g)
}

func concat(r [][]rune) string {
var s string
for _, r := range r {
s += string(r)
}
return s
}
14 changes: 13 additions & 1 deletion thespine_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package thespine

import "testing"
import (
"testing"
)

func Test_EncodeDecode(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -28,6 +30,16 @@ func Test_EncodeDecode(t *testing.T) {
name: "the tech",
want: "kubernetes",
},
{
str: "ᚬᚩᚡᚣ",
name: "the runes",
want: "ᚩᚡᚣᚬ",
},
{
str: "\xf0\x9f\x9a\x80ketroc",
name: "the cringe",
want: "rocket\xf0\x9f\x9a\x80",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down

0 comments on commit 873c944

Please sign in to comment.