egra: some X11 hacks
[iv.d.git] / strex.d
blobd4fe645ee4d020ec3d996f01d30cd58628f8d948
1 /* Invisible Vector Library
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // some string operations: quoting, `indexOf()` for non-utf8
18 module iv.strex /*is aliced*/;
21 /// quote string: append double quotes, screen all special chars;
22 /// so quoted string forms valid D string literal.
23 /// allocates.
24 string quote (const(char)[] s) {
25 import std.array : appender;
26 import std.format : formatElement, FormatSpec;
27 auto res = appender!string();
28 FormatSpec!char fspc; // defaults to 's'
29 formatElement(res, s, fspc);
30 return res.data;
34 /// convert integral number to number with commas
35 char[] intWithCommas(T) (char[] dest, T nn, char comma=',') if (__traits(isIntegral, T)) {
36 static if (__traits(isUnsigned, T)) {
37 enum neg = false;
38 //alias n = nn;
39 static if (T.sizeof < 8) {
40 uint n = nn;
41 } else {
42 ulong n = nn;
44 } else {
45 bool neg = (nn < 0);
46 static if (T.sizeof < 8) {
47 long n = nn;
48 if (neg) n = -n;
49 if (n < 0) n = T.max;
50 } else {
51 //alias n = nn;
52 long n = nn;
53 if (neg) n = -n;
54 if (n < 0) n = T.max; //FIXME
57 char[256] buf = void;
58 int bpos = cast(int)buf.length;
59 int leftToComma = 3;
60 do {
61 if (leftToComma-- == 0) { buf[--bpos] = comma; leftToComma = 2; }
62 buf[--bpos] = cast(char)('0'+n%10);
63 } while ((n /= 10) != 0);
64 if (neg) buf[--bpos] = '-';
65 auto len = buf.length-bpos;
66 if (dest is null) dest = new char[](len);
67 if (len > dest.length) len = dest.length;
68 dest[0..len] = buf[bpos..bpos+len];
69 return dest[0..len];
72 char[] intWithCommas(T) (T nn, char comma=',') if (__traits(isIntegral, T)) { return intWithCommas(null, nn, comma); }
75 //char tolower (char ch) pure nothrow @trusted @nogc { pragma(inline, true); return (ch >= 'A' && ch <= 'Z' ? cast(char)(ch-'A'+'a') : ch); }
76 //char toupper (char ch) pure nothrow @trusted @nogc { pragma(inline, true); return (ch >= 'a' && ch <= 'z' ? cast(char)(ch-'a'+'A') : ch); }
77 char tolower (char ch) pure nothrow @trusted @nogc { pragma(inline, true); return cast(char)(ch+((ch >= 'A' && ch <= 'Z')<<5)); }
78 char toupper (char ch) pure nothrow @trusted @nogc { pragma(inline, true); return cast(char)(ch-((ch >= 'a' && ch <= 'z')<<5)); }
80 bool islower (char ch) pure nothrow @trusted @nogc { pragma(inline, true); return (ch >= 'a' && ch <= 'z'); }
81 bool isupper (char ch) pure nothrow @trusted @nogc { pragma(inline, true); return (ch >= 'A' && ch <= 'Z'); }
83 bool isalpha (char ch) pure nothrow @trusted @nogc { pragma(inline, true); return ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')); }
84 bool isdigit (char ch) pure nothrow @trusted @nogc { pragma(inline, true); return (ch >= '0' && ch <= '9'); }
85 bool isalnum (char ch) pure nothrow @trusted @nogc { pragma(inline, true); return ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')); }
86 bool isxdigit (char ch) pure nothrow @trusted @nogc { pragma(inline, true); return ((ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f') || (ch >= '0' && ch <= '9')); }
88 /// case-insensitive char compare for ASCII
89 bool charEquCI (const char c0, const char c1) pure nothrow @trusted @nogc {
90 pragma(inline, true);
91 // (c0 |= 0x20) is lowercase-conversion for ASCII
92 // the good thing is that only uppercase letters will become lowercase letters,
93 // other things will become a garbage
94 // also, let's hope that any decent compiler is able to perform CSE here
95 return
96 c0 == c1 || // try the easiest case first
97 ((c0|0x20) >= 'a' && (c0|0x20) <= 'z' && // it wasn't a letter, no need to check the second char
98 (c0|0x20) == (c1|0x20)); // c1 will become a lowercase ascii only if it was uppercase/lowercase ascii
101 int digitInBase (const char ch, const int base=10) pure nothrow @trusted @nogc {
102 pragma(inline, true);
103 return
104 ch >= '0' && ch <= '9' && ch-'0' < base ? ch-'0' :
105 base > 10 && ch >= 'A' && ch < 'Z' && ch-'A'+10 < base ? ch-'A'+10 :
106 base > 10 && ch >= 'a' && ch < 'z' && ch-'a'+10 < base ? ch-'a'+10 :
111 alias atof = atofd!float; /// very simple atof/atod converter. accepts exponents. returns NaN on error.
112 alias atod = atofd!double; /// very simple atof/atod converter. accepts exponents. returns NaN on error.
114 /// very simple atof/atod converter. accepts exponents.
115 /// returns NaN on error.
116 T atofd(T) (const(char)[] str) pure nothrow @trusted @nogc if (is(T == float) || is(T == double)) {
117 if (str.length == 0) return T.nan; // oops
119 const(char)[] s = str;
120 double res = 0.0, sign = 1.0;
121 bool hasIntPart = false, hasFracPart = false;
123 char peekChar () nothrow @trusted @nogc { pragma(inline, true); return (s.length ? s.ptr[0] : '\0'); }
124 void skipChar () nothrow @trusted @nogc { pragma(inline, true); if (s.length > 0) s = s[1..$]; }
125 char getChar () nothrow @trusted @nogc { char ch = 0; if (s.length > 0) { ch = s.ptr[0]; s = s[1..$]; } return ch; }
127 // optional sign
128 switch (peekChar) {
129 case '-': sign = -1; goto case;
130 case '+': skipChar(); break;
131 default: break;
134 // integer part
135 if (isdigit(peekChar)) {
136 hasIntPart = true;
137 while (isdigit(peekChar)) res = res*10.0+(getChar()-'0');
140 // fractional part.
141 if (peekChar == '.') {
142 skipChar(); // skip '.'
143 if (isdigit(peekChar)) {
144 hasFracPart = true;
145 int divisor = 1;
146 long num = 0;
147 while (isdigit(peekChar)) {
148 divisor *= 10;
149 num = num*10+(getChar()-'0');
151 res += cast(double)num/divisor;
155 // valid number should have integer or fractional part
156 if (!hasIntPart && !hasFracPart) return T.nan;
158 // optional exponent
159 if (peekChar == 'e' || peekChar == 'E') {
160 skipChar(); // skip 'E'
161 // optional sign
162 bool epositive = true;
163 switch (peekChar) {
164 case '-': epositive = false; goto case;
165 case '+': skipChar(); break;
166 default: break;
168 int expPart = 0;
169 while (isdigit(peekChar)) expPart = expPart*10+(getChar()-'0');
170 if (epositive) {
171 foreach (immutable _; 0..expPart) res *= 10.0;
172 } else {
173 foreach (immutable _; 0..expPart) res /= 10.0;
177 return cast(T)(res*sign);
181 // ascii only
182 bool strEquCI (const(char)[] s0, const(char)[] s1) pure nothrow @trusted @nogc {
183 if (s0.length != s1.length) return false;
184 foreach (immutable idx, char c0; s0) {
185 // try the easiest case first
186 if (__ctfe) {
187 if (c0 == s1[idx]) continue;
188 } else {
189 if (c0 == s1.ptr[idx]) continue;
191 c0 |= 0x20; // convert to ascii lowercase
192 if (c0 < 'a' || c0 > 'z') return false; // it wasn't a letter, no need to check the second char
193 // c0 is guaranteed to be a lowercase ascii here
194 if (__ctfe) {
195 if (c0 != (s1[idx]|0x20)) return false; // c1 will become a lowercase ascii only if it was uppercase/lowercase ascii
196 } else {
197 if (c0 != (s1.ptr[idx]|0x20)) return false; // c1 will become a lowercase ascii only if it was uppercase/lowercase ascii
200 return true;
204 version(test_strex) unittest {
205 assert(strEquCI("Alice", "alice"));
206 assert(strEquCI("alice", "Alice"));
207 assert(strEquCI("alice", "alice"));
211 // ascii only
212 int strCmpCI (const(char)[] s0, const(char)[] s1) pure nothrow @trusted @nogc {
213 auto slen = s0.length;
214 if (slen > s1.length) slen = s1.length;
215 char c1;
216 foreach (immutable idx, char c0; s0[0..slen]) {
217 c0 = c0.tolower;
218 if (__ctfe) {
219 c1 = s1[idx].tolower;
220 } else {
221 c1 = s1.ptr[idx].tolower;
223 if (c0 < c1) return -1;
224 if (c0 > c1) return 1;
226 if (s0.length < s1.length) return -1;
227 if (s0.length > s1.length) return +1;
228 return 0;
232 inout(char)[] xstrip (inout(char)[] s) pure nothrow @trusted @nogc {
233 if (__ctfe) {
234 while (s.length && s[0] <= ' ') s = s[1..$];
235 } else {
236 while (s.length && s.ptr[0] <= ' ') s = s[1..$];
238 while (s.length && s[$-1] <= ' ') s = s[0..$-1];
239 return s;
243 inout(char)[] xstripleft (inout(char)[] s) pure nothrow @trusted @nogc {
244 if (__ctfe) {
245 while (s.length && s[0] <= ' ') s = s[1..$];
246 } else {
247 while (s.length && s.ptr[0] <= ' ') s = s[1..$];
249 return s;
253 inout(char)[] xstripright (inout(char)[] s) pure nothrow @trusted @nogc {
254 while (s.length && s[$-1] <= ' ') s = s[0..$-1];
255 return s;
259 bool startsWith (const(char)[] str, const(char)[] pat) pure nothrow @trusted @nogc {
260 if (pat.length > str.length) return false;
261 return (str[0..pat.length] == pat);
265 bool endsWith (const(char)[] str, const(char)[] pat) pure nothrow @trusted @nogc {
266 if (pat.length > str.length) return false;
267 return (str[$-pat.length..$] == pat);
271 // ascii only
272 bool startsWithCI (const(char)[] str, const(char)[] pat) pure nothrow @trusted @nogc {
273 if (pat.length > str.length) return false;
274 return strEquCI(str[0..pat.length], pat);
278 // ascii only
279 bool endsWithCI (const(char)[] str, const(char)[] pat) pure nothrow @trusted @nogc {
280 if (pat.length > str.length) return false;
281 return strEquCI(str[$-pat.length..$], pat);
285 ptrdiff_t indexOf (const(char)[] hay, const(char)[] need, size_t stIdx=0) pure nothrow @trusted @nogc {
286 if (hay.length <= stIdx || need.length == 0 || need.length > hay.length-stIdx) {
287 return -1;
288 } else {
289 if (need.length == 1) {
290 if (__ctfe) {
291 return indexOf(hay, need[0], stIdx);
292 } else {
293 return indexOf(hay, need.ptr[0], stIdx);
295 } else {
296 if (__ctfe) {
297 foreach (immutable idx; stIdx..hay.length-need.length+1) {
298 if (hay[idx..idx+need.length] == need) return idx;
300 return -1;
301 } else {
302 auto res = cast(const(char)*)memmem(hay.ptr+stIdx, hay.length-stIdx, need.ptr, need.length);
303 return (res !is null ? cast(ptrdiff_t)(res-hay.ptr) : -1);
309 ptrdiff_t indexOf (const(char)[] hay, char ch, size_t stIdx=0) pure nothrow @trusted @nogc {
310 if (hay.length <= stIdx) {
311 return -1;
312 } else {
313 if (__ctfe) {
314 foreach (immutable idx; stIdx..hay.length) {
315 if (hay[idx] == ch) return idx;
317 return -1;
318 } else {
319 import core.stdc.string : memchr;
320 auto res = cast(const(char)*)memchr(hay.ptr+stIdx, ch, hay.length-stIdx);
321 return (res !is null ? cast(ptrdiff_t)(res-hay.ptr) : -1);
327 ptrdiff_t lastIndexOf (const(char)[] hay, const(char)[] need, size_t stIdx=0) pure nothrow @trusted @nogc {
328 if (hay.length <= stIdx || need.length == 0 || need.length > hay.length-stIdx) {
329 return -1;
330 } else {
331 if (hay.length == 1) {
332 if (__ctfe) {
333 return lastIndexOf(hay, need[0], stIdx);
334 } else {
335 return lastIndexOf(hay, need.ptr[0], stIdx);
337 } else {
338 if (__ctfe) {
339 foreach_reverse (immutable idx; stIdx..hay.length-need.length+1) {
340 if (hay[idx..idx+need.length] == need) return idx;
342 return -1;
343 } else {
344 auto res = cast(char*)memrmem(hay.ptr+stIdx, hay.length-stIdx, need.ptr, need.length);
345 return (res !is null ? cast(ptrdiff_t)(res-hay.ptr) : -1);
351 ptrdiff_t lastIndexOf (const(char)[] hay, char ch, size_t stIdx=0) pure nothrow @trusted @nogc {
352 if (hay.length <= stIdx) {
353 return -1;
354 } else {
355 if (__ctfe) {
356 foreach_reverse (immutable idx; stIdx..hay.length) {
357 if (hay[idx] == ch) return idx;
359 return -1;
360 } else {
361 auto res = cast(const(char)*)memrchr(hay.ptr+stIdx, ch, hay.length-stIdx);
362 return (res !is null ? cast(ptrdiff_t)(res-hay.ptr) : -1);
368 version(test_strex) unittest {
369 assert(indexOf("Alice & Miriel", " & ") == 5);
370 assert(indexOf("Alice & Miriel", " &!") == -1);
371 assert(indexOf("Alice & Miriel", "Alice & Miriel was here!") == -1);
372 assert(indexOf("Alice & Miriel", '&') == 6);
373 char ch = ' ';
374 assert(indexOf("Alice & Miriel", ch) == 5);
376 assert(indexOf("Alice & Miriel", "i") == 2);
377 assert(indexOf("Alice & Miriel", "i", 6) == 9);
378 assert(indexOf("Alice & Miriel", "i", 12) == -1);
380 assert(indexOf("Alice & Miriel", "Miriel", 8) == 8);
381 assert(indexOf("Alice & Miriel", "Miriel", 9) == -1);
383 assert(lastIndexOf("Alice & Miriel", "i") == 11);
384 assert(lastIndexOf("Alice & Miriel", "i", 6) == 11);
385 assert(lastIndexOf("Alice & Miriel", "i", 11) == 11);
386 assert(lastIndexOf("Alice & Miriel", "i", 12) == -1);
388 assert(lastIndexOf("iiii", "ii") == 2);
392 string detab (const(char)[] s, uint tabSize=8) {
393 assert(tabSize > 0);
395 import std.array : appender;
396 auto res = appender!string();
397 uint col = 0;
399 foreach (char ch; s) {
400 if (ch == '\n' || ch == '\r') {
401 col = 0;
402 } else if (ch == '\t') {
403 auto spins = tabSize-col%tabSize;
404 col += spins;
405 while (spins-- > 1) res.put(' ');
406 ch = ' ';
407 } else {
408 ++col;
410 res.put(ch);
413 return res.data;
417 version(test_strex) unittest {
418 assert(detab(" \n\tx", 9) == " \n x");
419 assert(detab(" ab\t asdf ") == " ab asdf ");
423 auto byLine(T) (T s) if (is(T:const(char)[])) {
424 static struct Range(T) {
425 nothrow @safe @nogc:
426 private:
427 T s;
428 size_t llen, npos;
429 this (T as) { s = as; popFront(); }
430 public:
431 @property bool empty () const { pragma(inline, true); return (s.length == 0); }
432 @property T front () const { pragma(inline, true); return cast(T)s[0..llen]; } // fuckin' const!
433 auto save () const @trusted { Range!T res = void; res.s = s; res.llen = llen; res.npos = npos; return res; }
434 void popFront () @trusted {
435 s = s[npos..$];
436 llen = npos = 0;
437 while (npos < s.length) {
438 if (s.ptr[npos] == '\r') {
439 llen = npos;
440 if (s.length-npos > 1 && s.ptr[npos+1] == '\n') ++npos;
441 ++npos;
442 return;
444 if (s.ptr[npos] == '\n') {
445 llen = npos;
446 ++npos;
447 return;
449 ++npos;
451 llen = npos;
454 return Range!T(s);
458 version(test_strex) unittest {
459 enum s = q{
460 import std.stdio;
461 void main() {
462 writeln("Hello");
465 enum ugly = q{
466 import std.stdio;
467 void main() {
468 writeln("Hello");
472 foreach (/+auto+/ line; s.byLine) {
473 import std.stdio;
474 writeln("LN: [", line, "]");
477 foreach (/+auto+/ line; ugly.byLine) {
478 import std.stdio;
479 writeln("LN: [", line, "]");
484 // string should be detabbed!
485 string outdentAll (const(char)[] s) {
486 import std.array : appender;
487 // first calculate maximum indent spaces
488 uint maxspc = uint.max;
489 foreach (/*auto*/ line; s.byLine) {
490 uint col = 0;
491 while (col < line.length && line.ptr[col] <= ' ') {
492 if (line.ptr[col] == '\t') assert(0, "can't outdent shit with tabs");
493 ++col;
495 if (col >= line.length) continue; // empty line, don't care
496 if (col < maxspc) maxspc = col;
497 if (col == 0) break; // nothing to do anymore
500 auto res = appender!string();
501 foreach (/*auto*/ line; s.byLine) {
502 uint col = 0;
503 while (col < line.length && line.ptr[col] <= ' ') ++col;
504 if (col < line.length) {
505 // non-empty line
506 res.put(line[maxspc..$]);
508 res.put('\n');
511 return res.data;
515 version(test_strex) unittest {
516 enum pretty = q{
517 import std.stdio;
518 void main() {
519 writeln("Hello");
521 }.outdentAll;
523 enum ugly = q{
524 import std.stdio;
525 void main() {
526 writeln("Hello");
531 import std.stdio;
532 assert(pretty == ugly);
536 //From: Yahoo Groups <confirm-s2-2ny0qbq23nljzefbilh5vpjrg1pik5hf-ketmar=ketmar.no-ip.org@yahoogroups.com>
537 private bool isValidEmailNameChar (char ch) pure nothrow @safe @nogc {
538 pragma(inline, true);
539 if (ch <= 32) return false;
540 if (ch >= '0' && ch <= '9') return true;
541 if (ch >= 'a' && ch <= 'z') ch -= 32; // poor man's tolower
542 if (ch >= 'A' && ch <= 'Z') return true;
543 if (ch == '_' || ch == '+' || ch == '-' || ch == '=' || ch == '.' || ch == '$') return true;
544 if (ch >= 128) return true; // why not?
545 // why not?
546 if (ch == '!' || ch == '%' || ch == '^' || ch == '&' || ch == '(' || ch == ')') return true;
547 if (ch == '?') return true;
548 return false;
552 private bool isValidEmailHostChar (char ch) pure nothrow @safe @nogc {
553 pragma(inline, true);
554 if (ch <= 32 || ch >= 127) return false;
555 if (ch >= '0' && ch <= '9') return true;
556 if (ch >= 'a' && ch <= 'z') ch -= 32; // poor man's tolower
557 if (ch >= 'A' && ch <= 'Z') return true;
558 if (ch == '-' || ch == '.') return true;
559 return false;
563 bool isGoodEmail (const(char)[] s) pure nothrow @trusted @nogc {
564 if (s.length == 0 || s.ptr[0] == '@') return false;
565 // parse part until '@'
566 while (s.length) {
567 char ch = s.ptr[0];
568 if (ch == '@') break;
569 if (!isValidEmailNameChar(ch)) return false;
570 s = s[1..$];
572 if (!s.length) return false; // no doggy
573 assert(s.ptr[0] == '@');
574 s = s[1..$];
575 if (s.length == 0) return false;
576 while (s.length) {
577 char ch = s.ptr[0];
578 if (!isValidEmailHostChar(ch)) return false;
579 s = s[1..$];
581 return true;
585 /// backslash in ranges is used to escaping; '<' and '>' matching word start and end
586 bool globmatch(bool casesens=true) (const(char)[] str, const(char)[] pat) pure nothrow @trusted @nogc {
587 static bool globIsWordChar (const char ch) pure nothrow @safe @nogc {
588 pragma(inline, true);
589 return
590 (ch >= 'A' && ch <= 'Z') ||
591 (ch >= 'a' && ch <= 'z') ||
592 (ch >= '0' && ch <= '9') ||
593 ch == '_' || ch >= 128;
596 // empty pattern cannot match non-empty string
597 if (pat.length == 0) return (str.length == 0);
598 // start matching
599 const(char)* realstart = str.ptr;
600 bool star = false;
601 usize patpos = void;
602 loopStart:
603 patpos = 0;
604 foreach (usize i; 0..str.length) {
605 static if (casesens) {
606 immutable char sch = str.ptr[i];
607 } else {
608 immutable char sch = tolower(str.ptr[i]);
610 matchAgain:
611 if (patpos >= pat.length) goto starCheck;
612 switch (pat.ptr[patpos++]) {
613 case '?': // match anything
614 break;
615 case '*':
616 star = true;
617 str = str[i..$];
618 pat = pat[patpos..$];
619 // skip excessive stars
620 while (pat.length && pat.ptr[0] == '*') pat = pat[1..$];
621 if (pat.length == 0) return true;
622 goto loopStart;
623 case '[':
625 bool hasMatch = false;
626 bool inverted = (pat.ptr[patpos] == '^');
627 if (inverted) ++patpos;
628 if (patpos >= pat.length) return false; // malformed pattern
629 do {
630 char c0 = pat.ptr[patpos++];
631 if (c0 == '\\' && patpos < pat.length) c0 = pat.ptr[patpos++];
632 if (patpos >= pat.length) return false; // malformed pattern
633 static if (!casesens) c0 = tolower(c0);
634 char c1 = c0;
635 if (pat.ptr[patpos] == '-') {
636 // char range
637 ++patpos; // skip '-'
638 if (patpos >= pat.length) return false; // malformed pattern
639 c1 = pat.ptr[patpos++];
640 if (c1 == '\\' && patpos < pat.length) c1 = pat.ptr[patpos++];
641 static if (!casesens) c1 = tolower(c1);
642 if (patpos >= pat.length) return false; // malformed pattern
644 hasMatch = (!hasMatch && sch >= c0 && sch <= c1);
645 } while (patpos < pat.length && pat.ptr[patpos] != ']');
646 if (patpos >= pat.length || pat.ptr[patpos] != ']') return false; // malformed pattern
647 ++patpos;
648 if (inverted) hasMatch = !hasMatch;
649 if (!hasMatch) goto starCheck;
650 break;
652 case '<': // word boundary (start)
653 // current char must be a word char
654 if (!globIsWordChar(sch)) goto starCheck;
656 const usize realpos = cast(ptrdiff_t)(str.ptr-realstart)+i;
657 // previous char must not be a word char
658 if (realpos > 0 && globIsWordChar(realstart[realpos-1u])) goto starCheck;
660 goto matchAgain;
661 case '>': // word boundary (end)
662 // current char must not be a word char
663 if (globIsWordChar(sch)) goto starCheck;
665 const usize realpos = cast(ptrdiff_t)(str.ptr-realstart)+i;
666 // previous char must be a word char
667 if (realpos > 0 && !globIsWordChar(realstart[realpos-1u])) goto starCheck;
669 goto matchAgain;
670 case '\\':
671 ++patpos;
672 if (patpos >= pat.length) return false; // malformed pattern
673 goto default;
674 default:
675 static if (casesens) {
676 if (sch != pat.ptr[patpos-1]) goto starCheck;
677 } else {
678 if (sch != tolower(pat.ptr[patpos-1])) goto starCheck;
680 break;
683 pat = pat[patpos..$];
684 // pattern may end with stars, skip them
685 // also skip word boundaries check (they are always true here)
686 while (pat.length && (pat.ptr[0] == '*' || pat.ptr[0] == '<' || pat.ptr[0] == '>')) pat = pat[1..$];
687 return (pat.length == 0);
689 starCheck:
690 if (!star) return false;
691 if (str.length) str = str[1..$];
692 goto loopStart;
695 /// ditto.
696 alias globmatchCI = globmatch!false;
699 pure nothrow @system @nogc:
700 version(linux) {
701 extern(C) inout(void)* memmem (inout(void)* haystack, size_t haystacklen, inout(void)* needle, size_t needlelen);
702 extern(C) inout(void)* memrchr (inout(void)* s, int ch, size_t slen);
703 } else {
704 inout(void)* memmem (inout(void)* haystack, size_t haystacklen, inout(void)* needle, size_t needlelen) {
705 // size_t is unsigned
706 if (needlelen > haystacklen || needlelen == 0) return null;
707 auto h = cast(const(ubyte)*)haystack;
708 auto n = cast(const(ubyte)*)needle;
709 foreach (immutable i; 0..haystacklen-needlelen+1) {
710 import core.stdc.string : memcmp;
711 if (memcmp(h+i, n, needlelen) == 0) return cast(typeof(return))(h+i);
713 return null;
716 inout(void)* memrchr (inout(void)* haystack, int ch, size_t haystacklen) {
717 // size_t is unsigned
718 if (haystacklen == 0) return null;
719 auto h = cast(const(ubyte)*)haystack;
720 ch &= 0xff;
721 foreach_reverse (immutable idx, ubyte v; h[0..haystacklen]) {
722 if (v == ch) return cast(typeof(return))(h+idx);
724 return null;
728 inout(void)* memrmem (inout(void)* haystack, size_t haystacklen, inout(void)* needle, size_t needlelen) {
729 if (needlelen > haystacklen) return null;
730 auto h = cast(const(ubyte)*)haystack;
731 const(ubyte)* res = null;
732 // size_t is unsigned
733 if (needlelen > haystacklen || needlelen == 0) return null;
734 version(none) {
735 size_t pos = 0;
736 while (pos < haystacklen-needlelen+1) {
737 auto ff = memmem(haystack+pos, haystacklen-pos, needle, needlelen);
738 if (ff is null) break;
739 res = cast(const(ubyte)*)ff;
740 pos = cast(size_t)(res-haystack)+1;
742 return cast(void*)res;
743 } else {
744 auto n = cast(const(ubyte)*)needle;
745 size_t len = haystacklen-needlelen+1;
746 while (len > 0) {
747 import core.stdc.string : memcmp;
748 auto ff = cast(const(ubyte)*)memrchr(haystack, *n, len);
749 if (ff is null) break;
750 if (memcmp(ff, needle, needlelen) == 0) return cast(void*)ff;
751 //if (ff is h) break;
752 len = cast(size_t)(ff-cast(ubyte*)haystack);
754 return null;