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

View File

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