Skip to content

Commit

Permalink
Use std::format() API now, instead of fmtlib
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Parpart <[email protected]>
  • Loading branch information
christianparpart committed Sep 22, 2024
1 parent 28aacf5 commit df2ccfb
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 138 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os_version: ['20.04', '22.04', '24.04']
os_version: ['22.04', '24.04']
name: "Ubuntu ${{ matrix.os_version }}"
runs-on: ubuntu-${{ matrix.os_version }}
steps:
Expand All @@ -48,6 +48,9 @@ jobs:
run: mkdir build
- name: "cmake"
run: |
if [[ '${{ matrix.os_version }}' == '22.04' ]]; then
export CXX=clang++-16
fi
cmake -S . -B build -G Ninja \
-D CMAKE_BUILD_TYPE="RelWithDebInfo" \
-D LIBUNICODE_BENCHMARK=ON \
Expand Down
8 changes: 0 additions & 8 deletions cmake/ThirdParties.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ if(LIBUNICODE_BENCHMARK)
endif()


if(TARGET fmt)
set(THIRDPARTY_BUILTIN_fmt "embedded")
else()
find_package(fmt REQUIRED)
set(THIRDPARTY_BUILTIN_fmt "system package")
endif()

macro(ThirdPartiesSummary2)
message(STATUS "==============================================================================")
message(STATUS " ThirdParties")
Expand All @@ -71,6 +64,5 @@ endif()
if(LIBUNICODE_BENCHMARK)
message(STATUS "Benchmark ${THIRDPARTY_BUILTIN_benchmark}")
endif()
message(STATUS "fmt ${THIRDPARTY_BUILTIN_fmt}")
message(STATUS "------------------------------------------------------------------------------")
endmacro()
39 changes: 22 additions & 17 deletions scripts/install-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ fetch_and_unpack_benchmark()
}


fetch_and_unpack_fmtlib()
{
fetch_and_unpack \
fmt-9.1.0 \
fmtlib-9.1.0.tar.gz \
https://github.com/fmtlib/fmt/archive/refs/tags/9.1.0.tar.gz
}

prepare_fetch_and_unpack()
{
mkdir -p "${SYSDEPS_BASE_DIR}"
Expand All @@ -98,14 +90,23 @@ prepare_fetch_and_unpack()
}
# }}}

get_llvm()
{
local VERSION=$1
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh $VERSION
rm llvm.sh
sudo apt-get install -y libc++-$VERSION-dev libc++abi-$VERSION-dev clang-$VERSION
}

install_deps_ubuntu()
{
local packages="
build-essential
cmake
debhelper
dpkg-dev
g++
libc6-dev
make
ninja-build
Expand All @@ -116,12 +117,21 @@ install_deps_ubuntu()
local NAME=`grep ^NAME /etc/os-release | cut -d= -f2 | cut -f1 | tr -d '"'`

case $RELEASE in
"20.04" | "22.04" | "24.04")
fetch_and_unpack_fmtlib
"20.04")
fetch_and_unpack_Catch2
get_llvm 16
;;
"22.04")
fetch_and_unpack_Catch2
get_llvm 16
;;
"24.04")
fetch_and_unpack_Catch2
packages="$packages g++-14"
;;
*)
packages="$packages libfmt-dev catch2"
packages="$packages g++"
packages="$packages catch2"
;;
esac

Expand All @@ -142,7 +152,6 @@ install_deps_FreeBSD()
su root -c "pkg install $SYSDEP_ASSUME_YES \
catch \
cmake \
libfmt \
ninja \
pkgconf \
range-v3
Expand All @@ -151,7 +160,6 @@ install_deps_FreeBSD()

