[clang][bytecode][NFC] Only get expr when checking for UB (#125397)
[llvm-project.git] / llvm / lib / Support / OptionStrCmp.cpp
blob8e4892fcb9e6814f37bab1148c8fdee6b1a5150d
1 //===- OptionStrCmp.cpp - Option String Comparison --------------*- 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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/OptionStrCmp.h"
10 #include "llvm/ADT/STLExtras.h"
12 using namespace llvm;
14 // Comparison function for Option strings (option names & prefixes).
15 // The ordering is *almost* case-insensitive lexicographic, with an exception.
16 // '\0' comes at the end of the alphabet instead of the beginning (thus options
17 // precede any other options which prefix them). Additionally, if two options
18 // are identical ignoring case, they are ordered according to case sensitive
19 // ordering if `FallbackCaseSensitive` is true.
20 int llvm::StrCmpOptionName(StringRef A, StringRef B,
21 bool FallbackCaseSensitive) {
22 size_t MinSize = std::min(A.size(), B.size());
23 if (int Res = A.substr(0, MinSize).compare_insensitive(B.substr(0, MinSize)))
24 return Res;
26 // If they are identical ignoring case, use case sensitive ordering.
27 if (A.size() == B.size())
28 return FallbackCaseSensitive ? A.compare(B) : 0;
30 return (A.size() == MinSize) ? 1 /* A is a prefix of B. */
31 : -1 /* B is a prefix of A */;
34 // Comparison function for Option prefixes.
35 int llvm::StrCmpOptionPrefixes(ArrayRef<StringRef> APrefixes,
36 ArrayRef<StringRef> BPrefixes) {
37 for (const auto &[APre, BPre] : zip(APrefixes, BPrefixes)) {
38 if (int Cmp = StrCmpOptionName(APre, BPre))
39 return Cmp;
41 // Both prefixes are identical.
42 return 0;