153fe4691SRui Ueyama //===- Strings.cpp -------------------------------------------------------===//
253fe4691SRui Ueyama //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
653fe4691SRui Ueyama //
753fe4691SRui Ueyama //===----------------------------------------------------------------------===//
853fe4691SRui Ueyama 
953fe4691SRui Ueyama #include "lld/Common/Strings.h"
10ee173718SRui Ueyama #include "lld/Common/ErrorHandler.h"
11ee173718SRui Ueyama #include "lld/Common/LLVM.h"
12f6417f5dSSimon Pilgrim #include "llvm/Support/FileSystem.h"
13ee173718SRui Ueyama #include "llvm/Support/GlobPattern.h"
14ee173718SRui Ueyama #include <algorithm>
15ee173718SRui Ueyama #include <mutex>
16ee173718SRui Ueyama #include <vector>
17ee173718SRui Ueyama 
1853fe4691SRui Ueyama using namespace llvm;
1953fe4691SRui Ueyama using namespace lld;
2053fe4691SRui Ueyama 
SingleStringMatcher(StringRef Pattern)21c42fe247SThomas Preud'homme SingleStringMatcher::SingleStringMatcher(StringRef Pattern) {
22c42fe247SThomas Preud'homme   if (Pattern.size() > 2 && Pattern.startswith("\"") &&
23c42fe247SThomas Preud'homme       Pattern.endswith("\"")) {
24c42fe247SThomas Preud'homme     ExactMatch = true;
25c42fe247SThomas Preud'homme     ExactPattern = Pattern.substr(1, Pattern.size() - 2);
26c42fe247SThomas Preud'homme   } else {
27c42fe247SThomas Preud'homme     Expected<GlobPattern> Glob = GlobPattern::create(Pattern);
28c42fe247SThomas Preud'homme     if (!Glob) {
29c42fe247SThomas Preud'homme       error(toString(Glob.takeError()));
30c42fe247SThomas Preud'homme       return;
31c42fe247SThomas Preud'homme     }
32c42fe247SThomas Preud'homme     ExactMatch = false;
33c42fe247SThomas Preud'homme     GlobPatternMatcher = *Glob;
34ee173718SRui Ueyama   }
35ee173718SRui Ueyama }
36ee173718SRui Ueyama 
match(StringRef s) const37c42fe247SThomas Preud'homme bool SingleStringMatcher::match(StringRef s) const {
38c42fe247SThomas Preud'homme   return ExactMatch ? (ExactPattern == s) : GlobPatternMatcher.match(s);
39c42fe247SThomas Preud'homme }
40c42fe247SThomas Preud'homme 
match(StringRef s) const41136d27abSRui Ueyama bool StringMatcher::match(StringRef s) const {
42c42fe247SThomas Preud'homme   for (const SingleStringMatcher &pat : patterns)
43136d27abSRui Ueyama     if (pat.match(s))
44ee173718SRui Ueyama       return true;
45ee173718SRui Ueyama   return false;
46ee173718SRui Ueyama }
47ee173718SRui Ueyama 
48ee173718SRui Ueyama // Converts a hex string (e.g. "deadbeef") to a vector.
parseHex(StringRef s)49136d27abSRui Ueyama std::vector<uint8_t> lld::parseHex(StringRef s) {
50136d27abSRui Ueyama   std::vector<uint8_t> hex;
51136d27abSRui Ueyama   while (!s.empty()) {
52136d27abSRui Ueyama     StringRef b = s.substr(0, 2);
53136d27abSRui Ueyama     s = s.substr(2);
54136d27abSRui Ueyama     uint8_t h;
55136d27abSRui Ueyama     if (!to_integer(b, h, 16)) {
56136d27abSRui Ueyama       error("not a hexadecimal value: " + b);
57ee173718SRui Ueyama       return {};
58ee173718SRui Ueyama     }
59136d27abSRui Ueyama     hex.push_back(h);
60ee173718SRui Ueyama   }
61136d27abSRui Ueyama   return hex;
62ee173718SRui Ueyama }
63ee173718SRui Ueyama 
64ee173718SRui Ueyama // Returns true if S is valid as a C language identifier.
isValidCIdentifier(StringRef s)65136d27abSRui Ueyama bool lld::isValidCIdentifier(StringRef s) {
66*0890b39eSFangrui Song   return !s.empty() && !isDigit(s[0]) &&
67*0890b39eSFangrui Song          llvm::all_of(s, [](char c) { return isAlnum(c) || c == '_'; });
68ee173718SRui Ueyama }
693ad27e92SSam Clegg 
703ad27e92SSam Clegg // Write the contents of the a buffer to a file
saveBuffer(StringRef buffer,const Twine & path)71136d27abSRui Ueyama void lld::saveBuffer(StringRef buffer, const Twine &path) {
72136d27abSRui Ueyama   std::error_code ec;
73d9b948b6SFangrui Song   raw_fd_ostream os(path.str(), ec, sys::fs::OpenFlags::OF_None);
74136d27abSRui Ueyama   if (ec)
75136d27abSRui Ueyama     error("cannot create " + path + ": " + ec.message());
76136d27abSRui Ueyama   os << buffer;
773ad27e92SSam Clegg }
78