1 /*
2    mkvmerge -- utility for splicing together matroska files
3    from component media subtypes
4 
5    Distributed under the GPL v2
6    see the file COPYING for details
7    or visit https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
8 
9    locale string splitting and creation
10 
11    Written by Moritz Bunkus <moritz@bunkus.org>.
12 */
13 
14 #include "common/common_pch.h"
15 
16 #include <QRegularExpression>
17 
18 #include "common/locale_string.h"
19 #include "common/qt.h"
20 
locale_string_c(std::string locale_string)21 locale_string_c::locale_string_c(std::string locale_string) {
22   QRegularExpression locale_re{"^([[:alpha:]]+)?(_[[:alpha:]]+)?(\\.[^@]+)?(@.+)?"};
23   auto matches = locale_re.match(Q(locale_string));
24 
25   if (!matches.hasMatch())
26     throw mtx::locale_string_format_x(locale_string);
27 
28   m_language  = to_utf8(matches.captured(1));
29   m_territory = to_utf8(matches.captured(2));
30   m_codeset   = to_utf8(matches.captured(3));
31   m_modifier  = to_utf8(matches.captured(4));
32 
33   if (!m_territory.empty())
34     m_territory.erase(0, 1);
35 
36   if (!m_codeset.empty())
37     m_codeset.erase(0, 1);
38 
39   if (!m_modifier.empty())
40     m_modifier.erase(0, 1);
41 }
42 
43 locale_string_c &
set_codeset_and_modifier(const locale_string_c & src)44 locale_string_c::set_codeset_and_modifier(const locale_string_c &src) {
45   m_codeset  = src.m_codeset;
46   m_modifier = src.m_modifier;
47 
48   return *this;
49 }
50 
51 std::string
str(eval_type_e type)52 locale_string_c::str(eval_type_e type) {
53   std::string locale = m_language;
54 
55   if ((type & territory) && !m_territory.empty())
56     locale += "_"s + m_territory;
57 
58   if ((type & codeset) && !m_codeset.empty())
59     locale += "."s + m_codeset;
60 
61   if ((type & modifier) && !m_modifier.empty())
62     locale += "@"s + m_modifier;
63 
64   return locale;
65 }
66