Решение на Piece table от Иван Иванов

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

Към профила на Иван Иванов

Резултати

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

Код

// Dragons below. Attempt to solve the homework in a small amount of time
package main
// EditorData is the struct containing all fragments and text buffers
type EditorData struct {
originText string
addBuffer string
length uint
fragments []Fragment
toUndo [][]Fragment
toRedo [][]Fragment
}
// Fragment points to part of the buffer
type Fragment struct {
origin bool
offset uint
length uint
}
// Editor interface
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
}
// Insert a new blob of text data in the editor.
func (e EditorData) Insert(position uint, text string) Editor {
if len(text) == 0 {
return e
}
e.toUndo = append(e.toUndo, clone(e.fragments))
var addOffset = uint(len(e.addBuffer))
e.addBuffer += text
e.length += uint(len(text))
index, offsetInFragment := findPosition(&e, position)
newFragment := Fragment{origin: false, offset: addOffset, length: uint(len(text))}
if position >= offsetInFragment+e.fragments[index].length {
e.fragments = append(e.fragments, newFragment)
} else {
oldFragment := e.fragments[index]
oldOffset := oldFragment.offset
inserts := []Fragment{
Fragment{origin: oldFragment.origin, offset: oldOffset, length: offsetInFragment},
newFragment,
Fragment{origin: oldFragment.origin, offset: offsetInFragment + oldOffset, length: oldFragment.length - offsetInFragment},
}
inserts = filter(inserts)
e.fragments = append(e.fragments[:index], append(inserts, e.fragments[index+1:]...)...)
}
return e
}
func findPosition(e *EditorData, position uint) (int, uint) {
for i, f := range e.fragments {
if position <= f.length {
return i, position
}
position -= f.length
}
return 0, 0
}
// Delete deletes, such wow
func (e EditorData) Delete(offset, length uint) Editor {
if offset >= e.length || length == 0 {
return e
}
e.toUndo = append(e.toUndo, clone(e.fragments))
// Delete could span multiple fragments
firstIndex, firstOffset := findPosition(&e, offset)
secondIndex, secondOffset := findPosition(&e, offset+length)
// But it also could not span multiple fragmemnts..
if firstIndex == secondIndex && firstOffset == e.fragments[firstIndex].offset {
e.fragments[firstIndex].length -= length
e.fragments[firstIndex].offset += length
return e
}
if firstIndex == secondIndex && e.fragments[firstIndex].offset+e.fragments[firstIndex].length == firstOffset {
e.fragments[firstIndex].length -= length
return e
}
first := e.fragments[firstIndex]
second := e.fragments[secondIndex]
inserts := []Fragment{
Fragment{origin: first.origin, offset: first.offset, length: firstOffset - first.offset},
Fragment{origin: second.origin, offset: secondOffset, length: second.length - secondOffset + second.offset},
}
inserts = filter(inserts)
e.fragments = append(e.fragments[:firstIndex], append(e.fragments[:secondIndex], inserts...)...)
return e
}
// Undo the last insert/delete/redo
func (e EditorData) Undo() Editor {
len := len(e.toUndo)
if len > 0 {
fragments := e.toUndo[len-1:][0]
e.toUndo = e.toUndo[:len-1]
e.toRedo = append(e.toRedo, e.fragments)
e.fragments = fragments
}
return e
}
// Redo the last undo
func (e EditorData) Redo() Editor {
len := len(e.toRedo)
if len > 0 {
fragments := e.toRedo[len-1:][0]
e.toRedo = e.toRedo[:len-1]
e.toUndo = append(e.toUndo, e.fragments)
e.fragments = fragments
}
return e
}
func (e EditorData) String() string {
var result string
for _, elem := range e.fragments {
if elem.origin {
result += e.originText[elem.offset : elem.offset+elem.length]
} else {
result += e.addBuffer[elem.offset : elem.offset+elem.length]
}
}
return result
}
// NewEditor creates a new editor
func NewEditor(content string) Editor {
fragments := make([]Fragment, 0, 10)
f := Fragment{
origin: true,
offset: 0,
length: uint(len(content)),
}
fragments = append(fragments, f)
e := EditorData{
originText: content,
fragments: fragments,
length: uint(len(content)),
toUndo: make([][]Fragment, 0),
toRedo: make([][]Fragment, 0),
}
return e
}
func filter(fragments []Fragment) []Fragment {
filtered := make([]Fragment, 0)
for _, f := range fragments {
if f.length > 0 {
filtered = append(filtered, f)
}
}
return filtered
}
func clone(fragments []Fragment) []Fragment {
newCopy := make([]Fragment, len(fragments))
copy(newCopy, fragments)
return newCopy
}

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

