Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Grpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tci-gateway-module
Grpc
Commits
abc09d8b
Commit
abc09d8b
authored
Jun 24, 2015
by
Julien Boeuf
Browse files
Options
Downloads
Patches
Plain Diff
Adding util to get a NULL terminated string from a slice.
parent
d9145d91
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
include/grpc/support/slice.h
+4
-0
4 additions, 0 deletions
include/grpc/support/slice.h
src/core/support/slice.c
+7
-0
7 additions, 0 deletions
src/core/support/slice.c
test/core/support/slice_test.c
+29
-0
29 additions, 0 deletions
test/core/support/slice_test.c
with
40 additions
and
0 deletions
include/grpc/support/slice.h
+
4
−
0
View file @
abc09d8b
...
...
@@ -172,6 +172,10 @@ gpr_slice gpr_empty_slice(void);
int
gpr_slice_cmp
(
gpr_slice
a
,
gpr_slice
b
);
int
gpr_slice_str_cmp
(
gpr_slice
a
,
const
char
*
b
);
/* Returns a null terminated C string from a slice. It is the responsibility of
the caller to call gpr_free on the result. */
char
*
gpr_slice_to_cstring
(
gpr_slice
s
);
#ifdef __cplusplus
}
#endif
...
...
This diff is collapsed.
Click to expand it.
src/core/support/slice.c
+
7
−
0
View file @
abc09d8b
...
...
@@ -325,3 +325,10 @@ int gpr_slice_str_cmp(gpr_slice a, const char *b) {
if
(
d
!=
0
)
return
d
;
return
memcmp
(
GPR_SLICE_START_PTR
(
a
),
b
,
b_length
);
}
char
*
gpr_slice_to_cstring
(
gpr_slice
slice
)
{
char
*
result
=
gpr_malloc
(
GPR_SLICE_LENGTH
(
slice
)
+
1
);
memcpy
(
result
,
GPR_SLICE_START_PTR
(
slice
),
GPR_SLICE_LENGTH
(
slice
));
result
[
GPR_SLICE_LENGTH
(
slice
)]
=
'\0'
;
return
result
;
}
This diff is collapsed.
Click to expand it.
test/core/support/slice_test.c
+
29
−
0
View file @
abc09d8b
...
...
@@ -35,6 +35,7 @@
#include
<string.h>
#include
<grpc/support/alloc.h>
#include
<grpc/support/log.h>
#include
"test/core/util/test_config.h"
...
...
@@ -211,6 +212,33 @@ static void test_slice_from_copied_string_works(void) {
gpr_slice_unref
(
slice
);
}
static
void
test_slice_to_cstring_works
(
void
)
{
static
const
char
*
text
=
"HELLO WORLD!"
;
static
const
char
*
long_text
=
"It was a bright cold day in April, and the clocks were striking "
"thirteen. Winston Smith, his chin nuzzled into his breast in an effort "
"to escape the vile wind, slipped quickly through the glass doors of "
"Victory Mansions, though not quickly enough to prevent a swirl of "
"gritty dust from entering along with him."
;
gpr_slice
slice
;
char
*
text2
;
char
*
long_text2
;
LOG_TEST_NAME
(
"test_slice_to_cstring_works"
);
slice
=
gpr_slice_from_copied_string
(
text
);
text2
=
gpr_slice_to_cstring
(
slice
);
GPR_ASSERT
(
strcmp
(
text
,
text2
)
==
0
);
gpr_free
(
text2
);
gpr_slice_unref
(
slice
);
slice
=
gpr_slice_from_copied_string
(
long_text
);
long_text2
=
gpr_slice_to_cstring
(
slice
);
GPR_ASSERT
(
strcmp
(
long_text
,
long_text2
)
==
0
);
gpr_free
(
long_text2
);
gpr_slice_unref
(
slice
);
}
int
main
(
int
argc
,
char
**
argv
)
{
unsigned
length
;
grpc_test_init
(
argc
,
argv
);
...
...
@@ -223,5 +251,6 @@ int main(int argc, char **argv) {
test_slice_split_tail_works
(
length
);
}
test_slice_from_copied_string_works
();
test_slice_to_cstring_works
();
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment