From 5670964e7814753a1aa70629e4ab3690b699dddf Mon Sep 17 00:00:00 2001 From: Mattia Cabrini Date: Tue, 9 Sep 2025 15:12:02 +0200 Subject: [PATCH] Init --- LICENSE | 21 +++++++++ Makefile | 24 ++++++++++ README.MD | 7 +++ cmc-find.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.MD create mode 100644 cmc-find.c diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..df2e18d --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Mattia Cabrini + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..932d77d --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +CFLAGS=-Wall -Werror -Wno-unused-function +CFLAGS_REL=$(CFLSGS) -O2 +CFLAGS_DEB=$(CFLAGS) -g -O0 + +SRC=cmc-find.c +BIN=cmc-find + +INDEND_FLAGS=-nbad -bap -nbc -br -brs -c33 -cd33 -ncdb -ce -ci4 \ + -cli0 -d0 -di1 -nfc1 -i8 -ip0 -l80 -lp -npcs -npsl \ + -ncs -nsc -sob -ts8 + +all: fmt release + +release: + gcc -o $(BIN) $(SRC) $(CFLAGS_REL) + +debug: + gcc -o $(BIN) $(SRC) $(CFLAGS_DEB) + +fmt: + indent $(INDEND_FLAGS) $(SRC) + +run: # on Unix-like systems... + cat /dev/urandom | ./find 1606 diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..481cbe2 --- /dev/null +++ b/README.MD @@ -0,0 +1,7 @@ +# CMC-Find + +Find the position(s) of a string in a stream. + +## License +This program is licensed under the MIT license. +See the file LICENSE. diff --git a/cmc-find.c b/cmc-find.c new file mode 100644 index 0000000..78a8372 --- /dev/null +++ b/cmc-find.c @@ -0,0 +1,131 @@ +/* Copyright (c) Mattia Cabrini */ +/* SPDX-License-Identifier: MIT */ + +#include +#include +#include +#include + +#ifndef MAX_LEN +#define MAX_LEN 64 +#endif + +typedef struct cs_t { + char buf[MAX_LEN]; + + size_t cap /* max string size; <= MAX_LEN */ ; + size_t len /* number of chars actually set */ ; + size_t start; /* index of firs char */ +} *cs; + +static void cs_init(cs S, size_t cap); +static void cs_add(cs S, char ch); +static int cs_cmp(cs S, char *ch); +static void cs_print(cs S); + +int main(int argc, char **argv) +{ + int ch = 0; + uint64_t n; + + char str[MAX_LEN]; + struct cs_t matcher; + + if (argc < 2) { + printf("Usage: %s \n", argv[0]); + exit(1); + } + strncpy(str, argv[1], sizeof(str)); + printf("Check against %s\n", str); + + cs_init(&matcher, strlen(str)); + + for (n = 1; (ch = fgetc(stdin)) != EOF; ++n) { + cs_add(&matcher, ch); + + if (matcher.len == matcher.cap) { + if (cs_cmp(&matcher, str)) { + printf("Found at %llu\n", n - matcher.cap); + /* cs_print(&matcher); printf("\n"); */ + } + } + } + + return 0; +} + +static void cs_init(cs S, size_t cap) +{ + S->cap = cap; + S->len = 0; + S->start = 0; +} + +static void cs_add(cs S, char ch) +{ + if (S->len < S->cap) { + S->buf[S->len] = ch; + ++S->len; + } else { + S->buf[S->start] = ch; + ++S->start; + if (S->start == S->cap) + S->start = 0; + } +} + +static int cs_cmp(cs S, char *ch) +{ + size_t i; + size_t count; + + i = S->start; + count = 0; + + while (count < S->len && *ch) { + if (*ch != S->buf[i]) + return 0; + + ++i; + if (i == S->cap) + i = 0; + ++count; + ++ch; + } + +/* + count == len | *ch == 0 | Ret + 1 | 1 | 1 + 1 | 0 | 0 + 0 | 1 | 0 + 0 | 0 | IMP. + + + if (count == S->len && *ch) + return 0; + if (count < S->len && *ch == '\0') + return 0; + + return 1; +*/ + return count == S->len && *ch == '\0'; +} + +static void cs_print(cs S) +{ + size_t i; + size_t count; + + i = S->start; + count = 0; + + while (count < S->len) { + putc(S->buf[i], stdout); + + ++i; + if (i == S->cap) + i = 0; + + ++count; + } +} -- 2.43.0