/* * kmp_str.cpp -- String manipulation routines. */ //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "kmp_str.h" #include <stdarg.h> // va_* #include <stdio.h> // vsnprintf() #include <stdlib.h> // malloc(), realloc() #include "kmp.h" #include "kmp_i18n.h" /* String buffer. Usage: // Declare buffer and initialize it. kmp_str_buf_t buffer; __kmp_str_buf_init( & buffer ); // Print to buffer. __kmp_str_buf_print(& buffer, "Error in file \"%s\" line %d\n", "foo.c", 12); __kmp_str_buf_print(& buffer, " <%s>\n", line); // Use buffer contents. buffer.str is a pointer to data, buffer.used is a // number of printed characters (not including terminating zero). write( fd, buffer.str, buffer.used ); // Free buffer. __kmp_str_buf_free( & buffer ); // Alternatively, you can detach allocated memory from buffer: __kmp_str_buf_detach( & buffer ); return buffer.str; // That memory should be freed eventually. Notes: * Buffer users may use buffer.str and buffer.used. Users should not change any fields of buffer directly. * buffer.str is never NULL. If buffer is empty, buffer.str points to empty string (""). * For performance reasons, buffer uses stack memory (buffer.bulk) first. If stack memory is exhausted, buffer allocates memory on heap by malloc(), and reallocates it by realloc() as amount of used memory grows. * Buffer doubles amount of allocated memory each time it is exhausted. */ // TODO: __kmp_str_buf_print() can use thread local memory allocator. #define KMP_STR_BUF_INVARIANT(b) … void __kmp_str_buf_clear(kmp_str_buf_t *buffer) { … } // __kmp_str_buf_clear void __kmp_str_buf_reserve(kmp_str_buf_t *buffer, size_t size) { … } // __kmp_str_buf_reserve void __kmp_str_buf_detach(kmp_str_buf_t *buffer) { … } // __kmp_str_buf_detach void __kmp_str_buf_free(kmp_str_buf_t *buffer) { … } // __kmp_str_buf_free void __kmp_str_buf_cat(kmp_str_buf_t *buffer, char const *str, size_t len) { … } // __kmp_str_buf_cat void __kmp_str_buf_catbuf(kmp_str_buf_t *dest, const kmp_str_buf_t *src) { … } // __kmp_str_buf_catbuf // Return the number of characters written int __kmp_str_buf_vprint(kmp_str_buf_t *buffer, char const *format, va_list args) { … } // __kmp_str_buf_vprint // Return the number of characters written int __kmp_str_buf_print(kmp_str_buf_t *buffer, char const *format, ...) { … } // __kmp_str_buf_print /* The function prints specified size to buffer. Size is expressed using biggest possible unit, for example 1024 is printed as "1k". */ void __kmp_str_buf_print_size(kmp_str_buf_t *buf, size_t size) { … } // __kmp_str_buf_print_size void __kmp_str_fname_init(kmp_str_fname_t *fname, char const *path) { … } // kmp_str_fname_init void __kmp_str_fname_free(kmp_str_fname_t *fname) { … } // kmp_str_fname_free int __kmp_str_fname_match(kmp_str_fname_t const *fname, char const *pattern) { … } // __kmp_str_fname_match // Get the numeric fields from source location string. // For clang these fields are Line/Col of the start of the construct. // For icc these are LineBegin/LineEnd of the construct. // Function is fast as it does not duplicate string (which involves memory // allocation), and parses the string in place. void __kmp_str_loc_numbers(char const *Psource, int *LineBeg, int *LineEndOrCol) { … } kmp_str_loc_t __kmp_str_loc_init(char const *psource, bool init_fname) { … } // kmp_str_loc_init void __kmp_str_loc_free(kmp_str_loc_t *loc) { … } // kmp_str_loc_free /* This function is intended to compare file names. On Windows* OS file names are case-insensitive, so functions performs case-insensitive comparison. On Linux* OS it performs case-sensitive comparison. Note: The function returns *true* if strings are *equal*. */ int __kmp_str_eqf( // True, if strings are equal, false otherwise. char const *lhs, // First string. char const *rhs // Second string. ) { … } // __kmp_str_eqf /* This function is like sprintf, but it *allocates* new buffer, which must be freed eventually by __kmp_str_free(). The function is very convenient for constructing strings, it successfully replaces strdup(), strcat(), it frees programmer from buffer allocations and helps to avoid buffer overflows. Examples: str = __kmp_str_format("%s", orig); //strdup() doesn't care about buffer size __kmp_str_free( & str ); str = __kmp_str_format( "%s%s", orig1, orig2 ); // strcat(), doesn't care // about buffer size. __kmp_str_free( & str ); str = __kmp_str_format( "%s/%s.txt", path, file ); // constructing string. __kmp_str_free( & str ); Performance note: This function allocates memory with malloc() calls, so do not call it from performance-critical code. In performance-critical code consider using kmp_str_buf_t instead, since it uses stack-allocated buffer for short strings. Why does this function use malloc()? 1. __kmp_allocate() returns cache-aligned memory allocated with malloc(). There are no reasons in using __kmp_allocate() for strings due to extra overhead while cache-aligned memory is not necessary. 2. __kmp_thread_malloc() cannot be used because it requires pointer to thread structure. We need to perform string operations during library startup (for example, in __kmp_register_library_startup()) when no thread structures are allocated yet. So standard malloc() is the only available option. */ char *__kmp_str_format( // Allocated string. char const *format, // Format string. ... // Other parameters. ) { … } // func __kmp_str_format void __kmp_str_free(char **str) { … } // func __kmp_str_free /* If len is zero, returns true iff target and data have exact case-insensitive match. If len is negative, returns true iff target is a case-insensitive substring of data. If len is positive, returns true iff target is a case-insensitive substring of data or vice versa, and neither is shorter than len. */ int __kmp_str_match(char const *target, int len, char const *data) { … } // __kmp_str_match // If data contains all of target, returns true, otherwise returns false. // len should be the length of target bool __kmp_str_contains(char const *target, int len, char const *data) { … } // __kmp_str_contains int __kmp_str_match_false(char const *data) { … } // __kmp_str_match_false int __kmp_str_match_true(char const *data) { … } // __kmp_str_match_true void __kmp_str_replace(char *str, char search_for, char replace_with) { … } // __kmp_str_replace void __kmp_str_split(char *str, // I: String to split. char delim, // I: Character to split on. char **head, // O: Pointer to head (may be NULL). char **tail // O: Pointer to tail (may be NULL). ) { … } // __kmp_str_split /* strtok_r() is not available on Windows* OS. This function reimplements strtok_r(). */ char *__kmp_str_token( char *str, // String to split into tokens. Note: String *is* modified! char const *delim, // Delimiters. char **buf // Internal buffer. ) { … } // __kmp_str_token int __kmp_basic_str_to_int(char const *str) { … } int __kmp_str_to_int(char const *str, char sentinel) { … } // __kmp_str_to_int /* The routine parses input string. It is expected it is a unsigned integer with optional unit. Units are: "b" for bytes, "kb" or just "k" for kilobytes, "mb" or "m" for megabytes, ..., "yb" or "y" for yottabytes. :-) Unit name is case-insensitive. The routine returns 0 if everything is ok, or error code: -1 in case of overflow, -2 in case of unknown unit. *size is set to parsed value. In case of overflow *size is set to KMP_SIZE_T_MAX, in case of unknown unit *size is set to zero. */ void __kmp_str_to_size( // R: Error code. char const *str, // I: String of characters, unsigned number and unit ("b", // "kb", etc). size_t *out, // O: Parsed number. size_t dfactor, // I: The factor if none of the letters specified. char const **error // O: Null if everything is ok, error message otherwise. ) { … } // __kmp_str_to_size void __kmp_str_to_uint( // R: Error code. char const *str, // I: String of characters, unsigned number. kmp_uint64 *out, // O: Parsed number. char const **error // O: Null if everything is ok, error message otherwise. ) { … } // __kmp_str_to_unit // end of file //