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 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Support/OptionStrCmp.h" 10 #include "llvm/ADT/STLExtras.h" 11 12 using namespace llvm; 13 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; 25 26 // If they are identical ignoring case, use case sensitive ordering. 27 if (A.size() == B.size()) 28 return FallbackCaseSensitive ? A.compare(B) : 0; 29 30 return (A.size() == MinSize) ? 1 /* A is a prefix of B. */ 31 : -1 /* B is a prefix of A */; 32 } 33 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; 40 } 41 // Both prefixes are identical. 42 return 0; 43 } 44