STRGSUB(3pub)
π STRGSUB(3pub) π
okay bestie, listen up β this is a page from the C Programmer's Manual, and it's called strgsub, and it's lowkey one of the most useful little guys in the whole publib friend group. no cap. let's get into it π«Ά
β¨ NAME β¨
strgsub β substitute all occurrences of pattern with another string
like, all of them. every single one. she does not miss π
πΈ SYNOPSIS πΈ
#include <publib.h>
int strgsub(char *str, const char *pat, const char *sub, size_t max);
π¦ DESCRIPTION π¦
so here's the tea, bestie π
strgsub goes through the string str and finds every occurrence of the pattern pat, then replaces each one with sub. the way she finds them is with a method similar to strstr(3) β meaning no regular expressions, bestie, none, zero, she's not that kind of girl, this is plain substring matching and that's her whole vibe. β¨
if pat doesn't occur in str at all? then no substitution is made. nothing happens. she simply looks, sees nothing, and moves on with her life π
now for the part you actually need to pay attention to: the size of the string after the substitutions β and yes that size includes the terminating '\0' β may be at most max chars. if the result would be bigger than that, then no substitutions are made at all. not some of them. not a partial job. none. it's giving all-or-nothing and honestly? iconic of her.
and of course, if sub is an empty string, then the occurrences of the pattern get deleted from the string. poof. gone. π§
π RETURN VALUE π
strgsub returns the number of substitutions made π β so if she swapped three things, you get a 3.
but if the result would not have fit into max chars, she returns -1. that's your sign that nothing got substituted, bestie, don't ignore it. π
π EXAMPLE π
so say you wanna substitute every "foo" with "bar" in a line. one might do the following:
strgsub(line, "foo", "bar", strlen(line)+1);
look at that strlen(line)+1 β that's the +1 for the terminating '\0', because we're counting that too. slay. β
π₯ SEE ALSO π₯
iykyk π
π AUTHOR π
Lars Wirzenius (lars.wirzenius@helsinki.fi)
π COPYRIGHT & LICENSE π
okay so legal era, but it's important, so we're doing it properly, no skipping π«Ά
this is part of publib.
Copyright (c) 1994β2006 Lars Wirzenius. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
and that's the whole page, bestie!! π this cutie lives in the C Programmer's Manual as part of Publib (copyright 1994β2006, Lars Wirzenius), filed under STRGSUB(3pub) β now go forth and substitute responsibly, mwah πΈπ
β¨
π¦ where this page comes from
- debian bookworm: publib-dev 0.40-3+b1 Β· BSD-2-Clause
- debian trixie: publib-dev 0.40-4 Β· BSD-2-Clause
- ubuntu jammy: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu noble: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu questing: publib-dev 0.40-4
- ubuntu resolute: publib-dev 0.40-4build1 Β· BSD-2-Clause
- ubuntu stonking: publib-dev 0.40-4build1 Β· BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms