Locale system changes to better mimic Sun's VM.

This commit is contained in:
jet 2010-10-14 16:20:41 -06:00
parent cddc305a75
commit 0713f8d5cb
2 changed files with 25 additions and 17 deletions

View File

@ -133,10 +133,9 @@ namespace {
#endif #endif
} }
class Locale { class Locale { // represents an ISO two-char language/country pair
static const unsigned MINLEN = 2; static const unsigned FIELDLEN = 2;
static const unsigned MAXLEN = 3; static const unsigned FIELDSIZE = FIELDLEN + 1;
static const unsigned FIELDSIZE = MAXLEN + 1;
static const char* DEFAULT_LANGUAGE; static const char* DEFAULT_LANGUAGE;
static const char* DEFAULT_REGION; static const char* DEFAULT_REGION;
@ -147,7 +146,7 @@ class Locale {
bool isLanguage(const char* language) { bool isLanguage(const char* language) {
if (!language) return false; if (!language) return false;
unsigned len = strlen(language); unsigned len = strlen(language);
if (len < MINLEN || len > MAXLEN) return false; if (len != FIELDLEN) return false;
const char* p = language - 1; const char* p = language - 1;
while (islower(*++p)); while (islower(*++p));
if (*p != '\0') return false; if (*p != '\0') return false;
@ -157,7 +156,7 @@ class Locale {
bool isRegion(const char* region) { bool isRegion(const char* region) {
if (!region) return false; if (!region) return false;
unsigned len = strlen(region); unsigned len = strlen(region);
if ((len != 0 && len < MINLEN) || len > MAXLEN) return false; if (len != FIELDLEN) return false;
const char* p = region - 1; const char* p = region - 1;
while (isupper(*++p)); while (isupper(*++p));
if (*p != '\0') return false; if (*p != '\0') return false;
@ -165,13 +164,16 @@ class Locale {
} }
public: public:
Locale(const char* language = "", const char* region = "") { Locale(const char* language = "") {
if (!isLanguage(language) || !isRegion(region)) { Locale l(language, "");
language = DEFAULT_LANGUAGE; *this = l;
region = DEFAULT_REGION; }
}
memcpy(this->language, language, strlen(language) + 1); Locale(const char* language, const char* region) {
memcpy(this->region, region, strlen(region) + 1); language = isLanguage(language) ? language : DEFAULT_LANGUAGE;
region = isRegion(region) ? region : DEFAULT_REGION;
memcpy(this->language, language, FIELDSIZE);
memcpy(this->region, region, FIELDSIZE);
} }
Locale& operator=(const Locale& l) { Locale& operator=(const Locale& l) {
@ -295,8 +297,8 @@ Locale getLocale() {
case 0x004: { case 0x004: {
lang = "zh"; lang = "zh";
switch (sublang) { switch (sublang) {
case 0x01: reg = "Hant"; break; case 0x01: reg = "CN"; break;
case 0x02: reg = "Hans"; break; case 0x02: reg = "TW"; break;
case 0x03: reg = "HK"; break; case 0x03: reg = "HK"; break;
case 0x04: reg = "SG"; break; case 0x04: reg = "SG"; break;
} }

View File

@ -11,12 +11,18 @@
package java.util; package java.util;
public class Locale { public class Locale {
public static final Locale ENGLISH = new Locale("en", "us"); private static final Locale DEFAULT;
public static final Locale ENGLISH = new Locale("en", "");
private final String language; private final String language;
private final String country; private final String country;
private final String variant; private final String variant;
static {
DEFAULT = new Locale(System.getProperty("user.language"),
System.getProperty("user.region"));
}
public Locale(String language, String country, String variant) { public Locale(String language, String country, String variant) {
this.language = language; this.language = language;
this.country = country; this.country = country;
@ -44,6 +50,6 @@ public class Locale {
} }
public static Locale getDefault() { public static Locale getDefault() {
return ENGLISH; return DEFAULT;
} }
} }