GRPC Core  9.0.0
string.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_GPR_STRING_H
20 #define GRPC_CORE_LIB_GPR_STRING_H
21 
23 
25 
26 #include <stdbool.h>
27 #include <stddef.h>
28 
29 /* String utility functions */
30 
31 /* Flags for gpr_dump function. */
32 #define GPR_DUMP_HEX 0x00000001
33 #define GPR_DUMP_ASCII 0x00000002
34 
35 /* Converts array buf, of length len, into a C string according to the flags.
36  Result should be freed with gpr_free() */
37 char* gpr_dump(const char* buf, size_t len, uint32_t flags);
38 /* Converts array buf, of length len, into a C string according to the flags.
39  The length of the returned buffer is stored in out_len.
40  Result should be freed with gpr_free() */
41 char* gpr_dump_return_len(const char* buf, size_t len, uint32_t flags,
42  size_t* out_len);
43 
44 /* Parses an array of bytes into an integer (base 10). Returns 1 on success,
45  0 on failure. */
46 int gpr_parse_bytes_to_uint32(const char* data, size_t length,
47  uint32_t* result);
48 
49 /* Minimum buffer size for calling ltoa */
50 #define GPR_LTOA_MIN_BUFSIZE (3 * sizeof(long))
51 
52 /* Convert a long to a string in base 10; returns the length of the
53  output string (or 0 on failure).
54  output must be at least GPR_LTOA_MIN_BUFSIZE bytes long. */
55 int gpr_ltoa(long value, char* output);
56 
57 /* Minimum buffer size for calling int64toa */
58 #define GPR_INT64TOA_MIN_BUFSIZE (3 * sizeof(int64_t))
59 
60 /* Convert an int64 to a string in base 10; returns the length of the
61 output string (or 0 on failure).
62 output must be at least GPR_INT64TOA_MIN_BUFSIZE bytes long.
63 NOTE: This function ensures sufficient bit width even on Win x64,
64 where long is 32bit is size.*/
65 int int64_ttoa(int64_t value, char* output);
66 
67 // Parses a non-negative number from a value string. Returns -1 on error.
68 int gpr_parse_nonnegative_int(const char* value);
69 
70 /* Reverse a run of bytes */
71 void gpr_reverse_bytes(char* str, int len);
72 
73 /* Pad a string with flag characters. The given length specifies the minimum
74  field width. The input string is never truncated. */
75 char* gpr_leftpad(const char* str, char flag, size_t length);
76 
77 /* Join a set of strings, returning the resulting string.
78  Total combined length (excluding null terminator) is returned in total_length
79  if it is non-null. */
80 char* gpr_strjoin(const char** strs, size_t nstrs, size_t* total_length);
81 
82 /* Join a set of strings using a separator, returning the resulting string.
83  Total combined length (excluding null terminator) is returned in total_length
84  if it is non-null. */
85 char* gpr_strjoin_sep(const char** strs, size_t nstrs, const char* sep,
86  size_t* total_length);
87 
88 void gpr_string_split(const char* input, const char* sep, char*** strs,
89  size_t* nstrs);
90 
91 /* Returns an allocated string that represents tm according to RFC-3339, and,
92  more specifically, follows:
93  https://developers.google.com/protocol-buffers/docs/proto3#json
94 
95  Uses RFC 3339, where generated output will always be Z-normalized and uses
96  0, 3, 6 or 9 fractional digits. */
98 
99 /* A vector of strings... for building up a final string one piece at a time */
100 typedef struct {
101  char** strs;
102  size_t count;
103  size_t capacity;
104 } gpr_strvec;
105 
106 /* Initialize/destroy */
107 void gpr_strvec_init(gpr_strvec* strs);
108 void gpr_strvec_destroy(gpr_strvec* strs);
109 /* Add a string to a strvec, takes ownership of the string */
110 void gpr_strvec_add(gpr_strvec* strs, char* add);
111 /* Return a joined string with all added substrings, optionally setting
112  total_length as per gpr_strjoin */
113 char* gpr_strvec_flatten(gpr_strvec* strs, size_t* total_length);
114 
117 int gpr_stricmp(const char* a, const char* b);
118 int gpr_strincmp(const char* a, const char* b, size_t n);
119 
120 void* gpr_memrchr(const void* s, int c, size_t n);
121 
122 /* Try to parse given string into a boolean value.
123  When parsed successfully, dst will have the value and returns true.
124  Otherwise, it returns false. */
125 bool gpr_parse_bool_value(const char* value, bool* dst);
126 
127 #endif /* GRPC_CORE_LIB_GPR_STRING_H */
void * gpr_memrchr(const void *s, int c, size_t n)
Definition: string.cc:335
int gpr_parse_nonnegative_int(const char *value)
Definition: string.cc:217
char * gpr_format_timespec(gpr_timespec)
Definition: string.cc:59
int gpr_stricmp(const char *a, const char *b)
Case insensitive string comparison...
Definition: string.cc:304
char * gpr_strvec_flatten(gpr_strvec *strs, size_t *total_length)
Definition: string.cc:288
char * gpr_dump_return_len(const char *buf, size_t len, uint32_t flags, size_t *out_len)
Definition: string.cc:129
char * gpr_strjoin(const char **strs, size_t nstrs, size_t *total_length)
Definition: string.cc:234
char * gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep, size_t *total_length)
Definition: string.cc:238
char ** strs
Definition: string.h:101
Definition: string.h:100
char * gpr_leftpad(const char *str, char flag, size_t length)
Definition: string.cc:224
bool gpr_parse_bool_value(const char *value, bool *dst)
Definition: string.cc:347
void gpr_reverse_bytes(char *str, int len)
Definition: string.cc:166
void gpr_strvec_add(gpr_strvec *strs, char *add)
Definition: string.cc:279
int int64_ttoa(int64_t value, char *output)
Definition: string.cc:196
int gpr_strincmp(const char *a, const char *b, size_t n)
Definition: string.cc:292
int gpr_parse_bytes_to_uint32(const char *data, size_t length, uint32_t *result)
Definition: string.cc:148
void gpr_strvec_init(gpr_strvec *strs)
Definition: string.cc:269
void gpr_strvec_destroy(gpr_strvec *strs)
Definition: string.cc:271
void gpr_string_split(const char *input, const char *sep, char ***strs, size_t *nstrs)
Definition: string.cc:322
Analogous to struct timespec.
Definition: gpr_types.h:47
int gpr_ltoa(long value, char *output)
Definition: string.cc:175
size_t count
Definition: string.h:102
size_t capacity
Definition: string.h:103
char * gpr_dump(const char *buf, size_t len, uint32_t flags)
Definition: string.cc:143