1 /* 2 * kmp_str.h -- String manipulation routines. 3 */ 4 5 //===----------------------------------------------------------------------===// 6 // 7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 8 // See https://llvm.org/LICENSE.txt for license information. 9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef KMP_STR_H 14 #define KMP_STR_H 15 16 #include <stdarg.h> 17 #include <string.h> 18 19 #include "kmp_os.h" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif // __cplusplus 24 25 #if KMP_OS_WINDOWS 26 #define strdup _strdup 27 #endif 28 29 /* some macros to replace ctype.h functions */ 30 #define TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) + 'a' - 'A') : (c)) 31 32 struct kmp_str_buf { 33 char *str; // Pointer to buffer content, read only. 34 unsigned int size; // Do not change this field! 35 int used; // Number of characters printed to buffer, read only. 36 char bulk[512]; // Do not use this field! 37 }; // struct kmp_str_buf 38 typedef struct kmp_str_buf kmp_str_buf_t; 39 40 #define __kmp_str_buf_init(b) \ 41 { \ 42 (b)->str = (b)->bulk; \ 43 (b)->size = sizeof((b)->bulk); \ 44 (b)->used = 0; \ 45 (b)->bulk[0] = 0; \ 46 } 47 48 void __kmp_str_buf_clear(kmp_str_buf_t *buffer); 49 void __kmp_str_buf_reserve(kmp_str_buf_t *buffer, size_t size); 50 void __kmp_str_buf_detach(kmp_str_buf_t *buffer); 51 void __kmp_str_buf_free(kmp_str_buf_t *buffer); 52 void __kmp_str_buf_cat(kmp_str_buf_t *buffer, char const *str, size_t len); 53 void __kmp_str_buf_catbuf(kmp_str_buf_t *dest, const kmp_str_buf_t *src); 54 int __kmp_str_buf_vprint(kmp_str_buf_t *buffer, char const *format, 55 va_list args); 56 int __kmp_str_buf_print(kmp_str_buf_t *buffer, char const *format, ...); 57 void __kmp_str_buf_print_size(kmp_str_buf_t *buffer, size_t size); 58 59 /* File name parser. 60 Usage: 61 62 kmp_str_fname_t fname = __kmp_str_fname_init( path ); 63 // Use fname.path (copy of original path ), fname.dir, fname.base. 64 // Note fname.dir concatenated with fname.base gives exact copy of path. 65 __kmp_str_fname_free( & fname ); 66 */ 67 struct kmp_str_fname { 68 char *path; 69 char *dir; 70 char *base; 71 }; // struct kmp_str_fname 72 typedef struct kmp_str_fname kmp_str_fname_t; 73 void __kmp_str_fname_init(kmp_str_fname_t *fname, char const *path); 74 void __kmp_str_fname_free(kmp_str_fname_t *fname); 75 // Compares file name with specified pattern. If pattern is NULL, any fname 76 // matched. 77 int __kmp_str_fname_match(kmp_str_fname_t const *fname, char const *pattern); 78 79 /* The compiler provides source locations in string form 80 ";file;func;line;col;;". It is not convenient for manipulation. This 81 structure keeps source location in more convenient form. 82 Usage: 83 84 kmp_str_loc_t loc = __kmp_str_loc_init(ident->psource, false); 85 // use loc.file, loc.func, loc.line, loc.col. 86 // loc.fname is available if second argument of __kmp_str_loc_init is true. 87 __kmp_str_loc_free( & loc ); 88 89 If psource is NULL or does not follow format above, file and/or func may be 90 NULL pointers. 91 */ 92 struct kmp_str_loc { 93 char *_bulk; // Do not use thid field. 94 kmp_str_fname_t fname; // Will be initialized if init_fname is true. 95 char *file; 96 char *func; 97 int line; 98 int col; 99 }; // struct kmp_str_loc 100 typedef struct kmp_str_loc kmp_str_loc_t; 101 kmp_str_loc_t __kmp_str_loc_init(char const *psource, bool init_fname); 102 void __kmp_str_loc_numbers(char const *Psource, int *Line, int *Col); 103 void __kmp_str_loc_free(kmp_str_loc_t *loc); 104 105 int __kmp_str_eqf(char const *lhs, char const *rhs); 106 char *__kmp_str_format(char const *format, ...); 107 void __kmp_str_free(char **str); 108 int __kmp_str_match(char const *target, int len, char const *data); 109 bool __kmp_str_contains(char const *target, int len, char const *data); 110 int __kmp_str_match_false(char const *data); 111 int __kmp_str_match_true(char const *data); 112 void __kmp_str_replace(char *str, char search_for, char replace_with); 113 void __kmp_str_split(char *str, char delim, char **head, char **tail); 114 char *__kmp_str_token(char *str, char const *delim, char **buf); 115 int __kmp_basic_str_to_int(char const *str); 116 int __kmp_str_to_int(char const *str, char sentinel); 117 118 void __kmp_str_to_size(char const *str, size_t *out, size_t dfactor, 119 char const **error); 120 void __kmp_str_to_uint(char const *str, kmp_uint64 *out, char const **error); 121 122 #ifdef __cplusplus 123 } // extern "C" 124 #endif // __cplusplus 125 126 #endif // KMP_STR_H 127 128 // end of file // 129