Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 70f8cbf

Browse files
Ernesto CastellottiPetarKirov
authored andcommitted
Add memmem to core.sys
memmem is a function present in the "string.h" header of many posix operating systems, although its presence in the standard library is not covered by the C standard nor by the posix standard. However it is present in most parts of posix implementations of the C standard library, so i think it's possible to expose it in the druntime. memmem is very useful for analyzing data when pointers need to be used, so far it was necessary to manually implement a similar function to do it and it was quite inconvenient and redundant. Signed-off-by: Ernesto Castellotti <[email protected]>
1 parent ca8e728 commit 70f8cbf

File tree

10 files changed

+196
-0
lines changed

10 files changed

+196
-0
lines changed

mak/COPY

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ COPY=\
6767
\
6868
$(IMPDIR)\core\sync\event.d \
6969
\
70+
$(IMPDIR)\core\sys\bionic\string.d \
71+
\
7072
$(IMPDIR)\core\sys\darwin\crt_externs.d \
7173
$(IMPDIR)\core\sys\darwin\dlfcn.d \
7274
$(IMPDIR)\core\sys\darwin\execinfo.d \
7375
$(IMPDIR)\core\sys\darwin\pthread.d \
76+
$(IMPDIR)\core\sys\darwin\string.d \
7477
\
7578
$(IMPDIR)\core\sys\darwin\mach\dyld.d \
7679
$(IMPDIR)\core\sys\darwin\mach\getsect.d \
@@ -92,6 +95,7 @@ COPY=\
9295
$(IMPDIR)\core\sys\freebsd\netinet\in_.d \
9396
\
9497
$(IMPDIR)\core\sys\freebsd\pthread_np.d \
98+
$(IMPDIR)\core\sys\freebsd\string.d \
9599
$(IMPDIR)\core\sys\freebsd\sys\_bitset.d \
96100
$(IMPDIR)\core\sys\freebsd\sys\_cpuset.d \
97101
$(IMPDIR)\core\sys\freebsd\sys\cdefs.d \
@@ -112,6 +116,7 @@ COPY=\
112116
$(IMPDIR)\core\sys\dragonflybsd\netinet\in_.d \
113117
\
114118
$(IMPDIR)\core\sys\dragonflybsd\pthread_np.d \
119+
$(IMPDIR)\core\sys\dragonflybsd\string.d \
115120
$(IMPDIR)\core\sys\dragonflybsd\sys\_bitset.d \
116121
$(IMPDIR)\core\sys\dragonflybsd\sys\_cpuset.d \
117122
$(IMPDIR)\core\sys\dragonflybsd\sys\cdefs.d \
@@ -134,6 +139,7 @@ COPY=\
134139
$(IMPDIR)\core\sys\linux\ifaddrs.d \
135140
$(IMPDIR)\core\sys\linux\link.d \
136141
$(IMPDIR)\core\sys\linux\sched.d \
142+
$(IMPDIR)\core\sys\linux\string.d \
137143
$(IMPDIR)\core\sys\linux\termios.d \
138144
$(IMPDIR)\core\sys\linux\time.d \
139145
$(IMPDIR)\core\sys\linux\timerfd.d \
@@ -156,7 +162,10 @@ COPY=\
156162
$(IMPDIR)\core\sys\linux\sys\time.d \
157163
$(IMPDIR)\core\sys\linux\sys\prctl.d \
158164
\
165+
$(IMPDIR)\core\sys\netbsd\string.d \
166+
\
159167
$(IMPDIR)\core\sys\openbsd\dlfcn.d \
168+
$(IMPDIR)\core\sys\openbsd\string.d \
160169
\
161170
$(IMPDIR)\core\sys\posix\arpa\inet.d \
162171
$(IMPDIR)\core\sys\posix\aio.d \

