| 1 | /* |
| 2 | * cf_str.h |
| 3 | * |
| 4 | * Copyright (C) 2008-2017 Aerospike, Inc. |
| 5 | * |
| 6 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
| 7 | * license agreements. |
| 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify it under |
| 10 | * the terms of the GNU Affero General Public License as published by the Free |
| 11 | * Software Foundation, either version 3 of the License, or (at your option) any |
| 12 | * later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
| 17 | * details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see http://www.gnu.org/licenses/ |
| 21 | */ |
| 22 | |
| 23 | #pragma once |
| 24 | |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | // These functions convert integers into a string, writing into the provided |
| 28 | // buffer, and return the number of bytes written. |
| 29 | unsigned int cf_str_itoa(int value, char *s, int radix); |
| 30 | unsigned int cf_str_itoa_u64(uint64_t value, char *s, int radix); |
| 31 | unsigned int cf_str_itoa_u32(uint32_t value, char *s, int radix); |
| 32 | |
| 33 | // These functions convert a string to a number of different integer types, and |
| 34 | // returns 0 on success. |
| 35 | int cf_str_atoi(char *s, int *value); |
| 36 | int cf_str_atoi_u32(char *s, uint32_t *value); |
| 37 | int cf_str_atoi_64(char *s, int64_t *value); |
| 38 | int cf_str_atoi_u64(char *s, uint64_t *value); |
| 39 | int cf_str_atoi_seconds(char *s, uint32_t *value); |
| 40 | |
| 41 | int cf_strtoul_x64(const char *s, uint64_t *value); |
| 42 | int cf_strtoul_u32(char *s, uint32_t *value); |
| 43 | int cf_strtoul_u64(char *s, uint64_t *value); |
| 44 | |
| 45 | // Split the string 'str' based on input breaks in 'fmt'. |
| 46 | // - The splitting is destructive. |
| 47 | // - The pointers will be added to the end of vector '*v'. |
| 48 | // - The vector better be created with object size 'void *'. |
| 49 | struct cf_vector_s; |
| 50 | extern void cf_str_split(char *fmt, char *str, struct cf_vector_s *v); |
| 51 | |
| 52 | static inline int |
| 53 | cf_str_strnchr(uint8_t *s, int sz, int c) |
| 54 | { |
| 55 | for (int i = 0; i < sz; i++) { |
| 56 | if (s[i] == c) { |
| 57 | return i; |
| 58 | } |
| 59 | } |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | static inline const char * |
| 64 | cf_str_safe_as_empty(const char *s) |
| 65 | { |
| 66 | return s ? s : "" ; |
| 67 | } |
| 68 | |
| 69 | static inline const char * |
| 70 | cf_str_safe_as_null(const char *s) |
| 71 | { |
| 72 | return s ? s : "null" ; |
| 73 | } |
| 74 | |