Skip to content

Commit ded092e

Browse files
committed
Add examples
1 parent aab2708 commit ded092e

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ if(BUILD_TESTS)
3939
enable_testing()
4040
add_subdirectory(test)
4141
endif()
42+
43+
option(BUILD_EXAMPLES "Whether to build the examples" ON)
44+
if(BUILD_EXAMPLES)
45+
add_subdirectory(example)
46+
endif()

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ This is a header-only library, so if you don't want to build, install, and link
8787
Create an empty directory to use with CMake, and run CMake there with the path to this repository.
8888
You can simply create a `build` subfolder within this repository and run `cmake ..` or the CMake GUI.
8989
Specify the installation path of your choice with [`-DCMAKE_INSTALL_PREFIX=path`](https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html) if the default on your system is not to your liking.
90-
The tests are enabled by default, but if you aren't interested in those, set `-DBUILD_TESTS=OFF`.
9190
Also, be sure to set [`CMAKE_BUILD_TYPE`](https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE) appropriately.
9291

92+
The tests and examples are built by default, but if you aren't interested in those, set `-DBUILD_TESTS=OFF` and/or `-DBUILD_EXAMPLES=OFF`.
93+
9394
Then, simply build with the generator of your choice, or ask CMake to do it for you with `cmake --build .`.
9495
Since this is a header-only library, the build step is only really for building the tests.
9596

example/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
macro(simple_example _name)
3+
add_executable(example-${_name}
4+
"${_name}.cpp"
5+
)
6+
target_link_libraries(example-${_name}
7+
PUBLIC
8+
utf
9+
)
10+
endmacro()
11+
12+
simple_example(num_code_points)
13+
simple_example(encode_all)

example/encode_all.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "utf.hpp"
2+
3+
#include <cstdint>
4+
#include <cstdlib>
5+
#include <fstream>
6+
7+
/**
8+
* Outputs a file with all code points up to 10FFFF
9+
*/
10+
int main()
11+
{
12+
if(std::ofstream out {"encode_all.txt", std::ios::out|std::ios::binary|std::ios::trunc})
13+
{
14+
for(std::uint32_t cp = 0; cp <= 0x10FFFF; ++cp)
15+
{
16+
out << cp << ": \"" << LB::utf::encode_code_point<char>(cp) << "\"\n";
17+
}
18+
19+
return EXIT_SUCCESS;
20+
}
21+
return EXIT_FAILURE;
22+
}

example/num_code_points.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include "utf.hpp"
2+
3+
#include <cstdint>
4+
#include <cstdlib>
5+
#include <functional>
6+
#include <fstream>
7+
#include <iostream>
8+
#include <iterator>
9+
#include <string>
10+
#include <utility>
11+
12+
/**
13+
* Give a UTF-8 filename for a count of valid code points and invalid code units
14+
*/
15+
int main(int nargs, char const *const *args)
16+
{
17+
if(nargs != 2)
18+
{
19+
std::cerr << "Please pass the filename as an argument" << std::endl;
20+
return EXIT_FAILURE;
21+
}
22+
std::string const utf_string = [&]
23+
{
24+
if(std::ifstream in {args[1], std::ios::in|std::ios::binary})
25+
{
26+
return std::string(std::istreambuf_iterator<char>(in), {});
27+
}
28+
std::cerr << "Cannot find file: " << args[1] << std::endl;
29+
std::exit(EXIT_FAILURE);
30+
}();
31+
32+
std::size_t valid = 0
33+
, invalid = 0;
34+
for(auto it = std::cbegin(utf_string), end = std::cend(utf_string); it != end; )
35+
{
36+
std::uint32_t cp {};
37+
std::size_t num_code_units {};
38+
auto r = std::make_pair(std::ref(it), std::ref(num_code_units));
39+
r = LB::utf::read_code_point(it, end, cp);
40+
41+
if(num_code_units)
42+
{
43+
++valid;
44+
}
45+
else
46+
{
47+
++invalid;
48+
++it;
49+
}
50+
}
51+
52+
std::cout
53+
<< "Number of original code units: " << utf_string.size()
54+
<< '\n'
55+
<< "Number of valid code points: " << valid
56+
<< '\n'
57+
<< "Number of invalid code units: " << invalid
58+
<< std::endl;
59+
return EXIT_SUCCESS;
60+
}

0 commit comments

Comments
 (0)