Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract counting commas/characters in a string #1094

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ libshadow_la_SOURCES = \
string/sprintf/stpeprintf.h \
string/sprintf/xasprintf.c \
string/sprintf/xasprintf.h \
string/strchr/strchrcnt.c \
string/strchr/strchrcnt.h \
string/strchr/stpspn.c \
string/strchr/stpspn.h \
string/strchr/strnul.c \
Expand Down
26 changes: 5 additions & 21 deletions lib/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "alloc/x/xmalloc.h"
#include "prototypes.h"
#include "defines.h"
#include "string/strchr/strchrcnt.h"
#include "string/strdup/xstrdup.h"


Expand Down Expand Up @@ -179,7 +180,8 @@ bool is_on_list (char *const *list, const char *member)
* comma_to_list - convert comma-separated list to (char *) array
*/

/*@only@*/char **comma_to_list (const char *comma)
/*@only@*/char **
comma_to_list(const char *comma)
{
char *members;
char **array;
Expand All @@ -195,30 +197,12 @@ bool is_on_list (char *const *list, const char *member)

members = xstrdup (comma);

/*
* Count the number of commas in the list
*/

for (cp = members, i = 0;; i++) {
cp2 = strchr (cp, ',');
if (NULL != cp2) {
cp = cp2 + 1;
} else {
break;
}
}

/*
* Add 2 - one for the ending NULL, the other for the last item
*/

i += 2;

/*
* Allocate the array we're going to store the pointers into.
* n: number of delimiters + last element + NULL
*/

array = XMALLOC(i, char *);
array = XMALLOC(strchrcnt(members, ',') + 2, char *);

/*
* Empty list is special - 0 members, not 1 empty member. --marekm
Expand Down
12 changes: 12 additions & 0 deletions lib/string/strchr/strchrcnt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

#include "string/strchr/strchrcnt.h"

#include <stddef.h>


extern inline size_t strchrcnt(const char *s, char c);
35 changes: 35 additions & 0 deletions lib/string/strchr/strchrcnt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STRCHRCNT_H_
#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STRCHRCNT_H_


#include <config.h>

#include <stddef.h>

#include "attr.h"
#include "string/strchr/strchrcnt.h"


ATTR_STRING(1)
inline size_t strchrcnt(const char *s, char c);


inline size_t
strchrcnt(const char *s, char c)
{
size_t n;

for (n = 0; *s != '\0'; s++) {
if (*s == c)
n++;
}

return n;
}


#endif // include guard
Loading