1// 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
15package parser
16
17import (
18	"fmt"
19	"io"
20	"math"
21	"sort"
22)
23
24func 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()))
28		}
29
30		if sv, ok := v.(*String); ok && sv.Value == s {
31			// string already exists
32			return false
33		}
34	}
35
36	list.Values = append(list.Values, &String{
37		LiteralPos: list.RBracePos,
38		Value:      s,
39	})
40
41	return true
42}
43
44func 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()))
48		}
49
50		if sv, ok := v.(*String); ok && sv.Value == s {
51			list.Values = append(list.Values[:i], list.Values[i+1:]...)
52			return true
53		}
54	}
55
56	return false
57}
58
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}
121