blob: 09faadb68f1ab95fc5aecf1deb1e220c12a9f156 [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
2 * kmp_str.h -- String manipulation routines.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003 */
4
Jim Cownie5e8470a2013-09-27 10:38:44 +00005//===----------------------------------------------------------------------===//
6//
Chandler Carruth57b08b02019-01-19 10:56:40 +00007// 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
Jim Cownie5e8470a2013-09-27 10:38:44 +000010//
11//===----------------------------------------------------------------------===//
12
Jim Cownie5e8470a2013-09-27 10:38:44 +000013#ifndef KMP_STR_H
14#define KMP_STR_H
15
Jim Cownie5e8470a2013-09-27 10:38:44 +000016#include <stdarg.h>
Jonathan Peyton30419822017-05-12 18:01:32 +000017#include <string.h>
Jim Cownie5e8470a2013-09-27 10:38:44 +000018
19#include "kmp_os.h"
20
21#ifdef __cplusplus
Jonathan Peyton30419822017-05-12 18:01:32 +000022extern "C" {
Jim Cownie5e8470a2013-09-27 10:38:44 +000023#endif // __cplusplus
24
25#if KMP_OS_WINDOWS
Jonathan Peyton30419822017-05-12 18:01:32 +000026#define strdup _strdup
Jim Cownie5e8470a2013-09-27 10:38:44 +000027#endif
28
29/* some macros to replace ctype.h functions */
Jonathan Peyton30419822017-05-12 18:01:32 +000030#define TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) + 'a' - 'A') : (c))
Jim Cownie5e8470a2013-09-27 10:38:44 +000031
32struct kmp_str_buf {
Jonathan Peyton30419822017-05-12 18:01:32 +000033 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!
Jim Cownie5e8470a2013-09-27 10:38:44 +000037}; // struct kmp_str_buf
Jonathan Peyton30419822017-05-12 18:01:32 +000038typedef struct kmp_str_buf kmp_str_buf_t;
Jim Cownie5e8470a2013-09-27 10:38:44 +000039
Jonathan Peyton30419822017-05-12 18:01:32 +000040#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 }
Jim Cownie5e8470a2013-09-27 10:38:44 +000047
Jonathan Peyton30419822017-05-12 18:01:32 +000048void __kmp_str_buf_clear(kmp_str_buf_t *buffer);
49void __kmp_str_buf_reserve(kmp_str_buf_t *buffer, int size);
50void __kmp_str_buf_detach(kmp_str_buf_t *buffer);
51void __kmp_str_buf_free(kmp_str_buf_t *buffer);
52void __kmp_str_buf_cat(kmp_str_buf_t *buffer, char const *str, int len);
Jonathan Peyton6d88e042018-12-13 23:14:24 +000053void __kmp_str_buf_catbuf(kmp_str_buf_t *dest, const kmp_str_buf_t *src);
54int __kmp_str_buf_vprint(kmp_str_buf_t *buffer, char const *format,
55 va_list args);
56int __kmp_str_buf_print(kmp_str_buf_t *buffer, char const *format, ...);
Jonathan Peyton30419822017-05-12 18:01:32 +000057void __kmp_str_buf_print_size(kmp_str_buf_t *buffer, size_t size);
Jim Cownie5e8470a2013-09-27 10:38:44 +000058
Jonathan Peyton30419822017-05-12 18:01:32 +000059/* File name parser.
60 Usage:
Jim Cownie5e8470a2013-09-27 10:38:44 +000061
Jonathan Peyton30419822017-05-12 18:01:32 +000062 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 );
Jim Cownie5e8470a2013-09-27 10:38:44 +000066*/
67struct kmp_str_fname {
Jonathan Peyton30419822017-05-12 18:01:32 +000068 char *path;
69 char *dir;
70 char *base;
Jim Cownie5e8470a2013-09-27 10:38:44 +000071}; // struct kmp_str_fname
72typedef struct kmp_str_fname kmp_str_fname_t;
Jonathan Peyton30419822017-05-12 18:01:32 +000073void __kmp_str_fname_init(kmp_str_fname_t *fname, char const *path);
74void __kmp_str_fname_free(kmp_str_fname_t *fname);
75// Compares file name with specified patern. If pattern is NULL, any fname
76// matched.
77int __kmp_str_fname_match(kmp_str_fname_t const *fname, char const *pattern);
Jim Cownie5e8470a2013-09-27 10:38:44 +000078
Jonathan Peyton30419822017-05-12 18:01:32 +000079/* The compiler provides source locations in string form
80 ";file;func;line;col;;". It is not convenient for manupulation. This
81 structure keeps source location in more convenient form.
82 Usage:
Jim Cownie5e8470a2013-09-27 10:38:44 +000083
Jonathan Peyton30419822017-05-12 18:01:32 +000084 kmp_str_loc_t loc = __kmp_str_loc_init( ident->psource, 0 );
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 );
Jim Cownie5e8470a2013-09-27 10:38:44 +000088
Jonathan Peyton30419822017-05-12 18:01:32 +000089 If psource is NULL or does not follow format above, file and/or func may be
90 NULL pointers.
Jim Cownie5e8470a2013-09-27 10:38:44 +000091*/
92struct kmp_str_loc {
Jonathan Peyton30419822017-05-12 18:01:32 +000093 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;
Jim Cownie5e8470a2013-09-27 10:38:44 +000099}; // struct kmp_str_loc
100typedef struct kmp_str_loc kmp_str_loc_t;
Jonathan Peyton30419822017-05-12 18:01:32 +0000101kmp_str_loc_t __kmp_str_loc_init(char const *psource, int init_fname);
102void __kmp_str_loc_free(kmp_str_loc_t *loc);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000103
Jonathan Peyton30419822017-05-12 18:01:32 +0000104int __kmp_str_eqf(char const *lhs, char const *rhs);
105char *__kmp_str_format(char const *format, ...);
Jonas Hahnfeldaeb40ad2017-11-09 15:52:25 +0000106void __kmp_str_free(char **str);
Jonathan Peyton30419822017-05-12 18:01:32 +0000107int __kmp_str_match(char const *target, int len, char const *data);
108int __kmp_str_match_false(char const *data);
109int __kmp_str_match_true(char const *data);
110void __kmp_str_replace(char *str, char search_for, char replace_with);
111void __kmp_str_split(char *str, char delim, char **head, char **tail);
112char *__kmp_str_token(char *str, char const *delim, char **buf);
113int __kmp_str_to_int(char const *str, char sentinel);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000114
Jonathan Peyton30419822017-05-12 18:01:32 +0000115void __kmp_str_to_size(char const *str, size_t *out, size_t dfactor,
116 char const **error);
117void __kmp_str_to_uint(char const *str, kmp_uint64 *out, char const **error);
Jim Cownie5e8470a2013-09-27 10:38:44 +0000118
119#ifdef __cplusplus
Jonathan Peyton30419822017-05-12 18:01:32 +0000120} // extern "C"
Jim Cownie5e8470a2013-09-27 10:38:44 +0000121#endif // __cplusplus
122
123#endif // KMP_STR_H
124
125// end of file //