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
458c48e8
Commit
458c48e8
authored
Jul 28, 2008
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
strsplit()
parent
80cb7a61
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
104 additions
and
5 deletions
+104
-5
ccan/string/_info.c
ccan/string/_info.c
+4
-3
ccan/string/string.c
ccan/string/string.c
+33
-0
ccan/string/string.h
ccan/string/string.h
+33
-0
ccan/string/test/run.c
ccan/string/test/run.c
+34
-2
No files found.
ccan/string/_info.c
View file @
458c48e8
...
...
@@ -5,7 +5,7 @@
/**
* string - string helper routines
*
* This is a grab bag of modules for string
comparis
ons, designed to enhance
* This is a grab bag of modules for string
operati
ons, designed to enhance
* the standard string.h.
*
* Example:
...
...
@@ -27,9 +27,10 @@ int main(int argc, char *argv[])
if
(
argc
!=
2
)
return
1
;
if
(
strcmp
(
argv
[
1
],
"depends"
)
==
0
)
/* Nothing. */
if
(
strcmp
(
argv
[
1
],
"depends"
)
==
0
)
{
printf
(
"ccan/talloc
\n
"
);
return
0
;
}
return
1
;
}
ccan/string/string.c
0 → 100644
View file @
458c48e8
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <stdlib.h>
#include "string.h"
#include "talloc/talloc.h"
char
**
strsplit
(
const
void
*
ctx
,
const
char
*
string
,
const
char
*
delims
,
unsigned
int
*
nump
)
{
char
**
lines
=
NULL
;
unsigned
int
max
=
64
,
num
=
0
;
lines
=
talloc_array
(
ctx
,
char
*
,
max
+
1
);
while
(
*
string
!=
'\0'
)
{
unsigned
int
len
=
strcspn
(
string
,
delims
);
lines
[
num
]
=
talloc_array
(
lines
,
char
,
len
+
1
);
memcpy
(
lines
[
num
],
string
,
len
);
lines
[
num
][
len
]
=
'\0'
;
string
+=
len
;
string
+=
strspn
(
string
,
delims
)
?
1
:
0
;
if
(
++
num
==
max
)
lines
=
talloc_realloc
(
ctx
,
lines
,
char
*
,
max
*=
2
+
1
);
}
lines
[
num
]
=
NULL
;
if
(
nump
)
*
nump
=
num
;
return
lines
;
}
ccan/string/string.h
View file @
458c48e8
...
...
@@ -43,4 +43,37 @@ static inline bool strends(const char *str, const char *postfix)
return
streq
(
str
+
strlen
(
str
)
-
strlen
(
postfix
),
postfix
);
}
/**
* strsplit - Split string into an array of substrings
* @ctx: the context to tallocate from (often NULL)
* @string: the string to split
* @delims: delimiters where lines should be split.
* @nump: optional pointer to place resulting number of lines
*
* 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.
*
* The final char * in the array will be NULL, so you can use this or
* @nump to find the array length.
*
* Example:
* unsigned int count_long_lines(const char *text)
* {
* char **lines;
* unsigned int i, long_lines = 0;
*
* // Can only fail on out-of-memory.
* lines = strsplit(NULL, string, "\n", NULL);
* for (i = 0; lines[i] != NULL; i++)
* if (strlen(lines[i]) > 80)
* long_lines++;
* talloc_free(lines);
* return long_lines;
* }
*/
char
**
strsplit
(
const
void
*
ctx
,
const
char
*
string
,
const
char
*
delims
,
unsigned
int
*
nump
);
#endif
/* CCAN_STRING_H */
ccan/string/test/run.c
View file @
458c48e8
#include "string/string.h"
#include <stdlib.h>
#include <stdio.h>
#include "string/string.
h
"
#include "string/string.
c
"
#include "tap/tap.h"
/* FIXME: ccanize */
...
...
@@ -21,6 +22,8 @@ static char *strdup_rev(const char *s)
int
main
(
int
argc
,
char
*
argv
[])
{
unsigned
int
i
,
j
,
n
;
char
**
split
;
void
*
ctx
;
char
*
strings
[
ARRAY_SIZE
(
substrings
)
*
ARRAY_SIZE
(
substrings
)];
n
=
0
;
...
...
@@ -33,7 +36,7 @@ int main(int argc, char *argv[])
}
}
plan_tests
(
n
*
n
*
5
);
plan_tests
(
n
*
n
*
5
+
16
);
for
(
i
=
0
;
i
<
n
;
i
++
)
{
for
(
j
=
0
;
j
<
n
;
j
++
)
{
unsigned
int
k
,
identical
=
0
;
...
...
@@ -73,5 +76,34 @@ int main(int argc, char *argv[])
}
}
}
split
=
strsplit
(
NULL
,
"hello world"
,
" "
,
&
n
);
ok1
(
n
==
3
);
ok1
(
streq
(
split
[
0
],
"hello"
));
ok1
(
streq
(
split
[
1
],
""
));
ok1
(
streq
(
split
[
2
],
"world"
));
ok1
(
split
[
3
]
==
NULL
);
talloc_free
(
split
);
split
=
strsplit
(
NULL
,
"hello world"
,
" "
,
NULL
);
ok1
(
streq
(
split
[
0
],
"hello"
));
ok1
(
streq
(
split
[
1
],
""
));
ok1
(
streq
(
split
[
2
],
"world"
));
ok1
(
split
[
3
]
==
NULL
);
talloc_free
(
split
);
split
=
strsplit
(
NULL
,
"hello world"
,
"o "
,
NULL
);
ok1
(
streq
(
split
[
0
],
"hell"
));
ok1
(
streq
(
split
[
1
],
""
));
ok1
(
streq
(
split
[
2
],
""
));
ok1
(
streq
(
split
[
3
],
"w"
));
ok1
(
streq
(
split
[
4
],
"rld"
));
ok1
(
split
[
5
]
==
NULL
);
ctx
=
split
;
split
=
strsplit
(
ctx
,
"hello world"
,
"o "
,
NULL
);
ok1
(
talloc_parent
(
split
)
==
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