mlibc: Implement strstr.
* lib/libc+tcc.c (_memmem, memmem): New function. Import from GNU LilyPond. (strstr): Implement using memmem. * include/string.h (_memmem, memmem): Declare. * AUTHORS: Add Han-Wen Nienhuys.
This commit is contained in:
parent
26e1876d12
commit
9f90960392
3
AUTHORS
3
AUTHORS
|
@ -5,7 +5,8 @@ All files except the imported files listed below
|
||||||
Jeremiah Orians <jeremiah@pdp10.guru>
|
Jeremiah Orians <jeremiah@pdp10.guru>
|
||||||
lib/libc.c (fopen)
|
lib/libc.c (fopen)
|
||||||
|
|
||||||
|
Han-Wen Nienhuys <hanwen@xs4all.nl>
|
||||||
|
lib/libc+tcc.c (_memmem, memmem)
|
||||||
|
|
||||||
List of imported files
|
List of imported files
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
/* -*-comment-start: "//";comment-end:""-*-
|
/* -*-comment-start: "//";comment-end:""-*-
|
||||||
* Mes --- Maxwell Equations of Software
|
* Mes --- Maxwell Equations of Software
|
||||||
* Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
* Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
||||||
|
* Copyright (C) 2018 Han-Wen Nienhuys <hanwen@xs4all.nl>
|
||||||
*
|
*
|
||||||
* This file is part of Mes.
|
* This file is part of Mes.
|
||||||
*
|
*
|
||||||
|
@ -287,11 +288,50 @@ strrchr (char const *s, int c)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** locate a substring. #memmem# finds the first occurrence of
|
||||||
|
#needle# in #haystack#. This is not ANSI-C.
|
||||||
|
|
||||||
|
The prototype is not in accordance with the Linux Programmer's
|
||||||
|
Manual v1.15, but it is with /usr/include/string.h */
|
||||||
|
|
||||||
|
unsigned char *
|
||||||
|
_memmem (unsigned char const *haystack, int haystack_len,
|
||||||
|
unsigned char const *needle, int needle_len)
|
||||||
|
{
|
||||||
|
unsigned char const *end_haystack = haystack + haystack_len - needle_len + 1;
|
||||||
|
unsigned char const *end_needle = needle + needle_len;
|
||||||
|
|
||||||
|
/* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
|
||||||
|
is the spice of life */
|
||||||
|
while (haystack < end_haystack)
|
||||||
|
{
|
||||||
|
unsigned char const *subneedle = needle;
|
||||||
|
unsigned char const *subhaystack = haystack;
|
||||||
|
while (subneedle < end_needle)
|
||||||
|
if (*subneedle++ != *subhaystack++)
|
||||||
|
goto next;
|
||||||
|
|
||||||
|
/* Completed the needle. Gotcha. */
|
||||||
|
return (unsigned char *) haystack;
|
||||||
|
next:
|
||||||
|
haystack++;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
memmem (void const *haystack, int haystack_len,
|
||||||
|
void const *needle, int needle_len)
|
||||||
|
{
|
||||||
|
unsigned char const *haystack_byte_c = (unsigned char const *)haystack;
|
||||||
|
unsigned char const *needle_byte_c = (unsigned char const *)needle;
|
||||||
|
return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
|
||||||
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
strstr (char const *haystack, char const *needle)
|
strstr (char const *haystack, char const *needle)
|
||||||
{
|
{
|
||||||
eputs ("strstr stub\n");
|
return memmem (haystack, strlen (haystack), needle, strlen (needle));
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double
|
double
|
||||||
|
|
Loading…
Reference in a new issue