LCOV - code coverage report
Current view: top level - core/support - string.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 150 150 100.0 %
Date: 2015-12-10 22:15:08 Functions: 19 19 100.0 %

          Line data    Source code
       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             : #include "src/core/support/string.h"
      35             : 
      36             : #include <ctype.h>
      37             : #include <stddef.h>
      38             : #include <string.h>
      39             : 
      40             : #include <grpc/support/alloc.h>
      41             : #include <grpc/support/log.h>
      42             : #include <grpc/support/port_platform.h>
      43             : #include <grpc/support/useful.h>
      44             : 
      45      417156 : char *gpr_strdup(const char *src) {
      46             :   char *dst;
      47             :   size_t len;
      48             : 
      49      417156 :   if (!src) {
      50        8181 :     return NULL;
      51             :   }
      52             : 
      53      408934 :   len = strlen(src) + 1;
      54      408934 :   dst = gpr_malloc(len);
      55             : 
      56      408932 :   memcpy(dst, src, len);
      57             : 
      58      408932 :   return dst;
      59             : }
      60             : 
      61             : typedef struct {
      62             :   size_t capacity;
      63             :   size_t length;
      64             :   char *data;
      65             : } dump_out;
      66             : 
      67       18545 : static dump_out dump_out_create(void) {
      68       18545 :   dump_out r = {0, 0, NULL};
      69       18659 :   return r;
      70             : }
      71             : 
      72    35638592 : static void dump_out_append(dump_out *out, char c) {
      73    35638592 :   if (out->length == out->capacity) {
      74       68884 :     out->capacity = GPR_MAX(8, 2 * out->capacity);
      75       68884 :     out->data = gpr_realloc(out->data, out->capacity);
      76             :   }
      77    35638592 :   out->data[out->length++] = c;
      78    35638592 : }
      79             : 
      80       17834 : static void hexdump(dump_out *out, const char *buf, size_t len) {
      81             :   static const char hex[16] = "0123456789abcdef";
      82             : 
      83       17720 :   const gpr_uint8 *const beg = (const gpr_uint8 *)buf;
      84       17834 :   const gpr_uint8 *const end = beg + len;
      85             :   const gpr_uint8 *cur;
      86             : 
      87     8908665 :   for (cur = beg; cur != end; ++cur) {
      88     8890831 :     if (cur != beg) dump_out_append(out, ' ');
      89     8890831 :     dump_out_append(out, hex[*cur >> 4]);
      90     8890831 :     dump_out_append(out, hex[*cur & 0xf]);
      91             :   }
      92       17834 : }
      93             : 
      94       18655 : static void asciidump(dump_out *out, const char *buf, size_t len) {
      95       18541 :   const gpr_uint8 *const beg = (const gpr_uint8 *)buf;
      96       18655 :   const gpr_uint8 *const end = beg + len;
      97             :   const gpr_uint8 *cur;
      98       18655 :   int out_was_empty = (out->length == 0);
      99       18655 :   if (!out_was_empty) {
     100       17830 :     dump_out_append(out, ' ');
     101       17830 :     dump_out_append(out, '\'');
     102             :   }
     103     8930439 :   for (cur = beg; cur != end; ++cur) {
     104     8911784 :     dump_out_append(out, (char)(isprint(*cur) ? *(char *)cur : '.'));
     105             :   }
     106       18655 :   if (!out_was_empty) {
     107       17830 :     dump_out_append(out, '\'');
     108             :   }
     109       18655 : }
     110             : 
     111       18659 : char *gpr_dump(const char *buf, size_t len, gpr_uint32 flags) {
     112       18545 :   dump_out out = dump_out_create();
     113       18659 :   if (flags & GPR_DUMP_HEX) {
     114       17834 :     hexdump(&out, buf, len);
     115             :   }
     116       18659 :   if (flags & GPR_DUMP_ASCII) {
     117       18655 :     asciidump(&out, buf, len);
     118             :   }
     119       18659 :   dump_out_append(&out, 0);
     120       18659 :   return out.data;
     121             : }
     122             : 
     123       18406 : char *gpr_dump_slice(gpr_slice s, gpr_uint32 flags) {
     124       18406 :   return gpr_dump((const char *)GPR_SLICE_START_PTR(s), GPR_SLICE_LENGTH(s),
     125             :                   flags);
     126             : }
     127             : 
     128         548 : int gpr_parse_bytes_to_uint32(const char *buf, size_t len, gpr_uint32 *result) {
     129         543 :   gpr_uint32 out = 0;
     130             :   gpr_uint32 new;
     131             :   size_t i;
     132             : 
     133         548 :   if (len == 0) return 0; /* must have some bytes */
     134             : 
     135        1559 :   for (i = 0; i < len; i++) {
     136        1023 :     if (buf[i] < '0' || buf[i] > '9') return 0; /* bad char */
     137        1021 :     new = 10 * out + (gpr_uint32)(buf[i] - '0');
     138        1021 :     if (new < out) return 0; /* overflow */
     139        1010 :     out = new;
     140             :   }
     141             : 
     142         541 :   *result = out;
     143         541 :   return 1;
     144             : }
     145             : 
     146     1612413 : void gpr_reverse_bytes(char *str, int len) {
     147             :   char *p1, *p2;
     148     2216671 :   for (p1 = str, p2 = str + len - 1; p2 > p1; ++p1, --p2) {
     149      604258 :     char temp = *p1;
     150      604258 :     *p1 = *p2;
     151      604258 :     *p2 = temp;
     152             :   }
     153     1612413 : }
     154             : 
     155      564282 : int gpr_ltoa(long value, char *string) {
     156             :   long sign;
     157      564238 :   int i = 0;
     158             : 
     159      564282 :   if (value == 0) {
     160           5 :     string[0] = '0';
     161           5 :     string[1] = 0;
     162           5 :     return 1;
     163             :   }
     164             : 
     165      564277 :   sign = value < 0 ? -1 : 1;
     166     2332205 :   while (value) {
     167     1203651 :     string[i++] = (char)('0' + sign * (value % 10));
     168     1203651 :     value /= 10;
     169             :   }
     170      564277 :   if (sign < 0) string[i++] = '-';
     171      564277 :   gpr_reverse_bytes(string, i);
     172      564277 :   string[i] = 0;
     173      564277 :   return i;
     174             : }
     175             : 
     176     1048137 : int gpr_int64toa(gpr_int64 value, char *string) {
     177             :   gpr_int64 sign;
     178     1048135 :   int i = 0;
     179             : 
     180     1048137 :   if (value == 0) {
     181           1 :     string[0] = '0';
     182           1 :     string[1] = 0;
     183           1 :     return 1;
     184             :   }
     185             : 
     186     1048136 :   sign = value < 0 ? -1 : 1;
     187     3151265 :   while (value) {
     188     1054993 :     string[i++] = (char)('0' + sign * (value % 10));
     189     1054993 :     value /= 10;
     190             :   }
     191     1048136 :   if (sign < 0) string[i++] = '-';
     192     1048136 :   gpr_reverse_bytes(string, i);
     193     1048136 :   string[i] = 0;
     194     1048136 :   return i;
     195             : }
     196             : 
     197       17372 : char *gpr_strjoin(const char **strs, size_t nstrs, size_t *final_length) {
     198       17372 :   return gpr_strjoin_sep(strs, nstrs, "", final_length);
     199             : }
     200             : 
     201       17403 : char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep,
     202             :                       size_t *final_length) {
     203       17403 :   const size_t sep_len = strlen(sep);
     204       15387 :   size_t out_length = 0;
     205             :   size_t i;
     206             :   char *out;
     207      101376 :   for (i = 0; i < nstrs; i++) {
     208       83973 :     out_length += strlen(strs[i]);
     209             :   }
     210       17403 :   out_length += 1; /* null terminator */
     211       17403 :   if (nstrs > 0) {
     212       17401 :     out_length += sep_len * (nstrs - 1); /* separators */
     213             :   }
     214       17403 :   out = gpr_malloc(out_length);
     215       15385 :   out_length = 0;
     216      101371 :   for (i = 0; i < nstrs; i++) {
     217       83970 :     const size_t slen = strlen(strs[i]);
     218       83970 :     if (i != 0) {
     219       66571 :       memcpy(out + out_length, sep, sep_len);
     220       66571 :       out_length += sep_len;
     221             :     }
     222       83970 :     memcpy(out + out_length, strs[i], slen);
     223       83970 :     out_length += slen;
     224             :   }
     225       17401 :   out[out_length] = 0;
     226       17401 :   if (final_length != NULL) {
     227        4351 :     *final_length = out_length;
     228             :   }
     229       17401 :   return out;
     230             : }
     231             : 
     232             : /** Finds the initial (\a begin) and final (\a end) offsets of the next
     233             :  * substring from \a str + \a read_offset until the next \a sep or the end of \a
     234             :  * str.
     235             :  *
     236             :  * Returns 1 and updates \a begin and \a end. Returns 0 otherwise. */
     237         845 : static int slice_find_separator_offset(const gpr_slice str, const char *sep,
     238             :                                        const size_t read_offset, size_t *begin,
     239             :                                        size_t *end) {
     240             :   size_t i;
     241         845 :   const gpr_uint8 *str_ptr = GPR_SLICE_START_PTR(str) + read_offset;
     242         845 :   const size_t str_len = GPR_SLICE_LENGTH(str) - read_offset;
     243         845 :   const size_t sep_len = strlen(sep);
     244         845 :   if (str_len < sep_len) {
     245           3 :     return 0;
     246             :   }
     247             : 
     248       20926 :   for (i = 0; i <= str_len - sep_len; i++) {
     249       20128 :     if (memcmp(str_ptr + i, sep, sep_len) == 0) {
     250          44 :       *begin = read_offset;
     251          44 :       *end = read_offset + i;
     252          44 :       return 1;
     253             :     }
     254             :   }
     255         798 :   return 0;
     256             : }
     257             : 
     258         801 : void gpr_slice_split(gpr_slice str, const char *sep, gpr_slice_buffer *dst) {
     259         801 :   const size_t sep_len = strlen(sep);
     260             :   size_t begin, end;
     261             : 
     262         801 :   GPR_ASSERT(sep_len > 0);
     263             : 
     264         801 :   if (slice_find_separator_offset(str, sep, 0, &begin, &end) != 0) {
     265             :     do {
     266          44 :       gpr_slice_buffer_add_indexed(dst, gpr_slice_sub(str, begin, end));
     267          44 :     } while (slice_find_separator_offset(str, sep, end + sep_len, &begin,
     268          44 :                                          &end) != 0);
     269          28 :     gpr_slice_buffer_add_indexed(
     270          28 :         dst, gpr_slice_sub(str, end + sep_len, GPR_SLICE_LENGTH(str)));
     271             :   } else { /* no sep found, add whole input */
     272         776 :     gpr_slice_buffer_add_indexed(dst, gpr_slice_ref(str));
     273             :   }
     274         801 : }
     275             : 
     276     1592956 : void gpr_strvec_init(gpr_strvec *sv) { memset(sv, 0, sizeof(*sv)); }
     277             : 
     278     1592956 : void gpr_strvec_destroy(gpr_strvec *sv) {
     279             :   size_t i;
     280     3254458 :   for (i = 0; i < sv->count; i++) {
     281     1661502 :     gpr_free(sv->strs[i]);
     282             :   }
     283     1592956 :   gpr_free(sv->strs);
     284     1592956 : }
     285             : 
     286     1661503 : void gpr_strvec_add(gpr_strvec *sv, char *str) {
     287     1661503 :   if (sv->count == sv->capacity) {
     288     1074683 :     sv->capacity = GPR_MAX(sv->capacity + 8, sv->capacity * 2);
     289     1074683 :     sv->strs = gpr_realloc(sv->strs, sizeof(char *) * sv->capacity);
     290             :   }
     291     1661503 :   sv->strs[sv->count++] = str;
     292     1661503 : }
     293             : 
     294       17369 : char *gpr_strvec_flatten(gpr_strvec *sv, size_t *final_length) {
     295       17369 :   return gpr_strjoin((const char **)sv->strs, sv->count, final_length);
     296             : }

Generated by: LCOV version 1.11