Skip to content

Commit 0690d6d

Browse files
committed
Add examples with LLVM's StringRef
Also adds LLVM's libLLVMSUpport as a dependency.
1 parent b520d8a commit 0690d6d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ target_compile_options(${executable} PRIVATE
235235
"$<$<CXX_COMPILER_ID:MSVC>:${cpp-tutor_COMPILER_OPTIONS_MSVC}>")
236236
endforeach()
237237

238+
target_link_libraries(strings_1
239+
LLVMSupport
240+
)
241+
238242

239243
# EXTRA SET-UP FOR UNIT TESTS
240244
# ===========================
@@ -258,4 +262,4 @@ option(RUNTIME_ERROR "Enable code that leads to run-time errors (e.g. UB)." OFF)
258262
configure_file(
259263
${PROJECT_SOURCE_DIR}/include/cppt_ag.hpp.in
260264
${PROJECT_BINARY_DIR}/include/cppt_ag.hpp
261-
)
265+
)

src/strings_1_main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include <cppt_ag.hpp>
1818
#include <cppt_tools.hpp>
1919

20+
#include "llvm/Support/raw_ostream.h"
21+
#include <llvm/ADT/StringRef.h>
22+
2023
#include <algorithm>
2124
#include <iostream>
2225
#include <string>
@@ -37,6 +40,7 @@ int main(int argc, const char** argv) {
3740
const char* hello_c_c = "Hello World!";
3841
std::string hello_cpp(hello_c);
3942
std::string_view hello_sv(hello_cpp);
43+
llvm::StringRef hello_sr(hello_cpp);
4044

4145
//-------------------------------------------------------------------------
4246
// 2. PRINT THE STRINGS
@@ -45,6 +49,11 @@ int main(int argc, const char** argv) {
4549
std::cout << "C-string (const char*): " << hello_c_c << std::endl;
4650
std::cout << "C++ string: " << hello_cpp << std::endl;
4751
std::cout << "C++ string_view: " << hello_sv << std::endl;
52+
// As std::cout _does not_ understand llvm::StringRef, you need to grab the
53+
// underlying string for printing.
54+
std::cout << "C++ StringRef (via std::cout): " << hello_sr.str() << std::endl;
55+
// However, llvm::outs() _does_ understand llvm:StringRef.
56+
llvm::outs() << "C++ StringRef (via llvm::outs): " << hello_sr << "\n";
4857
std::cout << std::endl;
4958

5059
//-------------------------------------------------------------------------
@@ -54,6 +63,7 @@ int main(int argc, const char** argv) {
5463
std::cout << "Size of hello_c_c: " << sizeof(hello_c_c) << std::endl;
5564
std::cout << "Size of hello_cpp: " << sizeof(hello_cpp) << std::endl;
5665
std::cout << "Size of hello_sv: " << sizeof(hello_sv) << std::endl;
66+
std::cout << "Size of hello_sr: " << sizeof(hello_sr) << std::endl;
5767
std::cout << std::endl;
5868

5969
//-------------------------------------------------------------------------

0 commit comments

Comments
 (0)