mak/SRCS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ SRCS=\
7373
src\core\sync\rwmutex.d \
7474
src\core\sync\semaphore.d \
7575
\
76+
src\core\sys\bionic\string.d \
77+
\
7678
src\core\sys\darwin\crt_externs.d \
7779
src\core\sys\darwin\dlfcn.d \
7880
src\core\sys\darwin\execinfo.d \
7981
src\core\sys\darwin\pthread.d \
82+
src\core\sys\darwin\string.d \
8083
src\core\sys\darwin\mach\dyld.d \
8184
src\core\sys\darwin\mach\getsect.d \
8285
src\core\sys\darwin\mach\kern_return.d \
@@ -94,6 +97,7 @@ SRCS=\
9497
src\core\sys\freebsd\execinfo.d \
9598
src\core\sys\freebsd\netinet\in_.d \
9699
src\core\sys\freebsd\pthread_np.d \
100+
src\core\sys\freebsd\string.d \
97101
src\core\sys\freebsd\sys\_bitset.d \
98102
src\core\sys\freebsd\sys\_cpuset.d \
99103
src\core\sys\freebsd\sys\cdefs.d \
@@ -112,6 +116,7 @@ SRCS=\
112116
src\core\sys\dragonflybsd\execinfo.d \
113117
src\core\sys\dragonflybsd\netinet\in_.d \
114118
src\core\sys\dragonflybsd\pthread_np.d \
119+
src\core\sys\dragonflybsd\string.d \
115120
src\core\sys\dragonflybsd\sys\_bitset.d \
116121
src\core\sys\dragonflybsd\sys\_cpuset.d \
117122
src\core\sys\dragonflybsd\sys\cdefs.d \
@@ -134,6 +139,7 @@ SRCS=\
134139
src\core\sys\linux\ifaddrs.d \
135140
src\core\sys\linux\link.d \
136141
src\core\sys\linux\sched.d \
142+
src\core\sys\linux\string.d \
137143
src\core\sys\linux\termios.d \
138144
src\core\sys\linux\time.d \
139145
src\core\sys\linux\timerfd.d \
@@ -156,7 +162,10 @@ SRCS=\
156162
src\core\sys\linux\sys\time.d \
157163
src\core\sys\linux\sys\prctl.d \
158164
\
165+
src\core\sys\netbsd\string.d \
166+
\
159167
src\core\sys\openbsd\dlfcn.d \
168+
src\core\sys\openbsd\string.d \
160169
\
161170
src\core\sys\posix\arpa\inet.d \
162171
src\core\sys\posix\aio.d \

mak/WINDOWS

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ copydir: $(IMPDIR)
3434
mkdir $(IMPDIR)\core\stdc
3535
mkdir $(IMPDIR)\core\stdcpp
3636
mkdir $(IMPDIR)\core\internal
37+
mkdir $(IMPDIR)\core\sys\bionic
3738
mkdir $(IMPDIR)\core\sys\darwin\mach
3839
mkdir $(IMPDIR)\core\sys\darwin\netinet
3940
mkdir $(IMPDIR)\core\sys\darwin\sys
@@ -44,6 +45,7 @@ copydir: $(IMPDIR)
4445
mkdir $(IMPDIR)\core\sys\linux\netinet
4546
mkdir $(IMPDIR)\core\sys\linux\sys
4647
mkdir $(IMPDIR)\core\sys\linux\sys\netinet
48+
mkdir $(IMPDIR)\core\sys\netbsd
4749
mkdir $(IMPDIR)\core\sys\openbsd
4850
mkdir $(IMPDIR)\core\sys\posix\arpa
4951
mkdir $(IMPDIR)\core\sys\posix\net
@@ -239,6 +241,9 @@ $(IMPDIR)\core\stdcpp\type_traits.d : src\core\stdcpp\type_traits.d
239241
$(IMPDIR)\core\stdcpp\xutility.d : src\core\stdcpp\xutility.d
240242
copy $** $@
241243

244+
$(IMPDIR)\core\sys\bionic\string.d : src\core\sys\bionic\string.d
245+
copy $** $@
246+
242247
$(IMPDIR)\core\sys\darwin\crt_externs.d : src\core\sys\darwin\crt_externs.d
243248
copy $** $@
244249

