From 502f1db5c4eb02b85b05c76ba9c9564269763dd1 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 17 Nov 2015 04:07:09 +0100 Subject: add strset MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- include/string.h | 13 +++++++++++++ src/string/strset.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/string/strset.c diff --git a/include/string.h b/include/string.h index b3b3e10..b38e849 100644 --- a/include/string.h +++ b/include/string.h @@ -254,6 +254,19 @@ void* memccpy(void* restrict, const void* restrict, int, size_t); * one character passed the last written character. */ void* memcmove(void*, const void*, int, size_t); + +/** + * Override a NUL byte-terminated memory segment + * with a repeated character. + * + * This is a slibc extension. + * + * @param str The beginning of the memory segment. + * @param c The character (8 bits wide.) + * @return `str` is returned. + */ +char* strset(char* str, int c); + __GCC_ONLY(__attribute__((__returns_nonnull__, __nonnull__))); #endif /** diff --git a/src/string/strset.c b/src/string/strset.c new file mode 100644 index 0000000..c1eca65 --- /dev/null +++ b/src/string/strset.c @@ -0,0 +1,39 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include + + + +/** + * Override a NUL byte-terminated memory segment + * with a repeated character. + * + * This is a slibc extension. + * + * @param str The beginning of the memory segment. + * @param c The character (8 bits wide.) + * @return `str` is returned. + */ +char* strset(char* str, int c) +{ + char* s = segment; + while (*s) + *s++ = (char)c; + return segment; +} + -- cgit v1.2.3-70-g09d2