1 //===- Strings.h ------------------------------------------------*- 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 #ifndef LLD_STRINGS_H 10 #define LLD_STRINGS_H 11 12 #include "llvm/ADT/ArrayRef.h" 13 #include "llvm/ADT/SmallVector.h" 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/Support/GlobPattern.h" 16 #include <string> 17 #include <vector> 18 19 namespace lld { 20 21 llvm::SmallVector<uint8_t, 0> parseHex(llvm::StringRef s); 22 bool isValidCIdentifier(llvm::StringRef s); 23 24 // Write the contents of the a buffer to a file 25 void saveBuffer(llvm::StringRef buffer, const llvm::Twine &path); 26 27 // A single pattern to match against. A pattern can either be double-quoted 28 // text that should be matched exactly after removing the quoting marks or a 29 // glob pattern in the sense of GlobPattern. 30 class SingleStringMatcher { 31 public: 32 // Create a StringPattern from Pattern to be matched exactly regardless 33 // of globbing characters if ExactMatch is true. 34 SingleStringMatcher(llvm::StringRef Pattern); 35 36 // Match s against this pattern, exactly if ExactMatch is true. 37 bool match(llvm::StringRef s) const; 38 39 // Returns true for pattern "*" which will match all inputs. isTrivialMatchAll()40 bool isTrivialMatchAll() const { 41 return !ExactMatch && GlobPatternMatcher.isTrivialMatchAll(); 42 } 43 44 private: 45 // Whether to do an exact match regardless of wildcard characters. 46 bool ExactMatch; 47 48 // GlobPattern object if not doing an exact match. 49 llvm::GlobPattern GlobPatternMatcher; 50 51 // StringRef to match exactly if doing an exact match. 52 llvm::StringRef ExactPattern; 53 }; 54 55 // This class represents multiple patterns to match against. A pattern can 56 // either be a double-quoted text that should be matched exactly after removing 57 // the quoted marks or a glob pattern. 58 class StringMatcher { 59 private: 60 // Patterns to match against. 61 std::vector<SingleStringMatcher> patterns; 62 63 public: 64 StringMatcher() = default; 65 66 // Matcher for a single pattern. StringMatcher(llvm::StringRef Pattern)67 StringMatcher(llvm::StringRef Pattern) 68 : patterns({SingleStringMatcher(Pattern)}) {} 69 70 // Add a new pattern to the existing ones to match against. addPattern(SingleStringMatcher Matcher)71 void addPattern(SingleStringMatcher Matcher) { patterns.push_back(Matcher); } 72 empty()73 bool empty() const { return patterns.empty(); } 74 75 // Match s against the patterns. 76 bool match(llvm::StringRef s) const; 77 }; 78 79 } // namespace lld 80 81 #endif 82