Skip to content

Commit

Permalink
finish empty implementation of Text.Substring
Browse files Browse the repository at this point in the history
  • Loading branch information
chocolatkey committed Dec 9, 2024
1 parent 9817353 commit a841119
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
8 changes: 4 additions & 4 deletions pkg/fetcher/resource_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ func (r *BytesResource) Read(start int64, end int64) ([]byte, *ResourceError) {

// Bounds check
length := int64(len(r._bytes))
if start > (length - 1) {
start = length - 1
if start > length {
start = length
}
if end > length {
end = length
if end > (length - 1) {
end = length - 1
}

return r._bytes[start : end+1], nil
Expand Down
20 changes: 19 additions & 1 deletion pkg/manifest/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,29 @@ func TextFromJSON(rawJson map[string]interface{}) (t Text) {
return
}

func (t Text) Substring(start, end uint64) Text {
func (t Text) Substring(start, end int64) Text {
if t.Highlight == "" {
return t
}

length := int64(len(t.Highlight))
if start > length-1 {
start = length
}
if start < 0 {
start = 0
}
if end > length-1 {
end = length - 1
}
if end < 0 {
end = 0
}

t.Before += t.Highlight[:start]
t.After = t.Highlight[end+1:] + t.After
t.Highlight = t.Highlight[start : end+1]

return t
}

Expand Down
50 changes: 50 additions & 0 deletions pkg/manifest/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,53 @@ func TestTextJSON(t *testing.T) {
"after": "Text after"
}`, string(s), "JSON objects should be equal")
}

func TestSubstringFromRange(t *testing.T) {
text := Text{
Before: "before",
Highlight: "highlight",
After: "after",
}

assert.Equal(t, Text{
Before: "before",
Highlight: "h",
After: "ighlightafter",
}, text.Substring(0, -1))

assert.Equal(t, Text{
Before: "before",
Highlight: "h",
After: "ighlightafter",
}, text.Substring(0, 0))

assert.Equal(t, Text{
Before: "beforehigh",
Highlight: "lig",
After: "htafter",
}, text.Substring(4, 6))

assert.Equal(t, Text{
Before: "before",
Highlight: "highlight",
After: "after",
}, text.Substring(0, 8))

assert.Equal(t, Text{
Before: "beforehighli",
Highlight: "ght",
After: "after",
}, text.Substring(6, 12))

assert.Equal(t, Text{
Before: "beforehighligh",
Highlight: "t",
After: "after",
}, text.Substring(8, 12))

assert.Equal(t, Text{
Before: "beforehighlight",
Highlight: "",
After: "after",
}, text.Substring(9, 12))
}

0 comments on commit a841119

Please sign in to comment.