--- FAIL: TestExampleFromReadme (0.00s)
    solution_test.go:75: Expect: "A span of English text"; got "A span of "
    solution_test.go:75: Expect: "A span of text"; got "A A "
FAIL
exit status 1
FAIL	_/tmp/d20181107-53-1c68acs	0.002s
--- FAIL: TestOutOfBound (0.00s)
    solution_test.go:75: Expect: "A span of"; got "A span ofA span of text"
FAIL
exit status 1
FAIL	_/tmp/d20181107-53-1c68acs	0.002s
PASS
ok  	_/tmp/d20181107-53-1c68acs	0.002s
--- FAIL: TestSeveralUndos (0.00s)
    solution_test.go:75: Expect: "A span of text"; got "A A "
FAIL
exit status 1
FAIL	_/tmp/d20181107-53-1c68acs	0.002s
PASS
ok  	_/tmp/d20181107-53-1c68acs	0.002s
--- FAIL: TestSeveralRedos (0.00s)
    solution_test.go:75: Expect: "A span of text"; got "A A "
FAIL
exit status 1
FAIL	_/tmp/d20181107-53-1c68acs	0.002s
--- FAIL: TestOpAfterUndoInvalidatesRedo (0.00s)
    solution_test.go:75: Expect: "large span of text"; got "A large span of English text"
FAIL
exit status 1
FAIL	_/tmp/d20181107-53-1c68acs	0.003s
PASS
ok  	_/tmp/d20181107-53-1c68acs	0.002s

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

Иван обнови решението на 07.11.2018 00:53 (преди 9 месеца)

