Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
Simplify the Range Adaptor section and refer to the documentation page that discusses it.
  • Loading branch information
paulhuggett authored Jan 11, 2024
1 parent 3d0ea2d commit cf9d33a
Showing 1 changed file with 2 additions and 36 deletions.
38 changes: 2 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ C++ 17 [deprecated the standard library's `<codecvt>` header file](https://www.o

There are broadly three ways to use the icubaby library:

1. C++ 20 Range Adaptor
1. [C++ 20 Range Adaptor](#c-20-range-adaptor)
2. An iterator interface
3. Manually driving the conversion

Expand All @@ -37,41 +37,7 @@ std::vector<char16_t> out;
std::ranges::copy(r, std::back_inserter(out));
~~~
This code wants to convert a single Unicode code-point 😀 (U+1F600 GRINNING FACE) from UTF-32 to UTF-16.
As in the previous examples, the `out` vector will contain a two UTF-16 code units 0xD83D and 0xDE00.
#### Disecting this code
1. Define the input range:
~~~cpp
auto const in = std::array{char32_t{0x1F600}};
~~~
We express the input as a container with our input text consisting simply of U+1F600 GRINNING FACE expressed in UTF-32.
2. Create a range with a view of our container and pass it to the icubaby `transcode` range adaptor:
~~~cpp
auto r = in | icubaby::ranges::transcode<char32_t, char16_t>;
~~~
The first template argument for icubaby::ranges::transcode<> is the encoding of the input text (one of `icubaby::char8`, `char16_t`, `char32_t`), the second argument is the desired encoding of the output text.
We now have the range `r` containing the UTF-16 code-units that correspond to the original input text.
3. The final step is to record the values within the range `r`. In C++ 20, this can be achieved with the [std::ranges::copy()](https://en.cppreference.com/w/cpp/algorithm/ranges/copy) algorithm:
~~~cpp
std::vector<char16_t> out;
std::ranges::copy(r, std::back_inserter(out));
~~~
If you are using the C++ 23 ranges library, you can simplify this even further using [std::ranges::to()](https://en.cppreference.com/w/cpp/ranges/to):
~~~cpp
auto const out = r | std::ranges::to<std::vector> ();
~~~
This code converts a single Unicode code-point 😀 (U+1F600 GRINNING FACE) from UTF-32 to UTF-16 and will copy two UTF-16 code-units (0xD83D and 0xDE00) into the `out` vector. See the [C++20 Range Adaptor documentation](https://paulhuggett.github.io/icubaby/cxx20-range-adaptor.html) for more details.
### The Iterator Interface
Expand Down

0 comments on commit cf9d33a

Please sign in to comment.