0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-27 19:28:52 +02:00

ircd: Add locale:: namespace for portable internationalization.

This commit is contained in:
Jason Volk 2016-10-25 00:46:53 -07:00
parent 8f8f747628
commit fedeab2ddd
7 changed files with 164 additions and 56 deletions

View file

@ -31,13 +31,6 @@ struct string
using handle = JS::HandleString;
using handle_mutable = JS::MutableHandleString;
// SpiderMonkey may use utf-16/char16_t strings; these will help you then
static size_t convert(const char16_t *const &, char *const &buf, const size_t &max);
static std::string convert(const char16_t *const &);
static std::string convert(const std::u16string &);
static std::u16string convert(const char *const &);
static std::u16string convert(const std::string &);
static constexpr const size_t CBUFS = 8;
static const size_t CBUFSZ;
const char *c_str() const; // Copy into rotating buf

41
include/ircd/locale.h Normal file
View file

@ -0,0 +1,41 @@
/*
* Copyright (C) 2016 Charybdis Development Team
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#define HAVE_IRCD_LOCALE_H
//
// Utilities for internationalization.
//
namespace ircd {
namespace locale {
// On newer platforms (gcc-5 etc) these conversions are standard C++.
// On older platforms the definition file may use boost::locale.
size_t convert(const char16_t *const &, char *const &buf, const size_t &max);
std::string convert(const char16_t *const &);
std::string convert(const std::u16string &);
std::u16string convert(const char *const &);
std::u16string convert(const std::string &);
} // namespace locale
} // namespace ircd

View file

@ -75,6 +75,7 @@ namespace ircd
#include "util.h"
#include "util_timer.h"
#include "locale.h"
#include "life_guard.h"
#include "defaults.h"
#include "exception.h"

View file

@ -53,7 +53,8 @@ libircd_la_SOURCES = \
hook.cc \
fmt.cc \
db.cc \
js.cc
js.cc \
locale.cc
#authproc.cc \
#bandbi.cc \

View file

@ -979,50 +979,6 @@ const
return buf;
}
std::u16string
ircd::js::string::convert(const std::string &s)
{
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
return converter.from_bytes(s);
}
std::u16string
ircd::js::string::convert(const char *const &s)
{
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
return s? converter.from_bytes(s) : std::u16string{};
}
std::string
ircd::js::string::convert(const std::u16string &s)
{
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
return converter.to_bytes(s);
}
std::string
ircd::js::string::convert(const char16_t *const &s)
{
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
return s? converter.to_bytes(s) : std::string{};
}
size_t
ircd::js::string::convert(const char16_t *const &str,
char *const &buf,
const size_t &max)
{
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
//TODO: optimize
const auto s(converter.to_bytes(str));
return rb_strlcpy(buf, s.c_str(), max);
}
///////////////////////////////////////////////////////////////////////////////
//
// ircd/js/value.h
@ -1116,7 +1072,7 @@ ircd::js::jserror::jserror(pending_t)
report.lineno,
report.column);
const auto msg(report.ucmessage? string::convert(report.ucmessage) : std::string{});
const auto msg(report.ucmessage? locale::convert(report.ucmessage) : std::string{});
snprintf(ircd::exception::buf, sizeof(ircd::exception::buf), "%s%s%s%s",
reflect((JSExnType)report.exnType),
msg.empty()? "." : ": ",
@ -1163,7 +1119,7 @@ ircd::js::jserror::generate(const JSExnType &type,
va_list ap)
{
ircd::exception::generate(fmt, ap);
const auto msg(string::convert(what()));
const auto msg(locale::convert(what()));
JSErrorReport report;
report.ucmessage = msg.c_str();
@ -1291,10 +1247,10 @@ ircd::js::debug(const JSErrorReport &r)
ss << reflect(JSExnType(r.exnType)) << " ";
if(r.ucmessage)
ss << "\"" << string::convert(r.ucmessage) << "\" ";
ss << "\"" << locale::convert(r.ucmessage) << "\" ";
for(auto it(r.messageArgs); it && *it; ++it)
ss << "\"" << string::convert(*it) << "\" ";
ss << "\"" << locale::convert(*it) << "\" ";
return ss.str();
}

114
ircd/locale.cc Normal file
View file

@ -0,0 +1,114 @@
/*
* charybdis: standing on the shoulders of giant build times
*
* Copyright (C) 2016 Charybdis Development Team
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <boost/locale.hpp>
namespace ircd {
namespace locale {
} // namespace locale
} // namespace ircd
#ifdef HAVE_CODECVT
std::u16string
ircd::locale::convert(const std::string &s)
{
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
return converter.from_bytes(s);
}
#else
std::u16string
ircd::locale::convert(const std::string &s)
{
return boost::locale::conv::utf_to_utf<char16_t>(s);
}
#endif
#ifdef HAVE_CODECVT
std::u16string
ircd::locale::convert(const char *const &s)
{
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
return s? converter.from_bytes(s) : std::u16string{};
}
#else
std::u16string
ircd::locale::convert(const char *const &s)
{
return boost::locale::conv::utf_to_utf<char16_t>(s);
}
#endif
#ifdef HAVE_CODECVT
std::string
ircd::locale::convert(const std::u16string &s)
{
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
return converter.to_bytes(s);
}
#else
std::string
ircd::locale::convert(const std::u16string &s)
{
return boost::locale::conv::utf_to_utf<char>(s);
}
#endif
#ifdef HAVE_CODECVT
std::string
ircd::locale::convert(const char16_t *const &s)
{
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
return s? converter.to_bytes(s) : std::string{};
}
#else
std::string
ircd::locale::convert(const char16_t *const &s)
{
return boost::locale::conv::utf_to_utf<char>(s);
}
#endif
#ifdef HAVE_CODECVT
size_t
ircd::locale::convert(const char16_t *const &str,
char *const &buf,
const size_t &max)
{
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
//TODO: optimize
const auto s(converter.to_bytes(str));
return rb_strlcpy(buf, s.c_str(), max);
}
#else
size_t
ircd::locale::convert(const char16_t *const &str,
char *const &buf,
const size_t &max)
{
//TODO: optimize
const auto s(boost::locale::conv::utf_to_utf<char>(str));
return rb_strlcpy(buf, s.c_str(), max);
}
#endif

View file

@ -162,6 +162,8 @@ run git submodule update --init --recursive libs/tuple
run git submodule update --init --recursive libs/exception
run git submodule update --init --recursive libs/algorithm
run git submodule update --init --recursive libs/locale
### Install should go right into this local submodule repository
run ./bootstrap.sh --prefix=$PWD --libdir=$PWD/lib --with-libraries=$BLIBS $BSFLAGS
run ./b2 -d0 headers