Skip to content

Commit

Permalink
Adding utf8.RuneCountInString in place of len
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentFarris committed Jun 25, 2024
1 parent 8c08b3b commit 94f8db8
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/engine/entity_data.rt.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"reflect"
"runtime"
"strings"
"unicode/utf8"
)

type EntityData interface {
Expand All @@ -64,7 +65,8 @@ func RegisterEntityData(value EntityData) error {
if start == -1 {
return errors.New("failed to find the source package")
}
pkg = "*" + pkgPrefix + pkg[start+len(lookFor):]

pkg = "*" + pkgPrefix + pkg[start+utf8.RuneCountInString(lookFor):]
typ := reflect.TypeOf(value).Elem()
pkg += "." + typ.Name()
gob.RegisterName(pkg, value)
Expand Down
7 changes: 4 additions & 3 deletions src/generators/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"runtime"
"slices"
"strings"
"unicode/utf8"
)

//go:embed api_index.md
Expand Down Expand Up @@ -158,13 +159,13 @@ func writeMarkdown(md io.StringWriter, dir, text string) {
if len(text) == 0 {
return
}
e := len(text)
e := utf8.RuneCountInString(text)
if !strings.HasPrefix(text, "package") {
return
}
end := strings.Index(text, "\n")
if end < 0 {
end = len(text)
end = utf8.RuneCountInString(text)
}
writePackage(md, dir, text[:end])
positions := make(map[string]startEnd)
Expand All @@ -189,7 +190,7 @@ func writeMarkdown(md io.StringWriter, dir, text string) {
}
}
for k, v := range positions {
v.start += len(k) + 2
v.start += utf8.RuneCountInString(k) + 2
positions[k] = v
}
if p, ok := positions["CONSTANTS"]; ok {
Expand Down
3 changes: 2 additions & 1 deletion src/generators/copy_header/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"path/filepath"
"runtime"
"strings"
"unicode/utf8"
)

const containsCheck = "Copyright (c) 2015-present Brent Farris."
Expand Down Expand Up @@ -117,7 +118,7 @@ func main() {
}
if !strings.Contains(src, containsCheck) {
nameInsert := filepath.Base(path)
nameInsert = nameInsert + strings.Repeat(" ", 80-6-len(nameInsert))
nameInsert = nameInsert + strings.Repeat(" ", 80-6-utf8.RuneCountInString(nameInsert))
namedHeader := strings.Replace(header, "[NAME]", nameInsert, 1)
newSrc := namedHeader + "\n\n" + src
if err = filesystem.WriteTextFile(path, newSrc); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion src/rendering/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"strings"
"sync"
"unicode"
"unicode/utf8"
"unsafe"
)

Expand Down Expand Up @@ -645,7 +646,7 @@ func (cache FontCache) MeasureCharacter(face string, r rune, pixelSize float32)

func (cache *FontCache) PointOffsetWithin(face FontFace, text string, point matrix.Vec2, scale, maxWidth float32) int {
cache.requireFace(face)
textLen := len(text)
textLen := utf8.RuneCountInString(text)
idx := textLen
rects := cache.StringRectsWithinNew(face, text, scale, maxWidth)
for i := 0; i < textLen; i++ {
Expand Down
5 changes: 3 additions & 2 deletions src/ui/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"kaiju/systems/events"
"math"
"unicode"
"unicode/utf8"
)

type InputType = int32
Expand Down Expand Up @@ -337,7 +338,7 @@ func (input *Input) InsertText(text string) {
rhs := data.label.text[data.cursorOffset:]
str := lhs + text + rhs
input.setText(str)
data.cursorOffset += len(text)
data.cursorOffset += utf8.RuneCountInString(text)
input.makeCursorVisible()
input.updateCursorPosition()
}
Expand Down Expand Up @@ -460,7 +461,7 @@ func (input *Input) SetText(text string) {
if input.Text() != text {
input.moveCursor(0)
input.setText(text)
input.moveCursor(len(text))
input.moveCursor(utf8.RuneCountInString(text))
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/ui/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"kaiju/engine"
"kaiju/matrix"
"kaiju/rendering"
"unicode/utf8"
)

const (
Expand Down Expand Up @@ -80,7 +81,7 @@ type Label struct {
func NewLabel(host *engine.Host, text string, anchor Anchor) *Label {
label := &Label{
text: text,
textLength: len(text),
textLength: utf8.RuneCountInString(text),
fgColor: matrix.ColorWhite(),
bgColor: matrix.ColorBlack(),
fontSize: LabelFontSize,
Expand Down Expand Up @@ -241,7 +242,7 @@ func (label *Label) SetText(text string) {
label.text = text
label.renderRequired = true
// TODO: Put a cap on the length of the string
label.textLength = len(label.text)
label.textLength = utf8.RuneCountInString(label.text)
label.SetDirty(DirtyTypeGenerated)
label.colorRanges = label.colorRanges[:0]
}
Expand Down
1 change: 1 addition & 0 deletions src/windowing/shared_mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (e *evtMem) SetFatal(message string) {
e[0] = sharedMemFatal
msg := []byte(message)
for i := 0; i < len(msg) && i < len(e)-evtSharedMemDataStart; i++ {
// TODO: This could cut off right in the middle of a rune (utf8 char)
e[i+evtSharedMemDataStart] = msg[i]
}
}
Expand Down

0 comments on commit 94f8db8

Please sign in to comment.