Решение на Piece table от Даяна Веселинова

Обратно към всички решения

Към профила на Даяна Веселинова

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 4 успешни тест(а)
  • 4 неуспешни тест(а)

Код

package main
import "fmt"
var origin []byte
var add []byte
var result []byte
type Editor interface {
// Insert text starting from given position.
Insert(position uint, text string) Editor
// Delete length items from offset.
Delete(offset, length uint) Editor
// Undo reverts latest change.
Undo() Editor
// Redo re-applies latest undone change.
Redo() Editor
// String returns complete representation of what a file looks
// like after all manipulations.
String() string
}
type EditorsOrigin struct {
origin bool
offset int
length int
}
func NewEditor(m string) EditorsOrigin {
origin = []byte(m)
result = []byte(m)
add = nil
return EditorsOrigin{origin: true, offset: 0, length: len(m)}
}
func (ed EditorsOrigin) Insert(position uint, text string) EditorsOrigin {
result = make([]byte, 0, len(origin)+len(text))
add = []byte(text)
if len(text) > cap(add) {
add = make([]byte, 0, cap(add)*2)
add = append(add, add[:]...)
}
if int(position) > cap(result) {
result = append(result, origin[:]...)
result = append(result, []byte(text)...)
} else {
result = append(result, origin[:position]...)
result = append(result, []byte(text)...)
result = append(result, origin[position:]...)
}
return EditorsOrigin{origin: false, offset: int(position), length: len(text)}
}
func (ed EditorsOrigin) Delete(offset, length uint) EditorsOrigin {
if int(offset) > cap(result) {
} else if int(length) > cap(result) {
result = result[:offset]
} else {
result = append(result[:offset], result[offset+length:]...)
}
return EditorsOrigin{origin: false, offset: len(origin), length: len(origin) - int(length)}
}
func (ed EditorsOrigin) Undo() EditorsOrigin {
return EditorsOrigin{origin: false, offset: len(origin), length: len(origin)}
}
func (ed EditorsOrigin) Redo() EditorsOrigin {
return EditorsOrigin{origin: false, offset: len(origin), length: len(origin)}
}
func (ed EditorsOrigin) String() string {
fmt.Println(string(result))
return fmt.Sprintf(string(result))
}

Лог от изпълнението

A large span of English text
A span of English text
A span of text
PASS
ok  	_/tmp/d20181107-53-418glk	0.002s
A span of text
A span of text!
A span of text!
A span of text!
A span of
PASS
ok  	_/tmp/d20181107-53-418glk	0.002s
A large span of text
A large span of text
A large span of English text
A span of English text
--- FAIL: TestUndo (0.00s)
    solution_test.go:75: Expect: "A large span of English text"; got "A span of English text"
FAIL
exit status 1
FAIL	_/tmp/d20181107-53-418glk	0.002s
A span of text
A span of text
--- FAIL: TestSeveralUndos (0.00s)
    solution_test.go:75: Expect: "A large span of text"; got "A span of text"
FAIL
exit status 1
FAIL	_/tmp/d20181107-53-418glk	0.002s
A span of English text
A span of English text
PASS
ok  	_/tmp/d20181107-53-418glk	0.002s
A span of text
A span of text
--- FAIL: TestSeveralRedos (0.00s)
    solution_test.go:75: Expect: "A large span of text"; got "A span of text"
FAIL
exit status 1
FAIL	_/tmp/d20181107-53-418glk	0.002s
large span of English text
large span of English text
PASS
ok  	_/tmp/d20181107-53-418glk	0.002s
Жълтата дюля беше щастлива, че пухът, който цъфна, и замръзна като гьон.
--- FAIL: TestUnicode (0.00s)
    solution_test.go:75: Expect: "Жълтата дюля беше щастлива, че пухът, който цъфна, замръзна като гьон."; got "Жълтата дюля беше щастлива, че пухът, който цъфна, и замръзна като гьон."
FAIL
exit status 1
FAIL	_/tmp/d20181107-53-418glk	0.002s

История (1 версия и 0 коментара)

Даяна обнови решението на 07.11.2018 12:39 (преди 9 месеца)

+package main
+
+import "fmt"
+
+var origin []byte
+var add []byte
+var result []byte
+
+type Editor interface {
+
+ // Insert text starting from given position.
+ Insert(position uint, text string) Editor
+
+ // Delete length items from offset.
+ Delete(offset, length uint) Editor
+
+ // Undo reverts latest change.
+ Undo() Editor
+
+ // Redo re-applies latest undone change.
+ Redo() Editor
+
+ // String returns complete representation of what a file looks
+ // like after all manipulations.
+ String() string
+}
+
+type EditorsOrigin struct {
+ origin bool
+ offset int
+ length int
+}
+
+func NewEditor(m string) EditorsOrigin {
+
+ origin = []byte(m)
+ result = []byte(m)
+ add = nil
+
+ return EditorsOrigin{origin: true, offset: 0, length: len(m)}
+}
+
+func (ed EditorsOrigin) Insert(position uint, text string) EditorsOrigin {
+
+ result = make([]byte, 0, len(origin)+len(text))
+ add = []byte(text)
+
+ if len(text) > cap(add) {
+ add = make([]byte, 0, cap(add)*2)
+ add = append(add, add[:]...)
+ }
+
+ if int(position) > cap(result) {
+ result = append(result, origin[:]...)
+ result = append(result, []byte(text)...)
+ } else {
+ result = append(result, origin[:position]...)
+ result = append(result, []byte(text)...)
+ result = append(result, origin[position:]...)
+ }
+
+ return EditorsOrigin{origin: false, offset: int(position), length: len(text)}
+
+}
+
+func (ed EditorsOrigin) Delete(offset, length uint) EditorsOrigin {
+
+ if int(offset) > cap(result) {
+
+ } else if int(length) > cap(result) {
+ result = result[:offset]
+ } else {
+ result = append(result[:offset], result[offset+length:]...)
+
+ }
+ return EditorsOrigin{origin: false, offset: len(origin), length: len(origin) - int(length)}
+
+}
+
+func (ed EditorsOrigin) Undo() EditorsOrigin {
+
+ return EditorsOrigin{origin: false, offset: len(origin), length: len(origin)}
+}
+
+func (ed EditorsOrigin) Redo() EditorsOrigin {
+
+ return EditorsOrigin{origin: false, offset: len(origin), length: len(origin)}
+}
+
+func (ed EditorsOrigin) String() string {
+ fmt.Println(string(result))
+ return fmt.Sprintf(string(result))
+
+}