@@ -251,6 +256,9 @@ $(IMPDIR)\core\sys\darwin\execinfo.d : src\core\sys\darwin\execinfo.d
251256
$(IMPDIR)\core\sys\darwin\pthread.d : src\core\sys\darwin\pthread.d
252257
copy $** $@
253258

259+
$(IMPDIR)\core\sys\darwin\string.d : src\core\sys\darwin\string.d
260+
copy $** $@
261+
254262
$(IMPDIR)\core\sys\darwin\mach\dyld.d : src\core\sys\darwin\mach\dyld.d
255263
copy $** $@
256264

@@ -293,6 +301,9 @@ $(IMPDIR)\core\sys\freebsd\execinfo.d : src\core\sys\freebsd\execinfo.d
293301
$(IMPDIR)\core\sys\freebsd\pthread_np.d : src\core\sys\freebsd\pthread_np.d
294302
copy $** $@
295303

304+
$(IMPDIR)\core\sys\freebsd\string.d : src\core\sys\freebsd\string.d
305+
copy $** $@
306+
296307
$(IMPDIR)\core\sys\freebsd\time.d : src\core\sys\freebsd\time.d
297308
copy $** $@
298309

@@ -344,6 +355,9 @@ $(IMPDIR)\core\sys\dragonflybsd\execinfo.d : src\core\sys\dragonflybsd\execinfo.
344355
$(IMPDIR)\core\sys\dragonflybsd\pthread_np.d : src\core\sys\dragonflybsd\pthread_np.d
345356
copy $** $@
346357

358+
$(IMPDIR)\core\sys\dragonflybsd\string.d : src\core\sys\dragonflybsd\string.d
359+
copy $** $@
360+
347361
$(IMPDIR)\core\sys\dragonflybsd\time.d : src\core\sys\dragonflybsd\time.d
348362
copy $** $@
349363

@@ -410,6 +424,9 @@ $(IMPDIR)\core\sys\linux\link.d : src\core\sys\linux\link.d
410424
$(IMPDIR)\core\sys\linux\sched.d : src\core\sys\linux\sched.d
411425
copy $** $@
412426

427+
$(IMPDIR)\core\sys\linux\string.d : src\core\sys\linux\string.d
428+
copy $** $@
429+
413430
$(IMPDIR)\core\sys\linux\termios.d : src\core\sys\linux\termios.d
414431
copy $** $@
415432

@@ -467,9 +484,15 @@ $(IMPDIR)\core\sys\linux\sys\xattr.d : src\core\sys\linux\sys\xattr.d
467484
$(IMPDIR)\core\sys\linux\sys\time.d : src\core\sys\linux\sys\time.d
468485
copy $** $@
469486

487+
$(IMPDIR)\core\sys\netbsd\string.d : src\core\sys\netbsd\string.d
488+
copy $** $@
489+
470490
$(IMPDIR)\core\sys\openbsd\dlfcn.d : src\core\sys\openbsd\dlfcn.d
471491
copy $** $@
472492

493+
$(IMPDIR)\core\sys\openbsd\string.d : src\core\sys\openbsd\string.d
494+
copy $** $@
495+
473496
$(IMPDIR)\core\sys\posix\arpa\inet.d : src\core\sys\posix\arpa\inet.d
474497
copy $** $@
475498

src/core/sys/bionic/string.d

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* D header file for Bionic string.
3+
*
4+
* Copyright: Copyright © 2019, The D Language Foundation
5+
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
6+
* Authors: Ernesto Castellotti
7+
*/
8+
module core.sys.bionic.string;
9+
10+
public import core.stdc.string;
11+
12+
version (CRuntime_Bionic):
13+
extern (C):
14+
nothrow:
15+
@nogc:
16+
17+
pure void* memmem(return const void* haystack, size_t haystacklen, scope const void* needle, size_t needlelen);

