LLVM OpenMP* Runtime Library
kmp_adt.cpp
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 
27 bool 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 
56 char *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 is a non-owning string class (similar to llvm::StringRef).
Definition: kmp_adt.h:34
bool consume_front(kmp_str_ref prefix)
Definition: kmp_adt.h:51
char * copy() const
Definition: kmp_adt.cpp:56
bool consume_integer(int &value, bool allow_zero=true, bool allow_negative=false)
Definition: kmp_adt.cpp:27
size_t count_while(const Fn &predicate) const
Definition: kmp_adt.h:74
void drop_front(size_t n)
Definition: kmp_adt.h:83