blob: 3051f6676a92fbd0773deb4b77c9024b590fca61 [file] [log] [blame]
Colin Cross8e0c5112015-01-23 14:15:10 -08001// Copyright 2014 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross41c397a2015-01-12 17:43:04 -080015package parser
16
Colin Cross957b39c2018-03-21 18:10:01 -070017import (
18 "fmt"
19 "io"
20 "math"
21 "sort"
22)
Colin Cross41c397a2015-01-12 17:43:04 -080023
Colin Crosse32cc802016-06-07 12:28:16 -070024func AddStringToList(list *List, s string) (modified bool) {
25 for _, v := range list.Values {
26 if v.Type() != StringType {
27 panic(fmt.Errorf("expected string in list, got %s", v.Type()))
Colin Cross41c397a2015-01-12 17:43:04 -080028 }
29
Colin Crosse32cc802016-06-07 12:28:16 -070030 if sv, ok := v.(*String); ok && sv.Value == s {
Colin Cross41c397a2015-01-12 17:43:04 -080031 // string already exists
32 return false
33 }
Colin Cross41c397a2015-01-12 17:43:04 -080034 }
35
Colin Crosse32cc802016-06-07 12:28:16 -070036 list.Values = append(list.Values, &String{
37 LiteralPos: list.RBracePos,
38 Value: s,
Colin Cross41c397a2015-01-12 17:43:04 -080039 })
40
41 return true
42}
43
Colin Crosse32cc802016-06-07 12:28:16 -070044func RemoveStringFromList(list *List, s string) (modified bool) {
45 for i, v := range list.Values {
46 if v.Type() != StringType {
47 panic(fmt.Errorf("expected string in list, got %s", v.Type()))
Colin Cross41c397a2015-01-12 17:43:04 -080048 }
49
Colin Crosse32cc802016-06-07 12:28:16 -070050 if sv, ok := v.(*String); ok && sv.Value == s {
51 list.Values = append(list.Values[:i], list.Values[i+1:]...)
Colin Cross41c397a2015-01-12 17:43:04 -080052 return true
53 }
Colin Cross41c397a2015-01-12 17:43:04 -080054 }
55
56 return false
57}
Colin Cross957b39c2018-03-21 18:10:01 -070058
59// A Patch represents a region of a text buffer to be replaced [Start, End) and its Replacement
60type Patch struct {
61 Start, End int
62 Replacement string
63}
64
65// A PatchList is a list of sorted, non-overlapping Patch objects
66type PatchList []Patch
67
68type PatchOverlapError error
69
70// Add adds a Patch to a PatchList. It returns a PatchOverlapError if the patch cannot be added.
71func (list *PatchList) Add(start, end int, replacement string) error {
72 patch := Patch{start, end, replacement}
73 if patch.Start > patch.End {
74 return fmt.Errorf("invalid patch, start %d is after end %d", patch.Start, patch.End)
75 }
76 for _, p := range *list {
77 if (patch.Start >= p.Start && patch.Start < p.End) ||
78 (patch.End >= p.Start && patch.End < p.End) ||
79 (p.Start >= patch.Start && p.Start < patch.End) ||
80 (p.Start == patch.Start && p.End == patch.End) {
81 return PatchOverlapError(fmt.Errorf("new patch %d-%d overlaps with existing patch %d-%d",
82 patch.Start, patch.End, p.Start, p.End))
83 }
84 }
85 *list = append(*list, patch)
86 list.sort()
87 return nil
88}
89
90func (list *PatchList) sort() {
91 sort.SliceStable(*list,
92 func(i, j int) bool {
93 return (*list)[i].Start < (*list)[j].Start
94 })
95}
96
97// Apply applies all the Patch objects in PatchList to the data from an input ReaderAt to an output Writer.
98func (list *PatchList) Apply(in io.ReaderAt, out io.Writer) error {
99 var offset int64
100 for _, patch := range *list {
101 toWrite := int64(patch.Start) - offset
102 written, err := io.Copy(out, io.NewSectionReader(in, offset, toWrite))
103 if err != nil {
104 return err
105 }
106 offset += toWrite
107 if written != toWrite {
108 return fmt.Errorf("unexpected EOF at %d", offset)
109 }
110
111 _, err = io.WriteString(out, patch.Replacement)
112 if err != nil {
113 return err
114 }
115
116 offset += int64(patch.End - patch.Start)
117 }
118 _, err := io.Copy(out, io.NewSectionReader(in, offset, math.MaxInt64-offset))
119 return err
120}