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:
Jan Nieuwenhuizen 2018-05-29 19:35:20 +02:00
parent 26e1876d12
commit 9f90960392
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273
2 changed files with 44 additions and 3 deletions

View file

@ -5,7 +5,8 @@ All files except the imported files listed below
Jeremiah Orians <jeremiah@pdp10.guru>
lib/libc.c (fopen)
Han-Wen Nienhuys <hanwen@xs4all.nl>
lib/libc+tcc.c (_memmem, memmem)
List of imported files

View file

@ -1,6 +1,7 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
* Copyright (C) 2018 Han-Wen Nienhuys <hanwen@xs4all.nl>
*
* This file is part of Mes.
*
@ -287,11 +288,50 @@ strrchr (char const *s, int c)
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 *
strstr (char const *haystack, char const *needle)
{
eputs ("strstr stub\n");
return 0;
return memmem (haystack, strlen (haystack), needle, strlen (needle));
}
double