1 //===- Strings.cpp -------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lld/Common/Strings.h"
10 #include "lld/Common/ErrorHandler.h"
11 #include "lld/Common/LLVM.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/GlobPattern.h"
22 SingleStringMatcher::SingleStringMatcher(StringRef Pattern
) {
23 if (Pattern
.size() > 2 && Pattern
.starts_with("\"") &&
24 Pattern
.ends_with("\"")) {
26 ExactPattern
= Pattern
.substr(1, Pattern
.size() - 2);
28 Expected
<GlobPattern
> Glob
= GlobPattern::create(Pattern
);
30 error(toString(Glob
.takeError()) + ": " + Pattern
);
34 GlobPatternMatcher
= *Glob
;
38 bool SingleStringMatcher::match(StringRef s
) const {
39 return ExactMatch
? (ExactPattern
== s
) : GlobPatternMatcher
.match(s
);
42 bool StringMatcher::match(StringRef s
) const {
43 for (const SingleStringMatcher
&pat
: patterns
)
49 // Converts a hex string (e.g. "deadbeef") to a vector.
50 SmallVector
<uint8_t, 0> lld::parseHex(StringRef s
) {
51 SmallVector
<uint8_t, 0> hex
;
53 StringRef b
= s
.substr(0, 2);
56 if (!to_integer(b
, h
, 16)) {
57 error("not a hexadecimal value: " + b
);
65 // Returns true if S is valid as a C language identifier.
66 bool lld::isValidCIdentifier(StringRef s
) {
67 return !s
.empty() && !isDigit(s
[0]) &&
68 llvm::all_of(s
, [](char c
) { return isAlnum(c
) || c
== '_'; });
71 // Write the contents of the a buffer to a file
72 void lld::saveBuffer(StringRef buffer
, const Twine
&path
) {
74 raw_fd_ostream
os(path
.str(), ec
, sys::fs::OpenFlags::OF_None
);
76 error("cannot create " + path
+ ": " + ec
.message());