LLVM OpenMP
kmp_adt.h
Go to the documentation of this file.
1/*
2 * kmp_adt.h -- 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#ifndef KMP_ADT_H
21#define KMP_ADT_H
22
23#include <cassert>
24#include <cctype>
25#include <cstddef>
26#include <cstdint>
27#include <cstring>
28#include <type_traits>
29
30/// kmp_str_ref is a non-owning string class (similar to llvm::StringRef).
31class kmp_str_ref final {
32 const char *data;
33 size_t len;
34
35public:
36 static constexpr size_t npos = SIZE_MAX;
37
38 kmp_str_ref(const char *str) : data(str), len(str ? strlen(str) : 0) {}
39 kmp_str_ref(const char *str, size_t len) : data(str), len(len) {
40 assert((data || !len) && "len must be 0 for nullptr data");
41 }
42
43 kmp_str_ref(const kmp_str_ref &other) = default;
44 kmp_str_ref &operator=(const kmp_str_ref &other) = default;
45
46 /// Check if the string starts with the given prefix and remove it from the
47 /// string afterwards.
49 if (len < prefix.len)
50 return false;
51 if (empty() || prefix.empty()) // avoid calling memcmp on potential nullptr
52 return true;
53 if (memcmp(data, prefix.data, prefix.len) != 0)
54 return false;
55 drop_front(prefix.len);
56 return true;
57 }
58
59 /// Start consuming an integer from the start of the string and remove it from
60 /// the string afterwards.
61 /// The maximum integer value that can currently be parsed is INT_MAX - 1.
62 bool consume_integer(int &value, bool allow_zero = true,
63 bool allow_negative = false);
64
65 /// Get an own duplicate of the string.
66 /// Must be freed with KMP_INTERNAL_FREE().
67 char *copy() const;
68
69 /// Count the number of characters in the string while the predicate returns
70 /// true.
71 template <typename Fn> size_t count_while(const Fn &predicate) const {
72 static_assert(std::is_invocable_r_v<bool, Fn, char>,
73 "predicate must be callable as bool(char)");
74 size_t n = find_if_not(predicate);
75 return n == npos ? len : n;
76 }
77
78 /// Drop the first n characters from the string.
79 /// (Limit n to the length of the string.)
80 void drop_front(size_t n) {
81 if (n > len)
82 n = len;
83 data += n;
84 len -= n;
85 }
86
87 /// Drop characters from the string while the predicate returns true.
88 template <typename Fn> void drop_while(const Fn &predicate) {
89 static_assert(std::is_invocable_r_v<bool, Fn, char>,
90 "predicate must be callable as bool(char)");
91 drop_front(count_while(predicate));
92 }
93
94 /// Check if the string is empty.
95 bool empty() const { return len == 0; }
96
97 /// Return the index of the first character in the string for which the
98 /// predicate returns true.
99 /// Returns npos if no match is found.
100 template <typename Fn> size_t find_if(const Fn &predicate) const {
101 static_assert(std::is_invocable_r_v<bool, Fn, char>,
102 "predicate must be callable as bool(char)");
103 size_t i = 0;
104 while (i < len && !predicate(data[i]))
105 ++i;
106 return i < len ? i : npos;
107 }
108
109 /// Return the index of the first character in the string for which the
110 /// predicate returns false.
111 /// Returns npos if no match is found.
112 template <typename Fn> size_t find_if_not(const Fn &predicate) const {
113 static_assert(std::is_invocable_r_v<bool, Fn, char>,
114 "predicate must be callable as bool(char)");
115 return find_if([predicate](char c) { return !predicate(c); });
116 }
117
118 /// Get the length of the string.
119 size_t length() const { return len; }
120 size_t size() const { return length(); }
121
122 /// Drop space from the start of the string.
123 void skip_space() {
124 drop_while([](char c) {
125 return static_cast<bool>(isspace(static_cast<unsigned char>(c)));
126 });
127 }
128
129 /// Construct a new string with the longest prefix of the original string that
130 /// satisfies the predicate. Doesn't modify the original string.
131 template <typename Fn> kmp_str_ref take_while(const Fn &predicate) const {
132 static_assert(std::is_invocable_r_v<bool, Fn, char>,
133 "predicate must be callable as bool(char)");
134 return kmp_str_ref(data, count_while(predicate));
135 }
136
137 /// Iterator support (raw pointers work as iterators for contiguous storage)
138 const char *begin() const { return data; }
139 const char *end() const { return data + len; }
140};
141
142#endif // KMP_ADT_H
size_t size() const
Definition kmp_adt.h:120
kmp_str_ref take_while(const Fn &predicate) const
Construct a new string with the longest prefix of the original string that satisfies the predicate.
Definition kmp_adt.h:131
size_t find_if_not(const Fn &predicate) const
Return the index of the first character in the string for which the predicate returns false.
Definition kmp_adt.h:112
kmp_str_ref(const kmp_str_ref &other)=default
static constexpr size_t npos
Definition kmp_adt.h:36
kmp_str_ref(const char *str)
Definition kmp_adt.h:38
size_t length() const
Get the length of the string.
Definition kmp_adt.h:119
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
kmp_str_ref & operator=(const kmp_str_ref &other)=default
bool empty() const
Check if the string is empty.
Definition kmp_adt.h:95
kmp_str_ref(const char *str, size_t len)
Definition kmp_adt.h:39
const char * end() const
Definition kmp_adt.h:139
size_t find_if(const Fn &predicate) const
Return the index of the first character in the string for which the predicate returns true.
Definition kmp_adt.h:100
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
const char * begin() const
Iterator support (raw pointers work as iterators for contiguous storage)
Definition kmp_adt.h:138
void skip_space()
Drop space from the start of the string.
Definition kmp_adt.h:123
void drop_front(size_t n)
Drop the first n characters from the string.
Definition kmp_adt.h:80
void drop_while(const Fn &predicate)
Drop characters from the string while the predicate returns true.
Definition kmp_adt.h:88
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 i
Definition kmp_stub.cpp:87