Skip to content

Commit

Permalink
Merge pull request #252 from boostorg/refactor
Browse files Browse the repository at this point in the history
Refactor for speed and consistency
  • Loading branch information
Flamefire authored Jan 11, 2025
2 parents 5a58e65 + e540a63 commit 6c890c1
Show file tree
Hide file tree
Showing 45 changed files with 432 additions and 388 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright 2020, 2021 Peter Dimov
# Copyright 2022-2024 Alexander Grund
# Copyright 2022-2025 Alexander Grund
# Distributed under the Boost Software License, Version 1.0.
# https://www.boost.org/LICENSE_1_0.txt

Expand Down Expand Up @@ -35,6 +35,7 @@ add_library(boost_locale
src/util/locale_data.cpp
src/util/make_std_unique.hpp
src/util/numeric.hpp
src/util/numeric_conversion.hpp
src/util/timezone.hpp
${headers}
)
Expand All @@ -50,8 +51,8 @@ target_link_libraries(boost_locale
Boost::config
Boost::core
Boost::iterator
Boost::utility
PRIVATE
Boost::charconv
Boost::predef
Boost::thread
)
Expand Down
3 changes: 2 additions & 1 deletion build.jam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright René Ferdinand Rivera Morell 2023-2024
# Copyright(c) 2025 Alexander Grund
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -20,7 +21,7 @@ constant boost_dependencies :
/boost/config//boost_config
/boost/core//boost_core
/boost/iterator//boost_iterator
/boost/utility//boost_utility ;
;

project /boost/locale
: common-requirements
Expand Down
1 change: 1 addition & 0 deletions build/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import toolset ;
path-constant TOP : .. ;

