summaryrefslogtreecommitdiffstats
path: root/src/raptor_memstr.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 05:40:05 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 05:40:05 +0000
commit4038ab95a094b363f1748f3dcb51511a1217475d (patch)
tree7f393d66a783f91ddd263c78d681e485cf4f45ca /src/raptor_memstr.c
parentInitial commit. (diff)
downloadraptor2-4038ab95a094b363f1748f3dcb51511a1217475d.tar.xz
raptor2-4038ab95a094b363f1748f3dcb51511a1217475d.zip
Adding upstream version 2.0.16.upstream/2.0.16upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/raptor_memstr.c')
-rw-r--r--src/raptor_memstr.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/raptor_memstr.c b/src/raptor_memstr.c
new file mode 100644
index 0000000..2168e91
--- /dev/null
+++ b/src/raptor_memstr.c
@@ -0,0 +1,76 @@
+/* -*- Mode: c; c-basic-offset: 2 -*-
+ *
+ * raptor_memstr.c - search for a string in a block of memory
+ *
+ * Copyright (C) 2008, David Beckett http://www.dajobe.org/
+ *
+ * This package is Free Software and part of Redland http://librdf.org/
+ *
+ * It is licensed under the following three licenses as alternatives:
+ * 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
+ * 2. GNU General Public License (GPL) V2 or any newer version
+ * 3. Apache License, V2.0 or any newer version
+ *
+ * You may not use this file except in compliance with at least one of
+ * the above three licenses.
+ *
+ * See LICENSE.html or LICENSE.txt at the top of this package for the
+ * complete terms and further detail along with the license texts for
+ * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
+ *
+ *
+ */
+
+
+#ifdef HAVE_CONFIG_H
+#include <raptor_config.h>
+#endif
+
+#include <string.h>
+
+/* Raptor includes */
+#include "raptor2.h"
+#include "raptor_internal.h"
+
+
+/*
+ * raptor_memstr:
+ * @haystack: memory block to search in
+ * @haystack_len: size of memory block
+ * @needle: string to search with
+ *
+ * INTERNAL: Search for a string in a block of memory
+ *
+ * The block of memory in @haystack may not be NUL terminated but
+ * the searching for @needle will end if a NUL is found in @haystack.
+ *
+ * Return value: pointer to match string or NULL on failure or failed to find
+ */
+const char*
+raptor_memstr(const char *haystack, size_t haystack_len, const char *needle)
+{
+ size_t needle_len;
+ const char *p;
+
+ if(!haystack || !needle)
+ return NULL;
+
+ if(!*needle)
+ return haystack;
+
+ needle_len = strlen(needle);
+
+ /* loop invariant: haystack_len is always length of remaining buffer at *p */
+ for(p = haystack;
+ (haystack_len >= needle_len) && *p;
+ p++, haystack_len--) {
+
+ /* check match */
+ if(!memcmp(p, needle, needle_len))
+ return p;
+ }
+
+ return NULL;
+}
+
+