string, locale: make stdex::locale(_t) independent .hpp file

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2023-11-09 07:57:57 +01:00
parent 9d50a878dc
commit 2c45749f78
5 changed files with 74 additions and 60 deletions

View File

@ -12,6 +12,7 @@
#include <stdex/hex.hpp> #include <stdex/hex.hpp>
#include <stdex/idrec.hpp> #include <stdex/idrec.hpp>
#include <stdex/interval.hpp> #include <stdex/interval.hpp>
#include <stdex/locale.hpp>
#include <stdex/mapping.hpp> #include <stdex/mapping.hpp>
#include <stdex/math.hpp> #include <stdex/math.hpp>
#include <stdex/parser.hpp> #include <stdex/parser.hpp>

View File

@ -8,7 +8,7 @@
#include "compat.hpp" #include "compat.hpp"
#include "math.hpp" #include "math.hpp"
#include "system.hpp" #include "system.hpp"
#include "string.hpp" #include "locale.hpp"
#include <stdint.h> #include <stdint.h>
#include <chrono> #include <chrono>
#include <ctime> #include <ctime>

70
include/stdex/locale.hpp Normal file
View File

@ -0,0 +1,70 @@
/*
SPDX-License-Identifier: MIT
Copyright © 2016-2023 Amebis
*/
#pragma once
#include "compat.hpp"
#include <locale.h>
#include <memory>
namespace stdex
{
#ifdef _WIN32
using locale_t = _locale_t;
inline locale_t create_locale(_In_ int category, _In_z_ const char* locale) { return _create_locale(category, locale); }
inline locale_t create_locale(_In_ int category, _In_z_ const wchar_t* locale) { return _wcreate_locale(category, locale); }
inline void free_locale(_In_opt_ locale_t locale) { _free_locale(locale); }
#else
using locale_t = ::locale_t;
inline locale_t create_locale(_In_ int category, _In_z_ const char* locale)
{
int mask = 0;
switch (category) {
case LC_ALL : mask = LC_ALL_MASK ; break;
case LC_COLLATE : mask = LC_COLLATE_MASK ; break;
case LC_CTYPE : mask = LC_CTYPE_MASK ; break;
case LC_MESSAGES: mask = LC_MESSAGES_MASK; break;
case LC_MONETARY: mask = LC_MONETARY_MASK; break;
case LC_NUMERIC : mask = LC_NUMERIC_MASK ; break;
case LC_TIME : mask = LC_TIME_MASK ; break;
}
return newlocale(mask, locale, LC_GLOBAL_LOCALE);
}
inline void free_locale(_In_opt_ locale_t locale) { freelocale(locale); }
#endif
///
/// Deleter for unique_ptr using free_locale
///
struct free_locale_delete
{
///
/// Delete a pointer
///
void operator()(_In_ locale_t locale) const
{
free_locale(locale);
}
};
///
/// locale_t helper class to free_locale when going out of scope
///
#if defined(_WIN32)
using locale = std::unique_ptr<__crt_locale_pointers, free_locale_delete>;
#elif defined(__APPLE__)
using locale = std::unique_ptr<struct _xlocale, free_locale_delete>;
#else
using locale = std::unique_ptr<struct __locale_struct, free_locale_delete>;
#endif
///
/// Reusable C locale
///
const locale locale_C(create_locale(LC_ALL, "C"));
}

View File

@ -8,6 +8,7 @@
#include "compat.hpp" #include "compat.hpp"
#include "endian.hpp" #include "endian.hpp"
#include "interval.hpp" #include "interval.hpp"
#include "locale.hpp"
#include "math.hpp" #include "math.hpp"
#include "ring.hpp" #include "ring.hpp"
#include "string.hpp" #include "string.hpp"

View File

@ -6,8 +6,8 @@
#pragma once #pragma once
#include "compat.hpp" #include "compat.hpp"
#include "locale.hpp"
#include <ctype.h> #include <ctype.h>
#include <locale.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h> #include <stdint.h>
@ -19,68 +19,10 @@
#include <algorithm> #include <algorithm>
#include <climits> #include <climits>
#include <locale> #include <locale>
#include <memory>
#include <stdexcept> #include <stdexcept>
namespace stdex namespace stdex
{ {
#ifdef _WIN32
using locale_t = _locale_t;
inline locale_t create_locale(_In_ int category, _In_z_ const char* locale) { return _create_locale(category, locale); }
inline locale_t create_locale(_In_ int category, _In_z_ const wchar_t* locale) { return _wcreate_locale(category, locale); }
inline void free_locale(_In_opt_ locale_t locale) { _free_locale(locale); }
#else
using locale_t = ::locale_t;
inline locale_t create_locale(_In_ int category, _In_z_ const char* locale)
{
int mask = 0;
switch (category) {
case LC_ALL : mask = LC_ALL_MASK ; break;
case LC_COLLATE : mask = LC_COLLATE_MASK ; break;
case LC_CTYPE : mask = LC_CTYPE_MASK ; break;
case LC_MESSAGES: mask = LC_MESSAGES_MASK; break;
case LC_MONETARY: mask = LC_MONETARY_MASK; break;
case LC_NUMERIC : mask = LC_NUMERIC_MASK ; break;
case LC_TIME : mask = LC_TIME_MASK ; break;
}
return newlocale(mask, locale, LC_GLOBAL_LOCALE);
}
inline void free_locale(_In_opt_ locale_t locale) { freelocale(locale); }
#endif
///
/// Deleter for unique_ptr using free_locale
///
struct free_locale_delete
{
///
/// Delete a pointer
///
void operator()(_In_ locale_t locale) const
{
free_locale(locale);
}
};
///
/// locale_t helper class to free_locale when going out of scope
///
#if defined(_WIN32)
using locale = std::unique_ptr<__crt_locale_pointers, free_locale_delete>;
#elif defined(__APPLE__)
using locale = std::unique_ptr<struct _xlocale, free_locale_delete>;
#else
using locale = std::unique_ptr<struct __locale_struct, free_locale_delete>;
#endif
///
/// Reusable C locale
///
const locale locale_C(create_locale(LC_ALL, "C"));
/// ///
/// UTF-16 code unit /// UTF-16 code unit
/// ///