Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
053f9398
Commit
053f9398
authored
Jan 08, 2011
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
str: strcount
Useful routine to count number of matches in a string.
parent
d4929b8d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
2 deletions
+41
-2
ccan/str/str.c
ccan/str/str.c
+12
-0
ccan/str/str.h
ccan/str/str.h
+12
-0
ccan/str/test/run.c
ccan/str/test/run.c
+16
-2
tools/ccanlint/Makefile
tools/ccanlint/Makefile
+1
-0
No files found.
ccan/str/str.c
0 → 100644
View file @
053f9398
#include <ccan/str/str.h>
size_t
strcount
(
const
char
*
haystack
,
const
char
*
needle
)
{
size_t
i
=
0
,
nlen
=
strlen
(
needle
);
while
((
haystack
=
strstr
(
haystack
,
needle
))
!=
NULL
)
{
i
++
;
haystack
+=
nlen
;
}
return
i
;
}
ccan/str/str.h
View file @
053f9398
...
@@ -55,4 +55,16 @@ static inline bool strends(const char *str, const char *postfix)
...
@@ -55,4 +55,16 @@ static inline bool strends(const char *str, const char *postfix)
#define stringify(expr) stringify_1(expr)
#define stringify(expr) stringify_1(expr)
/* Double-indirection required to stringify expansions */
/* Double-indirection required to stringify expansions */
#define stringify_1(expr) #expr
#define stringify_1(expr) #expr
/**
* strcount - Count number of (non-overlapping) occurrences of a substring.
* @haystack: a C string
* @needle: a substring
*
* Example:
* #define PRINT_COND_IF_FALSE(cond) \
* ((cond) || printf("%s is false!", stringify(cond)))
*/
size_t
strcount
(
const
char
*
haystack
,
const
char
*
needle
);
#endif
/* CCAN_STR_H */
#endif
/* CCAN_STR_H */
ccan/str/test/run.c
View file @
053f9398
#include <ccan/str/str.h>
#include <ccan/str/str.h>
#include <ccan/str/str.c>
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <ccan/tap/tap.h>
#include <ccan/tap/tap.h>
/* FIXME: ccanize */
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
static
char
*
substrings
[]
=
{
"far"
,
"bar"
,
"baz"
,
"b"
,
"ba"
,
"z"
,
"ar"
,
NULL
};
static
char
*
substrings
[]
=
{
"far"
,
"bar"
,
"baz"
,
"b"
,
"ba"
,
"z"
,
"ar"
,
NULL
};
...
@@ -35,7 +35,7 @@ int main(int argc, char *argv[])
...
@@ -35,7 +35,7 @@ int main(int argc, char *argv[])
}
}
}
}
plan_tests
(
n
*
n
*
5
+
3
);
plan_tests
(
n
*
n
*
5
+
16
);
for
(
i
=
0
;
i
<
n
;
i
++
)
{
for
(
i
=
0
;
i
<
n
;
i
++
)
{
for
(
j
=
0
;
j
<
n
;
j
++
)
{
for
(
j
=
0
;
j
<
n
;
j
++
)
{
unsigned
int
k
,
identical
=
0
;
unsigned
int
k
,
identical
=
0
;
...
@@ -87,5 +87,19 @@ int main(int argc, char *argv[])
...
@@ -87,5 +87,19 @@ int main(int argc, char *argv[])
"(sizeof(substrings) / sizeof(substrings[0]))"
));
"(sizeof(substrings) / sizeof(substrings[0]))"
));
ok1
(
streq
(
stringify
(
i
==
0
),
"i == 0"
));
ok1
(
streq
(
stringify
(
i
==
0
),
"i == 0"
));
ok1
(
strcount
(
"aaaaaa"
,
"b"
)
==
0
);
ok1
(
strcount
(
"aaaaaa"
,
"a"
)
==
6
);
ok1
(
strcount
(
"aaaaaa"
,
"aa"
)
==
3
);
ok1
(
strcount
(
"aaaaaa"
,
"aaa"
)
==
2
);
ok1
(
strcount
(
"aaaaaa"
,
"aaaa"
)
==
1
);
ok1
(
strcount
(
"aaaaaa"
,
"aaaaa"
)
==
1
);
ok1
(
strcount
(
"aaaaaa"
,
"aaaaaa"
)
==
1
);
ok1
(
strcount
(
"aaa aaa"
,
"b"
)
==
0
);
ok1
(
strcount
(
"aaa aaa"
,
"a"
)
==
6
);
ok1
(
strcount
(
"aaa aaa"
,
"aa"
)
==
2
);
ok1
(
strcount
(
"aaa aaa"
,
"aaa"
)
==
2
);
ok1
(
strcount
(
"aaa aaa"
,
"aaaa"
)
==
0
);
ok1
(
strcount
(
"aaa aaa"
,
"aaaaa"
)
==
0
);
return
exit_status
();
return
exit_status
();
}
}
tools/ccanlint/Makefile
View file @
053f9398
...
@@ -9,6 +9,7 @@ CORE_OBJS := tools/ccanlint/ccanlint.o \
...
@@ -9,6 +9,7 @@ CORE_OBJS := tools/ccanlint/ccanlint.o \
tools/tools.o
\
tools/tools.o
\
tools/compile.o
\
tools/compile.o
\
ccan/str_talloc/str_talloc.o ccan/grab_file/grab_file.o
\
ccan/str_talloc/str_talloc.o ccan/grab_file/grab_file.o
\
ccan/str/str.o
\
ccan/asort/asort.o
\
ccan/asort/asort.o
\
ccan/btree/btree.o
\
ccan/btree/btree.o
\
ccan/talloc/talloc.o ccan/noerr/noerr.o
\
ccan/talloc/talloc.o ccan/noerr/noerr.o
\
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment