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
e2da7d35
Commit
e2da7d35
authored
Jul 28, 2008
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
strjoin()
parent
779d8308
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
8 deletions
+57
-8
ccan/string/string.c
ccan/string/string.c
+12
-1
ccan/string/string.h
ccan/string/string.h
+27
-1
ccan/string/test/run.c
ccan/string/test/run.c
+18
-6
No files found.
ccan/string/string.c
View file @
e2da7d35
...
...
@@ -30,4 +30,15 @@ char **strsplit(const void *ctx, const char *string, const char *delims,
*
nump
=
num
;
return
lines
;
}
char
*
strjoin
(
const
void
*
ctx
,
char
*
strings
[],
const
char
*
delim
)
{
unsigned
int
i
;
char
*
ret
=
talloc_strdup
(
ctx
,
""
);
for
(
i
=
0
;
strings
[
i
];
i
++
)
{
ret
=
talloc_append_string
(
ret
,
strings
[
i
]);
ret
=
talloc_append_string
(
ret
,
delim
);
}
return
ret
;
}
ccan/string/string.h
View file @
e2da7d35
...
...
@@ -54,7 +54,8 @@ static inline bool strends(const char *str, const char *postfix)
* This function splits a single string into multiple strings. The
* original string is untouched: an array is allocated (using talloc)
* pointing to copies of each substring. Multiple delimiters result
* in empty substrings.
* in empty substrings. By definition, no delimiters will appear in
* the substrings.
*
* The final char * in the array will be NULL, so you can use this or
* @nump to find the array length.
...
...
@@ -76,4 +77,29 @@ static inline bool strends(const char *str, const char *postfix)
*/
char
**
strsplit
(
const
void
*
ctx
,
const
char
*
string
,
const
char
*
delims
,
unsigned
int
*
nump
);
/**
* strjoin - Join an array of substrings into one long string
* @ctx: the context to tallocate from (often NULL)
* @strings: the NULL-terminated array of strings to join
* @delim: the delimiter to insert between the strings
*
* This function joins an array of strings into a single string. The
* return value is allocated using talloc. Each string in @strings is
* followed by a copy of @delim.
*
* Example:
* // Append the string "--EOL" to each line.
* char *append_to_all_lines(const char *string)
* {
* char **lines, *ret;
* unsigned int i, num, newnum;
*
* lines = strsplit(NULL, string, "\n", NULL);
* ret = strjoin(NULL, lines, "-- EOL\n");
* talloc_free(lines);
* return ret;
* }
*/
char
*
strjoin
(
const
void
*
ctx
,
char
*
strings
[],
const
char
*
delim
);
#endif
/* CCAN_STRING_H */
ccan/string/test/run.c
View file @
e2da7d35
...
...
@@ -7,7 +7,9 @@
/* FIXME: ccanize */
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
static
char
*
substrings
[]
=
{
"far"
,
"bar"
,
"baz"
,
"b"
,
"ba"
,
"z"
,
"ar"
};
static
char
*
substrings
[]
=
{
"far"
,
"bar"
,
"baz"
,
"b"
,
"ba"
,
"z"
,
"ar"
,
NULL
};
#define NUM_SUBSTRINGS (ARRAY_SIZE(substrings) - 1)
static
char
*
strdup_rev
(
const
char
*
s
)
{
...
...
@@ -22,13 +24,13 @@ static char *strdup_rev(const char *s)
int
main
(
int
argc
,
char
*
argv
[])
{
unsigned
int
i
,
j
,
n
;
char
**
split
;
char
**
split
,
*
str
;
void
*
ctx
;
char
*
strings
[
ARRAY_SIZE
(
substrings
)
*
ARRAY_SIZE
(
substrings
)
];
char
*
strings
[
NUM_SUBSTRINGS
*
NUM_SUBSTRINGS
];
n
=
0
;
for
(
i
=
0
;
i
<
ARRAY_SIZE
(
substrings
)
;
i
++
)
{
for
(
j
=
0
;
j
<
ARRAY_SIZE
(
substrings
)
;
j
++
)
{
for
(
i
=
0
;
i
<
NUM_SUBSTRINGS
;
i
++
)
{
for
(
j
=
0
;
j
<
NUM_SUBSTRINGS
;
j
++
)
{
strings
[
n
]
=
malloc
(
strlen
(
substrings
[
i
])
+
strlen
(
substrings
[
j
])
+
1
);
sprintf
(
strings
[
n
++
],
"%s%s"
,
...
...
@@ -36,7 +38,7 @@ int main(int argc, char *argv[])
}
}
plan_tests
(
n
*
n
*
5
+
1
6
);
plan_tests
(
n
*
n
*
5
+
1
9
);
for
(
i
=
0
;
i
<
n
;
i
++
)
{
for
(
j
=
0
;
j
<
n
;
j
++
)
{
unsigned
int
k
,
identical
=
0
;
...
...
@@ -104,6 +106,16 @@ int main(int argc, char *argv[])
split
=
strsplit
(
ctx
,
"hello world"
,
"o "
,
NULL
);
ok1
(
talloc_parent
(
split
)
==
ctx
);
talloc_free
(
ctx
);
str
=
strjoin
(
NULL
,
substrings
,
", "
);
ok1
(
streq
(
str
,
"far, bar, baz, b, ba, z, ar, "
));
ctx
=
str
;
str
=
strjoin
(
ctx
,
substrings
,
""
);
ok1
(
streq
(
str
,
"farbarbazbbazar"
));
ok1
(
talloc_parent
(
str
)
==
ctx
);
talloc_free
(
ctx
);
return
exit_status
();
}
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