GRPC Core  0.11.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
string.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_INTERNAL_CORE_SUPPORT_STRING_H
35 #define GRPC_INTERNAL_CORE_SUPPORT_STRING_H
36 
37 #include <stddef.h>
38 
41 #include <grpc/support/slice.h>
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /* String utility functions */
48 
49 /* Flags for gpr_dump function. */
50 #define GPR_DUMP_HEX 0x00000001
51 #define GPR_DUMP_ASCII 0x00000002
52 
53 /* Converts array buf, of length len, into a C string according to the flags.
54  Result should be freed with gpr_free() */
55 char *gpr_dump(const char *buf, size_t len, gpr_uint32 flags);
56 
57 /* Calls gpr_dump on a slice. */
58 char *gpr_dump_slice(gpr_slice slice, gpr_uint32 flags);
59 
60 /* Parses an array of bytes into an integer (base 10). Returns 1 on success,
61  0 on failure. */
62 int gpr_parse_bytes_to_uint32(const char *data, size_t length,
63  gpr_uint32 *result);
64 
65 /* Minimum buffer size for calling ltoa */
66 #define GPR_LTOA_MIN_BUFSIZE (3 * sizeof(long))
67 
68 /* Convert a long to a string in base 10; returns the length of the
69  output string (or 0 on failure).
70  output must be at least GPR_LTOA_MIN_BUFSIZE bytes long. */
71 int gpr_ltoa(long value, char *output);
72 
73 /* Reverse a run of bytes */
74 void gpr_reverse_bytes(char *str, int len);
75 
76 /* Join a set of strings, returning the resulting string.
77  Total combined length (excluding null terminator) is returned in total_length
78  if it is non-null. */
79 char *gpr_strjoin(const char **strs, size_t nstrs, size_t *total_length);
80 
81 /* Join a set of strings using a separator, returning the resulting string.
82  Total combined length (excluding null terminator) is returned in total_length
83  if it is non-null. */
84 char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep,
85  size_t *total_length);
86 
89 void gpr_slice_split(gpr_slice str, const char *sep, gpr_slice_buffer *dst);
90 
91 /* A vector of strings... for building up a final string one piece at a time */
92 typedef struct {
93  char **strs;
94  size_t count;
95  size_t capacity;
96 } gpr_strvec;
97 
98 /* Initialize/destroy */
99 void gpr_strvec_init(gpr_strvec *strs);
100 void gpr_strvec_destroy(gpr_strvec *strs);
101 /* Add a string to a strvec, takes ownership of the string */
102 void gpr_strvec_add(gpr_strvec *strs, char *add);
103 /* Return a joined string with all added substrings, optionally setting
104  total_length as per gpr_strjoin */
105 char *gpr_strvec_flatten(gpr_strvec *strs, size_t *total_length);
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif /* GRPC_INTERNAL_CORE_SUPPORT_STRING_H */
const char * value
Definition: hpack_table.c:44
char * gpr_strvec_flatten(gpr_strvec *strs, size_t *total_length)
Definition: string.c:273
int gpr_parse_bytes_to_uint32(const char *data, size_t length, gpr_uint32 *result)
Definition: string.c:128
char * gpr_strjoin(const char **strs, size_t nstrs, size_t *total_length)
Definition: string.c:176
uint32_t gpr_uint32
Definition: port_platform.h:312
char * gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep, size_t *total_length)
Definition: string.c:180
char * gpr_dump_slice(gpr_slice slice, gpr_uint32 flags)
Definition: string.c:123
Definition: slice_buffer.h:48
char ** strs
Definition: string.h:93
Definition: string.h:92
void gpr_reverse_bytes(char *str, int len)
Definition: string.c:146
void gpr_strvec_add(gpr_strvec *strs, char *add)
Definition: string.c:265
void gpr_strvec_init(gpr_strvec *strs)
Definition: string.c:255
void gpr_strvec_destroy(gpr_strvec *strs)
Definition: string.c:257
void gpr_slice_split(gpr_slice str, const char *sep, gpr_slice_buffer *dst)
Split str by the separator sep.
Definition: string.c:237
int gpr_ltoa(long value, char *output)
Definition: string.c:155
size_t count
Definition: string.h:94
char * gpr_dump(const char *buf, size_t len, gpr_uint32 flags)
Definition: string.c:111
Definition: slice.h:79
size_t capacity
Definition: string.h:95