Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
1178f2c1
Commit
1178f2c1
authored
Oct 24, 2014
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Util/Alloc: add xstrcatdup(), replacing g_strconcat()
parent
76f277ee
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
82 additions
and
0 deletions
+82
-0
Alloc.cxx
src/util/Alloc.cxx
+63
-0
Alloc.hxx
src/util/Alloc.hxx
+19
-0
No files found.
src/util/Alloc.cxx
View file @
1178f2c1
...
...
@@ -74,3 +74,66 @@ xstrndup(const char *s, size_t n)
return
p
;
}
template
<
typename
...
Args
>
static
inline
size_t
FillLengths
(
size_t
*
lengths
,
const
char
*
a
,
Args
&&
...
args
)
{
return
FillLengths
(
lengths
,
a
)
+
FillLengths
(
lengths
+
1
,
args
...);
}
template
<>
inline
size_t
FillLengths
(
gcc_unused
size_t
*
lengths
,
const
char
*
a
)
{
return
*
lengths
=
strlen
(
a
);
}
template
<
typename
...
Args
>
static
inline
void
StringCat
(
char
*
p
,
const
size_t
*
lengths
,
const
char
*
a
,
Args
&&
...
args
)
{
StringCat
(
p
,
lengths
,
a
);
StringCat
(
p
+
*
lengths
,
lengths
+
1
,
args
...);
}
template
<>
inline
void
StringCat
(
char
*
p
,
const
size_t
*
lengths
,
const
char
*
a
)
{
memcpy
(
p
,
a
,
*
lengths
);
}
template
<
typename
...
Args
>
gcc_malloc
gcc_nonnull_all
static
inline
char
*
t_xstrcatdup
(
Args
&&
...
args
)
{
constexpr
size_t
n
=
sizeof
...(
args
);
size_t
lengths
[
n
];
const
size_t
total
=
FillLengths
(
lengths
,
args
...);
char
*
p
=
(
char
*
)
xalloc
(
total
+
1
);
StringCat
(
p
,
lengths
,
args
...);
p
[
total
]
=
0
;
return
p
;
}
char
*
xstrcatdup
(
const
char
*
a
,
const
char
*
b
)
{
return
t_xstrcatdup
(
a
,
b
);
}
char
*
xstrcatdup
(
const
char
*
a
,
const
char
*
b
,
const
char
*
c
)
{
return
t_xstrcatdup
(
a
,
b
,
c
);
}
char
*
xstrcatdup
(
const
char
*
a
,
const
char
*
b
,
const
char
*
c
,
const
char
*
d
)
{
return
t_xstrcatdup
(
a
,
b
,
c
,
d
);
}
src/util/Alloc.hxx
View file @
1178f2c1
...
...
@@ -64,4 +64,23 @@ gcc_malloc gcc_nonnull_all
char
*
xstrndup
(
const
char
*
s
,
size_t
n
);
/**
* Concatenate two strings, returning a new allocation. Use free() to
* free it.
*
* This function never fails; in out-of-memory situations, it aborts
* the process.
*/
gcc_malloc
gcc_nonnull_all
char
*
xstrcatdup
(
const
char
*
a
,
const
char
*
b
);
gcc_malloc
gcc_nonnull_all
char
*
xstrcatdup
(
const
char
*
a
,
const
char
*
b
,
const
char
*
c
);
gcc_malloc
gcc_nonnull_all
char
*
xstrcatdup
(
const
char
*
a
,
const
char
*
b
,
const
char
*
c
,
const
char
*
d
);
#endif
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