/usr/include/d/undead/string.d is in libundead-dev 1.0.9-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | /**
* Contains the obsolete pattern matching functions from Phobos'
* `std.string`.
*/
module undead.string;
import std.traits;
/***********************************************
* See if character c is in the pattern.
* Patterns:
*
* A $(I pattern) is an array of characters much like a $(I character
* class) in regular expressions. A sequence of characters
* can be given, such as "abcde". The '-' can represent a range
* of characters, as "a-e" represents the same pattern as "abcde".
* "a-fA-F0-9" represents all the hex characters.
* If the first character of a pattern is '^', then the pattern
* is negated, i.e. "^0-9" means any character except a digit.
* The functions inPattern, $(B countchars), $(B removeschars),
* and $(B squeeze) use patterns.
*
* Note: In the future, the pattern syntax may be improved
* to be more like regular expression character classes.
*/
bool inPattern(S)(dchar c, in S pattern) @safe pure @nogc
if (isSomeString!S)
{
bool result = false;
int range = 0;
dchar lastc;
foreach (size_t i, dchar p; pattern)
{
if (p == '^' && i == 0)
{
result = true;
if (i + 1 == pattern.length)
return (c == p); // or should this be an error?
}
else if (range)
{
range = 0;
if (lastc <= c && c <= p || c == p)
return !result;
}
else if (p == '-' && i > result && i + 1 < pattern.length)
{
range = 1;
continue;
}
else if (c == p)
return !result;
lastc = p;
}
return result;
}
@safe pure @nogc unittest
{
assertCTFEable!(
{
assert(inPattern('x', "x") == 1);
assert(inPattern('x', "y") == 0);
assert(inPattern('x', string.init) == 0);
assert(inPattern('x', "^y") == 1);
assert(inPattern('x', "yxxy") == 1);
assert(inPattern('x', "^yxxy") == 0);
assert(inPattern('x', "^abcd") == 1);
assert(inPattern('^', "^^") == 0);
assert(inPattern('^', "^") == 1);
assert(inPattern('^', "a^") == 1);
assert(inPattern('x', "a-z") == 1);
assert(inPattern('x', "A-Z") == 0);
assert(inPattern('x', "^a-z") == 0);
assert(inPattern('x', "^A-Z") == 1);
assert(inPattern('-', "a-") == 1);
assert(inPattern('-', "^A-") == 0);
assert(inPattern('a', "z-a") == 1);
assert(inPattern('z', "z-a") == 1);
assert(inPattern('x', "z-a") == 0);
});
}
/**
* See if character c is in the intersection of the patterns.
*/
bool inPattern(S)(dchar c, S[] patterns) @safe pure @nogc
if (isSomeString!S)
{
foreach (string pattern; patterns)
{
if (!inPattern(c, pattern))
{
return false;
}
}
return true;
}
/**
* Count characters in s that match pattern.
*/
size_t countchars(S, S1)(S s, in S1 pattern) @safe pure @nogc
if (isSomeString!S && isSomeString!S1)
{
size_t count;
foreach (dchar c; s)
{
count += inPattern(c, pattern);
}
return count;
}
@safe pure @nogc unittest
{
assertCTFEable!(
{
assert(countchars("abc", "a-c") == 3);
assert(countchars("hello world", "or") == 3);
});
}
/**
* Return string that is s with all characters removed that match pattern.
*/
S removechars(S)(S s, in S pattern) @safe pure
if (isSomeString!S)
{
import std.utf : encode;
Unqual!(typeof(s[0]))[] r;
bool changed = false;
foreach (size_t i, dchar c; s)
{
if (inPattern(c, pattern))
{
if (!changed)
{
changed = true;
r = s[0 .. i].dup;
}
continue;
}
if (changed)
{
encode(r, c);
}
}
if (changed)
return r;
else
return s;
}
@safe pure unittest
{
assertCTFEable!(
{
assert(removechars("abc", "a-c").length == 0);
assert(removechars("hello world", "or") == "hell wld");
assert(removechars("hello world", "d") == "hello worl");
assert(removechars("hah", "h") == "a");
});
}
@safe pure unittest
{
assert(removechars("abc", "x") == "abc");
}
/***************************************************
* Return string where sequences of a character in s[] from pattern[]
* are replaced with a single instance of that character.
* If pattern is null, it defaults to all characters.
*/
S squeeze(S)(S s, in S pattern = null)
{
import std.utf : encode, stride;
Unqual!(typeof(s[0]))[] r;
dchar lastc;
size_t lasti;
int run;
bool changed;
foreach (size_t i, dchar c; s)
{
if (run && lastc == c)
{
changed = true;
}
else if (pattern is null || inPattern(c, pattern))
{
run = 1;
if (changed)
{
if (r is null)
r = s[0 .. lasti].dup;
encode(r, c);
}
else
lasti = i + stride(s, i);
lastc = c;
}
else
{
run = 0;
if (changed)
{
if (r is null)
r = s[0 .. lasti].dup;
encode(r, c);
}
}
}
return changed ? ((r is null) ? s[0 .. lasti] : cast(S) r) : s;
}
@system pure unittest
{
assertCTFEable!(
{
string s;
assert(squeeze("hello") == "helo");
s = "abcd";
assert(squeeze(s) is s);
s = "xyzz";
assert(squeeze(s).ptr == s.ptr); // should just be a slice
assert(squeeze("hello goodbyee", "oe") == "hello godbye");
});
}
/***************************************************************
Finds the position $(D_PARAM pos) of the first character in $(D_PARAM
s) that does not match $(D_PARAM pattern) (in the terminology used by
$(REF inPattern, std,string)). Updates $(D_PARAM s =
s[pos..$]). Returns the slice from the beginning of the original
(before update) string up to, and excluding, $(D_PARAM pos).
The $(D_PARAM munch) function is mostly convenient for skipping
certain category of characters (e.g. whitespace) when parsing
strings. (In such cases, the return value is not used.)
*/
S1 munch(S1, S2)(ref S1 s, S2 pattern) @safe pure @nogc
{
size_t j = s.length;
foreach (i, dchar c; s)
{
if (!inPattern(c, pattern))
{
j = i;
break;
}
}
scope(exit) s = s[j .. $];
return s[0 .. j];
}
///
@safe pure @nogc unittest
{
string s = "123abc";
string t = munch(s, "0123456789");
assert(t == "123" && s == "abc");
t = munch(s, "0123456789");
assert(t == "" && s == "abc");
}
@safe pure @nogc unittest
{
string s = "123€abc";
string t = munch(s, "0123456789");
assert(t == "123" && s == "€abc");
t = munch(s, "0123456789");
assert(t == "" && s == "€abc");
t = munch(s, "£$€¥");
assert(t == "€" && s == "abc");
}
// helper function for unit tests
private @property void assertCTFEable(alias dg)()
{
static assert({ cast(void) dg(); return true; }());
cast(void) dg();
}
|