install_deps_arch()
{
fetch_and_unpack_fmtlib
fetch_and_unpack_benchmark
[ x$PREPARE_ONLY_EMBEDS = xON ] && return

Expand All @@ -170,7 +178,6 @@ install_deps_fedora()
local packages="
catch-devel
cmake
fmt-devel
gcc-c++
google-benchmark-devel
ninja-build
Expand All @@ -193,7 +200,6 @@ install_deps_darwin()
# NB: Also available in brew: mimalloc
# catch2: available in brew, but too new (version 3+)
brew install $SYSDEP_ASSUME_YES \
fmt \
ninja \
pkg-config \
range-v3
Expand Down Expand Up @@ -230,7 +236,6 @@ main()
;;
*)
fetch_and_unpack_Catch2
fetch_and_unpack_fmtlib
fetch_and_unpack_benchmark
echo "OS $ID not supported."
echo "Dependencies were fetch manually and most likely libunicode will compile."
Expand Down
2 changes: 1 addition & 1 deletion src/libunicode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ if(LIBUNICODE_TESTING)
target_include_directories(Catch2 SYSTEM INTERFACE ${CATCH2_INCLUDE_DIRS})
endif()

target_link_libraries(unicode_test unicode Catch2::Catch2WithMain fmt::fmt-header-only)
target_link_libraries(unicode_test unicode Catch2::Catch2WithMain)
add_test(unicode_test unicode_test)
endif()
# }}}
Expand Down
3 changes: 1 addition & 2 deletions src/libunicode/capi_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
*/
#include <libunicode/capi.h>

#include <fmt/format.h>

#include <catch2/catch_test_macros.hpp>

#include <array>
#include <format>
#include <utility>

using namespace std;
Expand Down
3 changes: 1 addition & 2 deletions src/libunicode/convert_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
#include <libunicode/support.h>
#include <libunicode/utf8.h>

#include <fmt/format.h>

#include <catch2/catch_test_macros.hpp>

#include <format>
#include <iterator>

using namespace unicode;
Expand Down
85 changes: 39 additions & 46 deletions src/libunicode/emoji_segmenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@

#include <libunicode/support.h>

#include <algorithm>
#include <array>
#include <format>
#include <ostream>
#include <utility>

