Skip to content

Commit

Permalink
Replace some int types with uint
Browse files Browse the repository at this point in the history
  • Loading branch information
NickyMateev committed Nov 4, 2018
1 parent de00236 commit ee804be
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Homework2/solution.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

type Editor interface {
Insert(position int, text string) Editor
Insert(position uint, text string) Editor
Delete(offset, length uint) Editor
Undo() Editor
Redo() Editor
Expand All @@ -10,8 +10,8 @@ type Editor interface {

type piece struct {
origin bool
offset int
length int
offset uint
length uint
}

type PieceTable struct {
Expand All @@ -36,34 +36,34 @@ func NewEditor(text string) Editor {
editor.table = append(editor.table, piece{
origin: true,
offset: 0,
length: len(text),
length: uint(len(text)),
})

return &editor
}

func (editor *DefaultEditor) length() int {
length := 0
func (editor *DefaultEditor) length() uint {
var length uint
for _, v := range editor.table {
length += v.length
}
return length
}

func (editor *DefaultEditor) Insert(position int, text string) Editor {
func (editor *DefaultEditor) Insert(position uint, text string) Editor {
editor.addBuffer += text
newPiece := piece{
origin: false,
offset: len(editor.addBuffer) - len(text),
length: len(text),
offset: uint(len(editor.addBuffer) - len(text)),
length: uint(len(text)),
}

if position == 0 {
editor.table = append([]piece{newPiece}, editor.table...)
} else if position >= editor.length() {
editor.table = append(editor.table, newPiece)
} else {
curLength := 0
var curLength uint
for i, elem := range editor.table {
if (curLength + elem.length) < position {
curLength += elem.length
Expand Down

0 comments on commit ee804be

Please sign in to comment.