1 /**
2 Copyright: Copyright (c) 2017, Joakim Brännström. All rights reserved.
3 License: MPL-2
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 
6 This Source Code Form is subject to the terms of the Mozilla Public License,
7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
8 one at http://mozilla.org/MPL/2.0/.
9 
10 This file contains functionality to manipulate the VirtualFileSystem.
11 */
12 module dextool.plugin.mutate.backend.vfs;
13 
14 import cpptooling.utility.virtualfilesystem : VirtualFileSystem;
15 import dextool.type : AbsolutePath;
16 
17 public import dextool.plugin.mutate.backend.type : Offset;
18 
19 auto drop(T = string)(ref VirtualFileSystem vfs, const AbsolutePath fname, const Offset offset) {
20     import cpptooling.utility.virtualfilesystem;
21 
22     auto content = vfs.slice!T(cast(FileName) fname);
23 
24     return DropRange!T(content[0 .. offset.begin], content[offset.end .. $]);
25 }
26 
27 private:
28 
29 struct DropRange(T) {
30     private {
31         T[2] data;
32         size_t idx;
33     }
34 
35     this(T d0, T d1) {
36         data = [d0, d1];
37     }
38 
39     T front() @safe pure nothrow {
40         assert(!empty, "Can't get front of an empty range");
41         return data[idx];
42     }
43 
44     void popFront() @safe pure nothrow {
45         assert(!empty, "Can't pop front of an empty range");
46         ++idx;
47     }
48 
49     bool empty() @safe pure nothrow const @nogc {
50         return idx == data.length;
51     }
52 }