1 /*
2  * Copyright 2008 Google Inc. All Rights Reserved.
3  * Copyright 2013-2014 Jan Krüger. All Rights Reserved.
4  * Author: fraser@google.com (Neil Fraser)
5  * Author: anteru@developer.shelter13.net (Matthaeus G. Chajdas)
6  * Author: jan@jandoe.de (Jan Krüger)
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Diff Match and Patch
21  * http://code.google.com/p/google-diff-match-patch/
22  */
23 module ddmp.util;
24 
25 import std..string:indexOf;
26 
27 string substr(string str, size_t start, size_t len = size_t.max) {
28     auto end = len == size_t.max ? str.length : start + len;
29     if (start >= str.length) {
30         return "";
31     }
32     if (end > str.length) {
33         end = str.length;
34     }
35     return str[start..end];
36 }
37 
38 sizediff_t indexOfAlt(string str, string search, sizediff_t offset=0) {
39     auto index = str[offset..$].indexOf(search);
40     if (index > -1 ) return index + offset;
41     return -1;
42 }
43 
44 void insert(T)( ref T[] array, size_t i, T[] stuff)
45 {
46     array = array[0..i] ~ stuff ~ array[i..$];
47 }
48 void remove(T)( ref T[] array, size_t i, size_t count=1)
49 {
50     array = array[0..i] ~ array[i+count..$];
51 }
52 
53 T[] splice(T)(ref T[] list, sizediff_t start, sizediff_t count, T[] objects=null) {
54     T[] deletedRange = list[start..start+count];
55     list = list[0 .. start] ~ objects ~ list[start+count .. $];
56     return deletedRange;
57 }