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 "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Config/config.h"
23 using namespace lld::elf;
25 // Returns true if S matches T. S can contain glob meta-characters.
26 // The asterisk ('*') matches zero or more characters, and the question
27 // mark ('?') matches one character.
28 bool elf::globMatch(StringRef S, StringRef T) {
35 // Fast path. If a pattern is '*', it matches anything.
37 for (size_t I = 0, E = T.size(); I < E; ++I)
38 if (globMatch(S, T.substr(I)))
42 if (T.empty() || (S[0] != T[0] && S[0] != '?'))
49 // Converts a hex string (e.g. "deadbeef") to a vector.
50 std::vector<uint8_t> elf::parseHex(StringRef S) {
51 std::vector<uint8_t> Hex;
53 StringRef B = S.substr(0, 2);
56 if (B.getAsInteger(16, H)) {
57 error("not a hexadecimal value: " + B);
65 static bool isAlpha(char C) {
66 return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z') || C == '_';
69 static bool isAlnum(char C) { return isAlpha(C) || ('0' <= C && C <= '9'); }
71 // Returns true if S is valid as a C language identifier.
72 bool elf::isValidCIdentifier(StringRef S) {
73 return !S.empty() && isAlpha(S[0]) &&
74 std::all_of(S.begin() + 1, S.end(), isAlnum);
77 // Returns the demangled C++ symbol name for Name.
78 std::string elf::demangle(StringRef Name) {
79 #if !defined(HAVE_CXXABI_H)
82 // __cxa_demangle can be used to demangle strings other than symbol
83 // names which do not necessarily start with "_Z". Name can be
84 // either a C or C++ symbol. Don't call __cxa_demangle if the name
85 // does not look like a C++ symbol name to avoid getting unexpected
86 // result for a C symbol that happens to match a mangled type name.
87 if (!Name.startswith("_Z"))
91 abi::__cxa_demangle(Name.str().c_str(), nullptr, nullptr, nullptr);