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.
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
);
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
)) {
39 static if (T
.sizeof
< 8) {
46 static if (T
.sizeof
< 8) {
54 if (n
< 0) n
= T
.max
; //FIXME
58 int bpos
= cast(int)buf
.length
;
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
];
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 {
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
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);
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
; }
129 case '-': sign
= -1; goto case;
130 case '+': skipChar(); break;
135 if (isdigit(peekChar
)) {
137 while (isdigit(peekChar
)) res
= res
*10.0+(getChar()-'0');
141 if (peekChar
== '.') {
142 skipChar(); // skip '.'
143 if (isdigit(peekChar
)) {
147 while (isdigit(peekChar
)) {
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
;
159 if (peekChar
== 'e' || peekChar
== 'E') {
160 skipChar(); // skip 'E'
162 bool epositive
= true;
164 case '-': epositive
= false; goto case;
165 case '+': skipChar(); break;
169 while (isdigit(peekChar
)) expPart
= expPart
*10+(getChar()-'0');
171 foreach (immutable _
; 0..expPart
) res
*= 10.0;
173 foreach (immutable _
; 0..expPart
) res
/= 10.0;
177 return cast(T
)(res
*sign
);
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
187 if (c0
== s1
[idx
]) continue;
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
195 if (c0
!= (s1
[idx
]|
0x20)) return false; // c1 will become a lowercase ascii only if it was uppercase/lowercase ascii
197 if (c0
!= (s1
.ptr
[idx
]|
0x20)) return false; // c1 will become a lowercase ascii only if it was uppercase/lowercase ascii
204 version(test_strex
) unittest {
205 assert(strEquCI("Alice", "alice"));
206 assert(strEquCI("alice", "Alice"));
207 assert(strEquCI("alice", "alice"));
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
;
216 foreach (immutable idx
, char c0
; s0
[0..slen
]) {
219 c1
= s1
[idx
].tolower
;
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;
232 inout(char)[] xstrip (inout(char)[] s
) pure nothrow @trusted @nogc {
234 while (s
.length
&& s
[0] <= ' ') s
= s
[1..$];
236 while (s
.length
&& s
.ptr
[0] <= ' ') s
= s
[1..$];
238 while (s
.length
&& s
[$-1] <= ' ') s
= s
[0..$-1];
243 inout(char)[] xstripleft (inout(char)[] s
) pure nothrow @trusted @nogc {
245 while (s
.length
&& s
[0] <= ' ') s
= s
[1..$];
247 while (s
.length
&& s
.ptr
[0] <= ' ') s
= s
[1..$];
253 inout(char)[] xstripright (inout(char)[] s
) pure nothrow @trusted @nogc {
254 while (s
.length
&& s
[$-1] <= ' ') s
= s
[0..$-1];
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
);
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
);
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
) {
289 if (need
.length
== 1) {
291 return indexOf(hay
, need
[0], stIdx
);
293 return indexOf(hay
, need
.ptr
[0], stIdx
);
297 foreach (immutable idx
; stIdx
..hay
.length
-need
.length
+1) {
298 if (hay
[idx
..idx
+need
.length
] == need
) return idx
;
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
) {
314 foreach (immutable idx
; stIdx
..hay
.length
) {
315 if (hay
[idx
] == ch
) return idx
;
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
) {
331 if (hay
.length
== 1) {
333 return lastIndexOf(hay
, need
[0], stIdx
);
335 return lastIndexOf(hay
, need
.ptr
[0], stIdx
);
339 foreach_reverse (immutable idx
; stIdx
..hay
.length
-need
.length
+1) {
340 if (hay
[idx
..idx
+need
.length
] == need
) return idx
;
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
) {
356 foreach_reverse (immutable idx
; stIdx
..hay
.length
) {
357 if (hay
[idx
] == ch
) return idx
;
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);
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) {
395 import std
.array
: appender
;
396 auto res
= appender
!string();
399 foreach (char ch
; s
) {
400 if (ch
== '\n' || ch
== '\r') {
402 } else if (ch
== '\t') {
403 auto spins
= tabSize
-col
%tabSize
;
405 while (spins
-- > 1) res
.put(' ');
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
) {
429 this (T as
) { s
= as
; popFront(); }
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 {
437 while (npos
< s
.length
) {
438 if (s
.ptr
[npos
] == '\r') {
440 if (s
.length
-npos
> 1 && s
.ptr
[npos
+1] == '\n') ++npos
;
444 if (s
.ptr
[npos
] == '\n') {
458 version(test_strex) unittest {
472 foreach (/+auto+/ line; s.byLine) {
474 writeln("LN: [", line, "]");
477 foreach (/+auto+/ line; ugly.byLine) {
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
) {
491 while (col
< line
.length
&& line
.ptr
[col
] <= ' ') {
492 if (line
.ptr
[col
] == '\t') assert(0, "can't outdent shit with tabs");
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
) {
503 while (col
< line
.length
&& line
.ptr
[col
] <= ' ') ++col
;
504 if (col
< line
.length
) {
506 res
.put(line
[maxspc
..$]);
515 version(test_strex
) unittest {
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?
546 if (ch
== '!' || ch
== '%' || ch
== '^' || ch
== '&' || ch
== '(' || ch
== ')') return true;
547 if (ch
== '?') return true;
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;
563 bool isGoodEmail (const(char)[] s
) pure nothrow @trusted @nogc {
564 if (s
.length
== 0 || s
.ptr
[0] == '@') return false;
565 // parse part until '@'
568 if (ch
== '@') break;
569 if (!isValidEmailNameChar(ch
)) return false;
572 if (!s
.length
) return false; // no doggy
573 assert(s
.ptr
[0] == '@');
575 if (s
.length
== 0) return false;
578 if (!isValidEmailHostChar(ch
)) return false;
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);
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);
599 const(char)* realstart
= str.ptr
;
604 foreach (usize i
; 0..str.length
) {
605 static if (casesens
) {
606 immutable char sch
= str.ptr
[i
];
608 immutable char sch
= tolower(str.ptr
[i
]);
611 if (patpos
>= pat
.length
) goto starCheck
;
612 switch (pat
.ptr
[patpos
++]) {
613 case '?': // match anything
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;
625 bool hasMatch
= false;
626 bool inverted
= (pat
.ptr
[patpos
] == '^');
627 if (inverted
) ++patpos
;
628 if (patpos
>= pat
.length
) return false; // malformed pattern
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
);
635 if (pat
.ptr
[patpos
] == '-') {
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
648 if (inverted
) hasMatch
= !hasMatch
;
649 if (!hasMatch
) goto starCheck
;
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
;
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
;
672 if (patpos
>= pat
.length
) return false; // malformed pattern
675 static if (casesens
) {
676 if (sch
!= pat
.ptr
[patpos
-1]) goto starCheck
;
678 if (sch
!= tolower(pat
.ptr
[patpos
-1])) goto starCheck
;
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);
690 if (!star
) return false;
691 if (str.length
) str = str[1..$];
696 alias globmatchCI
= globmatch
!false;
699 pure nothrow @system @nogc:
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
);
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
);
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
;
721 foreach_reverse (immutable idx
, ubyte v
; h
[0..haystacklen
]) {
722 if (v
== ch
) return cast(typeof(return))(h
+idx
);
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;
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
;
744 auto n
= cast(const(ubyte)*)needle
;
745 size_t len
= haystacklen
-needlelen
+1;
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
);