LLVM 22.0.0git
IntegerInclusiveInterval.cpp
Go to the documentation of this file.
1//===- IntegerInclusiveInterval.cpp -----------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements utilities for handling lists of inclusive integer
10// intervals, such as parsing interval strings like "1-10,20-30,45", which are
11// used in debugging and bisection tools.
12//
13//===----------------------------------------------------------------------===//
14
17#include "llvm/Support/Error.h"
18#include "llvm/Support/Regex.h"
20#include <string>
21
22using namespace llvm;
23
25
27 IntervalList Intervals;
28
29 if (Str.empty())
30 return std::move(Intervals);
31
32 // Regex to match either single number or interval "num1-num2".
33 const Regex IntervalRegex("^([0-9]+)(-([0-9]+))?$");
34
35 for (StringRef Part : llvm::split(Str, Separator)) {
36 Part = Part.trim();
37 if (Part.empty())
38 continue;
39
41 if (!IntervalRegex.match(Part, &Matches))
42 return createStringError(std::errc::invalid_argument,
43 "Invalid interval format: '%s'",
44 Part.str().c_str());
45
46 int64_t Begin, End;
47 if (Matches[1].getAsInteger(10, Begin))
48 return createStringError(std::errc::invalid_argument,
49 "Failed to parse number: '%s'",
50 Matches[1].str().c_str());
51
52 if (!Matches[3].empty()) {
53 // Interval format "begin-end".
54 if (Matches[3].getAsInteger(10, End))
55 return createStringError(std::errc::invalid_argument,
56 "Failed to parse number: '%s'",
57 Matches[3].str().c_str());
58 if (Begin >= End)
59 return createStringError(std::errc::invalid_argument,
60 "Invalid interval: %lld >= %lld", Begin, End);
61 } else
62 // Single number.
63 End = Begin;
64
65 // Check ordering constraint (intervals must be in increasing order).
66 if (!Intervals.empty() && Begin <= Intervals.back().getEnd())
67 return createStringError(
68 std::errc::invalid_argument,
69 "Expected intervals to be in increasing order: %lld <= %lld", Begin,
70 Intervals.back().getEnd());
71
72 Intervals.push_back(IntegerInclusiveInterval(Begin, End));
73 }
74
75 return Intervals;
76}
77
79 for (const IntegerInclusiveInterval &It : Intervals) {
80 if (It.contains(Value))
81 return true;
82 }
83 return false;
84}
85
88 char Separator) {
89 if (Intervals.empty()) {
90 OS << "empty";
91 return;
92 }
93
94 std::string Sep(1, Separator);
95 ListSeparator LS(Sep);
96 for (const IntegerInclusiveInterval &It : Intervals) {
97 OS << LS;
98 It.print(OS);
99 }
100}
101
104 if (Intervals.empty())
105 return {};
106
107 IntervalList Result;
108 Result.push_back(Intervals[0]);
109
110 for (const IntegerInclusiveInterval &Current : Intervals.drop_front()) {
111 IntegerInclusiveInterval &Last = Result.back();
112 // Check if current interval is adjacent to the last merged interval.
113 if (Current.getBegin() == Last.getEnd() + 1) {
114 // Merge by extending the end of the last interval.
115 Last.setEnd(Current.getEnd());
116 } else {
117 // Not adjacent, add as separate interval.
118 Result.push_back(Current);
119 }
120 }
121
122 return Result;
123}
124
125} // end namespace llvm::IntegerInclusiveIntervalUtils
This file contains some functions that are useful when dealing with strings.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition ArrayRef.h:195
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
Tagged union holding either a T or a Error.
Definition Error.h:485
Represents an inclusive integer interval [Begin, End] where Begin <= End.
A helper class to return the specified delimiter string after the first invocation of operator String...
LLVM_ABI bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr, std::string *Error=nullptr) const
matches - Match the regex against a given String.
Definition Regex.cpp:83
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
void printIntervals(raw_ostream &OS, ArrayRef< IntegerInclusiveInterval > Intervals, char Separator=',')
Print intervals to output stream.
IntervalList mergeAdjacentIntervals(ArrayRef< IntegerInclusiveInterval > Intervals)
Merge adjacent/consecutive intervals into single intervals.
bool contains(ArrayRef< IntegerInclusiveInterval > Intervals, int64_t Value)
Check if a value is contained in any of the intervals.
SmallVector< IntegerInclusiveInterval, 8 > IntervalList
A list of integer intervals.
Expected< IntervalList > parseIntervals(StringRef IntervalStr, char Separator=',')
Parse a interval specification string like "1-10,20-30,45" or "1-10:20-30:45".
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
iterator_range< SplittingIterator > split(StringRef Str, StringRef Separator)
Split the specified string over a separator and return a range-compatible iterable over its partition...