+// Dragons below. Attempt to solve the homework in a small amount of time
+package main
+
+// EditorData is the struct containing all fragments and text buffers
+type EditorData struct {
+ originText string
+ addBuffer string
+ length uint
+ fragments []Fragment
+}
+
+// Fragment points to part of the buffer
+type Fragment struct {
+ origin bool
+ offset uint
+ length uint
+}
+
+// Editor interface
+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
+}
+
+// Insert a new blob of text data in the editor.
+func (e EditorData) Insert(position uint, text string) Editor {
+ if len(text) == 0 {
+ return e
+ }
+
+ var currentAddLength = uint(len(e.addBuffer))
+ e.addBuffer += text
+ e.length += uint(len(text))
+ index, cumulativeOffset := findPosition(&e, position)
+
+ newFragment := Fragment{origin: false, offset: currentAddLength, length: uint(len(text))}
+
+ if position >= cumulativeOffset+e.fragments[index].length {
+ e.fragments = append(e.fragments, newFragment)
+ } else {
+ oldFragment := e.fragments[index]
+ oldOffset := oldFragment.offset
+ inserts := []Fragment{
+ Fragment{origin: oldFragment.origin, offset: oldOffset, length: cumulativeOffset - oldOffset},
+ newFragment,
+ Fragment{origin: oldFragment.origin, offset: cumulativeOffset, length: oldFragment.length - cumulativeOffset + oldOffset},
+ }
+
+ e.fragments = append(e.fragments[:index], append(e.fragments[index+1:], inserts...)...)
+
+ }
+ return e
+}
+
+func findPosition(e *EditorData, position uint) (int, uint) {
+ for i, e := range e.fragments {
+ if position <= e.length {
+ return i, e.offset + position
+ }
+
+ position -= e.length
+ }
+ return 0, 0
+}
+
+// Delete deletes, such wow
+func (e EditorData) Delete(offset, length uint) Editor {
+ if offset >= e.length || length == 0 {
+ return e
+ }
+
+ // Delete could span multiple fragments
+ firstIndex, firstOffset := findPosition(&e, offset)
+ secondIndex, secondOffset := findPosition(&e, offset+length)
+
+ // But it also could not span multiple fragmemnts..
+ if firstIndex == secondIndex && firstOffset == e.fragments[firstIndex].offset {
+ e.fragments[firstIndex].length -= length
+ e.fragments[firstIndex].offset += length
+ return e
+ }
+
+ if firstIndex == secondIndex && e.fragments[firstIndex].offset+e.fragments[firstIndex].length == firstOffset {
+ e.fragments[firstIndex].length -= length
+ return e
+ }
+
+ first := e.fragments[firstIndex]
+ second := e.fragments[secondIndex]
+ inserts := []Fragment{
+ Fragment{origin: first.origin, offset: first.offset, length: firstOffset - first.offset},
+ Fragment{origin: second.origin, offset: secondOffset, length: second.length - secondOffset + second.offset},
+ }
+ e.fragments = append(e.fragments[:firstIndex], append(e.fragments[:secondIndex], inserts...)...)
+ return e
+}
+
+func (f EditorData) Undo() Editor {
+ return f
+}
+
+func (f EditorData) Redo() Editor {
+ return f
+}
+
+func (e EditorData) String() string {
+ var result string
+ for _, elem := range e.fragments {
+ if elem.origin {
+ result += e.originText[elem.offset : elem.offset+elem.length]
+ } else {
+ result += e.addBuffer[elem.offset : elem.offset+elem.length]
+
+ }
+ }
+ return result
+}
+
+// NewEditor creates a new editor
+func NewEditor(content string) Editor {
+ fragments := make([]Fragment, 0, 10)
+ f := Fragment{
+ origin: true,
+ offset: 0,
+ length: uint(len(content)),
+ }
+ fragments = append(fragments, f)
+
+ return EditorData{originText: content, fragments: fragments, length: uint(len(content))}
+}

Иван обнови решението на 07.11.2018 01:05 (преди 9 месеца)

// Dragons below. Attempt to solve the homework in a small amount of time
package main
// EditorData is the struct containing all fragments and text buffers
type EditorData struct {
originText string
addBuffer string
length uint
fragments []Fragment
+ history []EditorData
}
// Fragment points to part of the buffer
type Fragment struct {
origin bool
offset uint
length uint
}
// Editor interface
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
}
// Insert a new blob of text data in the editor.
func (e EditorData) Insert(position uint, text string) Editor {
if len(text) == 0 {
return e
}
var currentAddLength = uint(len(e.addBuffer))
e.addBuffer += text
e.length += uint(len(text))
index, cumulativeOffset := findPosition(&e, position)
newFragment := Fragment{origin: false, offset: currentAddLength, length: uint(len(text))}
if position >= cumulativeOffset+e.fragments[index].length {
e.fragments = append(e.fragments, newFragment)
} else {
oldFragment := e.fragments[index]
oldOffset := oldFragment.offset
inserts := []Fragment{
Fragment{origin: oldFragment.origin, offset: oldOffset, length: cumulativeOffset - oldOffset},
newFragment,
Fragment{origin: oldFragment.origin, offset: cumulativeOffset, length: oldFragment.length - cumulativeOffset + oldOffset},
}
e.fragments = append(e.fragments[:index], append(e.fragments[index+1:], inserts...)...)
-
}
+
return e
}
func findPosition(e *EditorData, position uint) (int, uint) {
for i, e := range e.fragments {
if position <= e.length {
return i, e.offset + position
}
position -= e.length
}
return 0, 0
}
// Delete deletes, such wow
func (e EditorData) Delete(offset, length uint) Editor {
if offset >= e.length || length == 0 {
return e
}
// Delete could span multiple fragments
firstIndex, firstOffset := findPosition(&e, offset)
secondIndex, secondOffset := findPosition(&e, offset+length)
// But it also could not span multiple fragmemnts..
if firstIndex == secondIndex && firstOffset == e.fragments[firstIndex].offset {
e.fragments[firstIndex].length -= length
e.fragments[firstIndex].offset += length
return e
}
if firstIndex == secondIndex && e.fragments[firstIndex].offset+e.fragments[firstIndex].length == firstOffset {
e.fragments[firstIndex].length -= length
return e
}
first := e.fragments[firstIndex]
second := e.fragments[secondIndex]
inserts := []Fragment{
Fragment{origin: first.origin, offset: first.offset, length: firstOffset - first.offset},
Fragment{origin: second.origin, offset: secondOffset, length: second.length - secondOffset + second.offset},
}
e.fragments = append(e.fragments[:firstIndex], append(e.fragments[:secondIndex], inserts...)...)
return e
}
func (f EditorData) Undo() Editor {
return f
}
func (f EditorData) Redo() Editor {
return f
}
func (e EditorData) String() string {
var result string
for _, elem := range e.fragments {
if elem.origin {
result += e.originText[elem.offset : elem.offset+elem.length]
} else {
result += e.addBuffer[elem.offset : elem.offset+elem.length]
}
}
return result
}
// NewEditor creates a new editor
func NewEditor(content string) Editor {
fragments := make([]Fragment, 0, 10)
f := Fragment{
origin: true,
offset: 0,
length: uint(len(content)),
}
fragments = append(fragments, f)
- return EditorData{originText: content, fragments: fragments, length: uint(len(content))}
+ return EditorData{
+ originText: content,
+ fragments: fragments,
+ length: uint(len(content)),
+ history: make([]EditorData, 0, 10),
+ }
}

