• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SRC_CORE_UTIL_STRING_H
20 #define GRPC_SRC_CORE_UTIL_STRING_H
21 
22 #include <grpc/support/port_platform.h>
23 #include <grpc/support/time.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 
27 #include <string>
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* buf, size_t len, uint32_t* result);
47 
48 // Minimum buffer size for calling ltoa
49 #define GPR_LTOA_MIN_BUFSIZE (3 * sizeof(long))
50 
51 // Convert a long to a string in base 10; returns the length of the
52 // output string (or 0 on failure).
53 // output must be at least GPR_LTOA_MIN_BUFSIZE bytes long.
54 int gpr_ltoa(long value, char* output);
55 
56 // Minimum buffer size for calling int64toa
57 #define GPR_INT64TOA_MIN_BUFSIZE (3 * sizeof(int64_t))
58 
59 // Convert an int64 to a string in base 10; returns the length of the
60 // output string (or 0 on failure).
61 // output must be at least GPR_INT64TOA_MIN_BUFSIZE bytes long.
62 // NOTE: This function ensures sufficient bit width even on Win x64,
63 // where long is 32bit is size.
64 int int64_ttoa(int64_t value, char* output);
65 
66 // Parses a non-negative number from a value string.  Returns -1 on error.
67 int gpr_parse_nonnegative_int(const char* value);
68 
69 // Reverse a run of bytes
70 void gpr_reverse_bytes(char* str, int len);
71 
72 // Pad a string with flag characters. The given length specifies the minimum
73 // field width. The input string is never truncated.
74 char* gpr_leftpad(const char* str, char flag, size_t length);
75 
76 // Join a set of strings, returning the resulting string.
77 // Total combined length (excluding null terminator) is returned in final_length
78 // if it is non-null.
79 char* gpr_strjoin(const char** strs, size_t nstrs, size_t* final_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 final_length
83 // if it is non-null.
84 char* gpr_strjoin_sep(const char** strs, size_t nstrs, const char* sep,
85                       size_t* final_length);
86 
87 void gpr_string_split(const char* input, const char* sep, char*** strs,
88                       size_t* nstrs);
89 
90 // Returns a string that represents tm according to RFC-3339, and,
91 // more specifically, follows:
92 // https://developers.google.com/protocol-buffers/docs/proto3#json
93 
94 // Uses RFC 3339, where generated output will always be Z-normalized and uses
95 // 0, 3, 6 or 9 fractional digits.
96 std::string gpr_format_timespec(gpr_timespec);
97 
98 /// Case insensitive string comparison... return <0 if lower(a)<lower(b), ==0 if
99 /// lower(a)==lower(b), >0 if lower(a)>lower(b)
100 int gpr_stricmp(const char* a, const char* b);
101 int gpr_strincmp(const char* a, const char* b, size_t n);
102 
103 void* gpr_memrchr(const void* s, int c, size_t n);
104 
105 // Try to parse given string into a boolean value.
106 // When parsed successfully, dst will have the value and returns true.
107 // Otherwise, it returns false.
108 bool gpr_parse_bool_value(const char* value, bool* dst);
109 
110 #endif  // GRPC_SRC_CORE_UTIL_STRING_H
111