1 //===- Strings.cpp -------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "lld/Common/ErrorHandler.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Demangle/Demangle.h"
22 using namespace lld::elf;
24 StringMatcher::StringMatcher(ArrayRef<StringRef> Pat) {
25 for (StringRef S : Pat) {
26 Expected<GlobPattern> Pat = GlobPattern::create(S);
28 error(toString(Pat.takeError()));
30 Patterns.push_back(*Pat);
34 bool StringMatcher::match(StringRef S) const {
35 for (const GlobPattern &Pat : Patterns)
41 // Converts a hex string (e.g. "deadbeef") to a vector.
42 std::vector<uint8_t> elf::parseHex(StringRef S) {
43 std::vector<uint8_t> Hex;
45 StringRef B = S.substr(0, 2);
48 if (!to_integer(B, H, 16)) {
49 error("not a hexadecimal value: " + B);
57 // Returns true if S is valid as a C language identifier.
58 bool elf::isValidCIdentifier(StringRef S) {
59 return !S.empty() && (isAlpha(S[0]) || S[0] == '_') &&
60 std::all_of(S.begin() + 1, S.end(),
61 [](char C) { return C == '_' || isAlnum(C); });