1*06f32e7eSjoerg //===- llvm/Support/StringSaver.h -------------------------------*- C++ -*-===// 2*06f32e7eSjoerg // 3*06f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*06f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information. 5*06f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*06f32e7eSjoerg // 7*06f32e7eSjoerg //===----------------------------------------------------------------------===// 8*06f32e7eSjoerg 9*06f32e7eSjoerg #ifndef LLVM_SUPPORT_STRINGSAVER_H 10*06f32e7eSjoerg #define LLVM_SUPPORT_STRINGSAVER_H 11*06f32e7eSjoerg 12*06f32e7eSjoerg #include "llvm/ADT/DenseSet.h" 13*06f32e7eSjoerg #include "llvm/ADT/StringRef.h" 14*06f32e7eSjoerg #include "llvm/ADT/Twine.h" 15*06f32e7eSjoerg #include "llvm/Support/Allocator.h" 16*06f32e7eSjoerg 17*06f32e7eSjoerg namespace llvm { 18*06f32e7eSjoerg 19*06f32e7eSjoerg /// Saves strings in the provided stable storage and returns a 20*06f32e7eSjoerg /// StringRef with a stable character pointer. 21*06f32e7eSjoerg class StringSaver final { 22*06f32e7eSjoerg BumpPtrAllocator &Alloc; 23*06f32e7eSjoerg 24*06f32e7eSjoerg public: StringSaver(BumpPtrAllocator & Alloc)25*06f32e7eSjoerg StringSaver(BumpPtrAllocator &Alloc) : Alloc(Alloc) {} 26*06f32e7eSjoerg 27*06f32e7eSjoerg // All returned strings are null-terminated: *save(S).end() == 0. save(const char * S)28*06f32e7eSjoerg StringRef save(const char *S) { return save(StringRef(S)); } 29*06f32e7eSjoerg StringRef save(StringRef S); save(const Twine & S)30*06f32e7eSjoerg StringRef save(const Twine &S) { return save(StringRef(S.str())); } save(const std::string & S)31*06f32e7eSjoerg StringRef save(const std::string &S) { return save(StringRef(S)); } 32*06f32e7eSjoerg }; 33*06f32e7eSjoerg 34*06f32e7eSjoerg /// Saves strings in the provided stable storage and returns a StringRef with a 35*06f32e7eSjoerg /// stable character pointer. Saving the same string yields the same StringRef. 36*06f32e7eSjoerg /// 37*06f32e7eSjoerg /// Compared to StringSaver, it does more work but avoids saving the same string 38*06f32e7eSjoerg /// multiple times. 39*06f32e7eSjoerg /// 40*06f32e7eSjoerg /// Compared to StringPool, it performs fewer allocations but doesn't support 41*06f32e7eSjoerg /// refcounting/deletion. 42*06f32e7eSjoerg class UniqueStringSaver final { 43*06f32e7eSjoerg StringSaver Strings; 44*06f32e7eSjoerg llvm::DenseSet<llvm::StringRef> Unique; 45*06f32e7eSjoerg 46*06f32e7eSjoerg public: UniqueStringSaver(BumpPtrAllocator & Alloc)47*06f32e7eSjoerg UniqueStringSaver(BumpPtrAllocator &Alloc) : Strings(Alloc) {} 48*06f32e7eSjoerg 49*06f32e7eSjoerg // All returned strings are null-terminated: *save(S).end() == 0. save(const char * S)50*06f32e7eSjoerg StringRef save(const char *S) { return save(StringRef(S)); } 51*06f32e7eSjoerg StringRef save(StringRef S); save(const Twine & S)52*06f32e7eSjoerg StringRef save(const Twine &S) { return save(StringRef(S.str())); } save(const std::string & S)53*06f32e7eSjoerg StringRef save(const std::string &S) { return save(StringRef(S)); } 54*06f32e7eSjoerg }; 55*06f32e7eSjoerg 56*06f32e7eSjoerg } 57*06f32e7eSjoerg #endif 58