kmp_str.h 4.6 KB

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