Иван обнови решението на 07.11.2018 11:04 (преди 9 месеца)

// Dragons below. Attempt to solve the homework in a small amount of time
package main
// EditorData is the struct containing all fragments and text buffers
type EditorData struct {
originText string
addBuffer string
length uint
fragments []Fragment
- history []EditorData
+ toUndo [][]Fragment
+ toRedo [][]Fragment
}
// Fragment points to part of the buffer
type Fragment struct {
origin bool
offset uint
length uint
}
// Editor interface
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
}
// Insert a new blob of text data in the editor.
func (e EditorData) Insert(position uint, text string) Editor {
if len(text) == 0 {
return e
}
- var currentAddLength = uint(len(e.addBuffer))
+ var addOffset = uint(len(e.addBuffer))
e.addBuffer += text
e.length += uint(len(text))
- index, cumulativeOffset := findPosition(&e, position)
- newFragment := Fragment{origin: false, offset: currentAddLength, length: uint(len(text))}
+ index, offsetInFragment := findPosition(&e, position)
+ e.toUndo = append(e.toUndo, clone(e.fragments))
- if position >= cumulativeOffset+e.fragments[index].length {
+ newFragment := Fragment{origin: false, offset: addOffset, length: uint(len(text))}
+ if position >= offsetInFragment+e.fragments[index].length {
e.fragments = append(e.fragments, newFragment)
} else {
oldFragment := e.fragments[index]
oldOffset := oldFragment.offset
inserts := []Fragment{
- Fragment{origin: oldFragment.origin, offset: oldOffset, length: cumulativeOffset - oldOffset},
+ Fragment{origin: oldFragment.origin, offset: oldOffset, length: offsetInFragment},
newFragment,
- Fragment{origin: oldFragment.origin, offset: cumulativeOffset, length: oldFragment.length - cumulativeOffset + oldOffset},
+ Fragment{origin: oldFragment.origin, offset: offsetInFragment + oldOffset, length: oldFragment.length - offsetInFragment},
}
+ inserts = filter(inserts)
- e.fragments = append(e.fragments[:index], append(e.fragments[index+1:], inserts...)...)
+ e.fragments = append(e.fragments[:index], append(inserts, e.fragments[index+1:]...)...)
}
return e
}
func findPosition(e *EditorData, position uint) (int, uint) {
- for i, e := range e.fragments {
- if position <= e.length {
- return i, e.offset + position
+ for i, f := range e.fragments {
+ if position <= f.length {
+ return i, position
}
- position -= e.length
+ position -= f.length
}
return 0, 0
}
// Delete deletes, such wow
func (e EditorData) Delete(offset, length uint) Editor {
if offset >= e.length || length == 0 {
return e
}
// Delete could span multiple fragments
firstIndex, firstOffset := findPosition(&e, offset)
secondIndex, secondOffset := findPosition(&e, offset+length)
// But it also could not span multiple fragmemnts..
if firstIndex == secondIndex && firstOffset == e.fragments[firstIndex].offset {
e.fragments[firstIndex].length -= length
e.fragments[firstIndex].offset += length
return e
}
if firstIndex == secondIndex && e.fragments[firstIndex].offset+e.fragments[firstIndex].length == firstOffset {
e.fragments[firstIndex].length -= length
return e
}
first := e.fragments[firstIndex]
second := e.fragments[secondIndex]
inserts := []Fragment{
Fragment{origin: first.origin, offset: first.offset, length: firstOffset - first.offset},
Fragment{origin: second.origin, offset: secondOffset, length: second.length - secondOffset + second.offset},
}
+ inserts = filter(inserts)
+
e.fragments = append(e.fragments[:firstIndex], append(e.fragments[:secondIndex], inserts...)...)
return e
}
-func (f EditorData) Undo() Editor {
- return f
+// Undo the last insert/delete/redo
+func (e EditorData) Undo() Editor {
+ len := len(e.toUndo)
+ if len > 0 {
+ fragments := e.toUndo[len-1:][0]
+ e.toUndo = e.toUndo[:len-1]
+ e.toRedo = append(e.toRedo, e.fragments)
+ e.fragments = fragments
+ }
+ return e
}
-func (f EditorData) Redo() Editor {
- return f
+// Redo the last undo
+func (e EditorData) Redo() Editor {
+ len := len(e.toRedo)
+ if len > 0 {
+ fragments := e.toRedo[len-1:][0]
+ e.toRedo = e.toRedo[:len-1]
+ e.toUndo = append(e.toUndo, e.fragments)
+ e.fragments = fragments
+ }
+ return e
}
func (e EditorData) String() string {
var result string
+
for _, elem := range e.fragments {
if elem.origin {
result += e.originText[elem.offset : elem.offset+elem.length]
} else {
result += e.addBuffer[elem.offset : elem.offset+elem.length]
}
}
return result
}
// NewEditor creates a new editor
func NewEditor(content string) Editor {
fragments := make([]Fragment, 0, 10)
f := Fragment{
origin: true,
offset: 0,
length: uint(len(content)),
}
fragments = append(fragments, f)
- return EditorData{
+ e := EditorData{
originText: content,
fragments: fragments,
length: uint(len(content)),
- history: make([]EditorData, 0, 10),
+ toUndo: make([][]Fragment, 0),
+ toRedo: make([][]Fragment, 0),
}
+
+ return e
+}
+
+func filter(fragments []Fragment) []Fragment {
+ filtered := make([]Fragment, 0)
+ for _, f := range fragments {
+ if f.length > 0 {
+ filtered = append(filtered, f)
+ }
+ }
+ return filtered
+}
+
+func clone(fragments []Fragment) []Fragment {
+ newCopy := make([]Fragment, len(fragments))
+
+ copy(newCopy, fragments)
+ return newCopy
}

Иван обнови решението на 07.11.2018 14:34 (преди 9 месеца)

// Dragons below. Attempt to solve the homework in a small amount of time
package main
// EditorData is the struct containing all fragments and text buffers
type EditorData struct {
originText string
addBuffer string
length uint
fragments []Fragment
toUndo [][]Fragment
toRedo [][]Fragment
}
// Fragment points to part of the buffer
type Fragment struct {
origin bool
offset uint
length uint
}
// Editor interface
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
}
// Insert a new blob of text data in the editor.
func (e EditorData) Insert(position uint, text string) Editor {
if len(text) == 0 {
return e
}
+ e.toUndo = append(e.toUndo, clone(e.fragments))
var addOffset = uint(len(e.addBuffer))
e.addBuffer += text
e.length += uint(len(text))
index, offsetInFragment := findPosition(&e, position)
- e.toUndo = append(e.toUndo, clone(e.fragments))
newFragment := Fragment{origin: false, offset: addOffset, length: uint(len(text))}
if position >= offsetInFragment+e.fragments[index].length {
e.fragments = append(e.fragments, newFragment)
} else {
oldFragment := e.fragments[index]
oldOffset := oldFragment.offset
inserts := []Fragment{
Fragment{origin: oldFragment.origin, offset: oldOffset, length: offsetInFragment},
newFragment,
Fragment{origin: oldFragment.origin, offset: offsetInFragment + oldOffset, length: oldFragment.length - offsetInFragment},
}
inserts = filter(inserts)
e.fragments = append(e.fragments[:index], append(inserts, e.fragments[index+1:]...)...)
}
return e
}
func findPosition(e *EditorData, position uint) (int, uint) {
for i, f := range e.fragments {
if position <= f.length {
return i, position
}
position -= f.length
}
return 0, 0
}
// Delete deletes, such wow
func (e EditorData) Delete(offset, length uint) Editor {
if offset >= e.length || length == 0 {
return e
}
+ e.toUndo = append(e.toUndo, clone(e.fragments))
// Delete could span multiple fragments
firstIndex, firstOffset := findPosition(&e, offset)
secondIndex, secondOffset := findPosition(&e, offset+length)
// But it also could not span multiple fragmemnts..
if firstIndex == secondIndex && firstOffset == e.fragments[firstIndex].offset {
e.fragments[firstIndex].length -= length
e.fragments[firstIndex].offset += length
return e
}
if firstIndex == secondIndex && e.fragments[firstIndex].offset+e.fragments[firstIndex].length == firstOffset {
e.fragments[firstIndex].length -= length
return e
}
first := e.fragments[firstIndex]
second := e.fragments[secondIndex]
inserts := []Fragment{
Fragment{origin: first.origin, offset: first.offset, length: firstOffset - first.offset},
Fragment{origin: second.origin, offset: secondOffset, length: second.length - secondOffset + second.offset},
}
inserts = filter(inserts)
e.fragments = append(e.fragments[:firstIndex], append(e.fragments[:secondIndex], inserts...)...)
return e
}
// Undo the last insert/delete/redo
func (e EditorData) Undo() Editor {
len := len(e.toUndo)
if len > 0 {
fragments := e.toUndo[len-1:][0]
e.toUndo = e.toUndo[:len-1]
e.toRedo = append(e.toRedo, e.fragments)
e.fragments = fragments
}
return e
}
// Redo the last undo
func (e EditorData) Redo() Editor {
len := len(e.toRedo)
if len > 0 {
fragments := e.toRedo[len-1:][0]
e.toRedo = e.toRedo[:len-1]
e.toUndo = append(e.toUndo, e.fragments)
e.fragments = fragments
}
return e
}
func (e EditorData) String() string {
var result string
-
for _, elem := range e.fragments {
if elem.origin {
result += e.originText[elem.offset : elem.offset+elem.length]
} else {
result += e.addBuffer[elem.offset : elem.offset+elem.length]
}
}
return result
}
// NewEditor creates a new editor
func NewEditor(content string) Editor {
fragments := make([]Fragment, 0, 10)
f := Fragment{
origin: true,
offset: 0,
length: uint(len(content)),
}
fragments = append(fragments, f)
e := EditorData{
originText: content,
fragments: fragments,
length: uint(len(content)),
toUndo: make([][]Fragment, 0),
toRedo: make([][]Fragment, 0),
}
return e
}
func filter(fragments []Fragment) []Fragment {
filtered := make([]Fragment, 0)
for _, f := range fragments {
if f.length > 0 {
filtered = append(filtered, f)
}
}
return filtered
}
func clone(fragments []Fragment) []Fragment {
newCopy := make([]Fragment, len(fragments))
copy(newCopy, fragments)
return newCopy
}