• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3 //
4 //  Distributed under the Boost Software License, Version 1.0. (See
5 //  accompanying file LICENSE_1_0.txt or copy at
6 //  http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #define BOOST_LOCALE_SOURCE
9 #include "locale_data.hpp"
10 #include "../encoding/conv.hpp"
11 #include <string>
12 
13 namespace boost {
14 namespace locale {
15 namespace util {
parse(std::string const & locale_name)16     void locale_data::parse(std::string const &locale_name)
17     {
18         language = "C";
19         country.clear();
20         variant.clear();
21         encoding = "us-ascii";
22         utf8=false;
23         parse_from_lang(locale_name);
24     }
25 
parse_from_lang(std::string const & locale_name)26     void locale_data::parse_from_lang(std::string const &locale_name)
27     {
28         size_t end = locale_name.find_first_of("-_@.");
29         std::string tmp = locale_name.substr(0,end);
30         if(tmp.empty())
31             return;
32         for(unsigned i=0;i<tmp.size();i++) {
33             if('A' <= tmp[i] && tmp[i]<='Z')
34                 tmp[i]=tmp[i]-'A'+'a';
35             else if(tmp[i] < 'a' || 'z' < tmp[i])
36                 return;
37         }
38         language = tmp;
39         if(end >= locale_name.size())
40             return;
41 
42         if(locale_name[end] == '-' || locale_name[end]=='_') {
43            parse_from_country(locale_name.substr(end+1));
44         }
45         else if(locale_name[end] == '.') {
46            parse_from_encoding(locale_name.substr(end+1));
47         }
48         else if(locale_name[end] == '@') {
49            parse_from_variant(locale_name.substr(end+1));
50         }
51     }
52 
parse_from_country(std::string const & locale_name)53     void locale_data::parse_from_country(std::string const &locale_name)
54     {
55         size_t end = locale_name.find_first_of("@.");
56         std::string tmp = locale_name.substr(0,end);
57         if(tmp.empty())
58             return;
59         for(unsigned i=0;i<tmp.size();i++) {
60             if('a' <= tmp[i] && tmp[i]<='z')
61                 tmp[i]=tmp[i]-'a'+'A';
62             else if(tmp[i] < 'A' || 'Z' < tmp[i])
63                 return;
64         }
65 
66         country = tmp;
67 
68         if(end >= locale_name.size())
69             return;
70         else if(locale_name[end] == '.') {
71            parse_from_encoding(locale_name.substr(end+1));
72         }
73         else if(locale_name[end] == '@') {
74            parse_from_variant(locale_name.substr(end+1));
75         }
76     }
77 
parse_from_encoding(std::string const & locale_name)78     void locale_data::parse_from_encoding(std::string const &locale_name)
79     {
80         size_t end = locale_name.find_first_of("@");
81         std::string tmp = locale_name.substr(0,end);
82         if(tmp.empty())
83             return;
84         for(unsigned i=0;i<tmp.size();i++) {
85             if('A' <= tmp[i] && tmp[i]<='Z')
86                 tmp[i]=tmp[i]-'A'+'a';
87         }
88         encoding = tmp;
89 
90         utf8 = conv::impl::normalize_encoding(encoding.c_str()) == "utf8";
91 
92         if(end >= locale_name.size())
93             return;
94 
95         if(locale_name[end] == '@') {
96            parse_from_variant(locale_name.substr(end+1));
97         }
98     }
99 
parse_from_variant(std::string const & locale_name)100     void locale_data::parse_from_variant(std::string const &locale_name)
101     {
102         variant = locale_name;
103         for(unsigned i=0;i<variant.size();i++) {
104             if('A' <= variant[i] && variant[i]<='Z')
105                 variant[i]=variant[i]-'A'+'a';
106         }
107     }
108 
109 } // util
110 } // locale
111 } // boost
112 
113 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
114