src/core/sys/darwin/string.d

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* D header file for Darwin string.
3+
*
4+
* Copyright: Copyright © 2019, The D Language Foundation
5+
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
6+
* Authors: Ernesto Castellotti
7+
*/
8+
module core.sys.darwin.string;
9+
10+
public import core.stdc.string;
11+
import core.sys.darwin.sys.cdefs;
12+
13+
version (OSX)
14+
version = Darwin;
15+
else version (iOS)
16+
version = Darwin;
17+
else version (TVOS)
18+
version = Darwin;
19+
else version (WatchOS)
20+
version = Darwin;
21+
22+
version (Darwin):
23+
extern (C):
24+
nothrow:
25+
@nogc:
26+
27+
static if (__DARWIN_C_LEVEL >= __DARWIN_C_FULL)
28+
{
29+
// ^ __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
30+
pure void* memmem(return const void* haystack, size_t haystacklen, scope const void* needle, size_t needlelen);
31+
}

src/core/sys/dragonflybsd/string.d

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* D header file for DragonFlyBSD string.
3+
*
4+
* Copyright: Copyright © 2019, The D Language Foundation
5+
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
6+
* Authors: Ernesto Castellotti
7+
*/
8+
module core.sys.dragonflybsd.string;
9+
10+
public import core.stdc.string;
11+
import core.sys.dragonflybsd.sys.cdefs;
12+
13+
version (DragonFlyBSD):
14+
extern (C):
15+
nothrow:
16+
@nogc:
17+
18+
static if (__BSD_VISIBLE)
19+
{
20+
pure void* memmem(return const void* haystack, size_t haystacklen, scope const void* needle, size_t needlelen);
21+
}
22+

src/core/sys/freebsd/string.d

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* D header file for FreeBSD string.
3+
*
4+
* Copyright: Copyright © 2019, The D Language Foundation
5+
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
6+
* Authors: Ernesto Castellotti
7+
*/
8+
module core.sys.freebsd.string;
9+
10+
public import core.stdc.string;
11+
import core.sys.freebsd.sys.cdefs;
12+
13+
version (FreeBSD):
14+
extern (C):
15+
nothrow:
16+
@nogc:
17+
18+
static if (__BSD_VISIBLE)
19+
{
20+
pure void* memmem(return const void* haystack, size_t haystacklen, scope const void* needle, size_t needlelen);
21+
}

src/core/sys/linux/string.d

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* D header file for Linux string.
3+
*
4+
* Copyright: Copyright © 2019, The D Language Foundation
5+
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
6+
* Authors: Ernesto Castellotti
7+
*/
8+
module core.sys.linux.string;
9+
10+
public import core.stdc.string;
11+
import core.sys.linux.config;
12+
13+
version (linux):
14+
extern (C):
15+
nothrow:
16+
@nogc:
17+
18+
static if (__USE_GNU)
19+
{
20+
pure void* memmem(return const void* haystack, size_t haystacklen, scope const void* needle, size_t needlelen);
21+
}

src/core/sys/netbsd/string.d

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* D header file for NetBSD string.
3+
*
4+
* Copyright: Copyright © 2019, The D Language Foundation
5+
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
6+
* Authors: Ernesto Castellotti
7+
*/
8+
module core.sys.netbsd.string;
9+
10+
public import core.stdc.string;
11+
12+
version (NetBSD):
13+
extern (C):
14+
nothrow:
15+
@nogc:
16+
17+
enum _NETBSD_SOURCE = true;
18+
19+
static if (_NETBSD_SOURCE)
20+
{
21+
pure void* memmem(return const void* haystack, size_t haystacklen, scope const void* needle, size_t needlelen);
22+
}

src/core/sys/openbsd/string.d

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* D header file for OpenBSD string.
3+
*
4+
* Copyright: Copyright © 2019, The D Language Foundation
5+
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
6+
* Authors: Ernesto Castellotti
7+
*/
8+
module core.sys.openbsd.string;
9+
10+
public import core.stdc.string;
11+
import core.sys.openbsd.sys.cdefs;
12+
13+
version (OpenBSD):
14+
extern (C):
15+
nothrow:
16+
@nogc:
17+
18+
static if (__BSD_VISIBLE)
19+
{
20+
pure void* memmem(return const void* haystack, size_t haystacklen, scope const void* needle, size_t needlelen);
21+
}

0 commit comments

Comments
 (0)