/src/llvm-project/llvm/lib/Support/StringSaver.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- StringSaver.cpp ---------------------------------------------------===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "llvm/Support/StringSaver.h" |
10 | | |
11 | | #include "llvm/ADT/SmallString.h" |
12 | | |
13 | | using namespace llvm; |
14 | | |
15 | 2.89k | StringRef StringSaver::save(StringRef S) { |
16 | 2.89k | char *P = Alloc.Allocate<char>(S.size() + 1); |
17 | 2.89k | if (!S.empty()) |
18 | 2.88k | memcpy(P, S.data(), S.size()); |
19 | 2.89k | P[S.size()] = '\0'; |
20 | 2.89k | return StringRef(P, S.size()); |
21 | 2.89k | } |
22 | | |
23 | 966 | StringRef StringSaver::save(const Twine &S) { |
24 | 966 | SmallString<128> Storage; |
25 | 966 | return save(S.toStringRef(Storage)); |
26 | 966 | } |
27 | | |
28 | 3.81k | StringRef UniqueStringSaver::save(StringRef S) { |
29 | 3.81k | auto R = Unique.insert(S); |
30 | 3.81k | if (R.second) // cache miss, need to actually save the string |
31 | 1.92k | *R.first = Strings.save(S); // safe replacement with equal value |
32 | 3.81k | return *R.first; |
33 | 3.81k | } |
34 | | |
35 | 0 | StringRef UniqueStringSaver::save(const Twine &S) { |
36 | 0 | SmallString<128> Storage; |
37 | 0 | return save(S.toStringRef(Storage)); |
38 | 0 | } |