1 //===- Strings.cpp -------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 // If an input string is in the form of "foo.N" where N is a number,
42 // return N. Otherwise, returns 65536, which is one greater than the
44 int elf::getPriority(StringRef S) {
45 size_t Pos = S.rfind('.');
46 if (Pos == StringRef::npos)
49 if (S.substr(Pos + 1).getAsInteger(10, V))
54 bool elf::hasWildcard(StringRef S) {
55 return S.find_first_of("?*[") != StringRef::npos;
58 StringRef elf::unquote(StringRef S) {
59 if (!S.startswith("\""))
61 return S.substr(1, S.size() - 2);
64 // Converts a hex string (e.g. "deadbeef") to a vector.
65 std::vector<uint8_t> elf::parseHex(StringRef S) {
66 std::vector<uint8_t> Hex;
68 StringRef B = S.substr(0, 2);
71 if (B.getAsInteger(16, H)) {
72 error("not a hexadecimal value: " + B);
80 static bool isAlpha(char C) {
81 return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z') || C == '_';
84 static bool isAlnum(char C) { return isAlpha(C) || ('0' <= C && C <= '9'); }
86 // Returns true if S is valid as a C language identifier.
87 bool elf::isValidCIdentifier(StringRef S) {
88 return !S.empty() && isAlpha(S[0]) &&
89 std::all_of(S.begin() + 1, S.end(), isAlnum);
92 // Returns the demangled C++ symbol name for Name.
93 Optional<std::string> elf::demangle(StringRef Name) {
94 // __cxa_demangle can be used to demangle strings other than symbol
95 // names which do not necessarily start with "_Z". Name can be
96 // either a C or C++ symbol. Don't call __cxa_demangle if the name
97 // does not look like a C++ symbol name to avoid getting unexpected
98 // result for a C symbol that happens to match a mangled type name.
99 if (!Name.startswith("_Z"))
102 char *Buf = itaniumDemangle(Name.str().c_str(), nullptr, nullptr, nullptr);