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
59func ReplaceStringsInList(list *List, replacements map[string]string) (replaced bool) {
60	modified := false
61	for i, v := range list.Values {
62		if v.Type() != StringType {
63			panic(fmt.Errorf("expected string in list, got %s", v.Type()))
64		}
65		if sv, ok := v.(*String); ok && replacements[sv.Value] != "" {
66			pos := list.Values[i].Pos()
67			list.Values[i] = &String{
68				LiteralPos: pos,
69				Value:      replacements[sv.Value],
70			}
71			modified = true
72		}
73	}
74	return modified
75}
76
77// A Patch represents a region of a text buffer to be replaced [Start, End) and its Replacement
78type Patch struct {
79	Start, End  int
80	Replacement string
81}
82
83// A PatchList is a list of sorted, non-overlapping Patch objects
84type PatchList []Patch
85
86type PatchOverlapError error
87
88// Add adds a Patch to a PatchList.  It returns a PatchOverlapError if the patch cannot be added.
89func (list *PatchList) Add(start, end int, replacement string) error {
90	patch := Patch{start, end, replacement}
91	if patch.Start > patch.End {
92		return fmt.Errorf("invalid patch, start %d is after end %d", patch.Start, patch.End)
93	}
94	for _, p := range *list {
95		if (patch.Start >= p.Start && patch.Start < p.End) ||
96			(patch.End >= p.Start && patch.End < p.End) ||
97			(p.Start >= patch.Start && p.Start < patch.End) ||
98			(p.Start == patch.Start && p.End == patch.End) {
99			return PatchOverlapError(fmt.Errorf("new patch %d-%d overlaps with existing patch %d-%d",
100				patch.Start, patch.End, p.Start, p.End))
101		}
102	}
103	*list = append(*list, patch)
104	list.sort()
105	return nil
106}
107
108func (list *PatchList) sort() {
109	sort.SliceStable(*list,
110		func(i, j int) bool {
111			return (*list)[i].Start < (*list)[j].Start
112		})
113}
114
115// Apply applies all the Patch objects in PatchList to the data from an input ReaderAt to an output Writer.
116func (list *PatchList) Apply(in io.ReaderAt, out io.Writer) error {
117	var offset int64
118	for _, patch := range *list {
119		toWrite := int64(patch.Start) - offset
120		written, err := io.Copy(out, io.NewSectionReader(in, offset, toWrite))
121		if err != nil {
122			return err
123		}
124		offset += toWrite
125		if written != toWrite {
126			return fmt.Errorf("unexpected EOF at %d", offset)
127		}
128
129		_, err = io.WriteString(out, patch.Replacement)
130		if err != nil {
131			return err
132		}
133
134		offset += int64(patch.End - patch.Start)
135	}
136	_, err := io.Copy(out, io.NewSectionReader(in, offset, math.MaxInt64-offset))
137	return err
138}
139