-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstring.d
More file actions
266 lines (216 loc) · 7.48 KB
/
string.d
File metadata and controls
266 lines (216 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
module util.string;
import std.typecons;
import test;
mixin registerUnittests;
// TODO: This could be optimized
auto uniquePostfixPath(R)(R names)
{
import std.algorithm;
import std.array;
import dccore.path;
struct SortHelper
{
int index;
string name;
string[] reversePathElements;
int pathElementsUsed;
}
int idx = 0;
static string[] myreverse(string[] arr)
{
arr.reverse();
return arr;
}
version (unittest)
{
version (linux)
{
import std.array;
auto ns = names.map!(a => a.replace("\\", "/"));
}
else
{
auto ns = names;
}
}
else
{
auto ns = names;
}
auto r = ns
.map!((a) => SortHelper(idx++, a, myreverse(a.pathSplitter.array))).array
.sort!"a.reversePathElements < b.reversePathElements"().array;
foreach (index, ref item; r)
{
import std.stdio;
item.pathElementsUsed = 1;
if (index != 0)
{
int lastPathElementsUsed = r[index-1].pathElementsUsed;
auto prefix = commonPrefix(r[index-1].reversePathElements, item.reversePathElements);
if (prefix.length != 0)
{
int newElementsUsed = cast(int)prefix.length + 1; // +1 to add the disambiguating elements
item.pathElementsUsed = min(newElementsUsed, item.reversePathElements.length);
r[index - 1].pathElementsUsed = min(newElementsUsed, r[index - 1].reversePathElements.length);
}
}
}
return r.sort!((a,b) => a.index < b.index).map!((a) => tuple(myreverse(a.reversePathElements[0..a.pathElementsUsed]).buildPath, a.name));
}
version (unittest)
private string[] toarr(R)(R e)
{
import std.algorithm;
import std.array;
version (Windows)
return e.map!"a[0]".array;
else
return e.map!(a => a[0].replace("/", "\\")).array;
}
unittest
{
auto a = [ "a1", "a2", "b" ];
a = [ "b", "a", "b"];
Assert(uniquePostfixPath(a).toarr, a, "One level uniquePostfixPath");
a = [ "b", "a", "foo\\a" ];
Assert(uniquePostfixPath(a).toarr, a, "One conflict uniquePostfixPath");
a = [ "b", "foo\\a", "bar\\a"];
Assert(uniquePostfixPath(a).toarr, a, "One two conflicts uniquePostfixPath");
a = [ "b", "a", "foo\\a", "bar\\a"];
Assert(uniquePostfixPath(a).toarr, a, "One two conflicts uniquePostfixPath");
a = [ "b", "foo\\a", "bar\\a", "a"];
Assert(uniquePostfixPath(a).toarr, a, "One two conflicts reverse uniquePostfixPath");
a = [ "b", "foo\\a", "bar\\goo\\a", "a"];
Assert(uniquePostfixPath(a).toarr, [ "b", "foo\\a", "goo\\a", "a"], "One three conflicts reverse uniquePostfixPath");
// With prefix
a = [ "prefix\\b", "prefix\\a", "prefix\\b"];
Assert(uniquePostfixPath(a).toarr, [ "prefix\\b", "a", "prefix\\b"], "One level uniquePostfixPath");
a = [ "prefix\\b", "prefix\\a", "prefix\\foo\\a" ];
Assert(uniquePostfixPath(a).toarr, [ "b", "prefix\\a", "foo\\a" ], "One conflict uniquePostfixPath");
a = [ "prefix\\b", "prefix\\a", "prefix\\foo\\a", "prefix\\bar\\a"];
Assert(uniquePostfixPath(a).toarr, [ "b", "prefix\\a", "foo\\a", "bar\\a"], "One two conflicts uniquePostfixPath");
a = [ "prefix\\b", "prefix\\foo\\a", "prefix\\bar\\a", "prefix\\a"];
Assert(uniquePostfixPath(a).toarr, [ "b", "foo\\a", "bar\\a", "prefix\\a"], "One two conflicts reverse uniquePostfixPath");
a = [ "prefix\\b", "prefix\\foo\\a", "prefix\\bar\\goo\\a", "prefix\\a"];
Assert(uniquePostfixPath(a).toarr, [ "b", "foo\\a", "goo\\a", "prefix\\a"], "One three conflicts reverse uniquePostfixPath");
//printStats(true);
}
// TODO: replace with proper dynamic programming equivalent.
/*!
* string_score.c: String Scoring Algorithm 0.1
* https://github.com/kurige/string_score
*
* Based on Javascript code by Joshaven Potter
* https://github.com/joshaven/string_score
*
* MIT license: http://www.opensource.org/licenses/mit-license.php
*
* Date: Tue Mar 21 2011
*/
private
{
enum ACRONYM_BONUS = 0.8;
enum CONSECUTIVE_BONUS = 0.6;
enum START_OF_STR_BONUS = 0.1;
enum SAMECASE_BONUS = 0.1;
enum MATCH_BONUS = 0.1;
enum AFTER_NONALNUM_BONUS = 0.1;
}
double rank( string a, string b, double fuzziness = 0.0 )
{
import std.ascii;
import std.range;
import std.string;
/* If the original string is equal to the abbreviation, perfect match. */
if( a == b )
return 1.0;
/* If perfectly bad match. */
if( b.empty )
return 0.0;
/* Create a ref of original string, so that we can manipulate it. */
string aref = a;
double score = 0.0;
bool start_of_string_bonus = false;
size_t c_index;
double fuzzies = 1.0;
/* Walk through abbreviation and add up scores. */
foreach (i, c; b)
{
/* Find the first case-insensitive match of a character. */
//printf( "- %c (%s)\n", c, aptr );
c_index = aref.indexOf(c, CaseSensitive.no);
/* Set base score for any matching char. */
if( c_index == -1 )
{
if( fuzziness > 0.0 )
{
fuzzies += 1.0 - fuzziness;
continue;
}
return 0.0;
}
else
{
score += MATCH_BONUS;
// If char is matching just right after a non alnum char we add bonus
if (i > 0 && !isAlphaNum(b[i-1]))
score += AFTER_NONALNUM_BONUS;
}
/* Same-case bonus. */
if( aref[c_index] == c )
{
//printf( "* Same case bonus.\n" );
score += SAMECASE_BONUS;
}
/* Consecutive letter & start-of-string bonus. */
if( c_index == 0 )
{
/* Increase the score when matching first character of the
remainder of the string. */
//printf( "* Consecutive char bonus.\n" );
score += CONSECUTIVE_BONUS;
if( i == 0 )
/* If match is the first char of the string & the first char
of abbreviation, add a start-of-string match bonus. */
start_of_string_bonus = true;
}
else if( aref[c_index - 1] == ' ' )
{
/* Acronym Bonus
* Weighing Logic: Typing the first character of an acronym is as
* if you preceded it with two perfect character matches. */
//printf( "* Acronym bonus.\n" );
score += ACRONYM_BONUS;
}
/* Left trim the already matched part of the string.
(Forces sequential matching.) */
aref = aref[c_index + 1..$];
}
score /= b.length;
/* Reduce penalty for longer strings. */
score = ((score * (b.length / cast(double)a.length)) + score) / 2;
score /= fuzzies;
if( start_of_string_bonus && (score + START_OF_STR_BONUS < 1) )
{
//printf( "* Start of string bonus.\n" );
score += START_OF_STR_BONUS;
}
return score;
}
unittest
{
import dccore.log;
void t(string a, string b, double expected = -1)
{
import std.stdio;
auto r = a.rank(b);
log.i(a, " <=> ", b, " is ", r);
if (expected == -1)
return;
}
t("ResourceProviderFactory", "ResourceProviderFactory");
t("ResourceProviderFactory", "res");
t("ResourceProviderFactory", "rpf");
t("ResourceProviderFactory", "RPF");
}