constant boost_dependencies_private :
/boost/charconv//boost_charconv
/boost/predef//boost_predef
/boost/thread//boost_thread
;
Expand Down
2 changes: 1 addition & 1 deletion include/boost/locale/boundary/boundary_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace boost { namespace locale { namespace boundary {

typedef boundary_point<std::string::const_iterator> sboundary_point; ///< convenience typedef
typedef boundary_point<std::wstring::const_iterator> wsboundary_point; ///< convenience typedef
#ifndef BOOST_LOCALE_NO_CXX20_STRING8
#ifdef __cpp_lib_char8_t
typedef boundary_point<std::u8string::const_iterator> u8sboundary_point; ///< convenience typedef
#endif
#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
Expand Down
4 changes: 2 additions & 2 deletions include/boost/locale/boundary/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ namespace boost { namespace locale { namespace boundary {

typedef segment_index<std::string::const_iterator> ssegment_index; ///< convenience typedef
typedef segment_index<std::wstring::const_iterator> wssegment_index; ///< convenience typedef
#ifndef BOOST_LOCALE_NO_CXX20_STRING8
#ifdef __cpp_lib_char8_t
typedef segment_index<std::u8string::const_iterator> u8ssegment_index; ///< convenience typedef
#endif
#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
Expand All @@ -892,7 +892,7 @@ namespace boost { namespace locale { namespace boundary {

typedef boundary_point_index<std::string::const_iterator> sboundary_point_index; ///< convenience typedef
typedef boundary_point_index<std::wstring::const_iterator> wsboundary_point_index; ///< convenience typedef
#ifndef BOOST_LOCALE_NO_CXX20_STRING8
#ifdef __cpp_lib_char8_t
typedef boundary_point_index<std::u8string::const_iterator> u8sboundary_point_index; ///< convenience typedef
#endif
#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
Expand Down
2 changes: 1 addition & 1 deletion include/boost/locale/boundary/segment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ namespace boost { namespace locale { namespace boundary {

typedef segment<std::string::const_iterator> ssegment; ///< convenience typedef
typedef segment<std::wstring::const_iterator> wssegment; ///< convenience typedef
#ifndef BOOST_LOCALE_NO_CXX20_STRING8
#ifdef __cpp_lib_char8_t
typedef segment<std::u8string::const_iterator> u8ssegment; ///< convenience typedef
#endif
#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
Expand Down
5 changes: 0 additions & 5 deletions include/boost/locale/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@
# define BOOST_LOCALE_NO_SANITIZE(what)
#endif

#if !defined(__cpp_lib_char8_t) || BOOST_WORKAROUND(BOOST_CLANG_VERSION, < 150000)
// No std::basic_string<char8_t> or bug in Clang: https://github.com/llvm/llvm-project/issues/55560
# define BOOST_LOCALE_NO_CXX20_STRING8
#endif

/// \endcond

#endif // boost/locale/config.hpp
8 changes: 4 additions & 4 deletions include/boost/locale/detail/any_string.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2023 Alexander Grund
// Copyright (c) 2023-2025 Alexander Grund
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
Expand All @@ -9,7 +9,7 @@

#include <boost/locale/config.hpp>
#include <boost/assert.hpp>
#include <boost/utility/string_view.hpp>
#include <boost/core/detail/string_view.hpp>
#include <memory>
#include <stdexcept>
#include <string>
Expand All @@ -31,7 +31,7 @@ namespace boost { namespace locale { namespace detail {
};
template<typename Char>
struct BOOST_SYMBOL_VISIBLE impl : base {
explicit impl(const boost::basic_string_view<Char> value) : s(value) {}
explicit impl(const core::basic_string_view<Char> value) : s(value) {}
impl* clone() const override { return new impl(*this); }
std::basic_string<Char> s;
};
Expand All @@ -49,7 +49,7 @@ namespace boost { namespace locale { namespace detail {
}

template<typename Char>
void set(const boost::basic_string_view<Char> s)
void set(const core::basic_string_view<Char> s)
{
BOOST_ASSERT(!s.empty());
s_.reset(new impl<Char>(s));
Expand Down
6 changes: 3 additions & 3 deletions include/boost/locale/detail/encoding.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright (c) 2022-2023 Alexander Grund
// Copyright (c) 2022-2025 Alexander Grund
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
Expand All @@ -9,7 +9,7 @@

#include <boost/locale/config.hpp>
#include <boost/locale/encoding_errors.hpp>
#include <boost/utility/string_view.hpp>
#include <boost/core/detail/string_view.hpp>
#include <memory>
#include <string>

Expand All @@ -24,7 +24,7 @@ namespace boost { namespace locale { namespace conv { namespace detail {

virtual ~charset_converter() = default;
virtual string_type convert(const CharIn* begin, const CharIn* end) = 0;
string_type convert(const boost::basic_string_view<CharIn>& text)
string_type convert(const core::basic_string_view<CharIn> text)
{
return convert(text.data(), text.data() + text.length());
}
Expand Down
11 changes: 6 additions & 5 deletions include/boost/locale/encoding.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
// Copyright (c) 2025 Alexander Grund
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
Expand Down Expand Up @@ -240,11 +241,11 @@ namespace boost { namespace locale {
/// Convert \a text to UTF
///
/// \throws conversion_error: Conversion failed
string_type convert(const boost::string_view& text) const { return impl_->convert(text); }
string_type convert(const core::string_view text) const { return impl_->convert(text); }
/// Convert \a text to UTF
///
/// \throws conversion_error: Conversion failed
string_type operator()(const boost::string_view& text) const { return convert(text); }
string_type operator()(const core::string_view text) const { return convert(text); }
};

/// Converter class to decode an UTF string and encode it using a local encoding
Expand All @@ -254,7 +255,7 @@ namespace boost { namespace locale {

public:
using char_type = CharType;
using stringview_type = boost::basic_string_view<CharType>;
using stringview_type = core::basic_string_view<CharType>;

/// Create an instance to convert UTF text to text encoded with \a charset according to policy \a how
///
Expand Down Expand Up @@ -298,11 +299,11 @@ namespace boost { namespace locale {
/// Convert \a text
///
/// \throws conversion_error: Conversion failed
std::string convert(const boost::string_view& text) const { return impl_->convert(text); }
std::string convert(const core::string_view text) const { return impl_->convert(text); }
/// Convert \a text
///
/// \throws conversion_error: Conversion failed
std::string operator()(const boost::string_view& text) const { return convert(text); }
std::string operator()(const core::string_view text) const { return convert(text); }
};
} // namespace conv
}} // namespace boost::locale
Expand Down
4 changes: 2 additions & 2 deletions include/boost/locale/format.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
// Copyright (c) 2021-2023 Alexander Grund
// Copyright (c) 2021-2024 Alexander Grund
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
Expand Down Expand Up @@ -418,7 +418,7 @@ namespace boost { namespace locale {
typedef basic_format<char> format;
/// Definition of wchar_t based format
typedef basic_format<wchar_t> wformat;
#ifndef BOOST_LOCALE_NO_CXX20_STRING8
#ifdef __cpp_lib_char8_t
/// Definition of char8_t based format
typedef basic_format<char8_t> u8format;
#endif
Expand Down
2 changes: 1 addition & 1 deletion include/boost/locale/formatting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ namespace boost { namespace locale {
/// Set time zone for formatting dates and time
void time_zone(const std::string&);
/// Get time zone for formatting dates and time
std::string time_zone() const;
const std::string& time_zone() const;

/// Set date/time pattern (strftime like)
template<typename CharType>
Expand Down
2 changes: 1 addition & 1 deletion include/boost/locale/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ namespace boost { namespace locale {
typedef basic_message<char> message;
/// Convenience typedef for wchar_t
typedef basic_message<wchar_t> wmessage;
#ifndef BOOST_LOCALE_NO_CXX20_STRING8
#ifdef __cpp_lib_char8_t
/// Convenience typedef for char8_t
typedef basic_message<char8_t> u8message;
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/encoding/codepage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ namespace boost { namespace locale { namespace conv {
BOOST_LOCALE_INSTANTIATE(char);
BOOST_LOCALE_INSTANTIATE_NO_CHAR(wchar_t);

#ifndef BOOST_LOCALE_NO_CXX20_STRING8
#ifdef __cpp_lib_char8_t
BOOST_LOCALE_INSTANTIATE_NO_CHAR(char8_t);
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/icu/conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ namespace boost { namespace locale { namespace impl_icu {
return std::locale(in, new utf8_converter_impl<char>(cd));
return std::locale(in, new converter_impl<char>(cd));
case char_facet_t::wchar_f: return std::locale(in, new converter_impl<wchar_t>(cd));
#ifndef BOOST_LOCALE_NO_CXX20_STRING8
#ifdef __cpp_lib_char8_t
case char_facet_t::char8_f: return std::locale(in, new utf8_converter_impl<char8_t>(cd));
#elif defined(__cpp_char8_t)
case char_facet_t::char8_f: break;
Expand Down
2 changes: 1 addition & 1 deletion src/posix/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ namespace boost { namespace locale { namespace impl_posix {
return std::locale(in, new std_converter<char>(std::move(lc)));
}
case char_facet_t::wchar_f: return std::locale(in, new std_converter<wchar_t>(std::move(lc)));
#ifndef BOOST_LOCALE_NO_CXX20_STRING8
#ifdef __cpp_lib_char8_t
case char_facet_t::char8_f: return std::locale(in, new utf8_converter<char8_t>(std::move(lc)));
#elif defined(__cpp_char8_t)
case char_facet_t::char8_f: break;
Expand Down
10 changes: 5 additions & 5 deletions src/shared/format.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
// Copyright (c) 2024 Alexander Grund
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/locale/format.hpp>
#include <boost/locale/generator.hpp>
#include <boost/locale/info.hpp>
#include "../util/numeric.hpp"
#include "../util/numeric_conversion.hpp"
#include <algorithm>
#include <iostream>
#include <limits>
Expand Down Expand Up @@ -63,10 +64,9 @@ namespace boost { namespace locale { namespace detail {
{
if(key.empty())
return;
int position;
if(util::try_to_int(key, position) && position > 0) {
static_assert(sizeof(unsigned) <= sizeof(decltype(d->position)), "Possible lossy conversion");
d->position = static_cast<unsigned>(position - 1);
decltype(d->position) position;
if(util::try_to_int(key, position) && position > 0u) {
d->position = position - 1u;
} else if(key == "num" || key == "number") {
as::number(ios_);

Expand Down
2 changes: 1 addition & 1 deletion src/shared/formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace boost { namespace locale {
{
time_zone_ = tz;
}
std::string ios_info::time_zone() const
const std::string& ios_info::time_zone() const
{
return time_zone_;
}
Expand Down
24 changes: 12 additions & 12 deletions src/shared/message.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Copyright (c) 2009-2015 Artyom Beilis (Tonkikh)
// Copyright (c) 2021-2023 Alexander Grund
// Copyright (c) 2021-2025 Alexander Grund
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
Expand All @@ -26,7 +26,7 @@
#include "mo_hash.hpp"
#include "mo_lambda.hpp"
#include <boost/assert.hpp>
#include <boost/utility/string_view.hpp>
#include <boost/core/detail/string_view.hpp>
#include <cstdio>
#include <map>
#include <memory>
Expand Down Expand Up @@ -141,7 +141,7 @@ namespace boost { namespace locale { namespace gnu_gettext {
hash_offset_ = get(24);
}

string_view find(const char* context_in, const char* key_in) const
core::string_view find(const char* context_in, const char* key_in) const
{
if(!has_hash())
return {};
Expand Down Expand Up @@ -192,13 +192,13 @@ namespace boost { namespace locale { namespace gnu_gettext {
return data_.data() + off;
}

string_view value(unsigned id) const
core::string_view value(unsigned id) const
{
const uint32_t len = get(translations_offset_ + id * 8);
const uint32_t off = get(translations_offset_ + id * 8 + 4);
if(len > data_.size() || off > data_.size() - len)
throw std::runtime_error("Bad mo-file format");
return string_view(&data_[off], len);
return core::string_view(&data_[off], len);
}

bool has_hash() const { return hash_size_ != 0; }
Expand Down Expand Up @@ -233,7 +233,7 @@ namespace boost { namespace locale { namespace gnu_gettext {
template<typename CharType>
struct mo_file_use_traits {
static constexpr bool in_use = false;
using string_view_type = basic_string_view<CharType>;
using string_view_type = core::basic_string_view<CharType>;
static string_view_type use(const mo_file&, const CharType*, const CharType*)
{
throw std::logic_error("Unexpected call"); // LCOV_EXCL_LINE
Expand All @@ -243,7 +243,7 @@ namespace boost { namespace locale { namespace gnu_gettext {
template<>
struct mo_file_use_traits<char> {
static constexpr bool in_use = true;
using string_view_type = basic_string_view<char>;
using string_view_type = core::basic_string_view<char>;
static string_view_type use(const mo_file& mo, const char* context, const char* key)
{
return mo.find(context, key);
Expand All @@ -254,10 +254,10 @@ namespace boost { namespace locale { namespace gnu_gettext {
template<>
struct mo_file_use_traits<char8_t> {
static constexpr bool in_use = true;
using string_view_type = basic_string_view<char8_t>;
using string_view_type = core::basic_string_view<char8_t>;
static string_view_type use(const mo_file& mo, const char8_t* context, const char8_t* key)
{
string_view res = mo.find(reinterpret_cast<const char*>(context), reinterpret_cast<const char*>(key));
core::string_view res = mo.find(reinterpret_cast<const char*>(context), reinterpret_cast<const char*>(key));
return {reinterpret_cast<const char8_t*>(res.data()), res.size()};
}
};
Expand Down Expand Up @@ -551,10 +551,10 @@ namespace boost { namespace locale { namespace gnu_gettext {
return true;
}

static std::string extract(boost::string_view meta, const std::string& key, const boost::string_view separators)
static std::string extract(core::string_view meta, const std::string& key, const core::string_view separators)
{
const size_t pos = meta.find(key);
if(pos == boost::string_view::npos)
if(pos == core::string_view::npos)
return "";
meta.remove_prefix(pos + key.size());
const size_t end_pos = meta.find_first_of(separators);
Expand Down Expand Up @@ -620,7 +620,7 @@ namespace boost { namespace locale { namespace detail {
case char_facet_t::nochar: break;
case char_facet_t::char_f: return std::locale(in, gnu_gettext::create_messages_facet<char>(minf));
case char_facet_t::wchar_f: return std::locale(in, gnu_gettext::create_messages_facet<wchar_t>(minf));
#ifndef BOOST_LOCALE_NO_CXX20_STRING8
#ifdef __cpp_lib_char8_t
case char_facet_t::char8_f: return std::locale(in, gnu_gettext::create_messages_facet<char8_t>(minf));
#elif defined(__cpp_char8_t)
case char_facet_t::char8_f: break;
Expand Down
Loading

0 comments on commit 6c890c1

Please sign in to comment.