namespace unicode
{
Expand Down Expand Up @@ -129,7 +127,6 @@ inline std::ostream& operator<<(std::ostream& os, EmojiSegmentationCategory valu
{
switch (value)
{
// clang-format off
case unicode::EmojiSegmentationCategory::Invalid: return os << "Invalid";
case unicode::EmojiSegmentationCategory::Emoji: return os << "Emoji";
case unicode::EmojiSegmentationCategory::EmojiTextPresentation: return os << "EmojiTextPresentation";
Expand All @@ -140,66 +137,62 @@ inline std::ostream& operator<<(std::ostream& os, EmojiSegmentationCategory valu
case unicode::EmojiSegmentationCategory::RegionalIndicator: return os << "RegionalIndicator";
case unicode::EmojiSegmentationCategory::KeyCapBase: return os << "KeyCapBase";
case unicode::EmojiSegmentationCategory::CombiningEnclosingKeyCap: return os << "CombiningEnclosingKeyCap";
case unicode::EmojiSegmentationCategory::CombiningEnclosingCircleBackslash: return os << "CombiningEnclosingCircleBackslash";
case unicode::EmojiSegmentationCategory::CombiningEnclosingCircleBackslash:
return os << "CombiningEnclosingCircleBackslash";
case unicode::EmojiSegmentationCategory::ZWJ: return os << "ZWJ";
case unicode::EmojiSegmentationCategory::VS15: return os << "VS15";
case unicode::EmojiSegmentationCategory::VS16: return os << "VS16";
case unicode::EmojiSegmentationCategory::TagBase: return os << "TagBase";
case unicode::EmojiSegmentationCategory::TagSequence: return os << "TagSequence";
case unicode::EmojiSegmentationCategory::TagTerm: return os << "TagTerm";
// clang-format off
}
return os;
}

} // namespace unicode

// clang-format off
#if __has_include(<fmt/ostream.h>)
#include <fmt/ostream.h>
#if FMT_VERSION >= (9 * 10000 + 1 * 100 + 0)
template <> struct fmt::formatter<unicode::PresentationStyle>: fmt::ostream_formatter {};


namespace fmt
{
template <>
struct formatter<unicode::EmojiSegmentationCategory>
struct std::formatter<unicode::PresentationStyle>: std::formatter<std::string_view>
{
template <typename ParseContext>
constexpr auto parse(ParseContext& ctx)
auto format(unicode::PresentationStyle value, format_context& ctx) const -> format_context::iterator
{
return ctx.begin();
string_view name;
switch (value)
{
case unicode::PresentationStyle::Text: name = "Text"; break;
case unicode::PresentationStyle::Emoji: name = "Emoji"; break;
}
return formatter<string_view>::format(name, ctx);
}
template <typename FormatContext>
auto format(unicode::EmojiSegmentationCategory value, FormatContext& ctx)
};

template <>
struct std::formatter<unicode::EmojiSegmentationCategory>: std::formatter<std::string_view>
{
auto format(unicode::EmojiSegmentationCategory value, format_context& ctx) const -> format_context::iterator
{
using unicode::EmojiSegmentationCategory;
string_view name;
switch (value)
{
// clang-format off
case unicode::EmojiSegmentationCategory::Invalid: return fmt::format_to(ctx.out(), "Invalid");
case unicode::EmojiSegmentationCategory::Emoji: return fmt::format_to(ctx.out(), "Emoji");
case unicode::EmojiSegmentationCategory::EmojiTextPresentation: return fmt::format_to(ctx.out(), "EmojiTextPresentation");
case unicode::EmojiSegmentationCategory::EmojiEmojiPresentation: return fmt::format_to(ctx.out(), "EmojiEmojiPresentation");
case unicode::EmojiSegmentationCategory::EmojiModifierBase: return fmt::format_to(ctx.out(), "EmojiModifierBase");
case unicode::EmojiSegmentationCategory::EmojiModifier: return fmt::format_to(ctx.out(), "EmojiModifier");
case unicode::EmojiSegmentationCategory::EmojiVSBase: return fmt::format_to(ctx.out(), "EmojiVSBase");
case unicode::EmojiSegmentationCategory::RegionalIndicator: return fmt::format_to(ctx.out(), "RegionalIndicator");
case unicode::EmojiSegmentationCategory::KeyCapBase: return fmt::format_to(ctx.out(), "KeyCapBase");
case unicode::EmojiSegmentationCategory::CombiningEnclosingKeyCap: return fmt::format_to(ctx.out(), "CombiningEnclosingKeyCap");
case unicode::EmojiSegmentationCategory::CombiningEnclosingCircleBackslash: return fmt::format_to(ctx.out(), "CombiningEnclosingCircleBackslash");
case unicode::EmojiSegmentationCategory::ZWJ: return fmt::format_to(ctx.out(), "ZWJ");
case unicode::EmojiSegmentationCategory::VS15: return fmt::format_to(ctx.out(), "VS15");
case unicode::EmojiSegmentationCategory::VS16: return fmt::format_to(ctx.out(), "VS16");
case unicode::EmojiSegmentationCategory::TagBase: return fmt::format_to(ctx.out(), "TagBase");
case unicode::EmojiSegmentationCategory::TagSequence: return fmt::format_to(ctx.out(), "TagSequence");
case unicode::EmojiSegmentationCategory::TagTerm: return fmt::format_to(ctx.out(), "TagTerm");
// clang-format off
case EmojiSegmentationCategory::Invalid: name = "Invalid"; break;
case EmojiSegmentationCategory::Emoji: name = "Emoji"; break;
case EmojiSegmentationCategory::EmojiTextPresentation: name = "EmojiTextPresentation"; break;
case EmojiSegmentationCategory::EmojiEmojiPresentation: name = "EmojiEmojiPresentation"; break;
case EmojiSegmentationCategory::EmojiModifierBase: name = "EmojiModifierBase"; break;
case EmojiSegmentationCategory::EmojiModifier: name = "EmojiModifier"; break;
case EmojiSegmentationCategory::EmojiVSBase: name = "EmojiVSBase"; break;
case EmojiSegmentationCategory::RegionalIndicator: name = "RegionalIndicator"; break;
case EmojiSegmentationCategory::KeyCapBase: name = "KeyCapBase"; break;
case EmojiSegmentationCategory::CombiningEnclosingKeyCap: name = "CombiningEnclosingKeyCap"; break;
case EmojiSegmentationCategory::CombiningEnclosingCircleBackslash: name = "CombiningEnclosingCircleBackslash"; break;
case EmojiSegmentationCategory::ZWJ: name = "ZWJ"; break;
case EmojiSegmentationCategory::VS15: name = "VS15"; break;
case EmojiSegmentationCategory::VS16: name = "VS16"; break;
case EmojiSegmentationCategory::TagBase: name = "TagBase"; break;
case EmojiSegmentationCategory::TagSequence: name = "TagSequence"; break;
case EmojiSegmentationCategory::TagTerm: name = "TagTerm"; break;
}
return fmt::format_to(ctx.out(), "({})", unsigned(value));
}
return formatter<string_view>::format(name, ctx);
}
};
}
#endif
#endif
// clang-format on
8 changes: 4 additions & 4 deletions src/libunicode/emoji_segmenter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
#include <libunicode/run_segmenter.h>
#include <libunicode/utf8.h>

#include <fmt/format.h>

#include <catch2/catch_test_macros.hpp>

#include <format>

using namespace unicode;
using namespace std::string_literals;
using namespace std;
Expand All @@ -46,14 +46,14 @@ void test_segments(int lineNo, std::vector<std::pair<std::u32string_view, Presen
i += text.size();
}

INFO(fmt::format("Testing emoji segmentation from line {}: {}", lineNo, to_utf8(fullText)));
INFO(std::format("Testing emoji segmentation from line {}: {}", lineNo, to_utf8(fullText)));

size_t size {};
auto presentationStyle = PresentationStyle {};
auto segmenter = unicode::emoji_segmenter { fullText };
for (size_t i = 0; i < expectations.size(); ++i)
{
INFO(fmt::format("run segmentation for part {}: \"{}\" to be {}",
INFO(std::format("run segmentation for part {}: \"{}\" to be {}",
i,
to_utf8(expectations[i].first),
(unsigned) expectations[i].second));
Expand Down
21 changes: 6 additions & 15 deletions src/libunicode/mktables.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,46 +212,37 @@ def __init__(self, _header_filename: str):
self.file.write('\n')
self.file.write('#include <libunicode/ucd_enums.h>\n')
self.file.write('\n')
self.file.write('#include <fmt/format.h>\n')
self.file.write('#include <format>\n')
self.file.write('\n')
self.file.write('namespace fmt\n{\n\n')

def begin(self, _enum_class, _first):
self.enum_class = 'unicode::' + _enum_class
self.file.write('template <>\n')
self.file.write('struct formatter<{}>\n{{\n'.format(self.enum_class))

self.file.write(' template <typename ParseContext>\n')
self.file.write(' constexpr auto parse(ParseContext& ctx)\n')
self.file.write(' {\n')
self.file.write(' return ctx.begin();\n')
self.file.write(' }\n')

self.file.write(' template <typename FormatContext>\n')
self.file.write(' auto format({} value, FormatContext& ctx)\n'.format(self.enum_class))
self.file.write('struct std::formatter<{}>: std::formatter<std::string_view>\n{{\n'.format(self.enum_class))
self.file.write(' auto format({} value, format_context& ctx) const -> format_context::iterator\n'.format(self.enum_class))
self.file.write(' {\n')
self.file.write(' std::string_view name;\n')
self.file.write(' switch (value)\n')
self.file.write(' {\n')
self.file.write(' // clang-format off\n')

def member(self, _member):
self.file.write(
' case {0}::{1}: return fmt::format_to(ctx.out(), "{2}");\n'.format(
' case {0}::{1}: name = "{2}"; break;\n'.format(
self.enum_class, sanitize_identifier(_member), _member)
)

def end(self):
self.file.write(' // clang-format off\n')
self.file.write(" }\n")
self.file.write(' return fmt::format_to(ctx.out(), "({})", unsigned(value));\n')
self.file.write(' return formatter<string_view>::format(name, ctx);\n')
self.file.write(" }\n")
self.file.write("};\n\n")

def output(self):
return self.filename

def close(self):
self.file.write("} // namespace fmt\n")
self.file.close()
# }}}
class MergedRange: # {{{
Expand Down
Loading

0 comments on commit df2ccfb

Please sign in to comment.