LLVM OpenMP
kmp_adt.cpp
Go to the documentation of this file.
1/*
2 * kmp_adt.cpp -- Advanced Data Types used internally
3 *
4 * FIXME: This is in intermediate solution until we agree and implement some
5 * common resource according to
6 * https://discourse.llvm.org/t/meta-rfc-adts-without-c-runtime-dependency/90317.
7 * As soon as we will have this common resource that can be used for runtimes
8 * such as openmp that want to avoid the link dependency to the C++ STL, this
9 * shall be refactored.
10 */
11
12//===----------------------------------------------------------------------===//
13//
14// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
15// See https://llvm.org/LICENSE.txt for license information.
16// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
17//
18//===----------------------------------------------------------------------===//
19
20#include "kmp_adt.h"
21#include "kmp.h"
22#include "kmp_str.h"
23
24#include <cctype>
25#include <cstring>
26
27bool kmp_str_ref::consume_integer(int &value, bool allow_zero,
28 bool allow_negative) {
29 kmp_str_ref orig = *this; // save state
30 bool is_negative = consume_front("-");
31 if (is_negative && !allow_negative) {
32 *this = orig;
33 return false;
34 }
35 size_t num_digits = count_while(
36 [](char c) { return isdigit(static_cast<unsigned char>(c)) != 0; });
37 if (!num_digits) {
38 *this = orig;
39 return false;
40 }
41 value = __kmp_basic_str_to_int(data, num_digits);
42 if (value == INT_MAX) {
43 *this = orig;
44 return false;
45 }
46 drop_front(num_digits);
47 if (is_negative)
48 value = -value;
49 if (!allow_zero && value == 0) {
50 *this = orig;
51 return false;
52 }
53 return true;
54}
55
56char *kmp_str_ref::copy() const {
57 char *copy_str = static_cast<char *>(KMP_INTERNAL_MALLOC(len + 1));
58 if (!copy_str)
59 KMP_FATAL(MemoryAllocFailed);
60 memcpy(copy_str, data, len);
61 copy_str[len] = '\0';
62 return copy_str;
63}
kmp_str_ref(const char *str)
Definition kmp_adt.h:38
bool consume_front(kmp_str_ref prefix)
Check if the string starts with the given prefix and remove it from the string afterwards.
Definition kmp_adt.h:48
char * copy() const
Get an own duplicate of the string.
Definition kmp_adt.cpp:56
bool consume_integer(int &value, bool allow_zero=true, bool allow_negative=false)
Start consuming an integer from the start of the string and remove it from the string afterwards.
Definition kmp_adt.cpp:27
size_t count_while(const Fn &predicate) const
Count the number of characters in the string while the predicate returns true.
Definition kmp_adt.h:71
void drop_front(size_t n)
Drop the first n characters from the string.
Definition kmp_adt.h:80
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
#define KMP_INTERNAL_MALLOC(sz)
Definition kmp.h:116
#define KMP_FATAL(...)
Definition kmp_i18n.h:146
int __kmp_basic_str_to_int(char const *str, size_t maxlen)
Definition kmp_str.cpp:622