OLD | NEW |
(Empty) | |
| 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 |
| 39 #include <grpc/support/port_platform.h> |
| 40 #include <grpc/support/slice_buffer.h> |
| 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, uint32_t flags); |
| 56 |
| 57 /* Calls gpr_dump on a slice. */ |
| 58 char *gpr_dump_slice(gpr_slice slice, uint32_t 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 uint32_t *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 /* Minimum buffer size for calling int64toa */ |
| 74 #define GPR_INT64TOA_MIN_BUFSIZE (3 * sizeof(int64_t)) |
| 75 |
| 76 /* Convert an int64 to a string in base 10; returns the length of the |
| 77 output string (or 0 on failure). |
| 78 output must be at least GPR_INT64TOA_MIN_BUFSIZE bytes long. |
| 79 NOTE: This function ensures sufficient bit width even on Win x64, |
| 80 where long is 32bit is size.*/ |
| 81 int int64_ttoa(int64_t value, char *output); |
| 82 |
| 83 /* Reverse a run of bytes */ |
| 84 void gpr_reverse_bytes(char *str, int len); |
| 85 |
| 86 /* Join a set of strings, returning the resulting string. |
| 87 Total combined length (excluding null terminator) is returned in total_length |
| 88 if it is non-null. */ |
| 89 char *gpr_strjoin(const char **strs, size_t nstrs, size_t *total_length); |
| 90 |
| 91 /* Join a set of strings using a separator, returning the resulting string. |
| 92 Total combined length (excluding null terminator) is returned in total_length |
| 93 if it is non-null. */ |
| 94 char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep, |
| 95 size_t *total_length); |
| 96 |
| 97 /** Split \a str by the separator \a sep. Results are stored in \a dst, which |
| 98 * should be a properly initialized instance. */ |
| 99 void gpr_slice_split(gpr_slice str, const char *sep, gpr_slice_buffer *dst); |
| 100 |
| 101 /* A vector of strings... for building up a final string one piece at a time */ |
| 102 typedef struct { |
| 103 char **strs; |
| 104 size_t count; |
| 105 size_t capacity; |
| 106 } gpr_strvec; |
| 107 |
| 108 /* Initialize/destroy */ |
| 109 void gpr_strvec_init(gpr_strvec *strs); |
| 110 void gpr_strvec_destroy(gpr_strvec *strs); |
| 111 /* Add a string to a strvec, takes ownership of the string */ |
| 112 void gpr_strvec_add(gpr_strvec *strs, char *add); |
| 113 /* Return a joined string with all added substrings, optionally setting |
| 114 total_length as per gpr_strjoin */ |
| 115 char *gpr_strvec_flatten(gpr_strvec *strs, size_t *total_length); |
| 116 |
| 117 #ifdef __cplusplus |
| 118 } |
| 119 #endif |
| 120 |
| 121 #endif /* GRPC_INTERNAL_CORE_SUPPORT_STRING_H */ |
OLD | NEW |